Found and fixed another Shortcut bug

This commit is contained in:
Tig
2024-08-03 21:59:28 -06:00
parent 18ac4abab4
commit 9526b4eabd
2 changed files with 50 additions and 17 deletions

View File

@@ -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);

View File

@@ -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);
}
}