diff --git a/Terminal.Gui/Views/Menu.cs b/Terminal.Gui/Views/Menu.cs index 326cc1036..7a7d6e3d3 100644 --- a/Terminal.Gui/Views/Menu.cs +++ b/Terminal.Gui/Views/Menu.cs @@ -1,13 +1,3 @@ -// -// Menu.cs: application menus and submenus -// -// Authors: -// Miguel de Icaza (miguel@gnome.org) -// -// TODO: -// Add accelerator support, but should also support chords (Shortcut in MenuItem) -// Allow menus inside menus - using System; using NStack; using System.Linq; @@ -26,18 +16,19 @@ namespace Terminal.Gui { NoCheck = 0b_0000_0000, /// - /// The menu item will indicate checked/un-checked state (see . + /// The menu item will indicate checked/un-checked state (see ). /// Checked = 0b_0000_0001, /// - /// The menu item is part of a menu radio group (see and will indicate selected state. + /// The menu item is part of a menu radio group (see ) and will indicate selected state. /// Radio = 0b_0000_0010, }; /// - /// A has a title, an associated help text, and an action to execute on activation. + /// A has title, an associated help text, and an action to execute on activation. + /// MenuItems can also have a checked indicator (see ). /// public class MenuItem { ustring title; @@ -78,14 +69,28 @@ namespace Terminal.Gui { } /// - /// The HotKey is used when the menu is active, the shortcut can be triggered when the menu is not active. - /// For example HotKey would be "N" when the File Menu is open (assuming there is a "_New" entry - /// if the Shortcut is set to "Control-N", this would be a global hotkey that would trigger as well + /// The HotKey is used to activate a with they keyboard. HotKeys are defined by prefixing the + /// of a MenuItem with an underscore ('_'). + /// + /// Pressing Alt-Hotkey for a (menu items on the menu bar) works even if the menu is not active). + /// Once a menu has focus and is active, pressing just the HotKey will activate the MenuItem. + /// + /// + /// For example for a MenuBar with a "_File" MenuBarItem that contains a "_New" MenuItem, Alt-F will open the File menu. + /// Pressing the N key will then activate the New MenuItem. + /// + /// + /// See also which enable global key-bindings to menu items. + /// /// public Rune HotKey; /// - /// This is the global setting that can be used as a global to invoke the action on the menu. + /// Shortcut defines a key binding to the MenuItem that will invoke the MenuItem's action globally for the that is + /// the parent of the or this . + /// + /// The will be drawn on the MenuItem to the right of the and text. See . + /// /// public Key Shortcut { get => shortcutHelper.Shortcut; @@ -97,12 +102,12 @@ namespace Terminal.Gui { } /// - /// The keystroke combination used in the as string. + /// Gets the text describing the keystroke combination defined by . /// public ustring ShortcutTag => ShortcutHelper.GetShortcutTag (shortcutHelper.Shortcut); /// - /// Gets or sets the title. + /// Gets or sets the title of the menu item . /// /// The title. public ustring Title { @@ -116,41 +121,46 @@ namespace Terminal.Gui { } /// - /// Gets or sets the help text for the menu item. + /// Gets or sets the help text for the menu item. The help text is drawn to the right of the . /// /// The help text. public ustring Help { get; set; } /// - /// Gets or sets the action to be invoked when the menu is triggered + /// Gets or sets the action to be invoked when the menu item is triggered. /// /// Method to invoke. public Action Action { get; set; } /// - /// Gets or sets the action to be invoked if the menu can be triggered + /// Gets or sets the action to be invoked to determine if the menu can be triggered. If returns + /// the menu item will be enabled. Otherwise it will be disabled. /// - /// Function to determine if action is ready to be executed. + /// Function to determine if the action is can be executed or not. public Func CanExecute { get; set; } /// - /// Shortcut to check if the menu item is enabled + /// Returns if the menu item is enabled. This method is a wrapper around . /// public bool IsEnabled () { return CanExecute == null ? true : CanExecute (); } + // // ┌─────────────────────────────┐ // │ Quit Quit UI Catalog Ctrl+Q │ // └─────────────────────────────┘ // ┌─────────────────┐ // │ ◌ TopLevel Alt+T │ // └─────────────────┘ - // TODO: Repalace the `2` literals with named constants (e.g. spacesAfterHelp and spacesAfterCheck and spacesAfterShortCutTag) - internal int Width => + TitleLength + (Help.ConsoleWidth > 0 ? Help.ConsoleWidth + 2 : 0) + - (Checked || CheckType.HasFlag (MenuItemCheckStyle.Checked) || CheckType.HasFlag (MenuItemCheckStyle.Radio) ? 2 : 0) + - (ShortcutTag.ConsoleWidth > 0 ? ShortcutTag.ConsoleWidth + 2 : 0) + 2; + // TODO: Repalace the `2` literals with named constants + internal int Width => 1 + // space before Title + TitleLength + + 2 + // space after Title - BUGBUG: This should be 1 + (Checked || CheckType.HasFlag (MenuItemCheckStyle.Checked) || CheckType.HasFlag (MenuItemCheckStyle.Radio) ? 2 : 0) + // check glyph + space + (Help.ConsoleWidth > 0 ? 2 + Help.ConsoleWidth : 0) + // Two spaces before Help + (ShortcutTag.ConsoleWidth > 0 ? 2 + ShortcutTag.ConsoleWidth : 0); // Pad two spaces before shortcut tag (which are also aligned right) /// /// Sets or gets whether the shows a check indicator or not. See . @@ -158,12 +168,12 @@ namespace Terminal.Gui { public bool Checked { set; get; } /// - /// Sets or gets the type selection indicator the menu item will be displayed with. + /// Sets or gets the of a menu item where is set to . /// public MenuItemCheckStyle CheckType { get; set; } /// - /// Gets or sets the parent for this . + /// Gets the parent for this . /// /// The parent. public MenuItem Parent { get; internal set; } @@ -174,7 +184,7 @@ namespace Terminal.Gui { internal bool IsFromSubMenu { get { return Parent != null; } } /// - /// Merely a debugging aid to see the interaction with main + /// Merely a debugging aid to see the interaction with main. /// public MenuItem GetMenuItem () { @@ -182,7 +192,7 @@ namespace Terminal.Gui { } /// - /// Merely a debugging aid to see the interaction with main + /// Merely a debugging aid to see the interaction with main. /// public bool GetMenuBarItem () { @@ -220,7 +230,8 @@ namespace Terminal.Gui { } /// - /// A contains s or s. + /// is a menu item on an app's . MenuBarItems do not support + /// . /// public class MenuBarItem : MenuItem { /// @@ -296,19 +307,6 @@ namespace Terminal.Gui { } } - //static int GetMaxTitleLength (MenuItem [] children) - //{ - // int maxLength = 0; - // foreach (var item in children) { - // int len = GetMenuBarItemLength (item.Title); - // if (len > maxLength) - // maxLength = len; - // item.IsFromSubMenu = true; - // } - - // return maxLength; - //} - void SetChildrensParent (MenuItem [] childrens) { foreach (var child in childrens) { @@ -370,12 +368,6 @@ namespace Terminal.Gui { Title = title; } - ///// - ///// Gets or sets the title to display. - ///// - ///// The title. - //public ustring Title { get; set; } - /// /// Gets or sets an array of objects that are the children of this /// @@ -485,14 +477,14 @@ namespace Terminal.Gui { Move (1, i + 1); Driver.SetAttribute (DetermineColorSchemeFor (item, i)); - for (int p = Bounds.X; p < Frame.Width - 2; p++) { // This - 2 is for the border? + for (int p = Bounds.X; p < Frame.Width - 2; p++) { // This - 2 is for the border if (p < 0) continue; if (item == null) Driver.AddRune (Driver.HLine); else if (i == 0 && p == 0 && host.UseSubMenusSingleFrame && item.Parent.Parent != null) Driver.AddRune (Driver.LeftArrow); - // TODO: Change this `- 3` to a const (is it spacesAfterTitle?) + // This `- 3` is left border + right border + one row in from right else if (p == Frame.Width - 3 && barItems.SubMenu (barItems.Children [i]) != null) Driver.AddRune (Driver.RightArrow); else @@ -525,7 +517,6 @@ namespace Terminal.Gui { textToDraw = item.Title; } - // Draw the item. The `2` is for the left border and the space before the text ViewToScreen (2, i + 1, out int vtsCol, out _, false); if (vtsCol < Driver.Cols) { Move (2, i + 1); @@ -537,7 +528,7 @@ namespace Terminal.Gui { HotKeySpecifier = MenuBar.HotKeySpecifier, Text = textToDraw }; - // TODO: Change this `- 3` to a const (is it spacesAfterTitle?) + // The -3 is left/right border + one space (not sure what for) tf.Draw (ViewToScreen (new Rect (2, i + 1, Frame.Width - 3, 1)), i == current ? ColorScheme.Focus : GetNormalColor (), i == current ? ColorScheme.HotFocus : ColorScheme.HotNormal, @@ -846,14 +837,19 @@ namespace Terminal.Gui { /// + /// /// Provides a menu bar with drop-down and cascading menus. + /// + /// + /// + /// /// /// /// - /// The appears on the first row of the terminal. + /// The appears on the first row of the terminal and uses the full width. /// /// - /// The provides global hotkeys for the application. + /// The provides global hotkeys for the application. See . /// /// public class MenuBar : View { @@ -1035,7 +1031,7 @@ namespace Terminal.Gui { isCleaning = false; } - // The column where the MenuBar starts + // The column where the MenuBar starts static int xOrigin = 0; // Spaces before the Title static int leftPadding = 1; @@ -1086,15 +1082,10 @@ namespace Terminal.Gui { for (int i = 0; i < Menus.Length; i++) { if (i == selected) { pos++; - // BUGBUG: This if is not needed - if (IsMenuOpen) - Move (pos + 1, 0); - else { - Move (pos + 1, 0); - } + Move (pos + 1, 0); return; } else { - pos += leftPadding + Menus [i].TitleLength + (Menus [i].Help.ConsoleWidth > 0 ? Menus [i].Help.ConsoleWidth + parensAroundHelp : 0)+ rightPadding; + pos += leftPadding + Menus [i].TitleLength + (Menus [i].Help.ConsoleWidth > 0 ? Menus [i].Help.ConsoleWidth + parensAroundHelp : 0) + rightPadding; } } } diff --git a/UICatalog/UICatalog.cs b/UICatalog/UICatalog.cs index 5b94d3e55..9949c337f 100644 --- a/UICatalog/UICatalog.cs +++ b/UICatalog/UICatalog.cs @@ -318,7 +318,7 @@ namespace UICatalog { { List menuItems = new List (); var item = new MenuItem (); - item.Title = "_Disable/Enable Mouse"; + item.Title = "_Disable Mouse"; item.Shortcut = Key.CtrlMask | Key.AltMask | (Key)item.Title.ToString ().Substring (1, 1) [0]; item.CheckType |= MenuItemCheckStyle.Checked; item.Checked = Application.IsMouseDisabled; @@ -334,7 +334,8 @@ namespace UICatalog { List menuItems = new List (); var item = new MenuItem (); - item.Title = "Keybindings"; + item.Title = "_Key Bindings"; + item.Help = "Change which keys do what"; item.Action += () => { var dlg = new KeyBindingsDialog (); Application.Run (dlg);