ApplicationKeyBindings.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. private Dictionary<Key, ApplicationKeyBinding> Bindings { get; } = new (new KeyEqualityComparer ());
  80. /// <summary>
  81. /// Gets the keys that are bound.
  82. /// </summary>
  83. /// <returns></returns>
  84. public IEnumerable<Key> GetBoundKeys ()
  85. {
  86. return Bindings.Keys;
  87. }
  88. /// <summary>
  89. /// Gets the bindings bound to <paramref name="key"/>.
  90. /// </summary>
  91. /// <param name="key"></param>
  92. /// <returns></returns>
  93. public IEnumerable<KeyValuePair<Key, ApplicationKeyBinding>> GetBindings (Key key)
  94. {
  95. return Bindings.Where (b => b.Key == key.KeyCode);
  96. }
  97. /// <summary>Removes all <see cref="ApplicationKeyBinding"/> objects from the collection.</summary>
  98. public void Clear () { Bindings.Clear (); }
  99. /// <summary>
  100. /// Removes all key bindings that trigger the given command set. Views can have multiple different keys bound to
  101. /// the same command sets and this method will clear all of them.
  102. /// </summary>
  103. /// <param name="command"></param>
  104. public void Clear (params Command [] command)
  105. {
  106. KeyValuePair<Key, ApplicationKeyBinding> [] kvps = Bindings
  107. .Where (kvp => kvp.Value.Commands.SequenceEqual (command))
  108. .ToArray ();
  109. foreach (KeyValuePair<Key, ApplicationKeyBinding> kvp in kvps)
  110. {
  111. Remove (kvp.Key);
  112. }
  113. }
  114. /// <summary>Gets the <see cref="ApplicationKeyBinding"/> for the specified <see cref="Key"/>.</summary>
  115. /// <param name="key"></param>
  116. /// <returns></returns>
  117. public ApplicationKeyBinding Get (Key key)
  118. {
  119. if (TryGet (key, out ApplicationKeyBinding binding))
  120. {
  121. return binding;
  122. }
  123. throw new InvalidOperationException ($"Key {key} is not bound.");
  124. }
  125. /// <summary>Gets the array of <see cref="Command"/>s bound to <paramref name="key"/> if it exists.</summary>
  126. /// <param name="key">The key to check.</param>
  127. /// <returns>
  128. /// The array of <see cref="Command"/>s if <paramref name="key"/> is bound. An empty <see cref="Command"/> array
  129. /// if not.
  130. /// </returns>
  131. public Command [] GetCommands (Key key)
  132. {
  133. if (TryGet (key, out ApplicationKeyBinding bindings))
  134. {
  135. return bindings.Commands;
  136. }
  137. return [];
  138. }
  139. /// <summary>Gets the first Key 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 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>
  142. public Key? GetKeyFromCommands (params Command [] commands)
  143. {
  144. return Bindings.FirstOrDefault (a => a.Value.Commands.SequenceEqual (commands)).Key;
  145. }
  146. /// <summary>Gets Keys bound to the set of commands specified by <paramref name="commands"/>.</summary>
  147. /// <param name="commands">The set of commands to search.</param>
  148. /// <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>
  149. public IEnumerable<Key> GetKeysFromCommands (params Command [] commands)
  150. {
  151. return Bindings.Where (a => a.Value.Commands.SequenceEqual (commands)).Select (a => a.Key);
  152. }
  153. /// <summary>Removes a <see cref="ApplicationKeyBinding"/> from the collection.</summary>
  154. /// <param name="key"></param>
  155. public void Remove (Key key)
  156. {
  157. if (!TryGet (key, out ApplicationKeyBinding _))
  158. {
  159. return;
  160. }
  161. Bindings.Remove (key);
  162. }
  163. /// <summary>Replaces the commands already bound to a key.</summary>
  164. /// <remarks>
  165. /// <para>
  166. /// If the key is not already bound, it will be added.
  167. /// </para>
  168. /// </remarks>
  169. /// <param name="key">The key bound to the command to be replaced.</param>
  170. /// <param name="newCommands">The set of commands to replace the old ones with.</param>
  171. public void ReplaceCommands (Key key, params Command [] newCommands)
  172. {
  173. if (TryGet (key, out ApplicationKeyBinding binding))
  174. {
  175. Remove (key);
  176. Add (key, binding.Target, newCommands);
  177. return;
  178. }
  179. throw new InvalidOperationException ($"Key {key} is not bound.");
  180. }
  181. /// <summary>Replaces a key combination bound to a set of <see cref="Command"/>s.</summary>
  182. /// <remarks>If <paramref name="oldKey"/> is not bound, this method has the same effect as <see cref="Add(Terminal.Gui.Key,Terminal.Gui.ApplicationKeyBinding)"/>.</remarks>
  183. /// <param name="oldKey">The key to be replaced.</param>
  184. /// <param name="newKey">The new key to be used. If <see cref="Key.Empty"/> this method has the same effect as <see cref="Remove"/>.</param>
  185. public void ReplaceKey (Key oldKey, Key newKey)
  186. {
  187. if (!newKey.IsValid)
  188. {
  189. throw new InvalidOperationException ($"Key {newKey} is is not valid.");
  190. }
  191. if (newKey == Key.Empty)
  192. {
  193. Remove (oldKey);
  194. return;
  195. }
  196. if (TryGet (oldKey, out ApplicationKeyBinding binding))
  197. {
  198. Remove (oldKey);
  199. Add (newKey, binding);
  200. }
  201. else
  202. {
  203. Add (newKey, binding);
  204. }
  205. }
  206. /// <summary>Gets the commands bound with the specified Key.</summary>
  207. /// <remarks></remarks>
  208. /// <param name="key">The key to check.</param>
  209. /// <param name="binding">
  210. /// When this method returns, contains the commands bound with the specified Key, if the Key is
  211. /// found; otherwise, null. This parameter is passed uninitialized.
  212. /// </param>
  213. /// <returns><see langword="true"/> if the Key is bound; otherwise <see langword="false"/>.</returns>
  214. public bool TryGet (Key key, out ApplicationKeyBinding binding)
  215. {
  216. binding = new ([], null);
  217. if (key.IsValid)
  218. {
  219. return Bindings.TryGetValue (key, out binding);
  220. }
  221. return false;
  222. }
  223. }