forked from outbred/oops
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrackableScope.cs
More file actions
64 lines (59 loc) · 2.5 KB
/
Copy pathTrackableScope.cs
File metadata and controls
64 lines (59 loc) · 2.5 KB
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
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Text;
namespace oops
{
/// <summary>
/// indicates to the TrackableScopesManager where the TrackableScope should go and what state the system in in (just finished a redo? Or is this a brand new scope for some user-driven process?)
/// </summary>
public enum ScopeState
{
Do, Undo, Redo
}
/// <summary>
/// Tracks all changes within it's lifetime, aggregating them into one 'scope' or changeset. When disposed, pushes those aggregated changes
/// to the TrackableScopesManager's Undo stack. A scope undoes/redoes all changes in order from any monitored ViewModel or collection in the system.
///
/// Use like:
/// using(new TrackableScope("Dumpster diving"))
/// {
/// // put on throw away clothes
/// // open lid
/// // call family in case something happens and you die in the dumpster
/// // Find original Commodore64, put in bag. Score!
/// // Find unopened bag of mulch for your garden, toss out of the dumpster
/// // Find a very scared kitten, and...what do YOU do?
/// // Find a wedding ring and put in bag
/// // etc.
/// }
///
/// When disposed, TrackableScopesManager.Instance.Undables.First() is that collection of changes. Simply use the Undo command in the TrackableScopesManager
/// to roll everything back, and push the scope to the Redo stack. Using the Redo command in the TrackableScopesManager would then continue the game of ping pong
/// and push the scope back to the Undo stack, and so on and so forth.
/// </summary>
[DebuggerDisplay("{Accumulator.Name}")]
public class TrackableScope : IDisposable
{
private readonly ScopeState _state;
public TrackableScope(string name, ScopeState state = ScopeState.Do)
{
_state = state;
// if this isn't a 'sub' scope, then save it off to add to appropriate stack
if(Accumulator.CurrentOrNew(out var acc, name))
Accumulator = acc;
}
public Accumulator Accumulator { get; private set; } = null;
/// <inheritdoc />
public void Dispose()
{
if (Accumulator != null)
{
AccumulatorManager.Instance.Add(Accumulator, _state);
Accumulator.Current.Close(Accumulator.Name);
Accumulator = null;
}
}
}
}