diff --git a/Terminal.Gui/Views/Shortcut.cs b/Terminal.Gui/Views/Shortcut.cs index 02aea5332..c5e023ee7 100644 --- a/Terminal.Gui/Views/Shortcut.cs +++ b/Terminal.Gui/Views/Shortcut.cs @@ -610,6 +610,21 @@ public class Shortcut : View, IOrientation, IDesignable get => _keyBindingScope; 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; UpdateKeyBinding (Key.Empty); diff --git a/UnitTests/Views/ShortcutTests.cs b/UnitTests/Views/ShortcutTests.cs index ef32ec67b..e46e7f199 100644 --- a/UnitTests/Views/ShortcutTests.cs +++ b/UnitTests/Views/ShortcutTests.cs @@ -155,7 +155,29 @@ public class ShortcutTests 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 [Fact] @@ -177,15 +199,22 @@ public class ShortcutTests } [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] [InlineData (Orientation.Horizontal)] [InlineData (Orientation.Vertical)] @@ -567,20 +596,9 @@ public class ShortcutTests Application.OnKeyDown (key); Assert.Equal (expectedAction, action); - 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); - } }