MenuItem.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// A <see cref="MenuItem"/> has title, an associated help text, and an action to execute on activation. MenuItems
  5. /// can also have a checked indicator (see <see cref="Checked"/>).
  6. /// </summary>
  7. public class MenuItem
  8. {
  9. internal MenuBar _menuBar;
  10. /// <summary>Initializes a new instance of <see cref="MenuItem"/></summary>
  11. public MenuItem (Key? shortcutKey = null) : this ("", "", null, null, null, shortcutKey) { }
  12. /// <summary>Initializes a new instance of <see cref="MenuItem"/>.</summary>
  13. /// <param name="title">Title for the menu item.</param>
  14. /// <param name="help">Help text to display.</param>
  15. /// <param name="action">Action to invoke when the menu item is activated.</param>
  16. /// <param name="canExecute">Function to determine if the action can currently be executed.</param>
  17. /// <param name="parent">The <see cref="Parent"/> of this menu item.</param>
  18. /// <param name="shortcutKey">The <see cref="ShortcutKey"/> keystroke combination.</param>
  19. public MenuItem (
  20. string? title,
  21. string? help,
  22. Action? action,
  23. Func<bool>? canExecute = null,
  24. MenuItem? parent = null,
  25. Key? shortcutKey = null
  26. )
  27. {
  28. Title = title ?? "";
  29. Help = help ?? "";
  30. Action = action!;
  31. CanExecute = canExecute!;
  32. Parent = parent!;
  33. if (Parent is { } && Parent.ShortcutKey != Key.Empty)
  34. {
  35. Parent.ShortcutKey = Key.Empty;
  36. }
  37. // Setter will ensure Key.Empty if it's null
  38. ShortcutKey = shortcutKey!;
  39. }
  40. private bool _allowNullChecked;
  41. private MenuItemCheckStyle _checkType;
  42. private string _title;
  43. /// <summary>Gets or sets the action to be invoked when the menu item is triggered.</summary>
  44. /// <value>Method to invoke.</value>
  45. public Action? Action { get; set; }
  46. /// <summary>
  47. /// Used only if <see cref="CheckType"/> is of <see cref="MenuItemCheckStyle.Checked"/> type. If
  48. /// <see langword="true"/> allows <see cref="Checked"/> to be null, true or false. If <see langword="false"/> only
  49. /// allows <see cref="Checked"/> to be true or false.
  50. /// </summary>
  51. public bool AllowNullChecked
  52. {
  53. get => _allowNullChecked;
  54. set
  55. {
  56. _allowNullChecked = value;
  57. Checked ??= false;
  58. }
  59. }
  60. /// <summary>
  61. /// Gets or sets the action to be invoked to determine if the menu can be triggered. If <see cref="CanExecute"/>
  62. /// returns <see langword="true"/> the menu item will be enabled. Otherwise, it will be disabled.
  63. /// </summary>
  64. /// <value>Function to determine if the action is can be executed or not.</value>
  65. public Func<bool>? CanExecute { get; set; }
  66. /// <summary>
  67. /// Sets or gets whether the <see cref="MenuItem"/> shows a check indicator or not. See
  68. /// <see cref="MenuItemCheckStyle"/>.
  69. /// </summary>
  70. public bool? Checked { set; get; }
  71. /// <summary>
  72. /// Sets or gets the <see cref="MenuItemCheckStyle"/> of a menu item where <see cref="Checked"/> is set to
  73. /// <see langword="true"/>.
  74. /// </summary>
  75. public MenuItemCheckStyle CheckType
  76. {
  77. get => _checkType;
  78. set
  79. {
  80. _checkType = value;
  81. if (_checkType == MenuItemCheckStyle.Checked && !_allowNullChecked && Checked is null)
  82. {
  83. Checked = false;
  84. }
  85. }
  86. }
  87. /// <summary>Gets or sets arbitrary data for the menu item.</summary>
  88. /// <remarks>This property is not used internally.</remarks>
  89. public object Data { get; set; }
  90. /// <summary>Gets or sets the help text for the menu item. The help text is drawn to the right of the <see cref="Title"/>.</summary>
  91. /// <value>The help text.</value>
  92. public string Help { get; set; }
  93. /// <summary>
  94. /// Returns <see langword="true"/> if the menu item is enabled. This method is a wrapper around
  95. /// <see cref="CanExecute"/>.
  96. /// </summary>
  97. public bool IsEnabled () { return CanExecute?.Invoke () ?? true; }
  98. /// <summary>Gets the parent for this <see cref="MenuItem"/>.</summary>
  99. /// <value>The parent.</value>
  100. public MenuItem? Parent { get; set; }
  101. /// <summary>Gets or sets the title of the menu item .</summary>
  102. /// <value>The title.</value>
  103. public string Title
  104. {
  105. get => _title;
  106. set
  107. {
  108. if (_title == value)
  109. {
  110. return;
  111. }
  112. _title = value;
  113. GetHotKey ();
  114. }
  115. }
  116. /// <summary>
  117. /// Toggle the <see cref="Checked"/> between three states if <see cref="AllowNullChecked"/> is
  118. /// <see langword="true"/> or between two states if <see cref="AllowNullChecked"/> is <see langword="false"/>.
  119. /// </summary>
  120. public void ToggleChecked ()
  121. {
  122. if (_checkType != MenuItemCheckStyle.Checked)
  123. {
  124. throw new InvalidOperationException ("This isn't a Checked MenuItemCheckStyle!");
  125. }
  126. bool? previousChecked = Checked;
  127. if (AllowNullChecked)
  128. {
  129. Checked = previousChecked switch
  130. {
  131. null => true,
  132. true => false,
  133. false => null
  134. };
  135. }
  136. else
  137. {
  138. Checked = !Checked;
  139. }
  140. }
  141. /// <summary>Merely a debugging aid to see the interaction with main.</summary>
  142. internal bool GetMenuBarItem () { return IsFromSubMenu; }
  143. /// <summary>Merely a debugging aid to see the interaction with main.</summary>
  144. internal MenuItem GetMenuItem () { return this; }
  145. /// <summary>Gets if this <see cref="MenuItem"/> is from a sub-menu.</summary>
  146. internal bool IsFromSubMenu => Parent != null;
  147. internal int TitleLength => GetMenuBarItemLength (Title);
  148. //
  149. // ┌─────────────────────────────┐
  150. // │ Quit Quit UI Catalog Ctrl+Q │
  151. // └─────────────────────────────┘
  152. // ┌─────────────────┐
  153. // │ ◌ TopLevel Alt+T │
  154. // └─────────────────┘
  155. // TODO: Replace the `2` literals with named constants
  156. internal int Width => 1
  157. + // space before Title
  158. TitleLength
  159. + 2
  160. + // space after Title - BUGBUG: This should be 1
  161. (Checked == true || CheckType.HasFlag (MenuItemCheckStyle.Checked) || CheckType.HasFlag (MenuItemCheckStyle.Radio)
  162. ? 2
  163. : 0)
  164. + // check glyph + space
  165. (Help.GetColumns () > 0 ? 2 + Help.GetColumns () : 0)
  166. + // Two spaces before Help
  167. (ShortcutTag.GetColumns () > 0
  168. ? 2 + ShortcutTag.GetColumns ()
  169. : 0); // Pad two spaces before shortcut tag (which are also aligned right)
  170. private static int GetMenuBarItemLength (string title)
  171. {
  172. return title.EnumerateRunes ()
  173. .Where (ch => ch != MenuBar.HotKeySpecifier)
  174. .Sum (ch => Math.Max (ch.GetColumns (), 1));
  175. }
  176. #region Keyboard Handling
  177. private Key _hotKey = Key.Empty;
  178. /// <summary>
  179. /// The HotKey is used to activate a <see cref="MenuItem"/> with the keyboard. HotKeys are defined by prefixing the
  180. /// <see cref="Title"/> of a MenuItem with an underscore ('_').
  181. /// <para>
  182. /// Pressing Alt-Hotkey for a <see cref="MenuBarItem"/> (menu items on the menu bar) works even if the menu is
  183. /// not active. Once a menu has focus and is active, pressing just the HotKey will activate the MenuItem.
  184. /// </para>
  185. /// <para>
  186. /// For example for a MenuBar with a "_File" MenuBarItem that contains a "_New" MenuItem, Alt-F will open the
  187. /// File menu. Pressing the N key will then activate the New MenuItem.
  188. /// </para>
  189. /// <para>See also <see cref="ShortcutKey"/> which enable global key-bindings to menu items.</para>
  190. /// </summary>
  191. public Key? HotKey
  192. {
  193. get => _hotKey;
  194. private set
  195. {
  196. var oldKey = _hotKey;
  197. _hotKey = value ?? Key.Empty;
  198. UpdateHotKeyBinding (oldKey);
  199. }
  200. }
  201. private void GetHotKey ()
  202. {
  203. var nextIsHot = false;
  204. foreach (char x in _title)
  205. {
  206. if (x == MenuBar.HotKeySpecifier.Value)
  207. {
  208. nextIsHot = true;
  209. }
  210. else if (nextIsHot)
  211. {
  212. HotKey = char.ToLower (x);
  213. return;
  214. }
  215. }
  216. HotKey = Key.Empty;
  217. }
  218. private Key _shortcutKey = Key.Empty;
  219. /// <summary>
  220. /// Shortcut defines a key binding to the MenuItem that will invoke the MenuItem's action globally for the
  221. /// <see cref="View"/> that is the parent of the <see cref="MenuBar"/> or <see cref="ContextMenu"/> this
  222. /// <see cref="MenuItem"/>.
  223. /// <para>
  224. /// The <see cref="Key"/> will be drawn on the MenuItem to the right of the <see cref="Title"/> and
  225. /// <see cref="Help"/> text. See <see cref="ShortcutTag"/>.
  226. /// </para>
  227. /// </summary>
  228. public Key? ShortcutKey
  229. {
  230. get => _shortcutKey;
  231. set
  232. {
  233. var oldKey = _shortcutKey;
  234. _shortcutKey = value ?? Key.Empty;
  235. UpdateShortcutKeyBinding (oldKey);
  236. }
  237. }
  238. /// <summary>Gets the text describing the keystroke combination defined by <see cref="ShortcutKey"/>.</summary>
  239. public string ShortcutTag => ShortcutKey != Key.Empty ? ShortcutKey!.ToString () : string.Empty;
  240. internal void AddShortcutKeyBinding (MenuBar menuBar, Key key)
  241. {
  242. ArgumentNullException.ThrowIfNull (menuBar);
  243. _menuBar = menuBar;
  244. AddOrUpdateShortcutKeyBinding (key);
  245. }
  246. private void AddOrUpdateShortcutKeyBinding (Key key)
  247. {
  248. if (key != Key.Empty)
  249. {
  250. _menuBar.HotKeyBindings.Remove (key);
  251. }
  252. if (ShortcutKey != Key.Empty)
  253. {
  254. KeyBinding keyBinding = new ([Command.Select], null, data: this);
  255. // Remove an existent ShortcutKey
  256. _menuBar.HotKeyBindings.Remove (ShortcutKey!);
  257. _menuBar.HotKeyBindings.Add (ShortcutKey!, keyBinding);
  258. }
  259. }
  260. private void UpdateHotKeyBinding (Key oldKey)
  261. {
  262. if (_menuBar is null or { IsInitialized: false })
  263. {
  264. return;
  265. }
  266. if (oldKey != Key.Empty)
  267. {
  268. var index = _menuBar.Menus.IndexOf (this);
  269. if (index > -1)
  270. {
  271. _menuBar.HotKeyBindings.Remove (oldKey.WithAlt);
  272. }
  273. }
  274. if (HotKey != Key.Empty)
  275. {
  276. var index = _menuBar.Menus.IndexOf (this);
  277. if (index > -1)
  278. {
  279. _menuBar.HotKeyBindings.Remove (HotKey!.WithAlt);
  280. KeyBinding keyBinding = new ([Command.Toggle], null, data: this);
  281. _menuBar.HotKeyBindings.Add (HotKey.WithAlt, keyBinding);
  282. }
  283. }
  284. }
  285. private void UpdateShortcutKeyBinding (Key oldKey)
  286. {
  287. // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
  288. if (_menuBar is null)
  289. {
  290. return;
  291. }
  292. AddOrUpdateShortcutKeyBinding (oldKey);
  293. }
  294. #endregion Keyboard Handling
  295. /// <summary>
  296. /// Removes a <see cref="MenuItem"/> dynamically from the <see cref="Parent"/>.
  297. /// </summary>
  298. public virtual void RemoveMenuItem ()
  299. {
  300. if (Parent is { })
  301. {
  302. MenuItem? []? childrens = ((MenuBarItem)Parent).Children;
  303. var i = 0;
  304. foreach (MenuItem? c in childrens!)
  305. {
  306. if (c != this)
  307. {
  308. childrens [i] = c;
  309. i++;
  310. }
  311. }
  312. Array.Resize (ref childrens, childrens.Length - 1);
  313. if (childrens.Length == 0)
  314. {
  315. ((MenuBarItem)Parent).Children = null;
  316. }
  317. else
  318. {
  319. ((MenuBarItem)Parent).Children = childrens;
  320. }
  321. }
  322. if (ShortcutKey != Key.Empty)
  323. {
  324. // Remove an existent ShortcutKey
  325. _menuBar.HotKeyBindings.Remove (ShortcutKey!);
  326. }
  327. }
  328. }