Skip to content

Commit

Permalink
KeyBindings cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tig committed Dec 8, 2024
1 parent 921d125 commit 7e289f0
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 117 deletions.
29 changes: 15 additions & 14 deletions Terminal.Gui/Input/Keyboard/KeyBindings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void Add (Key key, KeyBinding binding)
// IMPORTANT: update the memory referenced by the key, and Dictionary uses caching for performance, and thus
// IMPORTANT: Apply will update the Dictionary with the new key, but the old key will still be in the dictionary.
// IMPORTANT: See the ConfigurationManager.Illustrate_DeepMemberWiseCopy_Breaks_Dictionary test for details.
Bindings.Add (new (key), binding);
_bindings.Add (new (key), binding);
}

#pragma warning disable CS1574 // XML comment has cref attribute that could not be resolved
Expand Down Expand Up @@ -102,26 +102,27 @@ public void Add (Key key, View? target, params Command [] commands)
Add (key, binding);
}

// TODO: Add a dictionary comparer that ignores Scope
// TODO: This should not be public!
/// <summary>The collection of <see cref="KeyBinding"/> objects.</summary>
public Dictionary<Key, KeyBinding> Bindings { get; } = new (new KeyEqualityComparer ());
private readonly Dictionary<Key, KeyBinding> _bindings = new (new KeyEqualityComparer ());

/// <summary>
/// Gets the bindings bound to <paramref name="key"/>.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public IEnumerable<KeyValuePair<Key, KeyBinding>> GetBindings (Key key)
public IEnumerable<KeyValuePair<Key, KeyBinding>> GetBindings (Key? key = null)
{
return Bindings.Where (b => b.Key == key.KeyCode);
if (key is null)
{
return _bindings;
}
return _bindings.Where (b => b.Key == key.KeyCode);
}

/// <summary>
/// Gets the keys that are bound.
/// </summary>
/// <returns></returns>
public IEnumerable<Key> GetBoundKeys () { return Bindings.Keys; }
public IEnumerable<Key> GetBoundKeys () { return _bindings.Keys; }

/// <summary>
/// The view that the <see cref="KeyBindings"/> are bound to.
Expand All @@ -132,7 +133,7 @@ public IEnumerable<KeyValuePair<Key, KeyBinding>> GetBindings (Key key)
public View? Target { get; init; }

/// <summary>Removes all <see cref="KeyBinding"/> objects from the collection.</summary>
public void Clear () { Bindings.Clear (); }
public void Clear () { _bindings.Clear (); }

/// <summary>
/// Removes all key bindings that trigger the given command set. Views can have multiple different keys bound to
Expand All @@ -141,7 +142,7 @@ public IEnumerable<KeyValuePair<Key, KeyBinding>> GetBindings (Key key)
/// <param name="command"></param>
public void Clear (params Command [] command)
{
KeyValuePair<Key, KeyBinding> [] kvps = Bindings
KeyValuePair<Key, KeyBinding> [] kvps = _bindings
.Where (kvp => kvp.Value.Commands.SequenceEqual (command))
.ToArray ();

Expand Down Expand Up @@ -186,7 +187,7 @@ public Command [] GetCommands (Key key)
/// The first <see cref="Key"/> bound to the set of commands specified by <paramref name="commands"/>.
/// <see langword="null"/> if the set of caommands was not found.
/// </returns>
public Key? GetKeyFromCommands (params Command [] commands) { return Bindings.FirstOrDefault (a => a.Value.Commands.SequenceEqual (commands)).Key; }
public Key? GetKeyFromCommands (params Command [] commands) { return _bindings.FirstOrDefault (a => a.Value.Commands.SequenceEqual (commands)).Key; }

/// <summary>Gets Keys bound to the set of commands specified by <paramref name="commands"/>.</summary>
/// <param name="commands">The set of commands to search.</param>
Expand All @@ -196,7 +197,7 @@ public Command [] GetCommands (Key key)
/// </returns>
public IEnumerable<Key> GetKeysFromCommands (params Command [] commands)
{
return Bindings.Where (a => a.Value.Commands.SequenceEqual (commands)).Select (a => a.Key);
return _bindings.Where (a => a.Value.Commands.SequenceEqual (commands)).Select (a => a.Key);
}

/// <summary>Removes a <see cref="KeyBinding"/> from the collection.</summary>
Expand All @@ -208,7 +209,7 @@ public void Remove (Key key)
return;
}

Bindings.Remove (key);
_bindings.Remove (key);
}

/// <summary>Replaces the commands already bound to a key.</summary>
Expand Down Expand Up @@ -275,7 +276,7 @@ public bool TryGet (Key key, out KeyBinding binding)

if (key.IsValid)
{
return Bindings.TryGetValue (key, out binding);
return _bindings.TryGetValue (key, out binding);
}

return false;
Expand Down
8 changes: 4 additions & 4 deletions UICatalog/Scenarios/KeyBindings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Pressing Esc or {Application.QuitKey} will cause it to quit the app.
};
appWindow.Add (appBindingsListView);

foreach (var key in Application.KeyBindings.GetBoundKeys())
foreach (var key in Application.KeyBindings.GetBoundKeys ())
{
var binding = Application.KeyBindings.Get (key);
appBindings.Add ($"{key} -> {binding.Target?.GetType ().Name} - {binding.Commands [0]}");
Expand All @@ -104,7 +104,7 @@ Pressing Esc or {Application.QuitKey} will cause it to quit the app.

foreach (var subview in appWindow.Subviews)
{
foreach (var binding in subview.HotKeyBindings.Bindings)
foreach (KeyValuePair<Key, KeyBinding> binding in subview.HotKeyBindings.GetBindings ())
{
hotkeyBindings.Add ($"{binding.Key} -> {subview.GetType ().Name} - {binding.Value.Commands [0]}");
}
Expand Down Expand Up @@ -148,8 +148,8 @@ private void Application_HasFocusChanged (object sender, EventArgs e)

_focusedBindingsListView.Title = $"_Focused ({focused?.GetType ().Name}) Bindings";

_focusedBindings.Clear();
foreach (var binding in focused?.KeyBindings!.Bindings)
_focusedBindings.Clear ();
foreach (var binding in focused?.KeyBindings!.GetBindings ())
{
_focusedBindings.Add ($"{binding.Key} -> {binding.Value.Commands [0]}");
}
Expand Down
5 changes: 1 addition & 4 deletions UnitTests/Input/Keyboard/KeyBindingsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void Clear_Clears ()
public void Defaults ()
{
var keyBindings = new KeyBindings (new ());
Assert.Empty (keyBindings.Bindings);
Assert.Empty (keyBindings.GetBindings ());
Assert.Null (keyBindings.GetKeyFromCommands (Command.Accept));
Assert.NotNull (keyBindings.Target);
}
Expand Down Expand Up @@ -305,9 +305,6 @@ public void TryGet_Succeeds ()
var key = new Key (Key.Q.WithCtrl);
bool result = keyBindings.TryGet (key, out KeyBinding _);
Assert.True (result); ;

result = keyBindings.Bindings.TryGetValue (key, out KeyBinding _);
Assert.True (result);
}

[Fact]
Expand Down
6 changes: 4 additions & 2 deletions UnitTests/View/Keyboard/HotKeyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public void AddKeyBindingsForHotKey_SetsBinding_Key ()
Assert.Equal (KeyCode.Z, view.HotKey);

view.AddKeyBindingsForHotKey (view.HotKey, Key.A);
Assert.Equal (Key.A, view.HotKeyBindings.Bindings [Key.A].Key);
view.HotKeyBindings.TryGet (Key.A, out var binding);
Assert.Equal (Key.A, binding.Key);
}

[Fact]
Expand All @@ -83,7 +84,8 @@ public void AddKeyBindingsForHotKey_SetsBinding_Data ()
Assert.Equal (KeyCode.Z, view.HotKey);

view.AddKeyBindingsForHotKey (view.HotKey, Key.A, "data");
Assert.Equal ("data", view.HotKeyBindings.Bindings [Key.A].Data);
view.HotKeyBindings.TryGet (Key.A, out var binding);
Assert.Equal ("data", binding.Data);
}

[Fact]
Expand Down
Loading

0 comments on commit 7e289f0

Please sign in to comment.