From 178ddffb31930685555d08078a022f48467ce58f Mon Sep 17 00:00:00 2001 From: Connor Davenport Date: Tue, 15 Jul 2025 12:36:29 -0400 Subject: [PATCH 1/2] updates to add extension to menubar and improve py opening --- DrawBot.roboFontExt/info.plist | 2 +- DrawBot.roboFontExt/lib/installDrawbot.py | 124 ++++++++++++++++++++-- DrawBot.roboFontExt/lib/settings.py | 3 +- 3 files changed, 121 insertions(+), 8 deletions(-) diff --git a/DrawBot.roboFontExt/info.plist b/DrawBot.roboFontExt/info.plist index 4b679b3..0c42d68 100644 --- a/DrawBot.roboFontExt/info.plist +++ b/DrawBot.roboFontExt/info.plist @@ -52,6 +52,6 @@ timeStamp 1417707288.332471 version - 3.131 + 3.132 diff --git a/DrawBot.roboFontExt/lib/installDrawbot.py b/DrawBot.roboFontExt/lib/installDrawbot.py index 2f24880..2c0fd5e 100644 --- a/DrawBot.roboFontExt/lib/installDrawbot.py +++ b/DrawBot.roboFontExt/lib/installDrawbot.py @@ -7,12 +7,22 @@ from drawBot.context.baseContext import BezierPath from drawBot.context import subscribeContext +from settings import DrawBotSettingsController + from mojo.events import addObserver -from mojo.extensions import getExtensionDefault +from mojo.extensions import getExtensionDefault, ExtensionBundle +from mojo.tools import CallbackWrapper from drawBotController import DrawBotController import glyphContext +import AppKit +from fontTools.ufoLib.glifLib import readGlyphFromString +from merz.tools.drawingTools import NSImageDrawingTools +import math +from vanilla.vanillaList import VanillaMenuBuilder + + root = os.path.dirname(__file__) # add drawBot to the sys path sys.path.append(root) @@ -85,17 +95,119 @@ class OpenFilesInDrawBotController(object): def __init__(self): addObserver(self, "openFile", "applicationOpenFile") + self._callbacks = [] + self.addToMenu() + + def addToMenu(self): + menubar = AppKit.NSApp().mainMenu() + + menuItem = menubar.itemWithTitle_("DrawBot") + if menuItem: menubar.removeItem_(menuItem) + + item = AppKit.NSMenuItem.alloc().initWithTitle_action_keyEquivalent_("DrawBot", "", "") + item.setImage_(self.menuBarIcon) + + menu = AppKit.NSMenu.alloc().initWithTitle_("DrawBot") + + # build this one separately so we can override the bindings + buildDrawBotItem = AppKit.NSMenuItem.alloc().initWithTitle_action_keyEquivalent_("DrawBot", "action:", "D") + target = CallbackWrapper(self.openDrawBotCallback) + self._callbacks.append(target) # we need to keep a live reference to the callback + buildDrawBotItem.setTarget_(target) + buildDrawBotItem.setKeyEquivalentModifierMask_(AppKit.NSShiftKeyMask | AppKit.NSCommandKeyMask); + + menuItems = [ + buildDrawBotItem, + dict(title="Settings", callback=self.drawBotSettingsCallback), + "----", + dict(title="Help", callback=self.drawBotHelpCallback), + ] + + VanillaMenuBuilder(self, menuItems, menu) + item.setSubmenu_(menu) + menubar.insertItem_atIndex_(item, menubar.numberOfItems() - 1) + + + def drawBotSettingsCallback(self, sender): + DrawBotSettingsController() + + def openDrawBotCallback(self, sender): + DrawBotController().open() + + def drawBotHelpCallback(self, sender): + ExtensionBundle("DrawBot").openHelp() def openFile(self, notification): fileHandler = notification["fileHandler"] path = notification["path"] _, ext = os.path.splitext(path) if ext.lower() == ".py": - with open(path, "r", encoding="utf8") as file: + with open(path) as file: header = file.readline().strip('\n') # dont be strict about case or whitespace if header.lower().replace(" ", "") == "#drawbot" or getExtensionDefault("com.drawBot.openPyFileDirectly", False): - DrawBotController().open(path) - fileHandler["opened"] = True - -OpenFilesInDrawBotController() + if not fileHandler.get("opened", False): # check if the file is already opened ! + DrawBotController().open(path) + fileHandler["opened"] = True + + + @property + def menuBarIcon(self): + iconGlifString = b""" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + """ + + iconGlyph = RGlyph() + readGlyphFromString(iconGlifString, glyphObject=iconGlyph, pointPen=iconGlyph.getPointPen()) + bot = NSImageDrawingTools((iconGlyph.width*1.2, iconGlyph.bounds[3]), scale=.02) + + bot.fill(1,1,1,.2) + bezPen = bot.BezierPath() + iconGlyph.draw(bezPen) + bot.drawPath(bezPen) + + bot.fill(0,0,0,1) + pencil = [(545,315),(575,185),(710,220),(875,535),(710,630)] + bot.polygon(*pencil, close=True) + + image = bot.getImage() + image.setTemplate_(True) + + return image + +if __name__ == "__main__": + OpenFilesInDrawBotController() diff --git a/DrawBot.roboFontExt/lib/settings.py b/DrawBot.roboFontExt/lib/settings.py index ddccaba..cd61e71 100644 --- a/DrawBot.roboFontExt/lib/settings.py +++ b/DrawBot.roboFontExt/lib/settings.py @@ -19,4 +19,5 @@ def openPythonFilesInDrawBotCallback(self, sender): setExtensionDefault("com.drawBot.openPyFileDirectly", sender.get()) -DrawBotSettingsController() +if __name__ == "__main__": + DrawBotSettingsController() From 475c82b2a0d354909ed5ab04ef701a97bc91ab6b Mon Sep 17 00:00:00 2001 From: Connor Davenport Date: Tue, 15 Jul 2025 12:39:46 -0400 Subject: [PATCH 2/2] fix utf encoding --- DrawBot.roboFontExt/lib/installDrawbot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DrawBot.roboFontExt/lib/installDrawbot.py b/DrawBot.roboFontExt/lib/installDrawbot.py index 2c0fd5e..e1a596f 100644 --- a/DrawBot.roboFontExt/lib/installDrawbot.py +++ b/DrawBot.roboFontExt/lib/installDrawbot.py @@ -142,7 +142,7 @@ def openFile(self, notification): path = notification["path"] _, ext = os.path.splitext(path) if ext.lower() == ".py": - with open(path) as file: + with open(path, "r", encoding="utf8") as file: header = file.readline().strip('\n') # dont be strict about case or whitespace if header.lower().replace(" ", "") == "#drawbot" or getExtensionDefault("com.drawBot.openPyFileDirectly", False):