ShortcutHelper.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Terminal.Gui {
  8. /// <summary>
  9. /// Represents a helper to manipulate shortcut keys used on views.
  10. /// </summary>
  11. public class ShortcutHelper {
  12. private Key shortcut;
  13. /// <summary>
  14. /// This is the global setting that can be used as a global shortcut to invoke the action on the view.
  15. /// </summary>
  16. public virtual Key Shortcut {
  17. get => shortcut;
  18. set {
  19. if (shortcut != value && (PostShortcutValidation (value) || value == Key.Null)) {
  20. shortcut = value;
  21. }
  22. }
  23. }
  24. /// <summary>
  25. /// The keystroke combination used in the <see cref="Shortcut"/> as string.
  26. /// </summary>
  27. public virtual ustring ShortcutTag => GetShortcutTag (shortcut);
  28. /// <summary>
  29. /// The action to run if the <see cref="Shortcut"/> is defined.
  30. /// </summary>
  31. public virtual Action ShortcutAction { get; set; }
  32. /// <summary>
  33. /// Gets the key with all the keys modifiers, especially the shift key that sometimes have to be injected later.
  34. /// </summary>
  35. /// <param name="kb">The <see cref="KeyEvent"/> to check.</param>
  36. /// <returns>The <see cref="KeyEvent.Key"/> with all the keys modifiers.</returns>
  37. public static Key GetModifiersKey (KeyEvent kb)
  38. {
  39. var key = kb.Key;
  40. if (kb.IsAlt && (key & Key.AltMask) == 0) {
  41. key |= Key.AltMask;
  42. }
  43. if (kb.IsCtrl && (key & Key.CtrlMask) == 0) {
  44. key |= Key.CtrlMask;
  45. }
  46. if (kb.IsShift && (key & Key.ShiftMask) == 0) {
  47. key |= Key.ShiftMask;
  48. }
  49. return key;
  50. }
  51. /// <summary>
  52. /// Get the <see cref="Shortcut"/> key as string.
  53. /// </summary>
  54. /// <param name="shortcut">The shortcut key.</param>
  55. /// <param name="delimiter">The delimiter string.</param>
  56. /// <returns></returns>
  57. public static ustring GetShortcutTag (Key shortcut, ustring delimiter = null)
  58. {
  59. if (shortcut == Key.Null) {
  60. return "";
  61. }
  62. var k = shortcut;
  63. if (delimiter == null) {
  64. delimiter = MenuBar.ShortcutDelimiter;
  65. }
  66. ustring tag = ustring.Empty;
  67. var sCut = GetKeyToString (k, out Key knm).ToString ();
  68. if (knm == Key.Unknown) {
  69. k &= ~Key.Unknown;
  70. sCut = GetKeyToString (k, out _).ToString ();
  71. }
  72. if ((k & Key.CtrlMask) != 0) {
  73. tag = "Ctrl";
  74. }
  75. if ((k & Key.ShiftMask) != 0) {
  76. if (!tag.IsEmpty) {
  77. tag += delimiter;
  78. }
  79. tag += "Shift";
  80. }
  81. if ((k & Key.AltMask) != 0) {
  82. if (!tag.IsEmpty) {
  83. tag += delimiter;
  84. }
  85. tag += "Alt";
  86. }
  87. ustring [] keys = ustring.Make (sCut).Split (",");
  88. for (int i = 0; i < keys.Length; i++) {
  89. var key = keys [i].TrimSpace ();
  90. if (key == Key.AltMask.ToString () || key == Key.ShiftMask.ToString () || key == Key.CtrlMask.ToString ()) {
  91. continue;
  92. }
  93. if (!tag.IsEmpty) {
  94. tag += delimiter;
  95. }
  96. if (!key.Contains ("F") && key.Length > 2 && keys.Length == 1) {
  97. k = (uint)Key.AltMask + k;
  98. tag += ((char)k).ToString ();
  99. } else if (key.Length == 2 && key.StartsWith ("D")) {
  100. tag += ((char)key.ElementAt (1)).ToString ();
  101. } else {
  102. tag += key;
  103. }
  104. }
  105. return tag;
  106. }
  107. /// <summary>
  108. /// Return key as string.
  109. /// </summary>
  110. /// <param name="key">The key to extract.</param>
  111. /// <param name="knm">Correspond to the non modifier key.</param>
  112. public static ustring GetKeyToString (Key key, out Key knm)
  113. {
  114. if (key == Key.Null) {
  115. knm = Key.Null;
  116. return "";
  117. }
  118. knm = key;
  119. var mK = key & (Key.AltMask | Key.CtrlMask | Key.ShiftMask);
  120. knm &= ~mK;
  121. for (uint i = (uint)Key.F1; i < (uint)Key.F12; i++) {
  122. if (knm == (Key)i) {
  123. mK |= (Key)i;
  124. }
  125. }
  126. knm &= ~mK;
  127. uint.TryParse (knm.ToString (), out uint c);
  128. var s = mK == Key.Null ? "" : mK.ToString ();
  129. if (s != "" && (knm != Key.Null || c > 0)) {
  130. s += ",";
  131. }
  132. s += c == 0 ? knm == Key.Null ? "" : knm.ToString () : ((char)c).ToString ();
  133. return s;
  134. }
  135. /// <summary>
  136. /// Allows to retrieve a <see cref="Key"/> from a <see cref="ShortcutTag"/>
  137. /// </summary>
  138. /// <param name="tag">The key as string.</param>
  139. /// <param name="delimiter">The delimiter string.</param>
  140. public static Key GetShortcutFromTag (ustring tag, ustring delimiter = null)
  141. {
  142. var sCut = tag;
  143. if (sCut.IsEmpty) {
  144. return default;
  145. }
  146. Key key = Key.Null;
  147. //var hasCtrl = false;
  148. if (delimiter == null) {
  149. delimiter = MenuBar.ShortcutDelimiter;
  150. }
  151. ustring [] keys = sCut.Split (delimiter);
  152. for (int i = 0; i < keys.Length; i++) {
  153. var k = keys [i];
  154. if (k == "Ctrl") {
  155. //hasCtrl = true;
  156. key |= Key.CtrlMask;
  157. } else if (k == "Shift") {
  158. key |= Key.ShiftMask;
  159. } else if (k == "Alt") {
  160. key |= Key.AltMask;
  161. } else if (k.StartsWith ("F") && k.Length > 1) {
  162. int.TryParse (k.Substring (1).ToString (), out int n);
  163. for (uint j = (uint)Key.F1; j <= (uint)Key.F12; j++) {
  164. int.TryParse (((Key)j).ToString ().Substring (1), out int f);
  165. if (f == n) {
  166. key |= (Key)j;
  167. }
  168. }
  169. } else {
  170. key |= (Key)Enum.Parse (typeof (Key), k.ToString ());
  171. }
  172. }
  173. return key;
  174. }
  175. /// <summary>
  176. /// Lookup for a <see cref="Key"/> on range of keys.
  177. /// </summary>
  178. /// <param name="key">The source key.</param>
  179. /// <param name="first">First key in range.</param>
  180. /// <param name="last">Last key in range.</param>
  181. public static bool CheckKeysFlagRange (Key key, Key first, Key last)
  182. {
  183. for (uint i = (uint)first; i < (uint)last; i++) {
  184. if ((key | (Key)i) == key) {
  185. return true;
  186. }
  187. }
  188. return false;
  189. }
  190. /// <summary>
  191. /// Used at key down or key press validation.
  192. /// </summary>
  193. /// <param name="key">The key to validate.</param>
  194. /// <returns><c>true</c> if is valid.<c>false</c>otherwise.</returns>
  195. public static bool PreShortcutValidation (Key key)
  196. {
  197. if ((key & (Key.CtrlMask | Key.ShiftMask | Key.AltMask)) == 0 && !CheckKeysFlagRange (key, Key.F1, Key.F12)) {
  198. return false;
  199. }
  200. return true;
  201. }
  202. /// <summary>
  203. /// Used at key up validation.
  204. /// </summary>
  205. /// <param name="key">The key to validate.</param>
  206. /// <returns><c>true</c> if is valid.<c>false</c>otherwise.</returns>
  207. public static bool PostShortcutValidation (Key key)
  208. {
  209. GetKeyToString (key, out Key knm);
  210. if (CheckKeysFlagRange (key, Key.F1, Key.F12) ||
  211. ((key & (Key.CtrlMask | Key.ShiftMask | Key.AltMask)) != 0 && knm != Key.Null && knm != Key.Unknown)) {
  212. return true;
  213. }
  214. return false;
  215. }
  216. /// <summary>
  217. /// Allows a view to run a <see cref="View.ShortcutAction"/> if defined.
  218. /// </summary>
  219. /// <param name="kb">The <see cref="KeyEvent"/></param>
  220. /// <param name="view">The <see cref="View"/></param>
  221. /// <returns><c>true</c> if defined <c>false</c>otherwise.</returns>
  222. public static bool FindAndOpenByShortcut (KeyEvent kb, View view = null)
  223. {
  224. if (view == null) {
  225. return false; }
  226. var key = kb.KeyValue;
  227. var keys = GetModifiersKey (kb);
  228. key |= (int)keys;
  229. foreach (var v in view.Subviews) {
  230. if (v.Shortcut != Key.Null && v.Shortcut == (Key)key) {
  231. var action = v.ShortcutAction;
  232. if (action != null) {
  233. Application.MainLoop.AddIdle (() => {
  234. action ();
  235. return false;
  236. });
  237. }
  238. return true;
  239. }
  240. if (FindAndOpenByShortcut (kb, v)) {
  241. return true;
  242. }
  243. }
  244. return false;
  245. }
  246. }
  247. }