Skip to content

Commit bf88795

Browse files
committed
Super rough sketch of what generics solution would look like
1 parent 9f4d30d commit bf88795

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed

Terminal.Gui/Input/Keyboard/KeyBinding.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Terminal.Gui;
1111
/// <seealso cref="Application.KeyBindings"/>
1212
/// <seealso cref="View.KeyBindings"/>
1313
/// <seealso cref="Command"/>
14-
public record struct KeyBinding
14+
public record struct KeyBinding : IInputBinding
1515
{
1616
/// <summary>Initializes a new instance.</summary>
1717
/// <param name="commands">The commands this key binding will invoke.</param>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#nullable enable
2+
namespace Terminal.Gui;
3+
4+
public interface IInputBinding
5+
{
6+
Command [] Commands { get; set; }
7+
}

Terminal.Gui/Input/Mouse/MouseBinding.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Terminal.Gui;
66
/// Provides a collection of <see cref="Command"/> objects for mouse events.
77
/// </summary>
88
/// <seealso cref="Command"/>
9-
public record struct MouseBinding
9+
public record struct MouseBinding : IInputBinding
1010
{
1111
/// <summary>Initializes a new instance.</summary>
1212
/// <param name="commands">The commands this mouse binding will invoke.</param>

Terminal.Gui/Input/Mouse/MouseBindings.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,48 @@
11
#nullable enable
22
namespace Terminal.Gui;
33

4+
public abstract class Bindings<TKey, TBind> where TKey: Enum where TBind : IInputBinding, new()
5+
{
6+
private readonly Dictionary<TKey, TBind> _bindings = new ();
7+
8+
/// <summary>Adds a <see cref="MouseBinding"/> to the collection.</summary>
9+
/// <param name="mouseEventArgs"></param>
10+
/// <param name="binding"></param>
11+
public void Add (TKey mouseEventArgs, TBind binding)
12+
{
13+
if (TryGet (mouseEventArgs, out TBind _))
14+
{
15+
throw new InvalidOperationException (@$"A binding for {mouseEventArgs} exists ({binding}).");
16+
}
17+
18+
// IMPORTANT: Add a COPY of the mouseEventArgs. This is needed because ConfigurationManager.Apply uses DeepMemberWiseCopy
19+
// IMPORTANT: update the memory referenced by the key, and Dictionary uses caching for performance, and thus
20+
// IMPORTANT: Apply will update the Dictionary with the new mouseEventArgs, but the old mouseEventArgs will still be in the dictionary.
21+
// IMPORTANT: See the ConfigurationManager.Illustrate_DeepMemberWiseCopy_Breaks_Dictionary test for details.
22+
_bindings.Add (mouseEventArgs, binding);
23+
}
24+
25+
26+
/// <summary>Gets the commands bound with the specified <see cref="MouseFlags"/>.</summary>
27+
/// <remarks></remarks>
28+
/// <param name="mouseEventArgs">The key to check.</param>
29+
/// <param name="binding">
30+
/// When this method returns, contains the commands bound with the specified mouse flags, if the mouse flags are
31+
/// found; otherwise, null. This parameter is passed uninitialized.
32+
/// </param>
33+
/// <returns><see langword="true"/> if the mouse flags are bound; otherwise <see langword="false"/>.</returns>
34+
public bool TryGet (TKey mouseEventArgs, out TBind? binding)
35+
{
36+
return _bindings.TryGetValue (mouseEventArgs, out binding);
37+
}
38+
}
39+
440
/// <summary>
541
/// Provides a collection of <see cref="MouseBinding"/> objects bound to a combination of <see cref="MouseFlags"/>.
642
/// </summary>
743
/// <seealso cref="View.MouseBindings"/>
844
/// <seealso cref="Command"/>
9-
public class MouseBindings
45+
public class MouseBindings : Bindings<MouseFlags,MouseBinding>
1046
{
1147
/// <summary>
1248
/// Initializes a new instance. This constructor is used when the <see cref="MouseBindings"/> are not bound to a

0 commit comments

Comments
 (0)