-
Notifications
You must be signed in to change notification settings - Fork 14
/
UndoRedoManager.cs
437 lines (369 loc) · 8.33 KB
/
UndoRedoManager.cs
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
using StructuredXmlEditor.View;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
//-----------------------------------------------------------------------
public class UndoRedoDescription
{
public bool IsUndo { get; set; }
public bool IsRedo { get; set; }
public string Description { get; set; }
public int CountFromCurrent { get; set; }
public bool IsSavePoint { get; set; }
public bool IsCurrentPoint { get; set; }
public UndoRedoDescription()
{
}
}
//-----------------------------------------------------------------------
public interface IUndoRedoAction
{
string Desc { get; }
void Do();
void Undo();
}
//-----------------------------------------------------------------------
public interface IValueChangeAction
{
string ValueName { get; }
object KeyObj { get; }
}
public class ValueChangeAction<T> : IValueChangeAction, IUndoRedoAction
{
public object oldData;
public object newData;
public T oldVal;
public T newVal;
public Action<T, object> setter;
public string ValueName { get; set; }
public object KeyObj { get; set; }
public string Desc { get { return "Change " + ValueName + " (" + oldVal + " -> " + newVal + ")"; } }
public ValueChangeAction(T oldVal, T newVal, Action<T, object> setter, object keyObj, string valueName)
{
this.oldVal = oldVal;
this.newVal = newVal;
this.setter = setter;
this.ValueName = valueName;
this.KeyObj = keyObj;
}
public bool IsChange
{
get
{
if (newVal == null)
{
return oldVal != null;
}
else
{
return !newVal.Equals(oldVal);
}
}
}
public void Do()
{
setter(newVal, newData);
}
public void Undo()
{
setter(oldVal, oldData);
}
}
//-----------------------------------------------------------------------
public class UndoRedoManager : NotifyPropertyChanged
{
public int GroupingMS { get; set; } = 500;
public Stack<UndoRedoGroup> UndoStack = new Stack<UndoRedoGroup>();
public Stack<UndoRedoGroup> RedoStack = new Stack<UndoRedoGroup>();
public IEnumerable<UndoRedoDescription> DescriptionStack
{
get
{
var temp = new List<UndoRedoDescription>();
int count = 0;
foreach (var item in UndoStack)
{
temp.Add(new UndoRedoDescription()
{
Description = item.Desc,
IsUndo = true,
IsSavePoint = item == savePoint,
IsCurrentPoint = count == 0,
CountFromCurrent = count++
});
}
temp.Reverse();
foreach (var item in temp) yield return item;
count = 1;
foreach (var item in RedoStack)
{
yield return new UndoRedoDescription()
{
Description = item.Desc,
IsSavePoint = item == savePoint,
IsRedo = true,
CountFromCurrent = count++
};
}
}
}
public bool IsModified
{
get
{
if (savePoint == null)
{
return UndoStack.Count != 0;
}
else
{
return UndoStack.Count == 0 ? true : UndoStack.Peek() != savePoint;
}
}
}
public bool CanUndo
{
get { return UndoStack.Count > 0; }
}
public bool CanRedo
{
get { return RedoStack.Count > 0; }
}
public UndoRedoManager()
{
}
public UsingContext DisableUndoScope()
{
--enableUndoRedo;
return new UsingContext(() =>
{
++enableUndoRedo;
});
}
public UsingContext ActionScope(string name)
{
var oldName = overrideName;
overrideName = name;
return new UsingContext(() =>
{
overrideName = oldName;
});
}
public void DoValueChange<T>(object keyObj, T prevValue, object prevData, T newValue, object newData, Action<T, object> setter, string valueName)
{
ValueChangeAction<T> desc = null;
if (enableUndoRedo != 0)
{
desc = new ValueChangeAction<T>(prevValue, newValue, setter, DateTime.Now, valueName);
desc.oldData = prevData;
desc.newData = newData;
desc.Do();
return;
}
// collapse into existing
var collapsed = false;
if (UndoStack.Count > 0)
{
var lastGroup = UndoStack.Peek();
var currentTime = DateTime.Now;
var diff = (currentTime - lastGroup.LastActionTime).TotalMilliseconds;
if (diff <= GroupingMS)
{
foreach (var action in lastGroup.Actions)
{
var valueChange = action as ValueChangeAction<T>;
if (valueChange != null)
{
if (valueChange.KeyObj == keyObj && valueChange.ValueName == valueName)
{
valueChange.newVal = newValue;
valueChange.newData = newData;
valueChange.Do();
lastGroup.LastActionTime = currentTime;
collapsed = true;
}
}
}
}
}
if (!collapsed)
{
desc = new ValueChangeAction<T>(prevValue, newValue, setter, keyObj, valueName);
desc.oldData = prevData;
desc.newData = newData;
AddUndoRedoAction(desc);
}
}
public void ApplyDoUndo(Action _do, Action _undo, string _desc = "")
{
if (overrideName != null) _desc = overrideName;
var action = new UndoRedoAction(_do, _undo, _desc);
AddUndoRedoAction(action);
}
public void AddUndoRedoAction(IUndoRedoAction action)
{
if (enableUndoRedo == 0)
{
if (isInApplyUndo)
{
Message.Show("Nested ApplyDoUndo calls! This is bad!", "Undo Redo borked", "Ok");
action.Do();
return;
}
isInApplyUndo = true;
action.Do();
RedoStack.Clear();
if (UndoStack.Count > 0)
{
var lastGroup = UndoStack.Peek();
var currentTime = DateTime.Now;
var diff = (currentTime - lastGroup.LastActionTime).TotalMilliseconds;
if (diff <= GroupingMS)
{
lastGroup.Actions.Add(action);
lastGroup.LastActionTime = currentTime;
}
else
{
var group = new UndoRedoGroup();
group.Actions.Add(action);
group.LastActionTime = currentTime;
UndoStack.Push(group);
}
}
else
{
var group = new UndoRedoGroup();
group.Actions.Add(action);
group.LastActionTime = DateTime.Now;
UndoStack.Push(group);
}
RaisePropertyChangedEvent("IsModified");
RaisePropertyChangedEvent("CanUndo");
RaisePropertyChangedEvent("CanRedo");
RaisePropertyChangedEvent("DescriptionStack");
isInApplyUndo = false;
}
else
{
action.Do();
}
}
public void Undo(int count)
{
for (int i = 0; i < count; i++)
{
Undo();
}
}
public void Redo(int count)
{
for (int i = 0; i < count; i++)
{
Redo();
}
}
public void Undo()
{
if (UndoStack.Count > 0)
{
var group = UndoStack.Pop();
group.Undo();
RedoStack.Push(group);
RaisePropertyChangedEvent("IsModified");
RaisePropertyChangedEvent("CanUndo");
RaisePropertyChangedEvent("CanRedo");
RaisePropertyChangedEvent("DescriptionStack");
}
}
public void Redo()
{
if (RedoStack.Count > 0)
{
var group = RedoStack.Pop();
group.Do();
UndoStack.Push(group);
RaisePropertyChangedEvent("IsModified");
RaisePropertyChangedEvent("CanUndo");
RaisePropertyChangedEvent("CanRedo");
RaisePropertyChangedEvent("DescriptionStack");
}
}
public void MarkSavePoint()
{
if (UndoStack.Count > 0)
{
savePoint = UndoStack.Peek();
}
else
{
savePoint = null;
}
RaisePropertyChangedEvent("IsModified");
RaisePropertyChangedEvent("DescriptionStack");
}
int enableUndoRedo;
UndoRedoGroup savePoint;
bool isInApplyUndo;
string overrideName;
}
//-----------------------------------------------------------------------
public class UndoRedoGroup
{
public List<IUndoRedoAction> Actions { get; set; } = new List<IUndoRedoAction>();
public DateTime LastActionTime { get; set; }
public void Do()
{
foreach (var action in Actions) action.Do();
}
public void Undo()
{
var reversed = Actions.ToList();
reversed.Reverse();
foreach (var action in reversed) action.Undo();
}
public string Desc
{
get
{
return string.Join(",", Actions.Select(e => e.Desc.Replace("\n", "")).Distinct());
}
}
}
//-----------------------------------------------------------------------
public class UndoRedoAction : IUndoRedoAction
{
public Action DoAction { get; set; }
public Action UndoAction { get; set; }
public string Desc { get; set; }
public UndoRedoAction(Action _do, Action _undo, string _desc)
{
this.DoAction = _do;
this.UndoAction = _undo;
this.Desc = _desc;
}
public void Do()
{
DoAction();
}
public void Undo()
{
UndoAction();
}
}
//-----------------------------------------------------------------------
public class UsingContext : IDisposable
{
private Action action;
public UsingContext(Action action)
{
this.action = action;
}
public void Dispose()
{
action();
}
}