Skip to content

Commit d15b302

Browse files
committed
Equality comparer support
1 parent c18cff2 commit d15b302

File tree

2 files changed

+9
-69
lines changed

2 files changed

+9
-69
lines changed

Terminal.Gui/Input/Keyboard/KeyBindings.cs

Lines changed: 3 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -10,71 +10,10 @@ namespace Terminal.Gui;
1010
public class KeyBindings : Bindings<Key,KeyBinding>
1111
{
1212
/// <summary>Initializes a new instance bound to <paramref name="target"/>.</summary>
13-
public KeyBindings (View? target) :base((commands,key)=> new KeyBinding (commands)) { Target = target; }
13+
public KeyBindings (View? target) :base(
14+
(commands,key)=> new KeyBinding (commands),
15+
new KeyEqualityComparer ()) { Target = target; }
1416

15-
/// <summary>Adds a <see cref="KeyBinding"/> to the collection.</summary>
16-
/// <param name="key"></param>
17-
/// <param name="binding"></param>
18-
/// <exception cref="ArgumentException">If <paramref name="binding"/> has no Commands or <paramref name="key"/> is invalid.</exception>
19-
public void Add (Key key, KeyBinding binding)
20-
{
21-
22-
if (!key.IsValid)
23-
{
24-
throw new ArgumentException (nameof (key));
25-
}
26-
27-
if (binding.Commands is { Length: 0 })
28-
{
29-
throw new ArgumentException (nameof (binding));
30-
}
31-
32-
if (TryGet (key, out KeyBinding _))
33-
{
34-
throw new InvalidOperationException (@$"A key binding for {key} exists ({binding}).");
35-
36-
//Bindings [key] = binding;
37-
}
38-
39-
if (Target is { })
40-
{
41-
binding.Target = Target;
42-
}
43-
44-
// IMPORTANT: Add a COPY of the key. This is needed because ConfigurationManager.Apply uses DeepMemberWiseCopy
45-
// IMPORTANT: update the memory referenced by the key, and Dictionary uses caching for performance, and thus
46-
// IMPORTANT: Apply will update the Dictionary with the new key, but the old key will still be in the dictionary.
47-
// IMPORTANT: See the ConfigurationManager.Illustrate_DeepMemberWiseCopy_Breaks_Dictionary test for details.
48-
_bindings.Add (new (key), binding);
49-
}
50-
51-
#pragma warning disable CS1574 // XML comment has cref attribute that could not be resolved
52-
/// <summary>
53-
/// <para>
54-
/// Adds a new key combination that will trigger the commands in <paramref name="commands"/> (if supported by the
55-
/// View - see <see cref="View.GetSupportedCommands"/>).
56-
/// </para>
57-
/// <para>
58-
/// If the key is already bound to a different array of <see cref="Command"/>s it will be rebound
59-
/// <paramref name="commands"/>.
60-
/// </para>
61-
/// </summary>
62-
/// <remarks>
63-
/// Commands are only ever applied to the current <see cref="View"/> (i.e. this feature cannot be used to switch
64-
/// focus to another view and perform multiple commands there).
65-
/// </remarks>
66-
/// <param name="key">The key to check.</param>
67-
/// <param name="commands">
68-
/// The command to invoked on the <see cref="View"/> when <paramref name="key"/> is pressed. When
69-
/// multiple commands are provided,they will be applied in sequence. The bound <paramref name="key"/> strike will be
70-
/// consumed if any took effect.
71-
/// </param>
72-
/// <exception cref="ArgumentException">If <paramref name="commands"/> is empty.</exception>
73-
#pragma warning restore CS1574 // XML comment has cref attribute that could not be resolved
74-
public void Add (Key key, params Command [] commands)
75-
{
76-
Add (key, new KeyBinding (commands));
77-
}
7817

7918

8019
/// <summary>
@@ -102,8 +41,6 @@ public void Add (Key key, View? target, params Command [] commands)
10241
Add (key, binding);
10342
}
10443

105-
private readonly Dictionary<Key, KeyBinding> _bindings = new (new KeyEqualityComparer ());
106-
10744
/// <summary>
10845
/// Gets the bindings.
10946
/// </summary>

Terminal.Gui/Input/Mouse/MouseBindings.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
#nullable enable
2+
using System.Collections;
23
using System.Collections.Generic;
34

45
namespace Terminal.Gui;
56

67
public abstract class Bindings<TKey, TBind> where TBind : IInputBinding, new()
78
{
8-
protected readonly Dictionary<TKey, TBind> _bindings = new ();
9+
protected readonly Dictionary<TKey, TBind> _bindings;
910
private readonly Func<Command [], TKey, TBind> _constructBinding;
1011

11-
protected Bindings (Func<Command [], TKey, TBind> constructBinding)
12+
protected Bindings (Func<Command [], TKey, TBind> constructBinding, IEqualityComparer<TKey> equalityComparer)
1213
{
1314
_constructBinding = constructBinding;
15+
_bindings = new (equalityComparer);
1416
}
1517

1618
/// <summary>Adds a <see cref="MouseBinding"/> to the collection.</summary>
@@ -151,7 +153,8 @@ public class MouseBindings : Bindings<MouseFlags,MouseBinding>
151153
/// <see cref="View"/>. This is used for Application.MouseBindings and unit tests.
152154
/// </summary>
153155
public MouseBindings ():base(
154-
(commands, flags)=> new MouseBinding (commands, flags)) { }
156+
(commands, flags)=> new MouseBinding (commands, flags),
157+
EqualityComparer<MouseFlags>.Default) { }
155158

156159

157160

0 commit comments

Comments
 (0)