Prev Up Next
Creation and Storage The Undo Mechanism Combining Undoinfo

Representation of Undoinfo

In Sketch, undoinfo is stored as a tuple whose first element is a callable object. The rest of the tuple are the arguments that the callable object must be called with to actually do the undo:

Undoinfo (1)

A tuple t is a tuple of undoinfo of an operation, iff the Python expression `apply(t[0], t[1:])' will undo the operation.

Example: The SetRadius method of the regular polygon plugin:
def SetRadius(self, radius):
    undo = self.SetRadius, self.radius	# create undoinfo
    self.radius = radius	# set the instance variable
    # ... update internal data here ...
    return undo			# finally, return undo info

Undoing SetRadius simply means to set the radius back to the old one, that is, one has to call the method SetRadius of the same object again, only this time with the old radius as parameter. Thus the undoinfo returned is `(self.SetRadius, self.radius)' which is executed before self.radius changes. Note, that in this example the internal data get recomputed automatically during the undo.

Closer examination reveals that performing this undo returns again some undoinfo. This new undo info tells us how to undo the undo! This is the information we need to perform the `redo' operation, the redoinfo.

That means, that if we refine the requirements of the tuple of undoinfo just a little bit, we get the `redo' for free:

Undoinfo (2)

A tuple t is a tuple of undoinfo of an operation, iff apply(t[0], t[1:]) undoes the operation and returns redoinfo.

As it turns out, in Sketch at least, this additional demand is trivial to meet.


Creation and Storage The Undo Mechanism Combining Undoinfo
Prev Up Next