MouseBindings.cs 11 KB

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