MenuBarItem.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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.Shortcut"/>.
  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.Shortcut != KeyCode.Null)
  87. {
  88. KeyBinding keyBinding = new ([Command.Select], KeyBindingScope.HotKey, menuItem);
  89. menuBar.KeyBindings.Remove (menuItem.Shortcut);
  90. menuBar.KeyBindings.Add (menuItem.Shortcut, keyBinding);
  91. }
  92. SubMenu (menuItem)?.AddShortcutKeyBindings (menuBar);
  93. }
  94. }
  95. private void SetInitialProperties (string title, object children, MenuItem parent = null, bool isTopLevel = false)
  96. {
  97. if (!isTopLevel && children is null)
  98. {
  99. throw new ArgumentNullException (
  100. nameof (children),
  101. @"The parameter cannot be null. Use an empty array instead."
  102. );
  103. }
  104. SetTitle (title ?? "");
  105. if (parent is { })
  106. {
  107. Parent = parent;
  108. }
  109. switch (children)
  110. {
  111. case List<MenuItem []> childrenList:
  112. {
  113. MenuItem [] newChildren = [];
  114. foreach (MenuItem [] grandChild in childrenList)
  115. {
  116. foreach (MenuItem child in grandChild)
  117. {
  118. SetParent (grandChild);
  119. Array.Resize (ref newChildren, newChildren.Length + 1);
  120. newChildren [^1] = child;
  121. }
  122. }
  123. Children = newChildren;
  124. break;
  125. }
  126. case MenuItem [] items:
  127. SetParent (items);
  128. Children = items;
  129. break;
  130. default:
  131. Children = null;
  132. break;
  133. }
  134. }
  135. private void SetParent (MenuItem [] children)
  136. {
  137. foreach (MenuItem child in children)
  138. {
  139. if (child is { Parent: null })
  140. {
  141. child.Parent = this;
  142. }
  143. }
  144. }
  145. private void SetTitle (string title)
  146. {
  147. title ??= string.Empty;
  148. Title = title;
  149. }
  150. }