96
|
1 module doodle.main.undo_manager;
|
|
2
|
|
3 class UndoManager : IUndoManager {
|
|
4 this() {
|
|
5 }
|
|
6
|
|
7 void addObserver(IUndoObserver observer) {
|
|
8 _observers.add(observer);
|
|
9 }
|
|
10
|
|
11 void removeObserver(IUndoObserver observer) {
|
|
12 _observers.remove(observer);
|
|
13 }
|
|
14
|
|
15 void reset() {
|
|
16 assert(!inTransaction());
|
|
17 _past.clear();
|
|
18 _future.clear();
|
|
19 foreach(IUndoObserver obs; _observers) {
|
|
20 obs.canUndo(false, "");
|
|
21 obs.canRedo(false, "");
|
|
22 }
|
|
23 }
|
|
24
|
|
25 void undo() {
|
|
26 assert(canUndo());
|
|
27 Transaction t = _past.pop();
|
|
28 t.undo();
|
|
29 _future.push(t);
|
|
30 }
|
|
31
|
|
32 void redo() {
|
|
33 assert(canRedo());
|
|
34 Transaction t = _future.pop();
|
|
35 t.redo();
|
|
36 _past.push(t);
|
|
37 }
|
|
38
|
|
39 bool canUndo() {
|
|
40 assert(!inTransaction());
|
|
41 return !_past.empty();
|
|
42 }
|
|
43
|
|
44 bool canRedo() {
|
|
45 assert(!inTransaction());
|
|
46 return !_future.empty();
|
|
47 }
|
|
48
|
|
49 void beginTransaction(char[] description) {
|
|
50 assert(!inTransaction());
|
|
51 _current_transaction = new Transaction(description);
|
|
52 }
|
|
53
|
|
54 void cancelTransaction() {
|
|
55 assert(inTransaction());
|
|
56 _current_transaction.cancel();
|
|
57 _current_transaction = null;
|
|
58 }
|
|
59
|
|
60 void endTransaction() {
|
|
61 assert(inTransaction());
|
|
62 _current_transaction.finalise();
|
|
63
|
|
64 if (!_future.empty()) {
|
|
65 _future.clear();
|
|
66 foreach(IUndoObserver obs; _observers) {
|
|
67 obs.canRedo(false, "");
|
|
68 }
|
|
69 }
|
|
70
|
|
71 _past.push(_current_transaction);
|
|
72
|
|
73 foreach(IUndoObserver obs; _observers) {
|
|
74 bs.canUndo(true, _current_transaction.name());
|
|
75 }
|
|
76
|
|
77 _current_transaction = null;
|
|
78 }
|
|
79
|
|
80 // IUndoManager implementations:
|
|
81
|
|
82 void addAction(Action action) {
|
|
83 assert(inTransaction());
|
|
84 _current_transaction.add(action);
|
|
85 }
|
|
86
|
|
87 private {
|
|
88 bool inTransaction() {
|
|
89 return _current_transaction !is null;
|
|
90 }
|
|
91
|
|
92 class Transaction {
|
|
93 enum State {
|
|
94 Accumulating,
|
|
95 Finalised,
|
|
96 Canceled
|
|
97 }
|
|
98
|
|
99 this(char[] description) {
|
|
100 _description = description;
|
|
101 _state = Accumulating;
|
|
102 }
|
|
103
|
|
104 char[] description() {
|
|
105 return _description;
|
|
106 }
|
|
107
|
|
108 void add(Action action) {
|
|
109 assert(_state == State.Accumulating);
|
|
110 _actions.addTail(action);
|
|
111 }
|
|
112
|
|
113 void finalise() {
|
|
114 assert(_state == State.Accumulating);
|
|
115 assert(!_actions.empty());
|
|
116 _finalised = true;
|
|
117 }
|
|
118
|
|
119 void cancel() {
|
|
120 assert(_state == State.Accumulating);
|
|
121 foreach_reverse(UndoAction ua; _actions) {
|
|
122 ua.undo();
|
|
123 }
|
|
124 }
|
|
125
|
|
126 void redo() {
|
|
127 assert(_finalised);
|
|
128 foreach (UndoAction ua; _actions) {
|
|
129 ua.redo();
|
|
130 }
|
|
131 }
|
|
132
|
|
133 void undo() {
|
|
134 assert(_finalised);
|
|
135 foreach_reverse(UndoAction ua; _actions) {
|
|
136 ua.undo();
|
|
137 }
|
|
138 }
|
|
139
|
|
140 private {
|
|
141 char[] _description;
|
|
142 List!(Action) _actions;
|
|
143 State _state;
|
|
144 }
|
|
145 }
|
|
146
|
|
147 Transaction _current_transaction;
|
|
148 Stack!(Transaction) _past;
|
|
149 Stack!(Transaction) _future;
|
|
150 Set!(IUndoObserver) _observers;
|
|
151 }
|
|
152 }
|