X Tutup
Skip to content

Commit 35405d1

Browse files
committed
Add undo/redo explorer
1 parent 37552ee commit 35405d1

File tree

13 files changed

+296
-19
lines changed

13 files changed

+296
-19
lines changed

src/Core/Core.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@
160160
<Compile Include="UndoRedo\Modification.cs" />
161161
<Compile Include="UndoRedo\ModificationEventArgs.cs" />
162162
<Compile Include="UndoRedo\TrackableValue.cs" />
163+
<Compile Include="UndoRedo\UndoRedoAction.cs" />
163164
<Compile Include="UndoRedo\UndoRedoEngine.cs" />
165+
<Compile Include="UndoRedo\UndoRedoEventArgs.cs" />
164166
</ItemGroup>
165167
<ItemGroup>
166168
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">

src/Core/Element.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,23 +79,22 @@ private void OnModified(ModificationEventArgs e)
7979

8080
protected Modification TrackPropertyModification<T, U>(Expression<Func<T, U>> propertySelector, U oldValue, U newValue) where T : Element
8181
{
82+
var body = (MemberExpression)propertySelector.Body;
83+
var property = (PropertyInfo)body.Member;
8284
Action undoAction = () =>
8385
{
8486
RaiseChangedEvent = false;
85-
var body = (MemberExpression) propertySelector.Body;
86-
var property = (PropertyInfo)body.Member;
87+
8788
property.SetValue(this, oldValue);
8889
RaiseChangedEvent = true;
8990
};
9091
Action redoAction = () =>
9192
{
9293
RaiseChangedEvent = false;
93-
var body = (MemberExpression) propertySelector.Body;
94-
var property = (PropertyInfo) body.Member;
9594
property.SetValue(this, newValue);
9695
RaiseChangedEvent = true;
9796
};
98-
return new Modification(undoAction, redoAction);
97+
return new Modification(undoAction, redoAction, $"Property changed: {body.Member.Name}");
9998
}
10099
}
101100
}

src/Core/UndoRedo/Modification.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ namespace NClass.Core.UndoRedo
1919
{
2020
public class Modification
2121
{
22-
public Modification(Action undoAction, Action redoAction)
22+
public Modification(Action undoAction, Action redoAction, string debugTag = "<unknown>")
2323
{
2424
UndoAction = undoAction;
2525
RedoAction = redoAction;
26+
DebugTag = debugTag;
2627
}
2728

28-
public Action RedoAction { get; set; }
29-
public Action UndoAction { get; set; }
29+
public Action RedoAction { get; }
30+
public Action UndoAction { get; }
31+
public string DebugTag { get; }
3032
}
3133
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
3+
namespace NClass.Core.UndoRedo
4+
{
5+
[Flags]
6+
public enum UndoRedoAction
7+
{
8+
UndoPush = 1,
9+
UndoPop = 1 << 1,
10+
RedoPush = 1 << 2,
11+
RedoPop = 1 << 3
12+
}
13+
}

src/Core/UndoRedo/UndoRedoEngine.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818

1919
namespace NClass.Core.UndoRedo
2020
{
21+
public delegate void UndoRedoHandler(object sender, UndoRedoEventArgs args);
22+
2123
public class UndoRedoEngine
2224
{
2325
private Stack<Modification> UndoStack { get; }
2426
private Stack<Modification> RedoStack { get; }
2527

26-
public event EventHandler UndoRedoChanged;
27-
28+
public event UndoRedoHandler UndoRedoChanged;
29+
2830
public UndoRedoEngine()
2931
{
3032
UndoStack = new Stack<Modification>(25);
@@ -38,7 +40,8 @@ public void Undo()
3840
var modification = UndoStack.Pop();
3941
modification.UndoAction();
4042
RedoStack.Push(modification);
41-
UndoRedoChanged?.Invoke(this, EventArgs.Empty);
43+
var args = new UndoRedoEventArgs(UndoRedoAction.UndoPop | UndoRedoAction.RedoPush, modification.DebugTag);
44+
UndoRedoChanged?.Invoke(this, args);
4245
}
4346

4447
public void Redo()
@@ -48,13 +51,15 @@ public void Redo()
4851
var modification = RedoStack.Pop();
4952
modification.RedoAction();
5053
UndoStack.Push(modification);
51-
UndoRedoChanged?.Invoke(this, EventArgs.Empty);
54+
var args = new UndoRedoEventArgs(UndoRedoAction.RedoPop | UndoRedoAction.UndoPush, modification.DebugTag);
55+
UndoRedoChanged?.Invoke(this, args);
5256
}
5357

5458
public void TrackModification(Modification modification)
5559
{
5660
UndoStack.Push(modification);
57-
UndoRedoChanged?.Invoke(this, EventArgs.Empty);
61+
var args = new UndoRedoEventArgs(UndoRedoAction.UndoPush, modification.DebugTag);
62+
UndoRedoChanged?.Invoke(this, args);
5863
}
5964

6065
public bool CanUndo => UndoStack.Count > 0;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
3+
namespace NClass.Core.UndoRedo
4+
{
5+
public class UndoRedoEventArgs : EventArgs
6+
{
7+
public UndoRedoEventArgs(UndoRedoAction action, string debugTag)
8+
{
9+
Action = action;
10+
DebugTag = debugTag;
11+
}
12+
public UndoRedoAction Action { get; }
13+
public string DebugTag { get; }
14+
}
15+
}

src/DiagramEditor/Diagrams/Diagram.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ protected enum State
8181
public event EventHandler SelectionChanged;
8282
public event EventHandler NeedsRedraw;
8383
public event EventHandler ClipboardAvailabilityChanged;
84-
public event EventHandler UndoRedoChanged;
84+
public event UndoRedoHandler UndoRedoChanged;
8585
public event PopupWindowEventHandler ShowingWindow;
8686
public event PopupWindowEventHandler HidingWindow;
8787
public event EventHandler Renamed;
@@ -101,7 +101,7 @@ protected Diagram()
101101
{
102102
this.Modified += OnModified;
103103
undoRedoEngine = new UndoRedoEngine();
104-
undoRedoEngine.UndoRedoChanged += (o, e) => UndoRedoChanged?.Invoke(this, EventArgs.Empty);
104+
undoRedoEngine.UndoRedoChanged += (o, e) => UndoRedoChanged?.Invoke(this, e);
105105
}
106106

107107
protected void OnModified(object sender, ModificationEventArgs e)

src/DiagramEditor/IEditable.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1515

1616
using System;
17+
using NClass.Core.UndoRedo;
1718

1819
namespace NClass.DiagramEditor
1920
{
@@ -35,6 +36,6 @@ public interface IEditable
3536
void DeleteSelectedElements();
3637
void Undo();
3738
void Redo();
38-
event EventHandler UndoRedoChanged;
39+
event UndoRedoHandler UndoRedoChanged;
3940
}
4041
}

src/GUI/GUI.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,12 @@
181181
<Compile Include="TabbedWindow.Designer.cs">
182182
<DependentUpon>TabbedWindow.cs</DependentUpon>
183183
</Compile>
184+
<Compile Include="UndoRedoExplorer.cs">
185+
<SubType>Form</SubType>
186+
</Compile>
187+
<Compile Include="UndoRedoExplorer.Designer.cs">
188+
<DependentUpon>UndoRedoExplorer.cs</DependentUpon>
189+
</Compile>
184190
<Compile Include="UpdatesChecker.cs" />
185191
<Compile Include="WindowSettings.Designer.cs">
186192
<AutoGen>True</AutoGen>
@@ -191,6 +197,9 @@
191197
<Compile Include="ZoomingToolStrip.cs">
192198
<SubType>Component</SubType>
193199
</Compile>
200+
<EmbeddedResource Include="UndoRedoExplorer.resx">
201+
<DependentUpon>UndoRedoExplorer.cs</DependentUpon>
202+
</EmbeddedResource>
194203
</ItemGroup>
195204
<ItemGroup>
196205
<ProjectReference Include="..\CodeGenerator\CodeGenerator.csproj">

src/GUI/MainForm.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using System.ComponentModel;
2222
using System.Windows.Forms;
2323
using NClass.Core;
24+
using NClass.Core.UndoRedo;
2425
using NClass.CSharp;
2526
using NClass.Java;
2627
using NClass.DiagramEditor;
@@ -39,6 +40,7 @@ public sealed partial class MainForm : Form
3940
bool showNavigator = true;
4041
DynamicMenu dynamicMenu = null;
4142
List<Plugin> plugins = new List<Plugin>();
43+
private readonly UndoRedoExplorer undoRedoExplorer = new UndoRedoExplorer();
4244

4345
public MainForm()
4446
{
@@ -54,6 +56,7 @@ public MainForm()
5456
modelExplorer.Workspace = Workspace.Default;
5557
tabbedWindow.DocumentManager = docManager;
5658
diagramNavigator.DocumentVisualizer = tabbedWindow.Canvas;
59+
undoRedoExplorer.Show(this);
5760

5861
UpdateTexts();
5962
UpdateStatusBar();
@@ -466,7 +469,7 @@ private void docManager_ActiveDocumentChanged(object sender, DocumentEventArgs e
466469
docManager.ActiveDocument.StatusChanged += ActiveDocument_StatusChanged;
467470
docManager.ActiveDocument.ClipboardAvailabilityChanged +=
468471
ActiveDocument_ClipboardAvailabilityChanged;
469-
docManager.ActiveDocument.UndoRedoChanged += ActiveDocument_UndoRedChanged;
472+
docManager.ActiveDocument.UndoRedoChanged += ActiveDocument_UndoRedoChanged;
470473
}
471474
else
472475
{
@@ -480,7 +483,7 @@ private void docManager_ActiveDocumentChanged(object sender, DocumentEventArgs e
480483
oldDocument.StatusChanged -= ActiveDocument_StatusChanged;
481484
oldDocument.ClipboardAvailabilityChanged -=
482485
ActiveDocument_ClipboardAvailabilityChanged;
483-
oldDocument.UndoRedoChanged -= ActiveDocument_UndoRedChanged;
486+
oldDocument.UndoRedoChanged -= ActiveDocument_UndoRedoChanged;
484487
}
485488

486489
UpdateStatusBar();
@@ -490,9 +493,10 @@ private void docManager_ActiveDocumentChanged(object sender, DocumentEventArgs e
490493
UpdateStandardToolStrip();
491494
}
492495

493-
private void ActiveDocument_UndoRedChanged(object sender, EventArgs e)
496+
private void ActiveDocument_UndoRedoChanged(object sender, UndoRedoEventArgs e)
494497
{
495498
UpdateUndoRedoButtons();
499+
undoRedoExplorer.Track(e);
496500
}
497501

498502
private void ActiveDocument_Modified(object sender, EventArgs e)

0 commit comments

Comments
 (0)
X Tutup