From d15b3021d115a06f343e71d331929217c414017a Mon Sep 17 00:00:00 2001 From: tznind Date: Mon, 9 Dec 2024 20:49:01 +0000 Subject: [PATCH] Equality comparer support --- Terminal.Gui/Input/Keyboard/KeyBindings.cs | 69 +--------------------- Terminal.Gui/Input/Mouse/MouseBindings.cs | 9 ++- 2 files changed, 9 insertions(+), 69 deletions(-) diff --git a/Terminal.Gui/Input/Keyboard/KeyBindings.cs b/Terminal.Gui/Input/Keyboard/KeyBindings.cs index 86aa655a7..ed3585c57 100644 --- a/Terminal.Gui/Input/Keyboard/KeyBindings.cs +++ b/Terminal.Gui/Input/Keyboard/KeyBindings.cs @@ -10,71 +10,10 @@ namespace Terminal.Gui; public class KeyBindings : Bindings { /// Initializes a new instance bound to . - public KeyBindings (View? target) :base((commands,key)=> new KeyBinding (commands)) { Target = target; } + public KeyBindings (View? target) :base( + (commands,key)=> new KeyBinding (commands), + new KeyEqualityComparer ()) { Target = target; } - /// Adds a to the collection. - /// - /// - /// If has no Commands or is invalid. - public void Add (Key key, KeyBinding binding) - { - - if (!key.IsValid) - { - throw new ArgumentException (nameof (key)); - } - - if (binding.Commands is { Length: 0 }) - { - throw new ArgumentException (nameof (binding)); - } - - if (TryGet (key, out KeyBinding _)) - { - throw new InvalidOperationException (@$"A key binding for {key} exists ({binding})."); - - //Bindings [key] = binding; - } - - if (Target is { }) - { - binding.Target = Target; - } - - // IMPORTANT: Add a COPY of the key. This is needed because ConfigurationManager.Apply uses DeepMemberWiseCopy - // 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); - } - -#pragma warning disable CS1574 // XML comment has cref attribute that could not be resolved - /// - /// - /// Adds a new key combination that will trigger the commands in (if supported by the - /// View - see ). - /// - /// - /// If the key is already bound to a different array of s it will be rebound - /// . - /// - /// - /// - /// Commands are only ever applied to the current (i.e. this feature cannot be used to switch - /// focus to another view and perform multiple commands there). - /// - /// The key to check. - /// - /// The command to invoked on the when is pressed. When - /// multiple commands are provided,they will be applied in sequence. The bound strike will be - /// consumed if any took effect. - /// - /// If is empty. -#pragma warning restore CS1574 // XML comment has cref attribute that could not be resolved - public void Add (Key key, params Command [] commands) - { - Add (key, new KeyBinding (commands)); - } /// @@ -102,8 +41,6 @@ public class KeyBindings : Bindings Add (key, binding); } - private readonly Dictionary _bindings = new (new KeyEqualityComparer ()); - /// /// Gets the bindings. /// diff --git a/Terminal.Gui/Input/Mouse/MouseBindings.cs b/Terminal.Gui/Input/Mouse/MouseBindings.cs index 37c11e63c..46ca72726 100644 --- a/Terminal.Gui/Input/Mouse/MouseBindings.cs +++ b/Terminal.Gui/Input/Mouse/MouseBindings.cs @@ -1,16 +1,18 @@ #nullable enable +using System.Collections; using System.Collections.Generic; namespace Terminal.Gui; public abstract class Bindings where TBind : IInputBinding, new() { - protected readonly Dictionary _bindings = new (); + protected readonly Dictionary _bindings; private readonly Func _constructBinding; - protected Bindings (Func constructBinding) + protected Bindings (Func constructBinding, IEqualityComparer equalityComparer) { _constructBinding = constructBinding; + _bindings = new (equalityComparer); } /// Adds a to the collection. @@ -151,7 +153,8 @@ public class MouseBindings : Bindings /// . This is used for Application.MouseBindings and unit tests. /// public MouseBindings ():base( - (commands, flags)=> new MouseBinding (commands, flags)) { } + (commands, flags)=> new MouseBinding (commands, flags), + EqualityComparer.Default) { }