2
0

KeyBindings.cs 7.7 KB

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