Skip to content

Instantly share code, notes, and snippets.

@connordavenport
Last active February 21, 2025 18:56
Show Gist options
  • Save connordavenport/3bf315942dfcc6c7e48a46dd3b96c28a to your computer and use it in GitHub Desktop.
Save connordavenport/3bf315942dfcc6c7e48a46dd3b96c28a to your computer and use it in GitHub Desktop.
a contextual menu for setting glyph mark colors
from mojo.subscriber import Subscriber, registerRoboFontSubscriber, unregisterRoboFontSubscriber
from mojo.UI import getDefault
from AppKit import NSMenuItem
from mojo.tools import CallbackWrapper
import ezui
class set_contextual_mark_colors(Subscriber):
debug = True
def build(self):
self._callbacks = []
self.color_map = {}
@property
def mark_colors(self):
items = []
for color, text in getDefault("markColors"):
self.color_map[text] = tuple(color) # store color and its name
item = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(text, "action:", "")
target = CallbackWrapper(self.set_mark_colors)
self._callbacks.append(target) # we need to keep a live reference to the callback
item.setTarget_(target)
image = ezui.makeImage(
symbolName="circle.fill",
symbolConfiguration=dict(
renderingMode='palette',
colors=[color]
)
) # thanks tal and ryan
item.setImage_(image)
items.append(item)
return items
def compile_color_menu(self):
myMenuItems = [
("Set Mark Color(s)",
[
color for color in self.mark_colors
]
),
]
return myMenuItems
def glyphEditorWantsContextualMenuItems(self, info):
info["itemDescriptions"].extend(self.compile_color_menu())
self.glyphs = [info["glyph"]]
def fontOverviewWantsContextualMenuItems(self, info):
info["itemDescriptions"].extend(self.compile_color_menu())
self.glyphs = CurrentFont().selectedGlyphs
def set_mark_colors(self, sender):
for glyph in self.glyphs:
with glyph.undo():
glyph.markColor = self.color_map[sender.title()]
glyph.changed()
if __name__ == '__main__':
registerRoboFontSubscriber(set_contextual_mark_colors)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment