SOLVED addToolbarItem problem



  • I'm trying to add a custom button to the font overview toolbar and am running into a problem.

    The button adds correctly to the first window, but each additional window causes the older windows' to gain blank icon spaces.

    I'm not sure if it's a bug in my code or in robofont or in vanilla, but I've reached the limit of my debugging ability.

    Screen Shot 2020-04-13 at 11.21.54 AM.png

    Below is my test code. Thanks.

    from AppKit import NSApp, NSImageNameAdvanced
    from vanilla import Window
    from mojo.events import addObserver, removeObserver
    from mojo.UI import CurrentFontWindow
    
    from mojo.UI import OutputWindow
    OutputWindow().clear()
    
    debug = True
    
    
    class AddToToolbar():
    
        def __init__(self):
            debugname = 'Debug Toolbar'
            if debug is True:
                self.debug = Window((500, 50), debugname)
                self.debug.bind('close', self.debugClose)
                for window in [w for w in NSApp().orderedWindows() if w.isVisible()]:
                    if window.title() == debugname:
                        window.close()
                self.debug.open()
            addObserver(self, 'addToolbarButton', 'fontWindowDidOpen')
    
        def debugClose(self, sender):
            removeObserver(self, 'fontWindowDidOpen')
    
        def addToolbarButton(self, notification):
            if notification['window'] is None:
                return
            window = notification['window'].window()
    
            newItem = dict(
                itemIdentifier='customButton',
                label='Custom Button',
                toolTip='ToolTip',
                imageNamed=NSImageNameAdvanced,
                callback=self.customButton
            )
    
            if newItem['itemIdentifier'] in window.getToolbarItems():
                window.removeToolbarItem(newItem['itemIdentifier'])
    
            window.addToolbarItem(
                newItem,
                index=2
            )
    
        def customButton(self, sender):
            print('click!')
    
    
    AddToToolbar()
    
    
    # edited to use the notification window
    
    

  • admin

    use the notification fontWindowWillShowToolbarItems

    from AppKit import NSApp, NSImageNameAdvanced
    from vanilla import Window
    from mojo.events import addObserver, removeObserver
    from mojo.UI import CurrentFontWindow
    
    from mojo.UI import OutputWindow
    OutputWindow().clear()
    
    debug = True
    
    
    class AddToToolbar():
    
        def __init__(self):
            debugname = 'Debug Toolbar'
            if debug is True:
                self.debug = Window((500, 50), debugname)
                self.debug.bind('close', self.debugClose)
                for window in [w for w in NSApp().orderedWindows() if w.isVisible()]:
                    if window.title() == debugname:
                        window.close()
                self.debug.open()
                
            addObserver(self, 'addToolbarButton', 'fontWindowWillShowToolbarItems')
    
        def debugClose(self, sender):
            removeObserver(self, 'fontWindowDidOpen')
    
        def addToolbarButton(self, notification):
            newItem = dict(
                itemIdentifier='customButton',
                label='Custom Button',
                toolTip='ToolTip',
                imageNamed=NSImageNameAdvanced,
                callback=self.customButton
            )
            
            notification['toolbarItems'].insert(2, newItem)
    
        def customButton(self, sender):
            print('click!')
    
    
    AddToToolbar()
    


  • That's even easier! Thanks.


  • admin

    Yeah: and with a mix of single and multi window mode this is the best solutions...


Log in to reply