MenuBarItem.cs 7.9 KB

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