Comment by jcelerier
9 years ago
> I wound up using a stack-based architecture that pushed operations stored as functions onto a main stack then its reverse operation onto an undo stack. Once I worked out the state management problems it worked out quite well.
that's quite common: it's called the Command pattern. https://en.wikipedia.org/wiki/Command_pattern
Another common approach is Memento (basically, "save everything")
A nice thing that can be added is a pair of serialization / deserialization functions for the state needed for your commands. This way you can save them regularly and restore them if a crash or something like this happens ; the user can then continue to work while keeping its undo / redo stack
Musescore (open-source music notation software) uses this pattern, for those interested... but not in a particularly DRY or stable way (so much duplicated logic!). I'm sure other open-source examples exist, but this should give you an idea:
https://github.com/musescore/MuseScore/blob/master/libmscore...
https://github.com/musescore/MuseScore/blob/master/libmscore...
https://github.com/musescore/MuseScore/blob/c4af0c22bc90a6ba...
Here's how I do it (interestingly, on a Qt-based art software too :p )
https://github.com/OSSIA/score/blob/master/base/lib/core/com...
https://github.com/OSSIA/score/blob/master/base/plugins/scor...
https://github.com/OSSIA/score/blob/master/base/plugins/scor...
https://github.com/OSSIA/score/blob/master/base/plugins/scor...
Also once your commands are serialized you can send them over the network for distributed edition
http://gameprogrammingpatterns.com/command.html
The command pattern only describes the encapsulation of an imperative, but not a complete undo/redo stack.
Technically, yes, but every book I read that talked about the Command pattern did so in the context of an undo-redo system
> A nice thing that can be added is a pair of serialization / deserialization functions for the state needed for your commands.
I wonder if this was the origin of modal text editors. Maybe they were designed for machine/programmer convenience more than machine convenience originally.
they were designed around the constraints of slow teletype machines and slow 300 baud modems. they make no sense as a UI until you take into account how long you used to need to wait for a computer to respond
A modal editor is not necessarily less efficient in keystrokes than one using control keys; modal just means you don't have to hold your control keys down while completing the command.
Using keyboard commands is often faster than pointing with a mouse, if you take advantage of structures in the text; e.g. "select everything inside the parens." And if you're doing that all day, it becomes second nature to build macros on the fly.
2 replies →