InputBindings.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// Abstract base class for <see cref="KeyBindings"/> and <see cref="MouseBindings"/>.
  5. /// </summary>
  6. /// <typeparam name="TEvent">The type of the event (e.g. <see cref="Key"/> or <see cref="MouseFlags"/>).</typeparam>
  7. /// <typeparam name="TBinding">The binding type (e.g. <see cref="KeyBinding"/>).</typeparam>
  8. public abstract class InputBindings<TEvent, TBinding> where TBinding : IInputBinding, new () where TEvent : notnull
  9. {
  10. /// <summary>
  11. /// The bindings.
  12. /// </summary>
  13. private readonly Dictionary<TEvent, TBinding> _bindings;
  14. private readonly Func<Command [], TEvent, TBinding> _constructBinding;
  15. /// <summary>
  16. /// Initializes a new instance.
  17. /// </summary>
  18. /// <param name="constructBinding"></param>
  19. /// <param name="equalityComparer"></param>
  20. protected InputBindings (Func<Command [], TEvent, TBinding> constructBinding, IEqualityComparer<TEvent> equalityComparer)
  21. {
  22. _constructBinding = constructBinding;
  23. _bindings = new (equalityComparer);
  24. }
  25. /// <summary>
  26. /// Tests whether <paramref name="eventArgs"/> is valid or not.
  27. /// </summary>
  28. /// <param name="eventArgs"></param>
  29. /// <returns></returns>
  30. public abstract bool IsValid (TEvent eventArgs);
  31. /// <summary>Adds a <typeparamref name="TEvent"/> bound to <typeparamref name="TBinding"/> to the collection.</summary>
  32. /// <param name="eventArgs"></param>
  33. /// <param name="binding"></param>
  34. public void Add (TEvent eventArgs, TBinding binding)
  35. {
  36. if (!IsValid (eventArgs))
  37. {
  38. throw new ArgumentException (@"Invalid newEventArgs", nameof (eventArgs));
  39. }
  40. if (TryGet (eventArgs, out TBinding _))
  41. {
  42. throw new InvalidOperationException (@$"A binding for {eventArgs} exists ({binding}).");
  43. }
  44. // IMPORTANT: Add a COPY of the eventArgs. 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 eventArgs, but the old eventArgs will still be in the dictionary.
  47. // IMPORTANT: See the ConfigurationManager.Illustrate_DeepMemberWiseCopy_Breaks_Dictionary test for details.
  48. _bindings.Add (eventArgs, binding);
  49. }
  50. /// <summary>
  51. /// <para>Adds a new <typeparamref name="TEvent"/> that will trigger the commands in <paramref name="commands"/>.</para>
  52. /// <para>
  53. /// If the <typeparamref name="TEvent"/> is already bound to a different set of <see cref="Command"/>s it will be rebound
  54. /// <paramref name="commands"/>.
  55. /// </para>
  56. /// </summary>
  57. /// <param name="eventArgs">The event to check.</param>
  58. /// <param name="commands">
  59. /// The command to invoked on the <see cref="View"/> when <paramref name="eventArgs"/> is received. When
  60. /// multiple commands are provided,they will be applied in sequence. The bound <paramref name="eventArgs"/> event
  61. /// will be
  62. /// consumed if any took effect.
  63. /// </param>
  64. public void Add (TEvent eventArgs, params Command [] commands)
  65. {
  66. if (commands.Length == 0)
  67. {
  68. throw new ArgumentException (@"At least one command must be specified", nameof (commands));
  69. }
  70. if (TryGet (eventArgs, out TBinding? binding))
  71. {
  72. throw new InvalidOperationException (@$"A binding for {eventArgs} exists ({binding}).");
  73. }
  74. Add (eventArgs, _constructBinding (commands, eventArgs));
  75. }
  76. /// <summary>
  77. /// Gets the bindings.
  78. /// </summary>
  79. /// <returns></returns>
  80. public IEnumerable<KeyValuePair<TEvent, TBinding>> GetBindings () { return _bindings; }
  81. /// <summary>Removes all <typeparamref name="TEvent"/> objects from the collection.</summary>
  82. public void Clear () { _bindings.Clear (); }
  83. /// <summary>
  84. /// Removes all bindings that trigger the given command set. Views can have multiple different <typeparamref name="TEvent"/>
  85. /// bound to
  86. /// the same command sets and this method will clear all of them.
  87. /// </summary>
  88. /// <param name="command"></param>
  89. public void Clear (params Command [] command)
  90. {
  91. KeyValuePair<TEvent, TBinding> [] kvps = _bindings
  92. .Where (kvp => kvp.Value.Commands.SequenceEqual (command))
  93. .ToArray ();
  94. foreach (KeyValuePair<TEvent, TBinding> kvp in kvps)
  95. {
  96. Remove (kvp.Key);
  97. }
  98. }
  99. /// <summary>Gets the <typeparamref name="TBinding"/> for the specified <typeparamref name="TEvent"/>.</summary>
  100. /// <param name="eventArgs"></param>
  101. /// <returns></returns>
  102. public TBinding? Get (TEvent eventArgs)
  103. {
  104. if (TryGet (eventArgs, out TBinding? binding))
  105. {
  106. return binding;
  107. }
  108. throw new InvalidOperationException ($"{eventArgs} is not bound.");
  109. }
  110. /// <summary>Gets the commands bound with the specified <typeparamref name="TEvent"/>.</summary>
  111. /// <remarks></remarks>
  112. /// <param name="eventArgs">The <typeparamref name="TEvent"/> to check.</param>
  113. /// <param name="binding">
  114. /// When this method returns, contains the commands bound with the <typeparamref name="TEvent"/>, if the <typeparamref name="TEvent"/> is
  115. /// not
  116. /// found; otherwise, null. This parameter is passed uninitialized.
  117. /// </param>
  118. /// <returns><see langword="true"/> if the <typeparamref name="TEvent"/> is bound; otherwise <see langword="false"/>.</returns>
  119. public bool TryGet (TEvent eventArgs, out TBinding? binding) { return _bindings.TryGetValue (eventArgs, out binding); }
  120. /// <summary>Gets the array of <see cref="Command"/>s bound to <paramref name="eventArgs"/> if it exists.</summary>
  121. /// <param name="eventArgs">The key to check.</param>
  122. /// <returns>
  123. /// The array of <see cref="Command"/>s if <paramref name="eventArgs"/> is bound. An empty <see cref="Command"/> array
  124. /// if not.
  125. /// </returns>
  126. public Command [] GetCommands (TEvent eventArgs)
  127. {
  128. if (TryGet (eventArgs, out TBinding? bindings))
  129. {
  130. return bindings!.Commands;
  131. }
  132. return [];
  133. }
  134. /// <summary>
  135. /// Gets the first matching <typeparamref name="TEvent"/> bound to the set of commands specified by
  136. /// <paramref name="commands"/>.
  137. /// </summary>
  138. /// <param name="commands">The set of commands to search.</param>
  139. /// <returns>
  140. /// The first matching <typeparamref name="TEvent"/> bound to the set of commands specified by
  141. /// <paramref name="commands"/>. <see langword="null"/> if the set of caommands was not found.
  142. /// </returns>
  143. public TEvent GetFirstFromCommands (params Command [] commands) { return _bindings.FirstOrDefault (a => a.Value.Commands.SequenceEqual (commands)).Key; }
  144. /// <summary>Gets all <typeparamref name="TEvent"/> bound to the set of commands specified by <paramref name="commands"/>.</summary>
  145. /// <param name="commands">The set of commands to search.</param>
  146. /// <returns>
  147. /// The <typeparamref name="TEvent"/>s bound to the set of commands specified by <paramref name="commands"/>. An empty list if
  148. /// the
  149. /// set of caommands was not found.
  150. /// </returns>
  151. public IEnumerable<TEvent> GetAllFromCommands (params Command [] commands)
  152. {
  153. return _bindings.Where (a => a.Value.Commands.SequenceEqual (commands)).Select (a => a.Key);
  154. }
  155. /// <summary>Replaces a <typeparamref name="TEvent"/> combination already bound to a set of <see cref="Command"/>s.</summary>
  156. /// <remarks></remarks>
  157. /// <param name="oldEventArgs">The <typeparamref name="TEvent"/> to be replaced.</param>
  158. /// <param name="newEventArgs">
  159. /// The new <typeparamref name="TEvent"/> to be used.
  160. /// </param>
  161. public void Replace (TEvent oldEventArgs, TEvent newEventArgs)
  162. {
  163. if (!IsValid (newEventArgs))
  164. {
  165. throw new ArgumentException (@"Invalid newEventArgs", nameof (newEventArgs));
  166. }
  167. if (TryGet (oldEventArgs, out TBinding? binding))
  168. {
  169. Remove (oldEventArgs);
  170. Add (newEventArgs, binding!);
  171. }
  172. else
  173. {
  174. Add (newEventArgs, binding!);
  175. }
  176. }
  177. /// <summary>Replaces the commands already bound to a combination of <typeparamref name="TEvent"/>.</summary>
  178. /// <remarks>
  179. /// <para>
  180. /// If the of <typeparamref name="TEvent"/> is not already bound, it will be added.
  181. /// </para>
  182. /// </remarks>
  183. /// <param name="eventArgs">The combination of <typeparamref name="TEvent"/> bound to the command to be replaced.</param>
  184. /// <param name="newCommands">The set of commands to replace the old ones with.</param>
  185. public void ReplaceCommands (TEvent eventArgs, params Command [] newCommands)
  186. {
  187. if (TryGet (eventArgs, out TBinding _))
  188. {
  189. Remove (eventArgs);
  190. Add (eventArgs, newCommands);
  191. }
  192. else
  193. {
  194. Add (eventArgs, newCommands);
  195. }
  196. }
  197. /// <summary>Removes a <typeparamref name="TEvent"/> from the collection.</summary>
  198. /// <param name="eventArgs"></param>
  199. public void Remove (TEvent eventArgs)
  200. {
  201. if (!TryGet (eventArgs, out _))
  202. {
  203. return;
  204. }
  205. _bindings.Remove (eventArgs);
  206. }
  207. }