2
2
using System . Collections . Generic ;
3
3
using System . ComponentModel ;
4
4
using System . Linq ;
5
+ using System . Threading . Tasks ;
6
+ using Components . Commands ;
5
7
6
8
namespace Components . Controllers
7
9
{
@@ -13,7 +15,8 @@ public class CommandInvoker
13
15
{
14
16
private static CommandInvoker _instance ;
15
17
private static readonly object Mutex = new object ( ) ;
16
- private readonly Stack < ICommand > _commandHistory = new Stack < ICommand > ( ) ;
18
+ private readonly Stack < ICommand > _executeCommandHistory = new Stack < ICommand > ( ) ;
19
+ private readonly Stack < ICommand > _undoCommandHistory = new Stack < ICommand > ( ) ;
17
20
18
21
private CommandInvoker ( )
19
22
{
@@ -40,36 +43,80 @@ public static CommandInvoker Instance
40
43
/// <returns>A list of command names in an ordered manner - recent commands come first.</returns>
41
44
public List < string > GetCommandHistory ( )
42
45
{
43
- return _commandHistory . Select ( x => x . Name ) . ToList ( ) ;
46
+ return _executeCommandHistory . Select ( x => x . Name ) . ToList ( ) ;
44
47
}
45
48
46
49
/// <summary>
47
- /// Executes a single command and adds it to the command history.
50
+ /// Executes a single command and adds it to the command history. Clears the undo history since all undoing has been done, the user now invokes commands.
48
51
/// </summary>
49
52
/// <typeparam name="T"></typeparam>
50
53
/// <param name="command">The command object whose execute methods should be called.</param>
51
- public void Execute < T > ( T command ) where T : ICommand
54
+ public async Task Execute < T > ( T command ) where T : ICommand
52
55
{
53
- command . Execute ( ) ;
54
- _commandHistory . Push ( command ) ;
56
+ var task = new Task ( ( ) =>
57
+ {
58
+ command . Execute ( ) ;
59
+ _executeCommandHistory . Push ( command ) ;
60
+ _undoCommandHistory . Clear ( ) ;
61
+ AutoSaver . Instance . Trigger ( ) ;
62
+ } ) ;
63
+
64
+ task . Start ( ) ;
65
+ await task ;
55
66
}
56
67
57
68
/// <summary>
58
69
/// Undoes a given number of commands (if that number is valid) and removes them from the command history.
70
+ /// Adds the command to the undo history for the possible redo.
59
71
/// </summary>
60
72
/// <param name="numberOfCommands">The number of commands to be undone. Default is one.</param>
61
- public void Undo ( int numberOfCommands = 1 )
73
+ public async Task Undo ( int numberOfCommands = 1 )
62
74
{
63
- if ( numberOfCommands < 1 || numberOfCommands > _commandHistory . Count )
75
+ var task = new Task ( ( ) =>
64
76
{
65
- throw new InvalidOperationException ( ) ;
66
- }
77
+ if ( numberOfCommands < 1 || numberOfCommands > _executeCommandHistory . Count )
78
+ {
79
+ throw new InvalidOperationException ( ) ;
80
+ }
67
81
68
- for ( var i = 0 ; i < numberOfCommands ; i ++ )
82
+ for ( var i = 0 ; i < numberOfCommands ; i ++ )
83
+ {
84
+ var command = _executeCommandHistory . Pop ( ) ;
85
+ command . Undo ( ) ;
86
+ _undoCommandHistory . Push ( command ) ;
87
+ }
88
+
89
+ AutoSaver . Instance . Trigger ( ) ;
90
+ } ) ;
91
+
92
+ task . Start ( ) ;
93
+ await task ;
94
+ }
95
+
96
+ /// <summary>
97
+ /// Redoes a given number of undone commands (if that number is valid) and removes them from the undo command history.
98
+ /// </summary>
99
+ /// <param name="numberOfCommands">The number of commands to be redone. Default is one.</param>
100
+ public async Task Redo ( int numberOfCommands = 1 )
101
+ {
102
+ var task = new Task ( ( ) =>
69
103
{
70
- var command = _commandHistory . Pop ( ) ;
71
- command . Undo ( ) ;
72
- }
104
+ if ( numberOfCommands < 1 || numberOfCommands > _undoCommandHistory . Count )
105
+ {
106
+ throw new InvalidOperationException ( ) ;
107
+ }
108
+
109
+ for ( var i = 0 ; i < numberOfCommands ; i ++ )
110
+ {
111
+ var command = _undoCommandHistory . Pop ( ) ;
112
+ command . Execute ( ) ;
113
+ }
114
+
115
+ AutoSaver . Instance . Trigger ( ) ;
116
+ } ) ;
117
+
118
+ task . Start ( ) ;
119
+ await task ;
73
120
}
74
121
}
75
122
}
0 commit comments