MenuBarItem.cs 8.4 KB

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