From 9002acf94255ab6b06982bfb21e2dd247c162d2e Mon Sep 17 00:00:00 2001 From: tznind Date: Mon, 9 Dec 2024 20:29:54 +0000 Subject: [PATCH] WIP move more to bind base class --- Terminal.Gui/Input/Mouse/MouseBindings.cs | 105 +++++++++++----------- 1 file changed, 50 insertions(+), 55 deletions(-) diff --git a/Terminal.Gui/Input/Mouse/MouseBindings.cs b/Terminal.Gui/Input/Mouse/MouseBindings.cs index a985d503d..d2fe089df 100644 --- a/Terminal.Gui/Input/Mouse/MouseBindings.cs +++ b/Terminal.Gui/Input/Mouse/MouseBindings.cs @@ -1,9 +1,17 @@ #nullable enable +using System.Collections.Generic; + namespace Terminal.Gui; public abstract class Bindings where TKey: Enum where TBind : IInputBinding, new() { - private readonly Dictionary _bindings = new (); + protected readonly Dictionary _bindings = new (); + private readonly Func _constructBinding; + + protected Bindings (Func constructBinding) + { + _constructBinding = constructBinding; + } /// Adds a to the collection. /// @@ -35,37 +43,7 @@ public abstract class Bindings where TKey: Enum where TBind : IInpu { return _bindings.TryGetValue (mouseEventArgs, out binding); } -} -/// -/// Provides a collection of objects bound to a combination of . -/// -/// -/// -public class MouseBindings : Bindings -{ - /// - /// Initializes a new instance. This constructor is used when the are not bound to a - /// . This is used for Application.MouseBindings and unit tests. - /// - public MouseBindings () { } - - /// Adds a to the collection. - /// - /// - public void Add (MouseFlags mouseEventArgs, MouseBinding binding) - { - if (TryGet (mouseEventArgs, out MouseBinding _)) - { - throw new InvalidOperationException (@$"A binding for {mouseEventArgs} exists ({binding})."); - } - - // IMPORTANT: Add a COPY of the mouseEventArgs. 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 mouseEventArgs, but the old mouseEventArgs will still be in the dictionary. - // IMPORTANT: See the ConfigurationManager.Illustrate_DeepMemberWiseCopy_Breaks_Dictionary test for details. - _bindings.Add (mouseEventArgs, binding); - } /// /// Adds a new mouse flag combination that will trigger the commands in . @@ -85,9 +63,9 @@ public class MouseBindings : Bindings /// will be /// consumed if any took effect. /// - public void Add (MouseFlags mouseFlags, params Command [] commands) + public void Add (TKey mouseFlags, params Command [] commands) { - if (mouseFlags == MouseFlags.None) + if (EqualityComparer.Default.Equals (mouseFlags, default)) { throw new ArgumentException (@"Invalid MouseFlag", nameof (mouseFlags)); } @@ -97,21 +75,19 @@ public class MouseBindings : Bindings throw new ArgumentException (@"At least one command must be specified", nameof (commands)); } - if (TryGet (mouseFlags, out MouseBinding binding)) + if (TryGet (mouseFlags, out var binding)) { throw new InvalidOperationException (@$"A binding for {mouseFlags} exists ({binding})."); } - Add (mouseFlags, new MouseBinding (commands, mouseFlags)); + Add (mouseFlags, _constructBinding(commands,mouseFlags)); } - private readonly Dictionary _bindings = new (); - /// /// Gets the bindings. /// /// - public IEnumerable> GetBindings () + public IEnumerable> GetBindings () { return _bindings; } @@ -126,11 +102,11 @@ public class MouseBindings : Bindings /// public void Clear (params Command [] command) { - KeyValuePair [] kvps = _bindings - .Where (kvp => kvp.Value.Commands.SequenceEqual (command)) - .ToArray (); + KeyValuePair [] kvps = _bindings + .Where (kvp => kvp.Value.Commands.SequenceEqual (command)) + .ToArray (); - foreach (KeyValuePair kvp in kvps) + foreach (KeyValuePair kvp in kvps) { Remove (kvp.Key); } @@ -139,9 +115,9 @@ public class MouseBindings : Bindings /// Gets the for the specified combination of . /// /// - public MouseBinding Get (MouseFlags mouseEventArgs) + public TBind? Get (TKey mouseEventArgs) { - if (TryGet (mouseEventArgs, out MouseBinding binding)) + if (TryGet (mouseEventArgs, out var binding)) { return binding; } @@ -149,6 +125,36 @@ public class MouseBindings : Bindings throw new InvalidOperationException ($"{mouseEventArgs} is not bound."); } + + /// Removes a from the collection. + /// + public void Remove (TKey mouseEventArgs) + { + if (!TryGet (mouseEventArgs, out var _)) + { + return; + } + + _bindings.Remove (mouseEventArgs); + } +} + +/// +/// Provides a collection of objects bound to a combination of . +/// +/// +/// +public class MouseBindings : Bindings +{ + /// + /// Initializes a new instance. This constructor is used when the are not bound to a + /// . This is used for Application.MouseBindings and unit tests. + /// + public MouseBindings ():base( + (commands, flags)=> new MouseBinding (commands, flags)) { } + + + /// /// Gets combination of bound to the set of commands specified by /// . @@ -200,17 +206,6 @@ public class MouseBindings : Bindings return _bindings.FirstOrDefault (a => a.Value.Commands.SequenceEqual (commands)).Key; } - /// Removes a from the collection. - /// - public void Remove (MouseFlags mouseEventArgs) - { - if (!TryGet (mouseEventArgs, out MouseBinding _)) - { - return; - } - - _bindings.Remove (mouseEventArgs); - } /// Replaces the commands already bound to a combination of . ///