Browse Source

Made Menuv2 work right

Tig 1 year ago
parent
commit
8cfd218f86

+ 32 - 8
Terminal.Gui/Views/Bar.cs

@@ -24,7 +24,6 @@ public class Bar : View
         Width = Dim.Auto ();
         Height = Dim.Auto ();
 
-        LayoutStarted += Bar_LayoutStarted;
         Initialized += Bar_Initialized;
 
         if (shortcuts is null)
@@ -137,8 +136,11 @@ public class Bar : View
         return toRemove as Shortcut;
     }
 
-    private void Bar_LayoutStarted (object sender, LayoutEventArgs e)
+    /// <inheritdoc />
+    internal override void OnLayoutStarted (LayoutEventArgs args)
     {
+        base.OnLayoutStarted (args);
+
         View prevBarItem = null;
 
         switch (Orientation)
@@ -160,6 +162,17 @@ public class Bar : View
 
             case Orientation.Vertical:
                 // Set the overall size of the Bar and arrange the views vertically
+
+                var minKeyWidth = 0;
+
+                List<Shortcut> shortcuts = Subviews.Where (s => s is Shortcut && s.Visible).Cast<Shortcut> ().ToList ();
+                foreach (Shortcut shortcut in shortcuts)
+                {
+                    // Let AutoSize do its thing to get the minimum width of each CommandView and HelpView
+                    //shortcut.CommandView.SetRelativeLayout (new Size (int.MaxValue, int.MaxValue));
+                    minKeyWidth = int.Max (minKeyWidth, shortcut.KeyView.Text.GetColumns ());
+                }
+
                 var maxBarItemWidth = 0;
                 var totalHeight = 0;
 
@@ -174,6 +187,14 @@ public class Bar : View
                         continue;
                     }
 
+                    if (barItem is Shortcut scBarItem)
+                    {
+                        scBarItem.MinimumKeyViewSize = minKeyWidth;
+                        // HACK: This should not be needed
+                        scBarItem.SetRelativeLayout (GetContentSize ());
+                        maxBarItemWidth = Math.Max (maxBarItemWidth, scBarItem.Frame.Width);
+                    }
+
                     if (prevBarItem == null)
                     {
                         barItem.Y = 0;
@@ -186,16 +207,19 @@ public class Bar : View
 
                     prevBarItem = barItem;
 
-                    //maxBarItemWidth = Math.Max (maxBarItemWidth, barItem.Frame.Width);
-
                     barItem.X = 0;
                     totalHeight += barItem.Frame.Height;
                 }
 
-                //foreach (Shortcut shortcut in shortcuts)
-                //{
-                //    shortcut.Width = maxBarItemWidth;
-                //}
+
+                foreach (View barItem in Subviews)
+                {
+                    barItem.Width = maxBarItemWidth;
+
+                    if (barItem is Line line)
+                    {
+                    }
+                }
 
                 Height = Dim.Auto (DimAutoStyle.Content, totalHeight);
 

+ 58 - 3
Terminal.Gui/Views/Line.cs

@@ -8,26 +8,81 @@ public class Line : View
     {
         BorderStyle = LineStyle.Single;
         Border.Thickness = new Thickness (0);
+        SuperViewRendersLineCanvas = true;
     }
 
+    public Dim Length { get; set; } = Dim.Fill ();
+
+    private Orientation _orientation;
+
     /// <summary>
     ///     The direction of the line.  If you change this you will need to manually update the Width/Height of the
     ///     control to cover a relevant area based on the new direction.
     /// </summary>
-    public Orientation Orientation { get; set; }
+    public Orientation Orientation
+    {
+        get => _orientation;
+        set
+        {
+            _orientation = value;
+
+            switch (Orientation)
+            {
+                case Orientation.Horizontal:
+                    Height = 1;
+                    // Width = Length;
+                    //Border.Thickness = new Thickness (1, 0, 1, 0);
+
+                    break;
+                case Orientation.Vertical:
+                    Height = Length;
+                    Width = 1;
+
+                    break;
+
+            }
+        }
+    }
+
+    /// <inheritdoc/>
+    public override void SetBorderStyle (LineStyle value)
+    {
+        // The default changes the thickness. We don't want that. We just set the style.
+        Border.LineStyle = value;
+    }
 
     /// <inheritdoc/>
     public override void OnDrawContent (Rectangle viewport)
     {
         LineCanvas lc = LineCanvas;
 
+        if (SuperViewRendersLineCanvas)
+        {
+            lc = SuperView.LineCanvas;
+        }
+
         if (SuperView is Adornment adornment)
         {
             lc = adornment.Parent.LineCanvas;
         }
+
+        Point pos = ViewportToScreen (viewport).Location;
+        int length = Orientation == Orientation.Horizontal ? Frame.Width : Frame.Height;
+
+        if (SuperViewRendersLineCanvas && Orientation == Orientation.Horizontal)
+        {
+            pos.Offset (-SuperView.Border.Thickness.Left, 0);
+            length += SuperView.Border.Thickness.Horizontal;
+        }
+
+        if (SuperViewRendersLineCanvas && Orientation == Orientation.Vertical)
+        {
+            pos.Offset (0, -SuperView.Border.Thickness.Top);
+            length += SuperView.Border.Thickness.Vertical;
+        }
         lc.AddLine (
-                    ViewportToScreen (viewport).Location,
-                    Orientation == Orientation.Horizontal ? Frame.Width : Frame.Height,
+                    pos,
+                    length,
                     Orientation,
                     BorderStyle
                    );

+ 1 - 51
Terminal.Gui/Views/MenuBarv2.cs

@@ -30,57 +30,7 @@ public class MenuBarv2 : Bar
     // The Shortcuts are configured with the command, help, and key views aligned in reverse order (EndToStart).
     private void MenuBarv2_LayoutStarted (object sender, LayoutEventArgs e)
     {
-        var minKeyWidth = 0;
-
-        List<Shortcut> shortcuts = Subviews.Where (s => s is Shortcut && s.Visible).Cast<Shortcut> ().ToList ();
-
-        foreach (Shortcut shortcut in shortcuts)
-        {
-            // Let AutoSize do its thing to get the minimum width of each CommandView and HelpView
-            //shortcut.CommandView.SetRelativeLayout (new Size (int.MaxValue, int.MaxValue));
-            minKeyWidth = int.Max (minKeyWidth, shortcut.KeyView.Text.GetColumns ());
-        }
-
-        View prevBarItem = null;
-        var maxBarItemWidth = 0;
-
-        for (int index = 0; index < Subviews.Count; index++)
-        {
-            View barItem = Subviews [index];
-
-            if (!barItem.Visible)
-            {
-                continue;
-            }
-
-            if (barItem is Shortcut scBarItem)
-            {
-                scBarItem.MinimumKeyViewSize = minKeyWidth;
-            }
-
-            if (index == Subviews.Count - 1)
-            {
-                barItem.Border.Thickness = new Thickness (0, 0, 0, 0);
-            }
-            else
-            {
-                barItem.Border.Thickness = new Thickness (0, 0, 0, 0);
-            }
-
-            if (barItem is Shortcut shortcut)
-            {
-                //                shortcut.Min
-                // shortcut.Orientation = Orientation.Vertical;
-            }
-
-            maxBarItemWidth = Math.Max (maxBarItemWidth, barItem.Frame.Width);
-
-        }
-
-        foreach (Shortcut shortcut in shortcuts)
-        {
-            shortcut.Width = maxBarItemWidth;
-        }
+       
     }
 
     /// <inheritdoc/>

+ 9 - 54
Terminal.Gui/Views/Menuv2.cs

@@ -17,29 +17,19 @@ public class Menuv2 : Bar
         Width = Dim.Auto ();
         Height = Dim.Auto (DimAutoStyle.Content, 1);
         ColorScheme = Colors.ColorSchemes ["Menu"];
+        Initialized += Menuv2_Initialized;
+    }
 
-        LayoutStarted += Menuv2_LayoutStarted;
+    private void Menuv2_Initialized (object sender, EventArgs e)
+    {
+        Border.Thickness = new Thickness (1, 1, 1, 1);
     }
 
     // Menuv2 arranges the items horizontally.
     // The first item has no left border, the last item has no right border.
     // The Shortcuts are configured with the command, help, and key views aligned in reverse order (EndToStart).
-    private void Menuv2_LayoutStarted (object sender, LayoutEventArgs e)
+    internal override void OnLayoutStarted (LayoutEventArgs args)
     {
-        var minKeyWidth = 0;
-
-        List<Shortcut> shortcuts = Subviews.Where (s => s is Shortcut && s.Visible).Cast<Shortcut> ().ToList ();
-
-        foreach (Shortcut shortcut in shortcuts)
-        {
-            // Let AutoSize do its thing to get the minimum width of each CommandView and HelpView
-            //shortcut.CommandView.SetRelativeLayout (new Size (int.MaxValue, int.MaxValue));
-            minKeyWidth = int.Max (minKeyWidth, shortcut.KeyView.Text.GetColumns ());
-        }
-
-        View prevBarItem = null;
-        var maxBarItemWidth = 0;
-
         for (int index = 0; index < Subviews.Count; index++)
         {
             View barItem = Subviews [index];
@@ -49,59 +39,24 @@ public class Menuv2 : Bar
                 continue;
             }
 
-            if (barItem is Shortcut scBarItem)
-            {
-                scBarItem.MinimumKeyViewSize = minKeyWidth;
-            }
-
-            if (index == Subviews.Count - 1)
-            {
-                barItem.Border.Thickness = new Thickness (1, 0, 1, 1);
-            }
-            else if (index == 0)
-            {
-                barItem.Border.Thickness = new Thickness (1, 1, 1, 0);
-            }
-            else
-            {
-                barItem.Border.Thickness = new Thickness (1, 0, 1, 0);
-            }
-
-            if (barItem is Shortcut shortcut)
-            {
-                //                shortcut.Min
-                // shortcut.Orientation = Orientation.Vertical;
-            }
-
-            prevBarItem = barItem;
-            // HACK: This should not be needed
-            barItem.SetRelativeLayout (GetContentSize ());
-
-            maxBarItemWidth = Math.Max (maxBarItemWidth, barItem.Frame.Width);
-        }
-
-        foreach (Shortcut shortcut in shortcuts)
-        {
-            shortcut.Width = maxBarItemWidth;
         }
+        base.OnLayoutStarted (args);
     }
 
     /// <inheritdoc/>
     public override View Add (View view)
     {
-        // Call base first, because otherwise it resets CanFocus to true
         base.Add (view);
 
-        view.CanFocus = true;
-
         if (view is Shortcut shortcut)
         {
+            shortcut.CanFocus = true;
             shortcut.KeyBindingScope = KeyBindingScope.Application;
+            shortcut.Orientation = Orientation.Vertical;
 
             // TODO: not happy about using AlignmentModes for this. Too implied.
             // TODO: instead, add a property (a style enum?) to Shortcut to control this
             //shortcut.AlignmentModes = AlignmentModes.EndToStart;
-
         }
 
         return view;

+ 9 - 3
UICatalog/Scenarios/Bars.cs

@@ -116,8 +116,7 @@ public class Bars : Scenario
             //Width = Dim.Percent (40),
             Orientation = Orientation.Vertical,
         };
-        bar.Border.Thickness = new (1);
-        ConfigureMenu (bar);
+            ConfigureMenu (bar);
 
         menuLikeExamples.Add (bar);
 
@@ -391,6 +390,13 @@ public class Bars : Scenario
             Key = Key.G.WithAlt,
         };
 
+        var line = new Line ()
+        {
+            BorderStyle = LineStyle.Dotted,
+            Orientation = Orientation.Horizontal,
+            CanFocus = false,
+        };
+
         var shortcut3 = new Shortcut
         {
             Title = "_Three",
@@ -398,7 +404,7 @@ public class Bars : Scenario
             Key = Key.D3.WithAlt,
         };
 
-        bar.Add (shortcut1, shortcut2, shortcut3);
+        bar.Add (shortcut1, shortcut2, line, shortcut3);
     }
 
     private void ConfigStatusBar (Bar bar)

+ 1 - 0
UICatalog/Scenarios/LineViewExample.cs

@@ -11,6 +11,7 @@ public class LineViewExample : Scenario
 {
     public override void Main ()
     {
+        Application.Init ();
         // Setup - Create a top-level application window and configure it.
         Toplevel appWindow = new ();
 

+ 3 - 0
UnitTests/UICatalog/ScenarioTests.cs

@@ -36,6 +36,9 @@ public class ScenarioTests : TestsAllViews
 
         Scenario scenario = (Scenario)Activator.CreateInstance (scenarioType);
 
+        // BUGBUG: Scenario.Main is supposed to call Init. This is a workaround for now.
+        // BUGBUG: To be able to test Scenarios we need Application to have an event like "Application.Started"
+        // BUGBUG: That tests like this could subscribe to.
         Application.Init (new FakeDriver ());
 
         // Press QuitKey