Browse Source

Menu code cleanup

Tig 1 year ago
parent
commit
f714a6bb35
2 changed files with 53 additions and 116 deletions
  1. 14 76
      Terminal.Gui/Views/Menu/Menu.cs
  2. 39 40
      Terminal.Gui/Views/Menu/MenuBarItem.cs

+ 14 - 76
Terminal.Gui/Views/Menu/Menu.cs

@@ -121,15 +121,6 @@ internal sealed class Menu : View
                    );
 
         AddKeyBindings (_barItems);
-#if SUPPORT_ALT_TO_ACTIVATE_MENU
-        Initialized += (s, e) =>
-                       {
-                           if (SuperView is { })
-                           {
-                               SuperView.KeyUp += SuperView_KeyUp;
-                           }
-                       };
-#endif
     }
 
     public Menu ()
@@ -175,9 +166,9 @@ internal sealed class Menu : View
                         return true;
                     }
                    );
-        AddCommand (Command.Select, (ctx) => _host?.SelectItem (ctx.KeyBinding?.Context as MenuItem));
-        AddCommand (Command.ToggleExpandCollapse, (ctx) => SelectOrRun (ctx.KeyBinding?.Context));
-        AddCommand (Command.HotKey, (ctx) => _host?.SelectItem (ctx.KeyBinding?.Context as MenuItem));
+        AddCommand (Command.Select, ctx => _host?.SelectItem (ctx.KeyBinding?.Context as MenuItem));
+        AddCommand (Command.ToggleExpandCollapse, ctx => ExpandCollapse (ctx.KeyBinding?.Context as MenuItem));
+        AddCommand (Command.HotKey, ctx => _host?.SelectItem (ctx.KeyBinding?.Context as MenuItem));
 
         // Default key bindings for this view
         KeyBindings.Add (Key.CursorUp, Command.LineUp);
@@ -186,26 +177,7 @@ internal sealed class Menu : View
         KeyBindings.Add (Key.CursorRight, Command.Right);
         KeyBindings.Add (Key.Esc, Command.Cancel);
         KeyBindings.Add (Key.Enter, Command.Accept);
-        KeyBindings.Add (Key.F9, KeyBindingScope.HotKey, Command.ToggleExpandCollapse);
-
-        KeyBindings.Add (
-                         KeyCode.CtrlMask | KeyCode.Space,
-                         KeyBindingScope.HotKey,
-                         Command.ToggleExpandCollapse
-                        );
-    }
-
-#if SUPPORT_ALT_TO_ACTIVATE_MENU
-    void SuperView_KeyUp (object sender, KeyEventArgs e)
-    {
-        if (SuperView is null || SuperView.CanFocus == false || SuperView.Visible == false)
-        {
-            return;
-        }
-
-        _host.AltKeyUpHandler (e);
     }
-#endif
 
     private void AddKeyBindings (MenuBarItem menuBarItem)
     {
@@ -235,35 +207,29 @@ internal sealed class Menu : View
         }
     }
 
-    // TODO: Remove these now that we're using context
-    private MenuItem _menuItemToSelect;
-
-    /// <summary>Called when a key bound to Command.Select is pressed. This means a hot key was pressed.</summary>
+    /// <summary>Called when a key bound to Command.ToggleExpandCollapse is pressed. This means a hot key was pressed.</summary>
     /// <returns></returns>
-    private bool SelectOrRun ([CanBeNull] object context)
+    private bool ExpandCollapse (MenuItem menuItem)
     {
         if (!IsInitialized || !Visible)
         {
             return true;
         }
 
-        if (context is MenuItem menuItem)
+
+        for (var c = 0; c < _barItems.Children.Length; c++)
         {
-            _menuItemToSelect = menuItem;
-            for (int c = 0; c < _barItems.Children.Length; c++)
+            if (_barItems.Children [c] == menuItem)
             {
-                if (_barItems.Children [c] == menuItem)
-                {
-                    _currentChild = c;
+                _currentChild = c;
 
-                    break;
-                }
+                break;
             }
         }
 
-        if (_menuItemToSelect is { })
+        if (menuItem is { })
         {
-            var m = _menuItemToSelect as MenuBarItem;
+            var m = menuItem as MenuBarItem;
 
             if (m?.Children?.Length > 0)
             {
@@ -291,7 +257,7 @@ internal sealed class Menu : View
             }
             else
             {
-                _host.SelectItem (_menuItemToSelect);
+                _host.SelectItem (menuItem);
             }
         }
         else if (_host.IsMenuOpen)
@@ -303,7 +269,6 @@ internal sealed class Menu : View
             _host.OpenMenu ();
         }
 
-        //_openedByHotKey = true;
         return true;
     }
 
@@ -344,7 +309,7 @@ internal sealed class Menu : View
             Application.MouseEvent -= Application_RootMouseEvent;
         }
     }
-    
+
     private void Application_RootMouseEvent (object sender, MouseEvent a)
     {
         if (a.View is { } and (MenuBar or not Menu))
@@ -939,33 +904,6 @@ internal sealed class Menu : View
         return true;
     }
 
-    private int GetSubMenuIndex (MenuBarItem subMenu)
-    {
-        int pos = -1;
-
-        if (Subviews.Count == 0)
-        {
-            return pos;
-        }
-
-        Menu v = null;
-
-        foreach (View menu in Subviews)
-        {
-            if (((Menu)menu)._barItems == subMenu)
-            {
-                v = (Menu)menu;
-            }
-        }
-
-        if (v is { })
-        {
-            pos = Subviews.IndexOf (v);
-        }
-
-        return pos;
-    }
-
     protected override void Dispose (bool disposing)
     {
         if (Application.Current is { })

+ 39 - 40
Terminal.Gui/Views/Menu/MenuBarItem.cs

@@ -11,7 +11,7 @@ public class MenuBarItem : MenuItem
     /// <param name="help">Help text to display. Will be displayed next to the Title surrounded by parentheses.</param>
     /// <param name="action">Action to invoke when the menu item is activated.</param>
     /// <param name="canExecute">Function to determine if the action can currently be executed.</param>
-    /// <param name="parent">The parent <see cref="MenuItem"/> of this if exist, otherwise is null.</param>
+    /// <param name="parent">The parent <see cref="MenuItem"/> of this if any.</param>
     public MenuBarItem (
         string title,
         string help,
@@ -26,13 +26,13 @@ public class MenuBarItem : MenuItem
     /// <summary>Initializes a new <see cref="MenuBarItem"/>.</summary>
     /// <param name="title">Title for the menu item.</param>
     /// <param name="children">The items in the current menu.</param>
-    /// <param name="parent">The parent <see cref="MenuItem"/> of this if exist, otherwise is null.</param>
+    /// <param name="parent">The parent <see cref="MenuItem"/> of this if any.</param>
     public MenuBarItem (string title, MenuItem [] children, MenuItem parent = null) { SetInitialProperties (title, children, parent); }
 
     /// <summary>Initializes a new <see cref="MenuBarItem"/> with separate list of items.</summary>
     /// <param name="title">Title for the menu item.</param>
     /// <param name="children">The list of items in the current menu.</param>
-    /// <param name="parent">The parent <see cref="MenuItem"/> of this if exist, otherwise is null.</param>
+    /// <param name="parent">The parent <see cref="MenuItem"/> of this if any.</param>
     public MenuBarItem (string title, List<MenuItem []> children, MenuItem parent = null) { SetInitialProperties (title, children, parent); }
 
     /// <summary>Initializes a new <see cref="MenuBarItem"/>.</summary>
@@ -40,7 +40,7 @@ public class MenuBarItem : MenuItem
     public MenuBarItem (MenuItem [] children) : this ("", children) { }
 
     /// <summary>Initializes a new <see cref="MenuBarItem"/>.</summary>
-    public MenuBarItem () : this (new MenuItem [] { }) { }
+    public MenuBarItem () : this ([]) { }
 
     /// <summary>
     ///     Gets or sets an array of <see cref="MenuItem"/> objects that are the children of this
@@ -58,17 +58,19 @@ public class MenuBarItem : MenuItem
     {
         var i = 0;
 
-        if (Children is { })
+        if (Children is null)
         {
-            foreach (MenuItem child in Children)
-            {
-                if (child == children)
-                {
-                    return i;
-                }
+            return -1;
+        }
 
-                i++;
+        foreach (MenuItem child in Children)
+        {
+            if (child == children)
+            {
+                return i;
             }
+
+            i++;
         }
 
         return -1;
@@ -79,15 +81,7 @@ public class MenuBarItem : MenuItem
     /// <returns>Returns <c>true</c> if it is a submenu. <c>false</c> otherwise.</returns>
     public bool IsSubMenuOf (MenuItem menuItem)
     {
-        foreach (MenuItem child in Children)
-        {
-            if (child == menuItem && child.Parent == menuItem.Parent)
-            {
-                return true;
-            }
-        }
-
-        return false;
+        return Children.Any (child => child == menuItem && child.Parent == menuItem.Parent);
     }
 
     /// <summary>Check if a <see cref="MenuItem"/> is a <see cref="MenuBarItem"/>.</summary>
@@ -133,30 +127,35 @@ public class MenuBarItem : MenuItem
             Parent = parent;
         }
 
-        if (children is List<MenuItem []> childrenList)
+        switch (children)
         {
-            MenuItem [] newChildren = [];
-
-            foreach (MenuItem [] grandChild in childrenList)
+            case List<MenuItem []> childrenList:
             {
-                foreach (MenuItem child in grandChild)
+                MenuItem [] newChildren = [];
+
+                foreach (MenuItem [] grandChild in childrenList)
                 {
-                    SetParent (grandChild);
-                    Array.Resize (ref newChildren, newChildren.Length + 1);
-                    newChildren [^1] = child;
+                    foreach (MenuItem child in grandChild)
+                    {
+                        SetParent (grandChild);
+                        Array.Resize (ref newChildren, newChildren.Length + 1);
+                        newChildren [^1] = child;
+                    }
                 }
+
+                Children = newChildren;
+
+                break;
             }
+            case MenuItem [] items:
+                SetParent (items);
+                Children = items;
 
-            Children = newChildren;
-        }
-        else if (children is MenuItem [] items)
-        {
-            SetParent (items);
-            Children = items;
-        }
-        else
-        {
-            Children = null;
+                break;
+            default:
+                Children = null;
+
+                break;
         }
     }
 
@@ -176,4 +175,4 @@ public class MenuBarItem : MenuItem
         title ??= string.Empty;
         Title = title;
     }
-}
+}