mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2026-01-01 16:59:35 +01:00
Fixed KeyBinding issue @bdisp found
This commit is contained in:
@@ -454,7 +454,7 @@ public class Shortcut : View, IOrientation, IDesignable
|
||||
SetHelpViewDefaultLayout ();
|
||||
SetKeyViewDefaultLayout ();
|
||||
ShowHide ();
|
||||
UpdateKeyBinding ();
|
||||
UpdateKeyBinding (Key.Empty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -546,9 +546,10 @@ public class Shortcut : View, IOrientation, IDesignable
|
||||
throw new ArgumentNullException ();
|
||||
}
|
||||
|
||||
Key oldKey = _key;
|
||||
_key = value;
|
||||
|
||||
UpdateKeyBinding ();
|
||||
UpdateKeyBinding (oldKey);
|
||||
|
||||
KeyView.Text = Key == Key.Empty ? string.Empty : $"{Key}";
|
||||
ShowHide ();
|
||||
@@ -567,7 +568,7 @@ public class Shortcut : View, IOrientation, IDesignable
|
||||
{
|
||||
_keyBindingScope = value;
|
||||
|
||||
UpdateKeyBinding ();
|
||||
UpdateKeyBinding (Key.Empty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -619,7 +620,7 @@ public class Shortcut : View, IOrientation, IDesignable
|
||||
KeyView.KeyBindings.Clear ();
|
||||
}
|
||||
|
||||
private void UpdateKeyBinding ()
|
||||
private void UpdateKeyBinding (Key oldKey)
|
||||
{
|
||||
if (Key != null)
|
||||
{
|
||||
@@ -629,11 +630,20 @@ public class Shortcut : View, IOrientation, IDesignable
|
||||
|
||||
if (KeyBindingScope.FastHasFlags (KeyBindingScope.Application))
|
||||
{
|
||||
if (oldKey != Key.Empty)
|
||||
{
|
||||
Application.KeyBindings.Remove (oldKey);
|
||||
}
|
||||
|
||||
Application.KeyBindings.Remove (Key);
|
||||
Application.KeyBindings.Add (Key, this, Command.Accept);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (oldKey != Key.Empty)
|
||||
{
|
||||
KeyBindings.Remove (oldKey);
|
||||
}
|
||||
KeyBindings.Remove (Key);
|
||||
KeyBindings.Add (Key, KeyBindingScope | KeyBindingScope.HotKey, Command.Accept);
|
||||
}
|
||||
|
||||
@@ -570,4 +570,26 @@ public class ShortcutTests
|
||||
|
||||
current.Dispose ();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Changing_Key_Removes_Previous ()
|
||||
{
|
||||
var newActionCount = 0;
|
||||
|
||||
Shortcut shortcut = new Shortcut (Key.N.WithCtrl, "New", () => newActionCount++);
|
||||
Application.Current = new Toplevel ();
|
||||
Application.Current.Add (shortcut);
|
||||
|
||||
Assert.Equal (0, newActionCount);
|
||||
Assert.True (Application.OnKeyDown (Key.N.WithCtrl));
|
||||
Assert.False (Application.OnKeyDown (Key.W.WithCtrl));
|
||||
Assert.Equal (1, newActionCount);
|
||||
|
||||
shortcut.Key = Key.W.WithCtrl;
|
||||
Assert.False (Application.OnKeyDown (Key.N.WithCtrl));
|
||||
Assert.True (Application.OnKeyDown (Key.W.WithCtrl));
|
||||
Assert.Equal (2, newActionCount);
|
||||
|
||||
Application.Current.Dispose ();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user