MouseBindings.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #nullable enable
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace Terminal.Gui;
  5. public abstract class Bindings<TKey, TBind> where TBind : IInputBinding, new()
  6. {
  7. protected readonly Dictionary<TKey, TBind> _bindings;
  8. private readonly Func<Command [], TKey, TBind> _constructBinding;
  9. protected Bindings (Func<Command [], TKey, TBind> constructBinding, IEqualityComparer<TKey> equalityComparer)
  10. {
  11. _constructBinding = constructBinding;
  12. _bindings = new (equalityComparer);
  13. }
  14. /// <summary>Adds a <see cref="MouseBinding"/> to the collection.</summary>
  15. /// <param name="mouseEventArgs"></param>
  16. /// <param name="binding"></param>
  17. public void Add (TKey mouseEventArgs, TBind binding)
  18. {
  19. if (TryGet (mouseEventArgs, out TBind _))
  20. {
  21. throw new InvalidOperationException (@$"A binding for {mouseEventArgs} exists ({binding}).");
  22. }
  23. // IMPORTANT: Add a COPY of the mouseEventArgs. This is needed because ConfigurationManager.Apply uses DeepMemberWiseCopy
  24. // IMPORTANT: update the memory referenced by the key, and Dictionary uses caching for performance, and thus
  25. // IMPORTANT: Apply will update the Dictionary with the new mouseEventArgs, but the old mouseEventArgs will still be in the dictionary.
  26. // IMPORTANT: See the ConfigurationManager.Illustrate_DeepMemberWiseCopy_Breaks_Dictionary test for details.
  27. _bindings.Add (mouseEventArgs, binding);
  28. }
  29. /// <summary>Gets the commands bound with the specified <see cref="MouseFlags"/>.</summary>
  30. /// <remarks></remarks>
  31. /// <param name="mouseEventArgs">The key to check.</param>
  32. /// <param name="binding">
  33. /// When this method returns, contains the commands bound with the specified mouse flags, if the mouse flags are
  34. /// found; otherwise, null. This parameter is passed uninitialized.
  35. /// </param>
  36. /// <returns><see langword="true"/> if the mouse flags are bound; otherwise <see langword="false"/>.</returns>
  37. public bool TryGet (TKey mouseEventArgs, out TBind? binding)
  38. {
  39. return _bindings.TryGetValue (mouseEventArgs, out binding);
  40. }
  41. /// <summary>
  42. /// <para>Adds a new mouse flag combination that will trigger the commands in <paramref name="commands"/>.</para>
  43. /// <para>
  44. /// If the key is already bound to a different array of <see cref="Command"/>s it will be rebound
  45. /// <paramref name="commands"/>.
  46. /// </para>
  47. /// </summary>
  48. /// <remarks>
  49. /// Commands are only ever applied to the current <see cref="View"/> (i.e. this feature cannot be used to switch
  50. /// focus to another view and perform multiple commands there).
  51. /// </remarks>
  52. /// <param name="mouseFlags">The mouse flags to check.</param>
  53. /// <param name="commands">
  54. /// The command to invoked on the <see cref="View"/> when <paramref name="mouseFlags"/> is received. When
  55. /// multiple commands are provided,they will be applied in sequence. The bound <paramref name="mouseFlags"/> event
  56. /// will be
  57. /// consumed if any took effect.
  58. /// </param>
  59. public void Add (TKey mouseFlags, params Command [] commands)
  60. {
  61. if (EqualityComparer<TKey>.Default.Equals (mouseFlags, default))
  62. {
  63. throw new ArgumentException (@"Invalid MouseFlag", nameof (mouseFlags));
  64. }
  65. if (commands.Length == 0)
  66. {
  67. throw new ArgumentException (@"At least one command must be specified", nameof (commands));
  68. }
  69. if (TryGet (mouseFlags, out var binding))
  70. {
  71. throw new InvalidOperationException (@$"A binding for {mouseFlags} exists ({binding}).");
  72. }
  73. Add (mouseFlags, _constructBinding(commands,mouseFlags));
  74. }
  75. /// <summary>
  76. /// Gets the bindings.
  77. /// </summary>
  78. /// <returns></returns>
  79. public IEnumerable<KeyValuePair<TKey, TBind>> GetBindings ()
  80. {
  81. return _bindings;
  82. }
  83. /// <summary>Removes all <see cref="MouseBinding"/> objects from the collection.</summary>
  84. public void Clear () { _bindings.Clear (); }
  85. /// <summary>
  86. /// Removes all bindings that trigger the given command set. Views can have multiple different events bound to
  87. /// the same command sets and this method will clear all of them.
  88. /// </summary>
  89. /// <param name="command"></param>
  90. public void Clear (params Command [] command)
  91. {
  92. KeyValuePair<TKey, TBind> [] kvps = _bindings
  93. .Where (kvp => kvp.Value.Commands.SequenceEqual (command))
  94. .ToArray ();
  95. foreach (KeyValuePair<TKey, TBind> kvp in kvps)
  96. {
  97. Remove (kvp.Key);
  98. }
  99. }
  100. /// <summary>Gets the <see cref="MouseBinding"/> for the specified combination of <see cref="MouseFlags"/>.</summary>
  101. /// <param name="mouseEventArgs"></param>
  102. /// <returns></returns>
  103. public TBind? Get (TKey mouseEventArgs)
  104. {
  105. if (TryGet (mouseEventArgs, out var binding))
  106. {
  107. return binding;
  108. }
  109. throw new InvalidOperationException ($"{mouseEventArgs} is not bound.");
  110. }
  111. /// <summary>Removes a <see cref="MouseBinding"/> from the collection.</summary>
  112. /// <param name="mouseEventArgs"></param>
  113. public void Remove (TKey mouseEventArgs)
  114. {
  115. if (!TryGet (mouseEventArgs, out var _))
  116. {
  117. return;
  118. }
  119. _bindings.Remove (mouseEventArgs);
  120. }
  121. }
  122. /// <summary>
  123. /// Provides a collection of <see cref="MouseBinding"/> objects bound to a combination of <see cref="MouseFlags"/>.
  124. /// </summary>
  125. /// <seealso cref="View.MouseBindings"/>
  126. /// <seealso cref="Command"/>
  127. public class MouseBindings : Bindings<MouseFlags,MouseBinding>
  128. {
  129. /// <summary>
  130. /// Initializes a new instance. This constructor is used when the <see cref="MouseBindings"/> are not bound to a
  131. /// <see cref="View"/>. This is used for Application.MouseBindings and unit tests.
  132. /// </summary>
  133. public MouseBindings ():base(
  134. (commands, flags)=> new MouseBinding (commands, flags),
  135. EqualityComparer<MouseFlags>.Default) { }
  136. /// <summary>
  137. /// Gets combination of <see cref="MouseFlags"/> bound to the set of commands specified by
  138. /// <paramref name="commands"/>.
  139. /// </summary>
  140. /// <param name="commands">The set of commands to search.</param>
  141. /// <returns>
  142. /// The combination of <see cref="MouseFlags"/> bound to the set of commands specified by
  143. /// <paramref name="commands"/>. An empty list if the set of caommands was not found.
  144. /// </returns>
  145. public IEnumerable<MouseFlags> GetAllMouseFlagsFromCommands (params Command [] commands)
  146. {
  147. return _bindings.Where (a => a.Value.Commands.SequenceEqual (commands)).Select (a => a.Key);
  148. }
  149. /// <summary>
  150. /// Gets the <see cref="MouseFlags"/> that are bound.
  151. /// </summary>
  152. /// <returns></returns>
  153. public IEnumerable<MouseFlags> GetBoundMouseFlags () { return _bindings.Keys; }
  154. /// <summary>Gets the array of <see cref="Command"/>s bound to <paramref name="mouseFlags"/> if it exists.</summary>
  155. /// <param name="mouseFlags">The key to check.</param>
  156. /// <returns>
  157. /// The array of <see cref="Command"/>s if <paramref name="mouseFlags"/> is bound. An empty <see cref="Command"/>
  158. /// array
  159. /// if not.
  160. /// </returns>
  161. public Command [] GetCommands (MouseFlags mouseFlags)
  162. {
  163. if (TryGet (mouseFlags, out MouseBinding bindings))
  164. {
  165. return bindings.Commands;
  166. }
  167. return [];
  168. }
  169. /// <summary>
  170. /// Gets the first combination of <see cref="MouseFlags"/> bound to the set of commands specified by
  171. /// <paramref name="commands"/>.
  172. /// </summary>
  173. /// <param name="commands">The set of commands to search.</param>
  174. /// <returns>
  175. /// The first combination of <see cref="MouseFlags"/> bound to the set of commands specified by
  176. /// <paramref name="commands"/>. <see langword="null"/> if the set of caommands was not found.
  177. /// </returns>
  178. public MouseFlags GetMouseFlagsFromCommands (params Command [] commands)
  179. {
  180. return _bindings.FirstOrDefault (a => a.Value.Commands.SequenceEqual (commands)).Key;
  181. }
  182. /// <summary>Replaces the commands already bound to a combination of <see cref="MouseFlags"/>.</summary>
  183. /// <remarks>
  184. /// <para>
  185. /// If the combination of <see cref="MouseFlags"/> is not already bound, it will be added.
  186. /// </para>
  187. /// </remarks>
  188. /// <param name="mouseEventArgs">The combination of <see cref="MouseFlags"/> bound to the command to be replaced.</param>
  189. /// <param name="newCommands">The set of commands to replace the old ones with.</param>
  190. public void ReplaceCommands (MouseFlags mouseEventArgs, params Command [] newCommands)
  191. {
  192. if (TryGet (mouseEventArgs, out MouseBinding binding))
  193. {
  194. Remove (mouseEventArgs);
  195. Add (mouseEventArgs, newCommands);
  196. }
  197. else
  198. {
  199. Add (mouseEventArgs, newCommands);
  200. }
  201. }
  202. /// <summary>Replaces a <see cref="MouseFlags"/> combination already bound to a set of <see cref="Command"/>s.</summary>
  203. /// <remarks></remarks>
  204. /// <param name="oldMouseFlags">The <see cref="MouseFlags"/> to be replaced.</param>
  205. /// <param name="newMouseFlags">
  206. /// The new <see cref="MouseFlags"/> to be used. If <see cref="Key.Empty"/> no action
  207. /// will be taken.
  208. /// </param>
  209. public void ReplaceMouseFlag (MouseFlags oldMouseFlags, MouseFlags newMouseFlags)
  210. {
  211. if (newMouseFlags == MouseFlags.None)
  212. {
  213. throw new ArgumentException (@"Invalid MouseFlag", nameof (newMouseFlags));
  214. }
  215. if (TryGet (oldMouseFlags, out MouseBinding binding))
  216. {
  217. Remove (oldMouseFlags);
  218. Add (newMouseFlags, binding);
  219. }
  220. else
  221. {
  222. Add (newMouseFlags, binding);
  223. }
  224. }
  225. /// <summary>Gets the commands bound with the specified <see cref="MouseFlags"/>.</summary>
  226. /// <remarks></remarks>
  227. /// <param name="mouseEventArgs">The key to check.</param>
  228. /// <param name="binding">
  229. /// When this method returns, contains the commands bound with the specified mouse flags, if the mouse flags are
  230. /// found; otherwise, null. This parameter is passed uninitialized.
  231. /// </param>
  232. /// <returns><see langword="true"/> if the mouse flags are bound; otherwise <see langword="false"/>.</returns>
  233. public bool TryGet (MouseFlags mouseEventArgs, out MouseBinding binding)
  234. {
  235. binding = new ([], mouseEventArgs);
  236. return _bindings.TryGetValue (mouseEventArgs, out binding);
  237. }
  238. }