I want to create a Reporter plugin that functions as a timer, displaying next to the currently edited glyph, to help me keep track of how much time I spend on it (in line with the Pomodoro Technique).
My idea is to use the updateInterfaceCallback to record the name of selectedLayers[0] if it’s existed. When the name of the new glyph is different from the last record, the timer will write the timer’s time into userData for accumulation in the next session.
However, it appears that updateInterfaceCallback gets called even when I’m working on the same glyph, making my idea ineffective.
Do you have any suggestions for implementing a timer, or perhaps know of an existing plugin that serves this purpose?
updateInterfaceCallback is called on every change in the ui, not only when working on the same glyph. But even when the selection changes, or you move your text cursor, etc. So if you use that, you need to filter out what changes you prefer.
GSGlyph.lastChange could be one helpful data, but I guess for your plan you might need to mix it with update callbacks, because using only the lastChange could mislead, as you might need to keep track of when you leave and come back to that glyph (otherwise the time will accumulate even if you work on another glyph).
Isn’t it tricky to filter out what “working on a glyph” is and is not in the first place? Having the layer open? Dragging the cursor across it? Modifying the curves? Spacing, kerning and proofing…? Might be more reliable to play/pause the timer manually.
Then just refresh the timer when the layer changes and add that into the layer’s timer? Here’s a quick macro sketch that seems to do so (needs finishing especially when no layer is selected etc.). Note that text/typing mode also counts as selectedLayers[0].
# Count time of the layer being selected.
from vanilla import FloatingWindow
import time
class Timer():
def __init__(self):
self.currentLayer = Glyphs.font.selectedLayers[0] if Glyphs.font.selectedLayers else None
self.start = time.time()
self.w = FloatingWindow((100, 100))
self.w.open()
self.addCallbacks()
self.w.bind('close', self.removeCallbacks)
def addCallbacks(self):
Glyphs.addCallback( self.updateInterface, UPDATEINTERFACE )
def removeCallbacks(self, sender):
Glyphs.removeCallback( self.updateInterface, UPDATEINTERFACE )
def updateInterface(self, sender):
# layer has changed
if self.currentLayer != Glyphs.font.selectedLayers[0]:
# get time elapsed
timeElapsed = time.time() - self.start
print(self.currentLayer)
print(timeElapsed)
# save in the current layer user data
if 'timer' not in self.currentLayer.userData:
self.currentLayer.userData['timer'] = timeElapsed
print('Layer’s initial timer: ', self.currentLayer.userData['timer'])
else:
print('Layer’s previous timer: ', self.currentLayer.userData['timer'])
print('Time elapsed: ', timeElapsed)
self.currentLayer.userData['timer'] += timeElapsed
print('Layer’s new timer: ', self.currentLayer.userData['timer'])
print('')
# reset timer
self.start = time.time()
self.currentLayer = Glyphs.font.selectedLayers[0]
Timer()