|
1 | 1 | #nullable enable
|
2 | 2 | namespace Terminal.Gui;
|
3 | 3 |
|
| 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 | + |
4 | 40 | /// <summary>
|
5 | 41 | /// Provides a collection of <see cref="MouseBinding"/> objects bound to a combination of <see cref="MouseFlags"/>.
|
6 | 42 | /// </summary>
|
7 | 43 | /// <seealso cref="View.MouseBindings"/>
|
8 | 44 | /// <seealso cref="Command"/>
|
9 |
| -public class MouseBindings |
| 45 | +public class MouseBindings : Bindings<MouseFlags,MouseBinding> |
10 | 46 | {
|
11 | 47 | /// <summary>
|
12 | 48 | /// Initializes a new instance. This constructor is used when the <see cref="MouseBindings"/> are not bound to a
|
|
0 commit comments