MenuItem.cs 13 KB

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