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..e1a596f 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,6 +95,47 @@ 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"] @@ -95,7 +146,68 @@ def openFile(self, notification): 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()