| Observing Arrays for Undo 'Recording' |
|
The previous snippet presented a helper class that handles observing element properties of an array – or "ordered to-many relation" to be correct. That first helper class was targeted towards view update notifications for triggering a redraw. Now here is an extended version that automatically records undo information for each observed change. This is very useful if the model is not core data based and is changed by multiple sources. It may also be useful if one change may trigger other changes which are harder to track for undo recording. A tricky thing with recording undo information with an observer is that modifications may occur in small increments but you want the undoing to jump right back to the beginning. Think of dragging some slider with the mouse: the view should be updated continuously but invoking undo should move the slider back to where the drag started. The views need continuous change notifications whereas undo recording should only store the starting values. Further changes during the same event must not be recorded. So the enhanced "UndoArrayObserver" helper class keeps track of which property has been changed during an event and uses the "close undo group" notification to detect the end of the event. But wait – what if the slider can be moved with the array keys or with the scroll wheel of the mouse? Those inputs will generate small adjustments with each event. To coalesce those changes the observer helper uses a timer that prevents it from recording undo information when the change occurs within a default period of 0.4 seconds. You may want to adapt this to your needs. Two comments about the implementation. Assignments to the array property are not (yet) supported/implemented and the model must support "prior" notifications. This implies that it will not work when obseving through a NSObjectController/NSArrayController. Download the initial version here (please report corrections and suggestions to stefan at infoatelier dot com). Comments (0)
![]() Write comment
|

