ApplicationKeyBindings.cs 9.4 KB

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