#nullable enable namespace Terminal.Gui; /// /// A -derived object to be used as items in a . /// MenuBarItems have a title, a hotkey, and an action to execute on activation. /// public class MenuBarItemv2 : MenuItemv2 { /// /// Creates a new instance of . /// public MenuBarItemv2 () : base (null, Command.NotBound) { } /// /// Creates a new instance of . Each MenuBarItem typically has a /// that is /// shown when the item is selected. /// /// /// /// /// The View that will be invoked on when user does something that causes the MenuBarItems's /// Accept event to be raised. /// /// /// The Command to invoke on . The Key /// has bound to will be used as /// /// The text to display for the command. /// The Popover Menu that will be displayed when this item is selected. public MenuBarItemv2 (View? targetView, Command command, string? commandText, PopoverMenu? popoverMenu = null) : base ( targetView, command, commandText) { TargetView = targetView; Command = command; PopoverMenu = popoverMenu; } /// /// Creates a new instance of with the specified . This is a /// helper for the most common MenuBar use-cases. /// /// /// /// The text to display for the command. /// The Popover Menu that will be displayed when this item is selected. public MenuBarItemv2 (string commandText, PopoverMenu? popoverMenu = null) : this ( null, Command.NotBound, commandText, popoverMenu) { } /// /// Creates a new instance of with the automatcialy added to a /// . /// This is a helper for the most common MenuBar use-cases. /// /// /// /// The text to display for the command. /// /// The menu items that will be added to the Popover Menu that will be displayed when this item is /// selected. /// public MenuBarItemv2 (string commandText, IEnumerable menuItems) : this ( null, Command.NotBound, commandText, new (menuItems)) { } /// /// Do not use this property. MenuBarItem does not support SubMenu. Use instead. /// /// public new Menuv2? SubMenu { get => null; set => throw new InvalidOperationException ("MenuBarItem does not support SubMenu. Use PopoverMenu instead."); } /// /// The Popover Menu that will be displayed when this item is selected. /// public PopoverMenu? PopoverMenu { get; set; } /// protected override bool OnKeyDownNotHandled (Key key) { Logging.Trace ($"{key}"); if (PopoverMenu is { Visible: true } && HotKeyBindings.TryGet (key, out _)) { // If the user presses the hotkey for a menu item that is already open, // it should close the menu item (Test: MenuBarItem_HotKey_DeActivates) if (SuperView is MenuBarv2 { } menuBar) { menuBar.HideActiveItem (); } return true; } return false; } /// protected override void Dispose (bool disposing) { if (disposing) { PopoverMenu?.Dispose (); PopoverMenu = null; } base.Dispose (disposing); } }