SOLVED Keep reference to an arbitrary vanilla window for later use



  • Hi,

    I'm trying to find a window by assigning a __name__ attribute to it. This used to work in RF1. Could you please direct me on how to get the window using the below example in RF3?

    from mojo.roboFont import OpenWindow
    from vanilla import *
    
    WIN_KEY = 'test'
    
    class WindowDemo(object):
        __name__ = WIN_KEY
        def __init__(self):
            self.w = Window((200, 70), "Window Demo")
            self.w.open()
    
    def GetWindow(key):
    	from AppKit import NSApp
    	for w in NSApp().orderedWindows():
    		delegate = w.delegate()
    		if delegate:
    			if not hasattr(delegate, "vanillaWrapper"):
    				continue
    			vanillaWrapper = delegate.vanillaWrapper()
    			if hasattr(vanillaWrapper, "__name__") and vanillaWrapper.__name__ == key:
    				return vanillaWrapper.w
    
    OpenWindow(WindowDemo)
    print(GetWindow(WIN_KEY))
    


  • Well that doesn't surprise me since I have no idea what I'm doing. I'll read up on notifications. It would be helpful to know why this is bad though.


  • admin

    wait ... a ... second! this is bad coding :)

    if two views needs to know something from each other much better to send a notification and retrieve data from there.



  • Great question, I've been wanting to figure how to do this for a while. I made a pair of scripts to test how it would work to get input from one window into another. It works, but things break if I remove the Button from the Set window.

    from vanilla import Window, EditText, Button
    import weakref
    
    WIN_KEY = 'VanillaObjectWindow'
    
    class setVanillaObject(object):
    
        __name__ = WIN_KEY  
        def __init__(self):
            self.windowname = 'Set'
            self.w = Window((333, 80), self.windowname)
            self.w.vanillaWrapper = weakref.ref(self)
            self.w.zzzzzzzz = EditText((12, 10, -12, 22), 'Placeholder Placeholder Placeholder')
            self.w.b = Button((12, 34, -12, 22), 'Button', callback=self.do) # doesn't work without this ??
            self.w.open()
    
        def do(self, sender):
            print(self.w)
    
    setVanillaObject()
    
    from vanilla import Window, Button, EditText
    from AppKit import NSApp
    
    WIN_KEY = 'VanillaObjectWindow'
    
    class getVanillaObject(object):
    
        def __init__(self):
            self.windowname = 'Get'
            self.w = Window((333, 70), self.windowname)
            self.w.t = EditText((12, 10, -12, 22), '...')
            self.w.b =   Button((12, 34, -12, 22), 'Get Placeholder from Set', callback=self.do)
            self.w.open()
    
        def do(self, sender):
            w = GetWindow(WIN_KEY)
            x = w.zzzzzzzz.get()
            self.w.t.set(x)
    
    def GetWindow(key):
        from AppKit import NSApp
        for w in NSApp().orderedWindows():
            delegate = w.delegate()
            if delegate:
                if not hasattr(delegate, "vanillaWrapper"):
                    continue
                vanillaWrapper = delegate.vanillaWrapper()
                if hasattr(vanillaWrapper, "__name__") and vanillaWrapper.__name__ == key:
                    return vanillaWrapper.w
    
    getVanillaObject()
    


  • hello @bahman,

    you have to set the vanillaWrapper yourself while creating the window controller, and connect it with a weakref:

    self.w.vanillaWrapper = weakref.ref(self)
    

    thanks @frederik for answering it :)

    cheers!