MenuBarItem.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
  86. foreach (MenuItem menuItem in Children.Where (m => m is { }))
  87. {
  88. // For MenuBar only add shortcuts for submenus
  89. if (menuItem.ShortcutKey != Key.Empty)
  90. {
  91. menuItem._menuBar = menuBar;
  92. menuItem.UpdateShortcutKeyBinding (menuItem._menuBar, Key.Empty);
  93. }
  94. SubMenu (menuItem)?.AddShortcutKeyBindings (menuBar);
  95. }
  96. }
  97. private void SetInitialProperties (string title, object? children, MenuItem? parent = null, bool isTopLevel = false)
  98. {
  99. if (!isTopLevel && children is null)
  100. {
  101. throw new ArgumentNullException (
  102. nameof (children),
  103. @"The parameter cannot be null. Use an empty array instead."
  104. );
  105. }
  106. SetTitle (title ?? "");
  107. if (parent is { })
  108. {
  109. Parent = parent;
  110. }
  111. switch (children)
  112. {
  113. case List<MenuItem []> childrenList:
  114. {
  115. MenuItem [] newChildren = [];
  116. foreach (MenuItem [] grandChild in childrenList)
  117. {
  118. foreach (MenuItem child in grandChild)
  119. {
  120. SetParent (grandChild);
  121. Array.Resize (ref newChildren, newChildren.Length + 1);
  122. newChildren [^1] = child;
  123. }
  124. }
  125. Children = newChildren;
  126. break;
  127. }
  128. case MenuItem [] items:
  129. SetParent (items);
  130. Children = items;
  131. break;
  132. default:
  133. Children = null;
  134. break;
  135. }
  136. }
  137. private void SetParent (MenuItem [] children)
  138. {
  139. foreach (MenuItem child in children)
  140. {
  141. if (child is { Parent: null })
  142. {
  143. child.Parent = this;
  144. }
  145. }
  146. }
  147. private void SetTitle (string title)
  148. {
  149. title ??= string.Empty;
  150. Title = title;
  151. }
  152. /// <summary>
  153. /// Add a <see cref="MenuBarItem"/> dynamically into the <see cref="MenuBar"/><c>.Menus</c>.
  154. /// </summary>
  155. /// <param name="menuItem"></param>
  156. public void AddMenuBarItem (MenuItem? menuItem = null)
  157. {
  158. if (menuItem is null)
  159. {
  160. MenuBarItem [] menus = _menuBar.Menus;
  161. Array.Resize (ref menus, menus.Length + 1);
  162. menus [^1] = this;
  163. _menuBar.Menus = menus;
  164. }
  165. else
  166. {
  167. MenuItem [] childrens = Children ?? [];
  168. Array.Resize (ref childrens, childrens.Length + 1);
  169. childrens [^1] = menuItem;
  170. Children = childrens;
  171. }
  172. }
  173. /// <inheritdoc />
  174. public override void RemoveMenuItem ()
  175. {
  176. if (Children is { })
  177. {
  178. foreach (MenuItem menuItem in Children)
  179. {
  180. if (menuItem.ShortcutKey != Key.Empty)
  181. {
  182. // Remove an existent ShortcutKey
  183. _menuBar?.KeyBindings.Remove (menuItem.ShortcutKey);
  184. }
  185. }
  186. }
  187. if (ShortcutKey != Key.Empty)
  188. {
  189. // Remove an existent ShortcutKey
  190. _menuBar?.KeyBindings.Remove (ShortcutKey);
  191. }
  192. var index = _menuBar!.Menus.IndexOf (this);
  193. if (index > -1)
  194. {
  195. if (_menuBar!.Menus [index].HotKey != Key.Empty)
  196. {
  197. // Remove an existent HotKey
  198. _menuBar?.KeyBindings.Remove (HotKey.WithAlt);
  199. }
  200. _menuBar!.Menus [index] = null!;
  201. }
  202. var i = 0;
  203. foreach (MenuBarItem m in _menuBar.Menus)
  204. {
  205. // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
  206. if (m != null)
  207. {
  208. _menuBar.Menus [i] = m;
  209. i++;
  210. }
  211. }
  212. MenuBarItem [] menus = _menuBar.Menus;
  213. Array.Resize (ref menus, menus.Length - 1);
  214. _menuBar.Menus = menus;
  215. }
  216. }