MenuBarItem.cs 6.1 KB

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