hi @jansindl3r,
is there a way how to do it without restarting the app?
yes: you can open/close a vanilla window to add/remove the observers.
here’s a simpler ‘button inside subview’ example, I hope this is clearer:
from random import random
from vanilla import FloatingWindow, Button
import mojo.drawingTools as ctx
from mojo.events import addObserver, removeObserver
from mojo.canvas import CanvasGroup
from mojo.UI import CurrentGlyphWindow, UpdateCurrentGlyphView
class OverlayViewExample(object):
color = 1, 0, 0, 0.5
def __init__(self):
### for debugging only
self.w = FloatingWindow((150, 50), "debug only")
self.w.bind("close", self.windowClose)
self.w.open()
### end debugging
self.view = None
addObserver(self, "observerGlyphWindowWillOpen", "glyphWindowWillOpen")
addObserver(self, "observerDraw", "draw")
addObserver(self, "observerDrawPreview", "drawPreview")
def windowClose(self, sender):
removeObserver(self, "glyphWindowWillOpen")
removeObserver(self, "draw")
removeObserver(self, "drawPreview")
def observerGlyphWindowWillOpen(self, notification):
self.window = notification["window"]
self.view = CanvasGroup((0, -200, -0, -0), delegate=self)
self.view.button = Button((-130, -50, 120, 22), "Hit Me", callback=self.buttonCallback)
self.window.addGlyphEditorSubview(self.view)
def observerDraw(self, notification):
if self.view:
self.view.show(True)
def observerDrawPreview(self, notification):
# hide the view in Preview mode
if self.view:
self.view.show(False)
# button callback
def buttonCallback(self, sender):
print('button pressed')
self.color = random(), random(), random(), 0.5
UpdateCurrentGlyphView()
# canvas delegate callbacks
def opaque(self):
return True
def acceptsFirstResponder(self):
return False
def acceptsMouseMoved(self):
return True
def becomeFirstResponder(self):
return False
def resignFirstResponder(self):
return False
def shouldDrawBackground(self):
return False
# draw callback
def draw(self):
g = self.window.getGlyph()
if g is None:
return
x, y, w, h = self.window.getVisibleRect()
ctx.fill(*self.color)
ctx.rect(x + 10, y + 10, 100, 100)
OverlayViewExample()
ps.
I don't understand connection between the callback and the functions at all, sorry :( Is it described anywhere? I tried to search.
not sure if this is what you mean, but the canvas delegate callbacks are inherited from the wrapped NSView object. see the introduction in Canvas and CanvasGroup