from vanilla import FloatingWindow, SquareButton
from mojo.events import addObserver, removeObserver
from mojo.drawingTools import *

class CustomGlyphCellLabel:

    key = "com.myDomain.customGlyphCellLabel"

    def __init__(self):
        x = y = padding = 10
        buttonHeight = 32
        width = 200
        height = buttonHeight + padding * 2

        self.w = FloatingWindow((width, height), "custom labels")
        self.w.button = SquareButton((x, y, -padding, -padding), "add/remove label", callback=self.actionCallback)
        self.w.bind("close", self.closeCallback)
        self.w.open()
        addObserver(self, "drawCell", "glyphCellDraw")

    def drawCell(self, info):
        glyph = info["glyph"]
        value = glyph.lib.get(self.key)
        if value:
            fill(1, 0, 0)
            rect(0, 0, 10, 10)

    def actionCallback(self, sender):

        font = CurrentFont()

        if font is None:
            return

        for glyphName in font.selectedGlyphNames:
            # get current value
            value = font[glyphName].lib.get(self.key, False)
            # toggle current value
            font[glyphName].lib[self.key] = not value
            font[glyphName].changed()

    def closeCallback(self, sender):

        font = CurrentFont()

        if font is None:
            return

        for glyph in font:
            if not self.key in glyph.lib:
                continue
            del glyph.lib[self.key]
            glyph.changed()

        removeObserver(self, "glyphCellDraw")

CustomGlyphCellLabel()
Last edited on 10/02/2019