فهرست منبع

Found and fixed another Shortcut bug

Tig 1 سال پیش
والد
کامیت
9526b4eabd
2فایلهای تغییر یافته به همراه50 افزوده شده و 17 حذف شده
  1. 15 0
      Terminal.Gui/Views/Shortcut.cs
  2. 35 17
      UnitTests/Views/ShortcutTests.cs

+ 15 - 0
Terminal.Gui/Views/Shortcut.cs

@@ -610,6 +610,21 @@ public class Shortcut : View, IOrientation, IDesignable
         get => _keyBindingScope;
         get => _keyBindingScope;
         set
         set
         {
         {
+            if (value == _keyBindingScope)
+            {
+                return;
+            }
+
+            if (_keyBindingScope == KeyBindingScope.Application)
+            {
+                Application.KeyBindings.Remove (Key);
+            }
+
+            if (_keyBindingScope is KeyBindingScope.HotKey or KeyBindingScope.Focused)
+            {
+                KeyBindings.Remove (Key);
+            }
+
             _keyBindingScope = value;
             _keyBindingScope = value;
 
 
             UpdateKeyBinding (Key.Empty);
             UpdateKeyBinding (Key.Empty);

+ 35 - 17
UnitTests/Views/ShortcutTests.cs

@@ -155,7 +155,29 @@ public class ShortcutTests
         Assert.Equal (Key.Empty, shortcut.Key);
         Assert.Equal (Key.Empty, shortcut.Key);
     }
     }
 
 
-    // Test KeyBindingScope
+
+    [Fact]
+    public void Key_Set_Binds_Key_To_CommandView_Accept ()
+    {
+        var shortcut = new Shortcut ();
+
+        shortcut.Key = Key.F1;
+
+        // TODO:
+    }
+
+    [Fact]
+    public void Key_Changing_Removes_Previous_Binding ()
+    {
+        Shortcut shortcut = new Shortcut ();
+
+        shortcut.Key = Key.A;
+        Assert.Contains (Key.A, shortcut.KeyBindings.Bindings.Keys);
+
+        shortcut.Key = Key.B;
+        Assert.DoesNotContain (Key.A, shortcut.KeyBindings.Bindings.Keys);
+        Assert.Contains (Key.B, shortcut.KeyBindings.Bindings.Keys);
+    }
 
 
     // Test Key gets bound correctly
     // Test Key gets bound correctly
     [Fact]
     [Fact]
@@ -177,15 +199,22 @@ public class ShortcutTests
     }
     }
 
 
     [Fact]
     [Fact]
-    public void Setting_Key_Binds_Key_To_CommandView_Accept ()
+    public void KeyBindingScope_Changing_Adjusts_KeyBindings ()
     {
     {
-        var shortcut = new Shortcut ();
+        Shortcut shortcut = new Shortcut ();
 
 
-        shortcut.Key = Key.F1;
+        shortcut.Key = Key.A;
+        Assert.Contains (Key.A, shortcut.KeyBindings.Bindings.Keys);
 
 
-        // TODO:
-    }
+        shortcut.KeyBindingScope = KeyBindingScope.Application;
+        Assert.DoesNotContain (Key.A, shortcut.KeyBindings.Bindings.Keys);
+        Assert.Contains (Key.A, Application.KeyBindings.Bindings.Keys);
 
 
+        shortcut.KeyBindingScope = KeyBindingScope.HotKey;
+        Assert.Contains (Key.A, shortcut.KeyBindings.Bindings.Keys);
+        Assert.DoesNotContain (Key.A, Application.KeyBindings.Bindings.Keys);
+    }
+    
     [Theory]
     [Theory]
     [InlineData (Orientation.Horizontal)]
     [InlineData (Orientation.Horizontal)]
     [InlineData (Orientation.Vertical)]
     [InlineData (Orientation.Vertical)]
@@ -567,20 +596,9 @@ public class ShortcutTests
         Application.OnKeyDown (key);
         Application.OnKeyDown (key);
 
 
         Assert.Equal (expectedAction, action);
         Assert.Equal (expectedAction, action);
-
         current.Dispose ();
         current.Dispose ();
     }
     }
 
 
-    [Fact]
-    public void Key_Changing_Removes_Previous ()
-    {
-        Shortcut shortcut = new Shortcut ();
 
 
-        shortcut.Key = Key.A;
-        Assert.Contains (Key.A, shortcut.KeyBindings.Bindings.Keys);
 
 
-        shortcut.Key = Key.B;
-        Assert.DoesNotContain (Key.A, shortcut.KeyBindings.Bindings.Keys);
-        Assert.Contains (Key.B, shortcut.KeyBindings.Bindings.Keys);
-    }
 }
 }