Can I set a name for my custom UndoGrouping action?

I use Undo Grouping in a plugin. My actions are grouped correctly when I Undo/Redo. However, I can’t set a custom name for a grouped action. By name I mean what I see in Menu → Edit → Undo/Redo, or on the pop-up tip at the bottom of the Edit tab when I press Undo/Redo.

layer = Glyphs.font.selectedLayers[0]
undoManager = layer.undoManager()
# Start Undo grouping
undoManager.beginUndoGrouping()
# Main script
try:
	# Group of actions is called here
	action_one()
	action_two()
finally:
	# End Undo grouping
	undoManager.endUndoGrouping()
	# Set a menu name for Undo action
	undoManager.setActionName_('Name for group of actions')
	undoManager = None

Is .setActionName_() the wrong command for this?

That is tricky to get right.
And try adding the action name before closing the group.
As long as you do all the action in one go, you don’t need the grouping. That is handled automatically.

Thanks. This works better but with side effect.

  • When script ends and grouping is ended, I see my custom name in Menu → Edit → Undo. Good.
  • But if I press Undo, after that my custom name is gone, never mind if I even press Redo again. Instead, a default action name appears there, like “Remove Shape”.

My actions are in one go exactly, but there is one more action before my actions, and that’s why I need this grouping:

  1. On mouse up event, wait until drawn path will be added to layer.
  2. Process drawn path with my script.

I needed to change something inside Glyphs. Thanks for making me look into this.

I don’t understand?

The plugin modifies behaviour of the Pencil tool when Option key is held. So, when I ended to draw the path (mouse up event fire, Pencil tool is active, and Option key was held when I started drawing), the plugin uses a timer with a delay of 25 milliseconds to make sure that drawn path is added to the layer, and then I change this last added path with the main script in the plugin.

So, the first action is: path added to layer (normally, when I ended to draw it). And the second action is my main script that processes the path specifically. I grouped those two actions into one UndoGrouping, because when I press Undo, I want to take two steps back — to the moment when I started drawing.

Here is more precise structure:

layer = Glyphs.font.selectedLayers[0]

# Start Undo grouping
self._undoManager = layer.undoManager()
self._undoManager.beginUndoGrouping()

# Wait until drawn path will be added to layer
self._getPathTimer = NSTimer.scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_(
	0.025, self, 'delayedModifyPath:', None, False
)

# Delayed main script
def delayedModifyPath_(self, timer)
	timer.invalidate()
	self._getPathTimer = None
	try:
		# Main script
		modifyPath()
	finally:
		# Set a name for Undo action
		self._undoManager.setActionName_('Name for group of actions')
		# End Undo grouping
		self._undoManager.endUndoGrouping()
		self._undoManager = None

I see.

I just checked and this issue is fixed in 3506. Big thanks.