MouseBindings.cs 9.6 KB

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