Browse Source

Changed how shortcut blocking works

flabbet 3 years ago
parent
commit
c2bf47e9e8

+ 1 - 1
PixiEditor/Helpers/Behaviours/ClearFocusOnClickBehavior.cs

@@ -20,7 +20,7 @@ namespace PixiEditor.Helpers.Behaviours
         private void AssociatedObject_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
         {
             AssociatedObject.Focus();
-            ShortcutController.BlockShortcutExecution = false;
+            ShortcutController.UnblockShortcutExecutionAll();
         }
     }
 }

+ 2 - 2
PixiEditor/Helpers/Behaviours/GlobalShortcutFocusBehavior.cs

@@ -27,12 +27,12 @@ namespace PixiEditor.Helpers.Behaviours
 
         private void AssociatedObject_LostKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e)
         {
-            ShortcutController.BlockShortcutExecution = false;
+            ShortcutController.UnblockShortcutExecution("GlobalShortcutFocusBehavior");
         }
 
         private void AssociatedObject_GotKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e)
         {
-            ShortcutController.BlockShortcutExecution = true;
+            ShortcutController.BlockShortcutExection("GlobalShortcutFocusBehavior");
         }
     }
 }

+ 23 - 2
PixiEditor/Models/Controllers/Shortcuts/ShortcutController.cs

@@ -1,6 +1,8 @@
 using System;
+using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.Linq;
+using System.Windows.Documents;
 using System.Windows.Input;
 
 namespace PixiEditor.Models.Controllers.Shortcuts
@@ -12,7 +14,9 @@ namespace PixiEditor.Models.Controllers.Shortcuts
             ShortcutGroups = new ObservableCollection<ShortcutGroup>(shortcutGroups);
         }
 
-        public static bool BlockShortcutExecution { get; set; }
+        public static bool ShortcutExecutionBlocked => _shortcutExecutionBlockers.Count > 0;
+
+        private static List<string> _shortcutExecutionBlockers = new List<string>();
 
         public ObservableCollection<ShortcutGroup> ShortcutGroups { get; init; }
 
@@ -20,6 +24,23 @@ namespace PixiEditor.Models.Controllers.Shortcuts
 
         public const Key MoveViewportToolTransientChangeKey = Key.Space;
 
+        public static void BlockShortcutExection(string blocker)
+        {
+            if (_shortcutExecutionBlockers.Contains(blocker)) return;
+            _shortcutExecutionBlockers.Add(blocker);
+        }
+
+        public static void UnblockShortcutExecution(string blocker)
+        {
+            if (!_shortcutExecutionBlockers.Contains(blocker)) return;
+            _shortcutExecutionBlockers.Remove(blocker);
+        }
+
+        public static void UnblockShortcutExecutionAll()
+        {
+            _shortcutExecutionBlockers.Clear();
+        }
+
         public Shortcut GetToolShortcut<T>()
         {
             return GetToolShortcut(typeof(T));
@@ -43,7 +64,7 @@ namespace PixiEditor.Models.Controllers.Shortcuts
 
         public void KeyPressed(Key key, ModifierKeys modifiers)
         {
-            if (!BlockShortcutExecution)
+            if (!ShortcutExecutionBlocked)
             {
                 Shortcut[] shortcuts = ShortcutGroups.SelectMany(x => x.Shortcuts).ToList().FindAll(x => x.ShortcutKey == key).ToArray();
                 if (shortcuts.Length < 1)

+ 3 - 3
PixiEditor/ViewModels/SubViewModels/Main/IoViewModel.cs

@@ -65,7 +65,7 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
                 Owner.ShortcutController.LastShortcut.Command == Owner.ToolsSubViewModel.SelectToolCommand)
             {
                 restoreToolOnKeyUp = true;
-                ShortcutController.BlockShortcutExecution = true;
+                ShortcutController.BlockShortcutExection("ShortcutDown");
             }
 
             Owner.ShortcutController.KeyPressed(key, Keyboard.Modifiers);
@@ -96,7 +96,7 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             {
                 restoreToolOnKeyUp = false;
                 Owner.ToolsSubViewModel.SetActiveTool(Owner.ToolsSubViewModel.LastActionTool);
-                ShortcutController.BlockShortcutExecution = false;
+                ShortcutController.UnblockShortcutExecution("ShortcutDown");
             }
         }
 
@@ -132,7 +132,7 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             else if (Owner.ToolsSubViewModel.LastActionTool != null && Owner.ToolsSubViewModel.MoveToolIsTransient)
             {
                 Owner.ToolsSubViewModel.SetActiveTool(Owner.ToolsSubViewModel.LastActionTool);
-                ShortcutController.BlockShortcutExecution = false;
+                ShortcutController.UnblockShortcutExecution("ShortcutDown");
             }
         }
 

+ 2 - 2
PixiEditor/Views/UserControls/EditableTextBlock.xaml.cs

@@ -62,7 +62,7 @@ namespace PixiEditor.Views
 
         public void EnableEditing()
         {
-            ShortcutController.BlockShortcutExecution = true;
+            ShortcutController.BlockShortcutExection("EditableTextBlock");
             TextBlockVisibility = Visibility.Hidden;
             IsEditing = true;
             Dispatcher.BeginInvoke(
@@ -78,7 +78,7 @@ namespace PixiEditor.Views
         public void DisableEditing()
         {
             TextBlockVisibility = Visibility.Visible;
-            ShortcutController.BlockShortcutExecution = false;
+            ShortcutController.UnblockShortcutExecution("EditableTextBlock");
             IsEditing = false;
         }
 

+ 3 - 0
PixiEditor/Views/UserControls/Layers/LayersManager.xaml.cs

@@ -1,4 +1,5 @@
 using PixiEditor.Models.Controllers;
+using PixiEditor.Models.Controllers.Shortcuts;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Undo;
@@ -367,6 +368,8 @@ namespace PixiEditor.Views.UserControls.Layers
             {
                 HandleGroupOpacityChange(groupControl.GroupData, val);
             }
+
+            ShortcutController.UnblockShortcutExecutionAll();
         }
 
         private void HandleLayerOpacityChange(float val, Layer layer)