-
Notifications
You must be signed in to change notification settings - Fork 11
/
hotreload_main.py
67 lines (46 loc) · 1.82 KB
/
hotreload_main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
import importlib
from juce_init import START_JUCE_COMPONENT
import popsicle as juce
class HotReloadContentComponent(juce.Component, juce.Timer):
comp = None
module = None
moduleName = "hotreload_component"
fileToWatch = juce.File(os.path.abspath(__file__)).getSiblingFile(f"{moduleName}.py")
fileLastModificationTime = None
def __init__(self):
juce.Component.__init__(self)
juce.Timer.__init__(self)
width = 600
height = 400
self.setSize(int(width), int(height))
self.setOpaque(True)
self.startTimerHz(5)
def timerCallback(self):
fileLastModificationTime = self.fileToWatch.getLastModificationTime()
if not self.fileLastModificationTime or self.fileLastModificationTime < fileLastModificationTime:
self.instantiateComponent()
self.resized()
self.fileLastModificationTime = fileLastModificationTime
def paint(self, g: juce.Graphics):
g.fillAll(juce.Colours.black)
def resized(self):
bounds = self.getLocalBounds()
if self.comp:
self.comp.setBounds(bounds)
def instantiateComponent(self):
try:
if not self.module:
self.module = importlib.import_module(self.moduleName)
else:
self.module = importlib.reload(self.module)
print("Module", self.fileToWatch.getFileName(), "reloaded correctly")
except Exception as e:
print(e)
return
if self.comp:
self.removeChildComponent(self.comp)
self.comp = self.module.TestComponent()
self.addAndMakeVisible(self.comp)
if __name__ == "__main__":
START_JUCE_COMPONENT(HotReloadContentComponent, name="Hot Reload Example", alwaysOnTop=True, catchExceptionsAndContinue=True)