MenuBarItem.cs 8.5 KB

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