2
0

MenuBarItem.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// <see cref="MenuBarItem"/> is a menu item on <see cref="MenuBar"/>. MenuBarItems do not support
  5. /// <see cref="MenuItem.ShortcutKey"/>.
  6. /// </summary>
  7. public class MenuBarItem : MenuItem
  8. {
  9. /// <summary>Initializes a new <see cref="MenuBarItem"/> as a <see cref="MenuItem"/>.</summary>
  10. /// <param name="title">Title for the menu item.</param>
  11. /// <param name="help">Help text to display. Will be displayed next to the Title surrounded by parentheses.</param>
  12. /// <param name="action">Action to invoke when the menu item is activated.</param>
  13. /// <param name="canExecute">Function to determine if the action can currently be executed.</param>
  14. /// <param name="parent">The parent <see cref="MenuItem"/> of this if any.</param>
  15. public MenuBarItem (
  16. string title,
  17. string help,
  18. Action action,
  19. Func<bool>? canExecute = null,
  20. MenuItem? parent = null
  21. ) : base (title, help, action, canExecute, parent)
  22. {
  23. SetInitialProperties (title, null, null, true);
  24. }
  25. /// <summary>Initializes a new <see cref="MenuBarItem"/>.</summary>
  26. /// <param name="title">Title for the menu item.</param>
  27. /// <param name="children">The items in the current menu.</param>
  28. /// <param name="parent">The parent <see cref="MenuItem"/> of this if any.</param>
  29. public MenuBarItem (string title, MenuItem [] children, MenuItem? parent = null) { SetInitialProperties (title, children, parent); }
  30. /// <summary>Initializes a new <see cref="MenuBarItem"/> with separate list of items.</summary>
  31. /// <param name="title">Title for the menu item.</param>
  32. /// <param name="children">The list of items in the current menu.</param>
  33. /// <param name="parent">The parent <see cref="MenuItem"/> of this if any.</param>
  34. public MenuBarItem (string title, List<MenuItem []> children, MenuItem? parent = null) { SetInitialProperties (title, children, parent); }
  35. /// <summary>Initializes a new <see cref="MenuBarItem"/>.</summary>
  36. /// <param name="children">The items in the current menu.</param>
  37. public MenuBarItem (MenuItem [] children) : this ("", children) { }
  38. /// <summary>Initializes a new <see cref="MenuBarItem"/>.</summary>
  39. public MenuBarItem () : this ([]) { }
  40. /// <summary>
  41. /// Gets or sets an array of <see cref="MenuItem"/> objects that are the children of this
  42. /// <see cref="MenuBarItem"/>
  43. /// </summary>
  44. /// <value>The children.</value>
  45. public MenuItem? []? Children { get; set; }
  46. internal bool IsTopLevel => Parent is null && (Children is null || Children.Length == 0) && Action != null;
  47. /// <summary>Get the index of a child <see cref="MenuItem"/>.</summary>
  48. /// <param name="children"></param>
  49. /// <returns>Returns a greater than -1 if the <see cref="MenuItem"/> is a child.</returns>
  50. public int GetChildrenIndex (MenuItem children)
  51. {
  52. var i = 0;
  53. if (Children is null)
  54. {
  55. return -1;
  56. }
  57. foreach (MenuItem? child in Children)
  58. {
  59. if (child == children)
  60. {
  61. return i;
  62. }
  63. i++;
  64. }
  65. return -1;
  66. }
  67. /// <summary>Check if a <see cref="MenuItem"/> is a submenu of this MenuBar.</summary>
  68. /// <param name="menuItem"></param>
  69. /// <returns>Returns <c>true</c> if it is a submenu. <c>false</c> otherwise.</returns>
  70. public bool IsSubMenuOf (MenuItem menuItem)
  71. {
  72. return Children!.Any (child => child == menuItem && child.Parent == menuItem.Parent);
  73. }
  74. /// <summary>Check if a <see cref="MenuItem"/> is a <see cref="MenuBarItem"/>.</summary>
  75. /// <param name="menuItem"></param>
  76. /// <returns>Returns a <see cref="MenuBarItem"/> or null otherwise.</returns>
  77. public MenuBarItem? SubMenu (MenuItem? menuItem) { return menuItem as MenuBarItem; }
  78. internal void AddShortcutKeyBindings (MenuBar menuBar)
  79. {
  80. if (Children is null)
  81. {
  82. return;
  83. }
  84. _menuBar = menuBar;
  85. IEnumerable<MenuItem> menuItems = Children.Where (m => m is { })!;
  86. foreach (MenuItem menuItem in menuItems)
  87. {
  88. // Initialize MenuItem _menuBar
  89. menuItem._menuBar = menuBar;
  90. // For MenuBar only add shortcuts for submenus
  91. if (menuItem.ShortcutKey != Key.Empty)
  92. {
  93. menuItem.AddShortcutKeyBinding (menuBar, Key.Empty);
  94. }
  95. SubMenu (menuItem)?.AddShortcutKeyBindings (menuBar);
  96. }
  97. }
  98. // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local
  99. private void SetInitialProperties (string title, object? children, MenuItem? parent = null, bool isTopLevel = false)
  100. {
  101. if (!isTopLevel && children is null)
  102. {
  103. throw new ArgumentNullException (
  104. nameof (children),
  105. @"The parameter cannot be null. Use an empty array instead."
  106. );
  107. }
  108. SetTitle (title);
  109. if (parent is { })
  110. {
  111. Parent = parent;
  112. }
  113. switch (children)
  114. {
  115. case List<MenuItem []> childrenList:
  116. {
  117. MenuItem [] newChildren = [];
  118. foreach (MenuItem [] grandChild in childrenList)
  119. {
  120. foreach (MenuItem child in grandChild)
  121. {
  122. SetParent (grandChild);
  123. Array.Resize (ref newChildren, newChildren.Length + 1);
  124. newChildren [^1] = child;
  125. }
  126. }
  127. Children = newChildren;
  128. break;
  129. }
  130. case MenuItem [] items:
  131. SetParent (items);
  132. Children = items;
  133. break;
  134. default:
  135. Children = null;
  136. break;
  137. }
  138. }
  139. private void SetParent (MenuItem [] children)
  140. {
  141. foreach (MenuItem child in children)
  142. {
  143. if (child is { Parent: null })
  144. {
  145. child.Parent = this;
  146. }
  147. }
  148. }
  149. private void SetTitle (string? title)
  150. {
  151. title ??= string.Empty;
  152. Title = title;
  153. }
  154. /// <summary>
  155. /// Add a <see cref="MenuBarItem"/> dynamically into the <see cref="MenuBar"/><c>.Menus</c>.
  156. /// </summary>
  157. /// <param name="menuBar"></param>
  158. /// <param name="menuItem"></param>
  159. public void AddMenuBarItem (MenuBar menuBar, MenuItem? menuItem = null)
  160. {
  161. ArgumentNullException.ThrowIfNull (menuBar);
  162. _menuBar = menuBar;
  163. if (menuItem is null)
  164. {
  165. MenuBarItem [] menus = _menuBar.Menus;
  166. Array.Resize (ref menus, menus.Length + 1);
  167. menus [^1] = this;
  168. _menuBar.Menus = menus;
  169. }
  170. else
  171. {
  172. MenuItem [] childrens = (Children ?? [])!;
  173. Array.Resize (ref childrens, childrens.Length + 1);
  174. menuItem._menuBar = menuBar;
  175. childrens [^1] = menuItem;
  176. Children = childrens;
  177. }
  178. }
  179. /// <inheritdoc />
  180. public override void RemoveMenuItem ()
  181. {
  182. if (Children is { })
  183. {
  184. foreach (MenuItem? menuItem in Children)
  185. {
  186. if (menuItem?.ShortcutKey != Key.Empty)
  187. {
  188. // Remove an existent ShortcutKey
  189. _menuBar?.KeyBindings.Remove (menuItem?.ShortcutKey!);
  190. }
  191. }
  192. }
  193. if (ShortcutKey != Key.Empty)
  194. {
  195. // Remove an existent ShortcutKey
  196. _menuBar?.KeyBindings.Remove (ShortcutKey!);
  197. }
  198. var index = _menuBar!.Menus.IndexOf (this);
  199. if (index > -1)
  200. {
  201. if (_menuBar.Menus [index].HotKey != Key.Empty)
  202. {
  203. // Remove an existent HotKey
  204. _menuBar.HotKeyBindings.Remove (HotKey!.WithAlt);
  205. }
  206. _menuBar.Menus [index] = null!;
  207. }
  208. var i = 0;
  209. foreach (MenuBarItem m in _menuBar.Menus)
  210. {
  211. // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
  212. if (m != null)
  213. {
  214. _menuBar.Menus [i] = m;
  215. i++;
  216. }
  217. }
  218. MenuBarItem [] menus = _menuBar.Menus;
  219. Array.Resize (ref menus, menus.Length - 1);
  220. _menuBar.Menus = menus;
  221. }
  222. }