KeyBindings.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #nullable enable
  2. using System.Diagnostics;
  3. namespace Terminal.Gui;
  4. /// <summary>
  5. /// Provides a collection of <see cref="KeyBinding"/> objects bound to a <see cref="Key"/>.
  6. /// </summary>
  7. public class KeyBindings
  8. {
  9. /// <summary>
  10. /// Initializes a new instance. This constructor is used when the <see cref="KeyBindings"/> are not bound to a
  11. /// <see cref="View"/>, such as in unit tests.
  12. /// </summary>
  13. public KeyBindings () { }
  14. /// <summary>Initializes a new instance bound to <paramref name="boundView"/>.</summary>
  15. public KeyBindings (View boundView) { BoundView = boundView; }
  16. /// <summary>
  17. /// The view that the <see cref="KeyBindings"/> are bound to.
  18. /// </summary>
  19. public View? BoundView { get; }
  20. // TODO: Add a dictionary comparer that ignores Scope
  21. // TODO: This should not be public!
  22. /// <summary>The collection of <see cref="KeyBinding"/> objects.</summary>
  23. public Dictionary<Key, KeyBinding> Bindings { get; } = new ();
  24. /// <summary>Adds a <see cref="KeyBinding"/> to the collection.</summary>
  25. /// <param name="key"></param>
  26. /// <param name="binding"></param>
  27. public void Add (Key key, KeyBinding binding)
  28. {
  29. if (TryGet (key, out KeyBinding _))
  30. {
  31. Bindings [key] = binding;
  32. }
  33. else
  34. {
  35. Bindings.Add (key, binding);
  36. if (binding.Scope.HasFlag (KeyBindingScope.Application))
  37. {
  38. Application.AddKeyBinding (key, BoundView);
  39. }
  40. }
  41. }
  42. /// <summary>
  43. /// <para>Adds a new key combination that will trigger the commands in <paramref name="commands"/>.</para>
  44. /// <para>
  45. /// If the key is already bound to a different array of <see cref="Command"/>s it will be rebound
  46. /// <paramref name="commands"/>.
  47. /// </para>
  48. /// </summary>
  49. /// <remarks>
  50. /// Commands are only ever applied to the current <see cref="View"/> (i.e. this feature cannot be used to switch
  51. /// focus to another view and perform multiple commands there).
  52. /// </remarks>
  53. /// <param name="key">The key to check.</param>
  54. /// <param name="scope">The scope for the command.</param>
  55. /// <param name="commands">
  56. /// The command to invoked on the <see cref="View"/> when <paramref name="key"/> is pressed. When
  57. /// multiple commands are provided,they will be applied in sequence. The bound <paramref name="key"/> strike will be
  58. /// consumed if any took effect.
  59. /// </param>
  60. public void Add (Key key, KeyBindingScope scope, params Command [] commands)
  61. {
  62. if (key is null || !key.IsValid)
  63. {
  64. //throw new ArgumentException ("Invalid Key", nameof (commands));
  65. return;
  66. }
  67. if (commands.Length == 0)
  68. {
  69. throw new ArgumentException (@"At least one command must be specified", nameof (commands));
  70. }
  71. if (TryGet (key, out KeyBinding _))
  72. {
  73. Bindings [key] = new (commands, scope);
  74. }
  75. else
  76. {
  77. Add (key, new KeyBinding (commands, scope));
  78. }
  79. }
  80. /// <summary>
  81. /// <para>
  82. /// Adds a new key combination that will trigger the commands in <paramref name="commands"/> (if supported by the
  83. /// View - see <see cref="View.GetSupportedCommands"/>).
  84. /// </para>
  85. /// <para>
  86. /// This is a helper function for <see cref="Add(Key,KeyBindingScope,Terminal.Gui.Command[])"/> for
  87. /// <see cref="KeyBindingScope.Focused"/> scoped commands.
  88. /// </para>
  89. /// <para>
  90. /// If the key is already bound to a different array of <see cref="Command"/>s it will be rebound
  91. /// <paramref name="commands"/>.
  92. /// </para>
  93. /// </summary>
  94. /// <remarks>
  95. /// Commands are only ever applied to the current <see cref="View"/> (i.e. this feature cannot be used to switch
  96. /// focus to another view and perform multiple commands there).
  97. /// </remarks>
  98. /// <param name="key">The key to check.</param>
  99. /// <param name="commands">
  100. /// The command to invoked on the <see cref="View"/> when <paramref name="key"/> is pressed. When
  101. /// multiple commands are provided,they will be applied in sequence. The bound <paramref name="key"/> strike will be
  102. /// consumed if any took effect.
  103. /// </param>
  104. public void Add (Key key, params Command [] commands)
  105. {
  106. Add (key, KeyBindingScope.Focused, commands);
  107. }
  108. /// <summary>Removes all <see cref="KeyBinding"/> objects from the collection.</summary>
  109. public void Clear ()
  110. {
  111. Application.ClearKeyBindings (BoundView);
  112. Bindings.Clear ();
  113. }
  114. /// <summary>
  115. /// Removes all key bindings that trigger the given command set. Views can have multiple different keys bound to
  116. /// the same command sets and this method will clear all of them.
  117. /// </summary>
  118. /// <param name="command"></param>
  119. public void Clear (params Command [] command)
  120. {
  121. KeyValuePair<Key, KeyBinding> [] kvps = Bindings
  122. .Where (kvp => kvp.Value.Commands.SequenceEqual (command))
  123. .ToArray ();
  124. foreach (KeyValuePair<Key, KeyBinding> kvp in kvps)
  125. {
  126. Remove (kvp.Key);
  127. }
  128. }
  129. /// <summary>Gets the <see cref="KeyBinding"/> for the specified <see cref="Key"/>.</summary>
  130. /// <param name="key"></param>
  131. /// <returns></returns>
  132. public KeyBinding Get (Key key)
  133. {
  134. if (TryGet (key, out KeyBinding binding))
  135. {
  136. return binding;
  137. }
  138. throw new InvalidOperationException ($"Key {key} is not bound.");
  139. }
  140. /// <summary>Gets the <see cref="KeyBinding"/> for the specified <see cref="Key"/>.</summary>
  141. /// <param name="key"></param>
  142. /// <param name="scope"></param>
  143. /// <returns></returns>
  144. public KeyBinding Get (Key key, KeyBindingScope scope)
  145. {
  146. if (TryGet (key, scope, out KeyBinding binding))
  147. {
  148. return binding;
  149. }
  150. throw new InvalidOperationException ($"Key {key}/{scope} is not bound.");
  151. }
  152. /// <summary>Gets the array of <see cref="Command"/>s bound to <paramref name="key"/> if it exists.</summary>
  153. /// <param name="key">The key to check.</param>
  154. /// <returns>
  155. /// The array of <see cref="Command"/>s if <paramref name="key"/> is bound. An empty <see cref="Command"/> array
  156. /// if not.
  157. /// </returns>
  158. public Command [] GetCommands (Key key)
  159. {
  160. if (TryGet (key, out KeyBinding bindings))
  161. {
  162. return bindings.Commands;
  163. }
  164. return Array.Empty<Command> ();
  165. }
  166. /// <summary>Gets the Key used by a set of commands.</summary>
  167. /// <remarks></remarks>
  168. /// <param name="commands">The set of commands to search.</param>
  169. /// <returns>The <see cref="Key"/> used by a <see cref="Command"/></returns>
  170. /// <exception cref="InvalidOperationException">If no matching set of commands was found.</exception>
  171. public Key GetKeyFromCommands (params Command [] commands) { return Bindings.First (a => a.Value.Commands.SequenceEqual (commands)).Key; }
  172. /// <summary>Removes a <see cref="KeyBinding"/> from the collection.</summary>
  173. /// <param name="key"></param>
  174. public void Remove (Key key)
  175. {
  176. Bindings.Remove (key);
  177. Application.RemoveKeyBinding (key, BoundView);
  178. }
  179. /// <summary>Replaces a key combination already bound to a set of <see cref="Command"/>s.</summary>
  180. /// <remarks></remarks>
  181. /// <param name="oldKey">The key to be replaced.</param>
  182. /// <param name="newKey">The new key to be used.</param>
  183. public void Replace (Key oldKey, Key newKey)
  184. {
  185. if (!TryGet (oldKey, out KeyBinding _))
  186. {
  187. return;
  188. }
  189. KeyBinding value = Bindings [oldKey];
  190. Remove (oldKey);
  191. Add (newKey, value);
  192. }
  193. /// <summary>Gets the commands bound with the specified Key.</summary>
  194. /// <remarks></remarks>
  195. /// <param name="key">The key to check.</param>
  196. /// <param name="binding">
  197. /// When this method returns, contains the commands bound with the specified Key, if the Key is
  198. /// found; otherwise, null. This parameter is passed uninitialized.
  199. /// </param>
  200. /// <returns><see langword="true"/> if the Key is bound; otherwise <see langword="false"/>.</returns>
  201. public bool TryGet (Key key, out KeyBinding binding)
  202. {
  203. if (key.IsValid)
  204. {
  205. return Bindings.TryGetValue (key, out binding);
  206. }
  207. binding = new (Array.Empty<Command> (), KeyBindingScope.Focused);
  208. return false;
  209. }
  210. /// <summary>Gets the commands bound with the specified Key that are scoped to a particular scope.</summary>
  211. /// <remarks></remarks>
  212. /// <param name="key">The key to check.</param>
  213. /// <param name="scope">the scope to filter on</param>
  214. /// <param name="binding">
  215. /// When this method returns, contains the commands bound with the specified Key, if the Key is
  216. /// found; otherwise, null. This parameter is passed uninitialized.
  217. /// </param>
  218. /// <returns><see langword="true"/> if the Key is bound; otherwise <see langword="false"/>.</returns>
  219. public bool TryGet (Key key, KeyBindingScope scope, out KeyBinding binding)
  220. {
  221. if (key.IsValid && Bindings.TryGetValue (key, out binding))
  222. {
  223. if (scope.HasFlag (binding.Scope))
  224. {
  225. return true;
  226. }
  227. }
  228. binding = new (Array.Empty<Command> (), KeyBindingScope.Focused);
  229. return false;
  230. }
  231. }