Make undo management more flexible

juce::UndoManager should be renamed to UnitBasedUndoManager.
UndoManager should be a stripped down, pure abstract class. That class should be passed to other classes like ValueTree. The only method ever called on the UndoManagers in juce_ValueTree.cpp is perform, so there is no reason to inject a concrete class into value trees, or is there?

4 Likes

You would still need to pass a pointer to the instance of the UndoManager that actually performs the action. Is there a specific issue you’re facing?

Yes but then I can supply my own subclass of UndoManager, the current version has no virtual methods.

I need some custom behaviour to send out ARA content change notifications based on the actions performed. I would also add an UndoManagerListener to my custom class, for more in-depth notifications.

A custom undo manager would also allow you to filter commands - e.g. to ignore value changes during a gesture, only storing the final value, or to only record changes for particular properties.

To be more specific I would handle listening with a callback like this

/**
* @param lastAction the last action performed. 
* Owned by MyCustomUndoManager and only guaranteed to be valid during the callback.
*/
UndoManagerListener::actionPerformed(ActionType type, UndoableAction* lastAction)
{
}

Where action type could be normal, undo or redo

This should already be working as the current manager tries to merge actions based on a virtual method inside action I believe.

Yes, but it doesn’t do so when multiple parameters are changed at once (say with an XY type of control). In that case you’ll end up with an awful lot of units.

I’d love to see an abstraction layer for the UndoManager, making customizations and extensions possible. I stumbled upon the current implementation a few times already!

1 Like

I want to revisit this topic, as I just faced an issue, where I’d like to extend the behaviour:

In my application, that is handling audio unit hosting, posting the creation of an audio unit comes with a significant size of the undo-able action. Once I hit undo on the “audio unit create” or “audio unit remove”, the audio unit is essentially only still living because the undo manager, that might be a good few MB or even more. This is great for the first couple of undo-steps, as the user might remove the audio unit on accident and then hitting undo is really fast, as the audio unit still exist and everything is still loaded.

However, if the undo action is moved down in history the first couple steps, I’d like to clear the audio unit from the undo action to save memory but still have it in the history just in case.