ShortcutHelper.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.Diagnostics;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Terminal.Gui;
  6. /// <summary>
  7. /// Represents a helper to manipulate shortcut keys used on views.
  8. /// </summary>
  9. public class ShortcutHelper {
  10. // TODO: Update this to use Key, not KeyCode
  11. private KeyCode shortcut;
  12. /// <summary>
  13. /// This is the global setting that can be used as a global shortcut to invoke the action on the view.
  14. /// </summary>
  15. public virtual KeyCode Shortcut {
  16. get => shortcut;
  17. set {
  18. if (shortcut != value && (PostShortcutValidation (value) || value is KeyCode.Null)) {
  19. shortcut = value;
  20. }
  21. }
  22. }
  23. /// <summary>
  24. /// The keystroke combination used in the <see cref="Shortcut"/> as string.
  25. /// </summary>
  26. public virtual string ShortcutTag => Key.ToString (shortcut, MenuBar.ShortcutDelimiter);
  27. /// <summary>
  28. /// Return key as string.
  29. /// </summary>
  30. /// <param name="key">The key to extract.</param>
  31. /// <param name="knm">Correspond to the non modifier key.</param>
  32. static string GetKeyToString (KeyCode key, out KeyCode knm)
  33. {
  34. if (key == KeyCode.Null) {
  35. knm = KeyCode.Null;
  36. return "";
  37. }
  38. knm = key;
  39. var mK = key & (KeyCode.AltMask | KeyCode.CtrlMask | KeyCode.ShiftMask);
  40. knm &= ~mK;
  41. for (uint i = (uint)KeyCode.F1; i < (uint)KeyCode.F12; i++) {
  42. if (knm == (KeyCode)i) {
  43. mK |= (KeyCode)i;
  44. }
  45. }
  46. knm &= ~mK;
  47. uint.TryParse (knm.ToString (), out uint c);
  48. var s = mK == KeyCode.Null ? "" : mK.ToString ();
  49. if (s != "" && (knm != KeyCode.Null || c > 0)) {
  50. s += ",";
  51. }
  52. s += c == 0 ? knm == KeyCode.Null ? "" : knm.ToString () : ((char)c).ToString ();
  53. return s;
  54. }
  55. /// <summary>
  56. /// Allows to retrieve a <see cref="KeyCode"/> from a <see cref="ShortcutTag"/>
  57. /// </summary>
  58. /// <param name="tag">The key as string.</param>
  59. /// <param name="delimiter">The delimiter string.</param>
  60. public static KeyCode GetShortcutFromTag (string tag, Rune delimiter = default)
  61. {
  62. var sCut = tag;
  63. if (string.IsNullOrEmpty (sCut)) {
  64. return default;
  65. }
  66. KeyCode key = KeyCode.Null;
  67. //var hasCtrl = false;
  68. if (delimiter == default) {
  69. delimiter = MenuBar.ShortcutDelimiter;
  70. }
  71. string [] keys = sCut.Split (delimiter.ToString());
  72. for (int i = 0; i < keys.Length; i++) {
  73. var k = keys [i];
  74. if (k == "Ctrl") {
  75. //hasCtrl = true;
  76. key |= KeyCode.CtrlMask;
  77. } else if (k == "Shift") {
  78. key |= KeyCode.ShiftMask;
  79. } else if (k == "Alt") {
  80. key |= KeyCode.AltMask;
  81. } else if (k.StartsWith ("F") && k.Length > 1) {
  82. int.TryParse (k.Substring (1).ToString (), out int n);
  83. for (uint j = (uint)KeyCode.F1; j <= (uint)KeyCode.F12; j++) {
  84. int.TryParse (((KeyCode)j).ToString ().Substring (1), out int f);
  85. if (f == n) {
  86. key |= (KeyCode)j;
  87. }
  88. }
  89. } else {
  90. key |= (KeyCode)Enum.Parse (typeof (KeyCode), k.ToString ());
  91. }
  92. }
  93. return key;
  94. }
  95. /// <summary>
  96. /// Lookup for a <see cref="KeyCode"/> on range of keys.
  97. /// </summary>
  98. /// <param name="key">The source key.</param>
  99. /// <param name="first">First key in range.</param>
  100. /// <param name="last">Last key in range.</param>
  101. public static bool CheckKeysFlagRange (KeyCode key, KeyCode first, KeyCode last)
  102. {
  103. for (uint i = (uint)first; i < (uint)last; i++) {
  104. if ((key | (KeyCode)i) == key) {
  105. return true;
  106. }
  107. }
  108. return false;
  109. }
  110. /// <summary>
  111. /// Used at key down or key press validation.
  112. /// </summary>
  113. /// <param name="key">The key to validate.</param>
  114. /// <returns><c>true</c> if is valid.<c>false</c>otherwise.</returns>
  115. public static bool PreShortcutValidation (KeyCode key)
  116. {
  117. if ((key & (KeyCode.CtrlMask | KeyCode.ShiftMask | KeyCode.AltMask)) == 0 && !CheckKeysFlagRange (key, KeyCode.F1, KeyCode.F12)) {
  118. return false;
  119. }
  120. return true;
  121. }
  122. /// <summary>
  123. /// Used at key up validation.
  124. /// </summary>
  125. /// <param name="key">The key to validate.</param>
  126. /// <returns><c>true</c> if is valid.<c>false</c>otherwise.</returns>
  127. public static bool PostShortcutValidation (KeyCode key)
  128. {
  129. GetKeyToString (key, out KeyCode knm);
  130. if (CheckKeysFlagRange (key, KeyCode.F1, KeyCode.F12) ||
  131. ((key & (KeyCode.CtrlMask | KeyCode.ShiftMask | KeyCode.AltMask)) != 0 && knm != KeyCode.Null)) {
  132. return true;
  133. }
  134. Debug.WriteLine ($"WARNING: {Key.ToString (key)} is not a valid shortcut key.");
  135. return false;
  136. }
  137. }