/
CPUndoManager.j
904 lines (729 loc) · 24.5 KB
/
CPUndoManager.j
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
/*
* CPUndoManager.j
* Foundation
*
* Created by Francisco Tolmasky.
* Copyright 2008, 280 North, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@import "CPArray.j"
@import "CPException.j"
@import "CPInvocation.j"
@import "CPNotificationCenter.j"
@import "CPObject.j"
@import "CPProxy.j"
@import "CPKeyValueObserving.j"
@import "CPRunLoop.j"
var CPUndoManagerNormal = 0,
CPUndoManagerUndoing = 1,
CPUndoManagerRedoing = 2;
CPUndoManagerCheckpointNotification = @"CPUndoManagerCheckpointNotification";
CPUndoManagerDidOpenUndoGroupNotification = @"CPUndoManagerDidOpenUndoGroupNotification";
CPUndoManagerDidCloseUndoGroupNotification = @"CPUndoManagerDidCloseUndoGroupNotification";
CPUndoManagerDidRedoChangeNotification = @"CPUndoManagerDidRedoChangeNotification";
CPUndoManagerDidUndoChangeNotification = @"CPUndoManagerDidUndoChangeNotification";
CPUndoManagerWillCloseUndoGroupNotification = @"CPUndoManagerWillCloseUndoGroupNotification";
CPUndoManagerWillRedoChangeNotification = @"CPUndoManagerWillRedoChangeNotification";
CPUndoManagerWillUndoChangeNotification = @"CPUndoManagerWillUndoChangeNotification";
CPUndoCloseGroupingRunLoopOrdering = 350000;
var _CPUndoGroupingPool = [],
_CPUndoGroupingPoolCapacity = 5;
/* @ignore */
@implementation _CPUndoGrouping : CPObject
{
_CPUndoGrouping _parent;
CPMutableArray _invocations;
CPString _actionName;
}
+ (void)_poolUndoGrouping:(_CPUndoGrouping)anUndoGrouping
{
if (!anUndoGrouping || _CPUndoGroupingPool.length >= _CPUndoGroupingPoolCapacity)
return;
_CPUndoGroupingPool.push(anUndoGrouping);
}
+ (id)undoGroupingWithParent:(_CPUndoGrouping)anUndoGrouping
{
if (_CPUndoGroupingPool.length)
{
var grouping = _CPUndoGroupingPool.pop();
grouping._parent = anUndoGrouping;
if (grouping._invocations.length)
grouping._invocations = [];
return grouping;
}
return [[self alloc] initWithParent:anUndoGrouping];
}
- (id)initWithParent:(_CPUndoGrouping)anUndoGrouping
{
self = [super init];
if (self)
{
_parent = anUndoGrouping;
_invocations = [];
_actionName = @"";
}
return self;
}
- (_CPUndoGrouping)parent
{
return _parent;
}
- (void)addInvocation:(CPInvocation)anInvocation
{
_invocations.push(anInvocation);
}
- (void)addInvocationsFromArray:(CPArray)invocations
{
[_invocations addObjectsFromArray:invocations];
}
- (BOOL)removeInvocationsWithTarget:(id)aTarget
{
var index = _invocations.length;
while (index--)
if ([_invocations[index] target] == aTarget)
_invocations.splice(index, 1);
}
- (CPArray)invocations
{
return _invocations;
}
- (void)invoke
{
var index = _invocations.length;
while (index--)
[_invocations[index] invoke];
}
- (void)setActionName:(CPString)aName
{
_actionName = aName;
}
- (CPString)actionName
{
return _actionName;
}
@end
var _CPUndoGroupingParentKey = @"_CPUndoGroupingParentKey",
_CPUndoGroupingInvocationsKey = @"_CPUndoGroupingInvocationsKey",
_CPUndoGroupingActionNameKey = @"_CPUndoGroupingActionNameKey";
@implementation _CPUndoGrouping (CPCoder)
- (id)initWithCoder:(CPCoder)aCoder
{
self = [super init];
if (self)
{
_parent = [aCoder decodeObjectForKey:_CPUndoGroupingParentKey];
_invocations = [aCoder decodeObjectForKey:_CPUndoGroupingInvocationsKey];
_actionName = [aCoder decodeObjectForKey:_CPUndoGroupingActionNameKey];
}
return self;
}
- (void)encodeWithCoder:(CPCoder)aCoder
{
[aCoder encodeObject:_parent forKey:_CPUndoGroupingParentKey];
[aCoder encodeObject:_invocations forKey:_CPUndoGroupingInvocationsKey];
[aCoder encodeObject:_actionName forKey:_CPUndoGroupingActionNameKey];
}
@end
/*!
@class CPUndoManager
@ingroup foundation
@brief A general mechanism for user action "undo".
CPUndoManager provides a general mechanism supporting implementation of user
action "undo" in applications. Essentially, it allows you to store sequences
of messages and receivers that need to be invoked to undo or redo an action.
The various methods in this class provide for grouping of sets of actions,
execution of undo or redo actions, and tuning behavior parameters such as
the size of the undo stack. Each application entity with its own editing
history (e.g., a document) should have its own undo manager instance.
Obtain an instance through a simple \c [[CPUndoManager \c alloc] \c init]
message.
*/
@implementation CPUndoManager : CPObject
{
CPMutableArray _redoStack;
CPMutableArray _undoStack;
BOOL _groupsByEvent;
int _disableCount;
int _levelsOfUndo;
id _currentGrouping;
int _state;
id _preparedTarget;
id _undoManagerProxy;
CPArray _runLoopModes;
BOOL _registeredWithRunLoop;
}
/*!
Initializes the undo manager
@return the initialized undo manager
*/
- (id)init
{
self = [super init];
if (self)
{
_redoStack = [];
_undoStack = [];
_state = CPUndoManagerNormal;
[self setRunLoopModes:[CPDefaultRunLoopMode]];
[self setGroupsByEvent:YES];
_undoManagerProxy = [_CPUndoManagerProxy alloc];
_undoManagerProxy._undoManager = self;
}
return self;
}
- (void)_addUndoInvocation:(CPInvocation)anInvocation
{
if (!_currentGrouping)
// Remember that we create these lazily...
if ([self groupsByEvent])
[self _beginUndoGroupingForEvent];
else
[CPException raise:CPInternalInconsistencyException reason:"No undo group is currently open"];
[_currentGrouping addInvocation:anInvocation];
if (_state === CPUndoManagerNormal)
[_redoStack removeAllObjects];
}
// Registering Undo Operations
/*!
Registers an undo operation. You invoke this method with the target of the undo action providing the selector which can perform the undo with the provided object. The object is often a dictionary of the identifying the attribute and their values before the change. The invocation will be added to the current grouping. If the registrations have been disabled through \c -disableUndoRegistration, this method does nothing.
@param aTarget the target for the undo invocation
@param aSelector the selector for the action message
@param anObject the argument for the action message
@throws CPInternalInconsistencyException if no undo group is currently open
*/
- (void)registerUndoWithTarget:(id)aTarget selector:(SEL)aSelector object:(id)anObject
{
// Don't do anything if we're disabled.
if (_disableCount > 0)
return;
//signature = [target methodSignatureForSelector:selector];
// FIXME: we need method signatures.
var invocation = [CPInvocation invocationWithMethodSignature:nil];
[invocation setTarget:aTarget];
[invocation setSelector:aSelector];
[invocation setArgument:anObject atIndex:2];
[self _addUndoInvocation:invocation];
}
/*!
Prepares the specified target for the undo action.
@param aTarget the target to receive the action
@return the undo manager
*/
- (id)prepareWithInvocationTarget:(id)aTarget
{
_preparedTarget = aTarget;
return _undoManagerProxy;
}
/*
FIXME This method doesn't seem to do anything right
@ignore
*/
- (CPMethodSignature)_methodSignatureOfPreparedTargetForSelector:(SEL)aSelector
{
if ([_preparedTarget respondsToSelector:aSelector])
return 1;
return nil;//[_preparedTarget methodSignatureForSelector:selector];
}
/*!
Records the specified invocation as an undo operation. Sets the
target on the invocation, and adds it to the current grouping.
@param anInvocation the message to record
*/
- (void)_forwardInvocationToPreparedTarget:(CPInvocation)anInvocation
{
// Don't do anything if we're disabled.
if (_disableCount > 0)
return;
/*
if (_currentGroup == nil)
[NSException raise:NSInternalInconsistencyException
format:@"forwardInvocation called without first opening an undo group"];
*/
[anInvocation setTarget:_preparedTarget];
[self _addUndoInvocation:anInvocation];
_preparedTarget = nil;
}
// Checking Undo Ability
/*!
Returns \c YES if the user can perform a redo operation.
*/
- (BOOL)canRedo
{
[[CPNotificationCenter defaultCenter]
postNotificationName:CPUndoManagerCheckpointNotification
object:self];
return [_redoStack count] > 0;
}
/*!
Returns \c YES if the user can perform an undo operation.
*/
- (BOOL)canUndo
{
if (_undoStack.length > 0)
return YES;
return [[_currentGrouping invocations] count] > 0;
}
// Preform Undo and Redo
/*!
Ends the current grouping, and performs an 'undo' operation.
*/
- (void)undo
{
if ([self groupingLevel] === 1)
[self endUndoGrouping];
[self undoNestedGroup];
}
/*!
Performs an undo on the last undo group.
*/
- (void)undoNestedGroup
{
if ([_undoStack count] <= 0)
return;
var defaultCenter = [CPNotificationCenter defaultCenter];
[defaultCenter postNotificationName:CPUndoManagerCheckpointNotification
object:self];
[defaultCenter postNotificationName:CPUndoManagerWillUndoChangeNotification
object:self];
var undoGrouping = _undoStack.pop(),
actionName = [undoGrouping actionName];
_state = CPUndoManagerUndoing;
[self _beginUndoGrouping];
[undoGrouping invoke];
[self endUndoGrouping];
[_CPUndoGrouping _poolUndoGrouping:undoGrouping];
_state = CPUndoManagerNormal;
[[_redoStack lastObject] setActionName:actionName];
[defaultCenter postNotificationName:CPUndoManagerDidUndoChangeNotification
object:self];
}
/*!
Performs the redo operation using the last grouping on the redo stack.
*/
- (void)redo
{
// Don't do anything if we have no redos.
if ([_redoStack count] <= 0)
return;
/* if (_state == NSUndoManagerUndoing)
[NSException raise:NSInternalInconsistencyException
format:@"redo called while undoing"];
*/
var defaultCenter = [CPNotificationCenter defaultCenter];
[defaultCenter postNotificationName:CPUndoManagerCheckpointNotification
object:self];
[defaultCenter postNotificationName:CPUndoManagerWillRedoChangeNotification
object:self];
var oldUndoGrouping = _currentGrouping,
undoGrouping = _redoStack.pop(),
actionName = [undoGrouping actionName];
_currentGrouping = nil;
_state = CPUndoManagerRedoing;
[self _beginUndoGrouping];
[undoGrouping invoke];
[self endUndoGrouping];
[_CPUndoGrouping _poolUndoGrouping:undoGrouping];
_currentGrouping = oldUndoGrouping;
_state = CPUndoManagerNormal;
[[_undoStack lastObject] setActionName:actionName];
[defaultCenter postNotificationName:CPUndoManagerDidRedoChangeNotification object:self];
}
// Creating Undo Groups
/*!
Starts a new grouping of undo tasks, and makes it the current grouping.
*/
- (void)beginUndoGrouping
{
// It doesn't matter that the user is creating a group themselves, we are
// pretending to have opened the group at the beginning of the run loop,
// so create an implicit one here.
if (!_currentGrouping && [self groupsByEvent])
[self _beginUndoGroupingForEvent];
[[CPNotificationCenter defaultCenter]
postNotificationName:CPUndoManagerCheckpointNotification
object:self];
[self _beginUndoGrouping];
}
/* @ignore */
- (void)_beginUndoGroupingForEvent
{
[self _beginUndoGrouping];
[self _registerWithRunLoop];
}
/* @ignore */
- (void)_beginUndoGrouping
{
_currentGrouping = [_CPUndoGrouping undoGroupingWithParent:_currentGrouping];
}
/*!
Closes the current undo grouping.
@throws CPInternalInconsistencyException if no undo group is open
*/
- (void)endUndoGrouping
{
if (!_currentGrouping)
[CPException raise:CPInternalInconsistencyException reason:"endUndoGrouping. No undo group is currently open."];
var defaultCenter = [CPNotificationCenter defaultCenter];
[defaultCenter postNotificationName:CPUndoManagerCheckpointNotification
object:self];
var parent = [_currentGrouping parent];
if (!parent && [_currentGrouping invocations].length > 0)
{
[defaultCenter
postNotificationName:CPUndoManagerWillCloseUndoGroupNotification
object:self];
// Put this group on the redo stack if we are currently undoing, otherwise
// put it on the undo stack. That way, "undos" become "redos".
var stack = _state === CPUndoManagerUndoing ? _redoStack : _undoStack;
stack.push(_currentGrouping);
if (_levelsOfUndo > 0 && stack.length > _levelsOfUndo)
stack.splice(0, 1);
[defaultCenter
postNotificationName:CPUndoManagerDidCloseUndoGroupNotification
object:self];
}
// Nested Undo Grouping
else
{
[parent addInvocationsFromArray:[_currentGrouping invocations]];
[_CPUndoGrouping _poolUndoGrouping:_currentGrouping];
}
_currentGrouping = parent;
}
/*!
Enables undo registrations. Calls to this method must
be balanced with calls to disableUndoRegistration.
So, if two disable calls were made, two enable calls are required
to actually enable undo registration again.
*/
- (void)enableUndoRegistration
{
if (_disableCount <= 0)
[CPException raise:CPInternalInconsistencyException
reason:"enableUndoRegistration. There are no disable messages in effect right now."];
_disableCount--;
}
/*!
Returns \c YES if the manager groups undo operations at every iteration of the run loop.
*/
- (BOOL)groupsByEvent
{
return _groupsByEvent;
}
/*!
Sets whether the manager should group undo operations at every iteration of the run loop.
@param aFlag \c YES groups undo operations
*/
- (void)setGroupsByEvent:(BOOL)aFlag
{
aFlag = !!aFlag;
if (_groupsByEvent === aFlag)
return;
_groupsByEvent = aFlag;
if (![self groupsByEvent])
[self _unregisterWithRunLoop];
}
/*!
Returns the number of undo/redo groups.
*/
- (unsigned)groupingLevel
{
var grouping = _currentGrouping,
level = _currentGrouping ? 1 : 0;
while (grouping = [grouping parent])
++level;
return level;
}
// Disabling Undo
/*!
Disables undo registrations.
*/
- (void)disableUndoRegistration
{
++_disableCount;
}
/*!
Returns \c YES if undo registration is enabled.
*/
- (BOOL)isUndoRegistrationEnabled
{
return _disableCount == 0;
}
// Checking Whether Undo or Redo Is Being Performed
/*!
Returns \c YES if the manager is currently performing an undo.
*/
- (BOOL)isUndoing
{
return _state === CPUndoManagerUndoing;
}
/*!
Returns \c YES if the manager is currently performing a redo.
*/
- (BOOL)isRedoing
{
return _state === CPUndoManagerRedoing;
}
// Clearing Undo Operations
/*!
Clears all redo and undo operations and enables undo registrations.
*/
- (void)removeAllActions
{
// Close off any groupings.
while (_currentGrouping)
[self endUndoGrouping];
// Won't need this anymore
[self _unregisterWithRunLoop];
_state = CPUndoManagerNormal;
_redoStack = [];
_undoStack = [];
_disableCount = 0;
}
/*!
Removes any redo and undo operations that use the specified target.
@param aTarget the target for which operations should be removed.
*/
- (void)removeAllActionsWithTarget:(id)aTarget
{
[_currentGrouping removeInvocationsWithTarget:aTarget];
var index = _redoStack.length;
while (index--)
{
var grouping = _redoStack[index];
[grouping removeInvocationsWithTarget:aTarget];
if (![grouping invocations].length)
_redoStack.splice(index, 1);
}
index = _undoStack.length;
while (index--)
{
var grouping = _undoStack[index];
[grouping removeInvocationsWithTarget:aTarget];
if (![grouping invocations].length)
_undoStack.splice(index, 1);
}
}
// Managing the Action Name
/*!
Sets the name associated with the actions of the current group.
Typically, you can call this method while registering the actions for the current group.
@param anActionName the new name for the current group
*/
- (void)setActionName:(CPString)anActionName
{
if (anActionName !== nil && _currentGrouping)
[_currentGrouping setActionName:anActionName];
}
/*!
If the receiver can perform a redo, this method returns the action
name previously associated with the top grouping with
\c -setActionName:. This name should identify the action to be redone.
@return the redo action's name, or \c nil if no there's no redo on the stack.
*/
- (CPString)redoActionName
{
if (![self canRedo])
return nil;
return [[_redoStack lastObject] actionName];
}
/*!
Returns the full localized title of the actions to be displayed
as a menu item. This method first invokes [-redoActionName] and
passes it to [-redoMenuTitleForUndoActionName:] and returns the result.
*/
- (CPString)redoMenuItemTitle
{
return [self redoMenuTitleForUndoActionName:[self redoActionName]];
}
/*!
Returns the localized title of the actions to be displayed
as a menu item identified by actionName, by appending a
localized command string like @"Redo <localized(actionName)>".
*/
- (CPString)redoMenuTitleForUndoActionName:(CPString)anActionName
{
// This handles the empty string ("") case as well.
if (anActionName || anActionName === 0)
// FIXME: The terms @"Redo" and @"Redo %@" should be localized.
// KEYWORDS: Localization
return @"Redo " + anActionName;
return @"Redo";
}
/*!
If the receiver can perform an undo, this method returns the action
name previously associated with the top grouping with
\c -setActionName:. This name should identify the action to be undone.
@return the undo action name or \c nil if no if there's no undo on the stack.
*/
- (CPString)undoActionName
{
if (![self canUndo])
return nil;
return [[_undoStack lastObject] actionName];
}
/*!
Returns the full localized title of the actions to be displayed
as a menu item. This method first invokes [-undoActionName] and
passes it to [-undoMenuTitleForUndoActionName:] and returns the result.
*/
- (CPString)undoMenuItemTitle
{
return [self undoMenuTitleForUndoActionName:[self undoActionName]];
}
/*!
Returns the localized title of the actions to be displayed
as a menu item identified by actionName, by appending a
localized command string like @"Undo <localized(actionName)>".
*/
- (CPString)undoMenuTitleForUndoActionName:(CPString)anActionName
{
// This handles the empty string ("") case as well.
if (anActionName || anActionName === 0)
// FIXME: The terms @"Undo" and @"Undo %@" should be localized.
// KEYWORDS: Localization
return @"Undo " + anActionName;
return @"Undo";
}
// Working With Run Loops
/*!
Returns the CPRunLoopModes in which the receiver registers
the \c -endUndoGrouping processing when it \c -groupsByEvent.
*/
- (CPArray)runLoopModes
{
return _runLoopModes;
}
/*!
Sets the modes in which the receiver registers the calls
with the current run loop to invoke \c -endUndoGrouping
when it \c -groupsByEvent. This method first
cancels any pending registrations in the old modes and
registers the invocation in the new modes.
@param modes the modes in which calls are registered
*/
- (void)setRunLoopModes:(CPArray)modes
{
_runLoopModes = [modes copy];
if (_registeredWithRunLoop)
{
[self _unregisterWithRunLoop];
[self _registerWithRunLoop];
}
}
- (void)_runLoopEndUndoGrouping
{
[self endUndoGrouping];
_registeredWithRunLoop = NO;
}
/* @ignore */
- (void)_registerWithRunLoop
{
if (_registeredWithRunLoop)
return;
_registeredWithRunLoop = YES;
[[CPRunLoop currentRunLoop]
performSelector:@selector(_runLoopEndUndoGrouping)
target:self
argument:nil
order:CPUndoCloseGroupingRunLoopOrdering
modes:_runLoopModes];
}
/* @ignore */
- (void)_unregisterWithRunLoop
{
if (!_registeredWithRunLoop)
return;
_registeredWithRunLoop = NO;
[[CPRunLoop currentRunLoop]
cancelPerformSelector:@selector(_runLoopEndUndoGrouping)
target:self
argument:nil];
}
- (void)observeChangesForKeyPath:(CPString)aKeyPath ofObject:(id)anObject
{
[anObject addObserver:self
forKeyPath:aKeyPath
options:CPKeyValueObservingOptionOld | CPKeyValueObservingOptionNew
context:NULL];
}
- (void)stopObservingChangesForKeyPath:(CPString)aKeyPath ofObject:(id)anObject
{
[anObject removeObserver:self forKeyPath:aKeyPath];
}
- (void)observeValueForKeyPath:(CPString)aKeyPath
ofObject:(id)anObject
change:(CPDictionary)aChange
context:(id)aContext
{
// Don't add no-ops to the undo stack.
var before = [aChange valueForKey:CPKeyValueChangeOldKey],
after = [aChange valueForKey:CPKeyValueChangeNewKey];
if (before === after || (before !== nil && before.isa && (after === nil || after.isa) && [before isEqual:after]))
return;
[[self prepareWithInvocationTarget:anObject]
applyChange:[aChange inverseChangeDictionary]
toKeyPath:aKeyPath];
}
@end
var CPUndoManagerRedoStackKey = @"CPUndoManagerRedoStackKey",
CPUndoManagerUndoStackKey = @"CPUndoManagerUndoStackKey",
CPUndoManagerLevelsOfUndoKey = @"CPUndoManagerLevelsOfUndoKey",
CPUndoManagerActionNameKey = @"CPUndoManagerActionNameKey",
CPUndoManagerCurrentGroupingKey = @"CPUndoManagerCurrentGroupingKey",
CPUndoManagerRunLoopModesKey = @"CPUndoManagerRunLoopModesKey",
CPUndoManagerGroupsByEventKey = @"CPUndoManagerGroupsByEventKey";
@implementation CPUndoManager (CPCoding)
- (id)initWithCoder:(CPCoder)aCoder
{
self = [super init];
if (self)
{
_redoStack = [aCoder decodeObjectForKey:CPUndoManagerRedoStackKey];
_undoStack = [aCoder decodeObjectForKey:CPUndoManagerUndoStackKey];
_levelsOfUndo = [aCoder decodeObjectForKey:CPUndoManagerLevelsOfUndoKey];
_actionName = [aCoder decodeObjectForKey:CPUndoManagerActionNameKey];
_currentGrouping = [aCoder decodeObjectForKey:CPUndoManagerCurrentGroupingKey];
_state = CPUndoManagerNormal;
[self setRunLoopModes:[aCoder decodeObjectForKey:CPUndoManagerRunLoopModesKey]];
[self setGroupsByEvent:[aCoder decodeBoolForKey:CPUndoManagerGroupsByEventKey]];
}
return self;
}
- (void)encodeWithCoder:(CPCoder)aCoder
{
[aCoder encodeObject:_redoStack forKey:CPUndoManagerRedoStackKey];
[aCoder encodeObject:_undoStack forKey:CPUndoManagerUndoStackKey];
[aCoder encodeInt:_levelsOfUndo forKey:CPUndoManagerLevelsOfUndoKey];
[aCoder encodeObject:_actionName forKey:CPUndoManagerActionNameKey];
[aCoder encodeObject:_currentGrouping forKey:CPUndoManagerCurrentGroupingKey];
[aCoder encodeObject:_runLoopModes forKey:CPUndoManagerRunLoopModesKey];
[aCoder encodeBool:_groupsByEvent forKey:CPUndoManagerGroupsByEventKey];
}
@end
@implementation _CPUndoManagerProxy : CPProxy
{
CPUndoManager _undoManager;
}
- (CPMethodSignature)methodSignatureForSelector:(SEL)aSelector
{
return [_undoManager _methodSignatureOfPreparedTargetForSelector:aSelector];
}
- (void)forwardInvocation:(CPInvocation)anInvocation
{
[_undoManager _forwardInvocationToPreparedTarget:anInvocation];
}
@end