MouseBindings.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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="mouseFlag"></param>
  17. /// <param name="binding"></param>
  18. public void Add (MouseFlags mouseFlag, MouseBinding binding)
  19. {
  20. if (TryGet (mouseFlag, out MouseBinding _))
  21. {
  22. throw new InvalidOperationException (@$"A binding for {mouseFlag} exists ({binding}).");
  23. //Bindings [key] = binding;
  24. }
  25. // IMPORTANT: Add a COPY of the key. This is needed because ConfigurationManager.Apply uses DeepMemberWiseCopy
  26. // IMPORTANT: update the memory referenced by the key, and Dictionary uses caching for performance, and thus
  27. // IMPORTANT: Apply will update the Dictionary with the new key, but the old key will still be in the dictionary.
  28. // IMPORTANT: See the ConfigurationManager.Illustrate_DeepMemberWiseCopy_Breaks_Dictionary test for details.
  29. Bindings.Add (mouseFlag, binding);
  30. }
  31. /// <summary>
  32. /// <para>Adds a new mouse flag combination that will trigger the commands in <paramref name="commands"/>.</para>
  33. /// <para>
  34. /// If the key is already bound to a different array of <see cref="Command"/>s it will be rebound
  35. /// <paramref name="commands"/>.
  36. /// </para>
  37. /// </summary>
  38. /// <remarks>
  39. /// Commands are only ever applied to the current <see cref="View"/> (i.e. this feature cannot be used to switch
  40. /// focus to another view and perform multiple commands there).
  41. /// </remarks>
  42. /// <param name="mouseFlags">The mouse flags to check.</param>
  43. /// <param name="commands">
  44. /// The command to invoked on the <see cref="View"/> when <paramref name="mouseFlags"/> is received. When
  45. /// multiple commands are provided,they will be applied in sequence. The bound <paramref name="mouseFlags"/> event 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 (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 (mouseFlags, out MouseBinding binding))
  59. {
  60. throw new InvalidOperationException (@$"A binding for {mouseFlags} exists ({binding}).");
  61. }
  62. Add (mouseFlags, new MouseBinding (commands));
  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>
  69. /// Gets the <see cref="MouseFlags"/> that are bound.
  70. /// </summary>
  71. /// <returns></returns>
  72. public IEnumerable<MouseFlags> GetBoundMouseFlags ()
  73. {
  74. return Bindings.Keys;
  75. }
  76. /// <summary>Removes all <see cref="MouseBinding"/> objects from the collection.</summary>
  77. public void Clear () { Bindings.Clear (); }
  78. /// <summary>
  79. /// Removes all bindings that trigger the given command set. Views can have multiple different events bound to
  80. /// the same command sets and this method will clear all of them.
  81. /// </summary>
  82. /// <param name="command"></param>
  83. public void Clear (params Command [] command)
  84. {
  85. KeyValuePair<MouseFlags, MouseBinding> [] kvps = Bindings
  86. .Where (kvp => kvp.Value.Commands.SequenceEqual (command))
  87. .ToArray ();
  88. foreach (KeyValuePair<MouseFlags, MouseBinding> kvp in kvps)
  89. {
  90. Remove (kvp.Key);
  91. }
  92. }
  93. /// <summary>Gets the <see cref="MouseBinding"/> for the specified combination of <see cref="MouseFlags"/>.</summary>
  94. /// <param name="mouseFlags"></param>
  95. /// <returns></returns>
  96. public MouseBinding Get (MouseFlags mouseFlags)
  97. {
  98. if (TryGet (mouseFlags, out MouseBinding binding))
  99. {
  100. return binding;
  101. }
  102. throw new InvalidOperationException ($"{mouseFlags} is not bound.");
  103. }
  104. /// <summary>Gets the array of <see cref="Command"/>s bound to <paramref name="mouseFlags"/> if it exists.</summary>
  105. /// <param name="mouseFlags">The key to check.</param>
  106. /// <returns>
  107. /// The array of <see cref="Command"/>s if <paramref name="mouseFlags"/> is bound. An empty <see cref="Command"/> array
  108. /// if not.
  109. /// </returns>
  110. public Command [] GetCommands (MouseFlags mouseFlags)
  111. {
  112. if (TryGet (mouseFlags, out MouseBinding bindings))
  113. {
  114. return bindings.Commands;
  115. }
  116. return [];
  117. }
  118. /// <summary>Gets the first combination of <see cref="MouseFlags"/> bound to the set of commands specified by <paramref name="commands"/>.</summary>
  119. /// <param name="commands">The set of commands to search.</param>
  120. /// <returns>The first combination of <see cref="MouseFlags"/> bound to the set of commands specified by <paramref name="commands"/>. <see langword="null"/> if the set of caommands was not found.</returns>
  121. public MouseFlags? GetMouseFlagsFromCommands (params Command [] commands)
  122. {
  123. return Bindings.FirstOrDefault (a => a.Value.Commands.SequenceEqual (commands)).Key;
  124. }
  125. /// <summary>Gets combination of <see cref="MouseFlags"/> bound to the set of commands specified by <paramref name="commands"/>.</summary>
  126. /// <param name="commands">The set of commands to search.</param>
  127. /// <returns>The combination of <see cref="MouseFlags"/> bound to the set of commands specified by <paramref name="commands"/>. An empty list if the set of caommands was not found.</returns>
  128. public IEnumerable<MouseFlags> GetAllMouseFlagsFromCommands (params Command [] commands)
  129. {
  130. return Bindings.Where (a => a.Value.Commands.SequenceEqual (commands)).Select (a => a.Key);
  131. }
  132. /// <summary>Removes a <see cref="MouseBinding"/> from the collection.</summary>
  133. /// <param name="mouseFlags"></param>
  134. public void Remove (MouseFlags mouseFlags)
  135. {
  136. if (!TryGet (mouseFlags, out MouseBinding _))
  137. {
  138. return;
  139. }
  140. Bindings.Remove (mouseFlags);
  141. }
  142. /// <summary>Replaces the commands already bound to a combination of <see cref="MouseFlags"/>.</summary>
  143. /// <remarks>
  144. /// <para>
  145. /// If the combination of <see cref="MouseFlags"/> is not already bound, it will be added.
  146. /// </para>
  147. /// </remarks>
  148. /// <param name="mouseFlags">The combination of <see cref="MouseFlags"/> bound to the command to be replaced.</param>
  149. /// <param name="commands">The set of commands to replace the old ones with.</param>
  150. public void ReplaceCommands (MouseFlags mouseFlags, params Command [] commands)
  151. {
  152. if (TryGet (mouseFlags, out MouseBinding binding))
  153. {
  154. binding.Commands = commands;
  155. }
  156. else
  157. {
  158. Add (mouseFlags, commands);
  159. }
  160. }
  161. /// <summary>Replaces a <see cref="MouseFlags"/> combination already bound to a set of <see cref="Command"/>s.</summary>
  162. /// <remarks></remarks>
  163. /// <param name="oldMouseFlags">The <see cref="MouseFlags"/> to be replaced.</param>
  164. /// <param name="newMouseFlags">The new <see cref="MouseFlags"/> to be used. If <see cref="Key.Empty"/> no action will be taken.</param>
  165. public void ReplaceKey (MouseFlags oldMouseFlags, MouseFlags newMouseFlags)
  166. {
  167. if (!TryGet (oldMouseFlags, out MouseBinding _))
  168. {
  169. throw new InvalidOperationException ($"Key {oldMouseFlags} is not bound.");
  170. }
  171. MouseBinding value = Bindings [oldMouseFlags];
  172. Remove (oldMouseFlags);
  173. Add (newMouseFlags, value);
  174. }
  175. /// <summary>Gets the commands bound with the specified <see cref="MouseFlags"/>.</summary>
  176. /// <remarks></remarks>
  177. /// <param name="mouseFlags">The key to check.</param>
  178. /// <param name="binding">
  179. /// When this method returns, contains the commands bound with the specified mouse flags, if the mouse flags are
  180. /// found; otherwise, null. This parameter is passed uninitialized.
  181. /// </param>
  182. /// <returns><see langword="true"/> if the mouse flags are bound; otherwise <see langword="false"/>.</returns>
  183. public bool TryGet (MouseFlags mouseFlags, out MouseBinding binding)
  184. {
  185. binding = new ([]);
  186. return Bindings.TryGetValue (mouseFlags, out binding);
  187. }
  188. }