DRAWFOREGROUND and undo

Hi! Could you please help to figure out how to put everything happening within DRAWFOREGROUND as a single undo step?
I tried this:

def drawforeground( self, layer, event ):
layer.parent.beginUndo()
do stuff that should be part of one undo
layer.parent.endUndo()

But seems like I still have to undo as many times as the callback has been called.
Thank you!

You should not do changes to the layer and effect undo in any way during drawing operations.

Hm, then maybe there’s a god practice how to undo in one step everything happened during the script?

Normally, all actions that are done in one run of a script are grouped automatically.
If that is not enough, there is: Glyphs.app Python Scripting API Documentation — Glyphs.app Python Scripting API 3.0 documentation

Hi! Can I ask what would be a proper way to implement code which changes a few layers, including the current one, live based on user’s changes in drawing? I currently run it from DRAWFOREGROUND but the undo behavior is not good because it doesn’t affect any layers except for the current one and keeps every callback call which is a lot.

Any guidance? Thank you :slight_smile:

Do you mean layer of different glyphs? Each glyph has its own undo so you can group across multiple glyphs. So you need to undo in each glyph.
And DRAWFOREGROUND is not a good place to change the layers as those changes might trigger a redraw and that could loop.

Thank you! Yes, layers of different glyphs. I found this in undoManager:

glyph.undoManager().beginUndoGrouping
*changes in different layers
glyph.undoManager().endUndoGrouping

but then undoing from the glyph doesn’t seem to affect other layers, so I guess that’s not how it should be done?

The undo grouping is only for all changes to layer their belong to the same glyph. But in most cases you don’t need to do that manually. Only if you do some changes while dragging kit otherwise over several callbacks.

I see! But in that case is there a way to trigger undo in other glyphs when undo is called in current glyph?

No, because you never know what else was done in that other glyph in the mean time. So if you do your change, then go to the other glyph and manually change something. Then come back to the first one and undo you get into problems.

1 Like