Browse Source

Terminate ToolSession on undo/redo (fix #296)

Equbuxu 3 years ago
parent
commit
ffe2eab672

+ 3 - 1
PixiEditor/Models/Controllers/BitmapManager.cs

@@ -86,7 +86,7 @@ namespace PixiEditor.Models.Controllers
         private ToolSession activeSession = null;
 
 
-        public BitmapManager(ToolsViewModel tools)
+        public BitmapManager(ToolsViewModel tools, UndoViewModel undo)
         {
             _tools = tools;
 
@@ -98,6 +98,8 @@ namespace PixiEditor.Models.Controllers
             ToolSessionController.KeyStateChanged += (_, _) => UpdateActionDisplay(_tools.ActiveTool);
             BitmapOperations = new BitmapOperationsUtility(this, tools);
 
+            undo.UndoRedoCalled += (_, _) => ToolSessionController.ForceStopActiveSessionIfAny();
+
             DocumentChanged += BitmapManager_DocumentChanged;
 
             _highlightPen = new PenTool(this)

+ 3 - 3
PixiEditor/Models/Controllers/UndoManager.cs

@@ -1,10 +1,10 @@
+using PixiEditor.Models.Undo;
+using PixiEditor.ViewModels;
 using System;
 using System.Collections.Generic;
 using System.Diagnostics;
 using System.Linq;
 using System.Reflection;
-using PixiEditor.Models.Undo;
-using PixiEditor.ViewModels;
 
 namespace PixiEditor.Models.Controllers
 {
@@ -210,4 +210,4 @@ namespace PixiEditor.Models.Controllers
             return new Tuple<PropertyInfo, object>(target.GetType().GetProperty(bits.Last()), target);
         }
     }
-}
+}

+ 13 - 2
PixiEditor/ViewModels/SubViewModels/Main/UndoViewModel.cs

@@ -1,5 +1,6 @@
 using PixiEditor.Helpers;
 using PixiEditor.Models.Undo;
+using System;
 using System.IO;
 
 namespace PixiEditor.ViewModels.SubViewModels.Main
@@ -10,6 +11,8 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
 
         public RelayCommand RedoCommand { get; set; }
 
+        public event EventHandler UndoRedoCalled;
+
         public UndoViewModel(ViewModelMain owner)
             : base(owner)
         {
@@ -29,7 +32,11 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
         /// <param name="parameter">CommandProperty.</param>
         public void Redo(object parameter)
         {
-            Owner.BitmapManager.ActiveDocument.UndoManager.Redo();
+            UndoRedoCalled?.Invoke(this, EventArgs.Empty);
+
+            //sometimes CanRedo gets changed after UndoRedoCalled invoke, so check again (normally this is checked by the relaycommand)
+            if (CanRedo(null))
+                Owner.BitmapManager.ActiveDocument.UndoManager.Redo();
         }
 
         /// <summary>
@@ -38,7 +45,11 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
         /// <param name="parameter">CommandParameter.</param>
         public void Undo(object parameter)
         {
-            Owner.BitmapManager.ActiveDocument.UndoManager.Undo();
+            UndoRedoCalled?.Invoke(this, EventArgs.Empty);
+
+            //sometimes CanUndo gets changed after UndoRedoCalled invoke, so check again (normally this is checked by the relaycommand)
+            if (CanUndo(null))
+                Owner.BitmapManager.ActiveDocument.UndoManager.Undo();
         }
 
         /// <summary>