MouseBindings.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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="mouseEventArgs">The mouse flags to check.</param>
  42. /// <param name="commands">
  43. /// The command to invoked on the <see cref="View"/> when <paramref name="mouseEventArgs"/> is received. When
  44. /// multiple commands are provided,they will be applied in sequence. The bound <paramref name="mouseEventArgs"/> event
  45. /// will be
  46. /// consumed if any took effect.
  47. /// </param>
  48. public void Add (MouseFlags mouseEventArgs, params Command [] commands)
  49. {
  50. if (mouseEventArgs == MouseFlags.None)
  51. {
  52. throw new ArgumentException (@"Invalid MouseFlag", nameof (commands));
  53. }
  54. if (commands.Length == 0)
  55. {
  56. throw new ArgumentException (@"At least one command must be specified", nameof (commands));
  57. }
  58. if (TryGet (mouseEventArgs, out MouseBinding binding))
  59. {
  60. throw new InvalidOperationException (@$"A binding for {mouseEventArgs} exists ({binding}).");
  61. }
  62. Add (mouseEventArgs, new MouseBinding (commands, mouseEventArgs));
  63. }
  64. // TODO: Add a dictionary comparer that ignores Scope
  65. // TODO: This should not be public!
  66. /// <summary>The collection of <see cref="MouseBinding"/> objects.</summary>
  67. public Dictionary<MouseFlags, MouseBinding> Bindings { get; } = new ();
  68. /// <summary>Removes all <see cref="MouseBinding"/> objects from the collection.</summary>
  69. public void Clear () { Bindings.Clear (); }
  70. /// <summary>
  71. /// Removes all bindings that trigger the given command set. Views can have multiple different events bound to
  72. /// the same command sets and this method will clear all of them.
  73. /// </summary>
  74. /// <param name="command"></param>
  75. public void Clear (params Command [] command)
  76. {
  77. KeyValuePair<MouseFlags, MouseBinding> [] kvps = Bindings
  78. .Where (kvp => kvp.Value.Commands.SequenceEqual (command))
  79. .ToArray ();
  80. foreach (KeyValuePair<MouseFlags, MouseBinding> kvp in kvps)
  81. {
  82. Remove (kvp.Key);
  83. }
  84. }
  85. /// <summary>Gets the <see cref="MouseBinding"/> for the specified combination of <see cref="MouseFlags"/>.</summary>
  86. /// <param name="mouseEventArgs"></param>
  87. /// <returns></returns>
  88. public MouseBinding Get (MouseFlags mouseEventArgs)
  89. {
  90. if (TryGet (mouseEventArgs, out MouseBinding binding))
  91. {
  92. return binding;
  93. }
  94. throw new InvalidOperationException ($"{mouseEventArgs} is not bound.");
  95. }
  96. /// <summary>
  97. /// Gets combination of <see cref="MouseFlags"/> bound to the set of commands specified by
  98. /// <paramref name="commands"/>.
  99. /// </summary>
  100. /// <param name="commands">The set of commands to search.</param>
  101. /// <returns>
  102. /// The combination of <see cref="MouseFlags"/> bound to the set of commands specified by
  103. /// <paramref name="commands"/>. An empty list if the set of caommands was not found.
  104. /// </returns>
  105. public IEnumerable<MouseFlags> GetAllMouseFlagsFromCommands (params Command [] commands)
  106. {
  107. return Bindings.Where (a => a.Value.Commands.SequenceEqual (commands)).Select (a => a.Key);
  108. }
  109. /// <summary>
  110. /// Gets the <see cref="MouseFlags"/> that are bound.
  111. /// </summary>
  112. /// <returns></returns>
  113. public IEnumerable<MouseFlags> GetBoundMouseFlags () { return Bindings.Keys; }
  114. /// <summary>Gets the array of <see cref="Command"/>s bound to <paramref name="mouseEventArgs"/> if it exists.</summary>
  115. /// <param name="mouseEventArgs">The key to check.</param>
  116. /// <returns>
  117. /// The array of <see cref="Command"/>s if <paramref name="mouseEventArgs"/> is bound. An empty <see cref="Command"/>
  118. /// array
  119. /// if not.
  120. /// </returns>
  121. public Command [] GetCommands (MouseFlags mouseEventArgs)
  122. {
  123. if (TryGet (mouseEventArgs, out MouseBinding bindings))
  124. {
  125. return bindings.Commands;
  126. }
  127. return [];
  128. }
  129. /// <summary>
  130. /// Gets the first combination of <see cref="MouseFlags"/> bound to the set of commands specified by
  131. /// <paramref name="commands"/>.
  132. /// </summary>
  133. /// <param name="commands">The set of commands to search.</param>
  134. /// <returns>
  135. /// The first combination of <see cref="MouseFlags"/> bound to the set of commands specified by
  136. /// <paramref name="commands"/>. <see langword="null"/> if the set of caommands was not found.
  137. /// </returns>
  138. public MouseFlags? GetMouseFlagsFromCommands (params Command [] commands)
  139. {
  140. return Bindings.FirstOrDefault (a => a.Value.Commands.SequenceEqual (commands)).Key;
  141. }
  142. /// <summary>Removes a <see cref="MouseBinding"/> from the collection.</summary>
  143. /// <param name="mouseEventArgs"></param>
  144. public void Remove (MouseFlags mouseEventArgs)
  145. {
  146. if (!TryGet (mouseEventArgs, out MouseBinding _))
  147. {
  148. return;
  149. }
  150. Bindings.Remove (mouseEventArgs);
  151. }
  152. /// <summary>Replaces the commands already bound to a combination of <see cref="MouseFlags"/>.</summary>
  153. /// <remarks>
  154. /// <para>
  155. /// If the combination of <see cref="MouseFlags"/> is not already bound, it will be added.
  156. /// </para>
  157. /// </remarks>
  158. /// <param name="mouseEventArgs">The combination of <see cref="MouseFlags"/> bound to the command to be replaced.</param>
  159. /// <param name="commands">The set of commands to replace the old ones with.</param>
  160. public void ReplaceCommands (MouseFlags mouseEventArgs, params Command [] commands)
  161. {
  162. if (TryGet (mouseEventArgs, out MouseBinding binding))
  163. {
  164. binding.Commands = commands;
  165. }
  166. else
  167. {
  168. Add (mouseEventArgs, commands);
  169. }
  170. }
  171. /// <summary>Replaces a <see cref="MouseFlags"/> combination already bound to a set of <see cref="Command"/>s.</summary>
  172. /// <remarks></remarks>
  173. /// <param name="oldMouseFlags">The <see cref="MouseFlags"/> to be replaced.</param>
  174. /// <param name="newMouseFlags">
  175. /// The new <see cref="MouseFlags"/> to be used. If <see cref="Key.Empty"/> no action
  176. /// will be taken.
  177. /// </param>
  178. public void ReplaceKey (MouseFlags oldMouseFlags, MouseFlags newMouseFlags)
  179. {
  180. if (!TryGet (oldMouseFlags, out MouseBinding _))
  181. {
  182. throw new InvalidOperationException ($"Key {oldMouseFlags} is not bound.");
  183. }
  184. MouseBinding value = Bindings [oldMouseFlags];
  185. Remove (oldMouseFlags);
  186. Add (newMouseFlags, value);
  187. }
  188. /// <summary>Gets the commands bound with the specified <see cref="MouseFlags"/>.</summary>
  189. /// <remarks></remarks>
  190. /// <param name="mouseEventArgs">The key to check.</param>
  191. /// <param name="binding">
  192. /// When this method returns, contains the commands bound with the specified mouse flags, if the mouse flags are
  193. /// found; otherwise, null. This parameter is passed uninitialized.
  194. /// </param>
  195. /// <returns><see langword="true"/> if the mouse flags are bound; otherwise <see langword="false"/>.</returns>
  196. public bool TryGet (MouseFlags mouseEventArgs, out MouseBinding binding)
  197. {
  198. binding = new ([], mouseEventArgs);
  199. return Bindings.TryGetValue (mouseEventArgs, out binding);
  200. }
  201. }