Browse Source

Fixed expand/collapse bugs

Tig 1 year ago
parent
commit
cf24f835ea

+ 0 - 4
Terminal.Gui/Application.cs

@@ -526,12 +526,8 @@ public static partial class Application
             MoveCurrent (Current);
         }
 
-        //if (Toplevel.LayoutStyle == LayoutStyle.Computed) {
         toplevel.SetRelativeLayout (Driver.Screen.Size);
 
-        //}
-
-        // BUGBUG: This call is likely not needed.
         toplevel.LayoutSubviews ();
         toplevel.PositionToplevels ();
         toplevel.FocusFirst ();

+ 7 - 7
Terminal.Gui/View/Layout/DimAuto.cs

@@ -78,7 +78,7 @@ public class DimAuto () : Dim
                 // TODO: This whole body of code is a WIP (for https://github.com/gui-cs/Terminal.Gui/pull/3451).
                 subviewsSize = 0;
 
-                List<View> visibleSubviews = us.Subviews.Where (v => v.Visible).ToList ();
+                List<View> includedSubviews = us.Subviews.ToList();//.Where (v => !v.ExcludeFromLayout).ToList ();
                 List<View> subviews;
 
                 #region Not Anchored and Are Not Dependent
@@ -95,14 +95,14 @@ public class DimAuto () : Dim
                 // [ ] DimView
                 if (dimension == Dimension.Width)
                 {
-                    subviews = visibleSubviews.Where (v => v.X is not PosAnchorEnd
+                    subviews = includedSubviews.Where (v => v.X is not PosAnchorEnd
                                                            && v.X is not PosAlign
                                                            // && v.X is not PosCenter
                                                            && v.Width is not DimFill).ToList ();
                 }
                 else
                 {
-                    subviews = visibleSubviews.Where (v => v.Y is not PosAnchorEnd
+                    subviews = includedSubviews.Where (v => v.Y is not PosAnchorEnd
                                                            && v.Y is not PosAlign
                                                            // && v.Y is not PosCenter
                                                            && v.Height is not DimFill).ToList ();
@@ -127,11 +127,11 @@ public class DimAuto () : Dim
                 // [x] PosAnchorEnd
                 if (dimension == Dimension.Width)
                 {
-                    subviews = visibleSubviews.Where (v => v.X is PosAnchorEnd).ToList ();
+                    subviews = includedSubviews.Where (v => v.X is PosAnchorEnd).ToList ();
                 }
                 else
                 {
-                    subviews = visibleSubviews.Where (v => v.Y is PosAnchorEnd).ToList ();
+                    subviews = includedSubviews.Where (v => v.Y is PosAnchorEnd).ToList ();
                 }
 
                 int maxAnchorEnd = 0;
@@ -171,13 +171,13 @@ public class DimAuto () : Dim
                 // [ ] DimPercent
                 if (dimension == Dimension.Width)
                 {
-                    subviews = visibleSubviews.Where (v => v.Width is DimFill
+                    subviews = includedSubviews.Where (v => v.Width is DimFill
                                                       // || v.X is PosCenter
                                                      ).ToList ();
                 }
                 else
                 {
-                    subviews = visibleSubviews.Where (v => v.Height is DimFill
+                    subviews = includedSubviews.Where (v => v.Height is DimFill
                                                       //|| v.Y is PosCenter
                                                      ).ToList ();
                 }

+ 23 - 0
Terminal.Gui/View/Layout/ViewLayout.cs

@@ -315,6 +315,29 @@ public partial class View
 
     // EndLayout
 
+    private bool _excludeFromLayout;
+
+    /// <summary>
+    ///  Gets or sets whether this View will be excluded from layout computations. 
+    /// </summary>
+    /// <remarks>
+    /// If set to <see langword="true"/>, the layout engine will ignore this view. Any subviews of this view will still be laid out.
+    /// Any views that have Pos or Dim objects that reference this view must have <see cref="ExcludeFromLayout"/> set to <see langword="true"/> as well
+    /// </remarks>
+    public bool ExcludeFromLayout
+    {
+        get => _excludeFromLayout;
+        set
+        {
+            if (value == _excludeFromLayout)
+            {
+                return;
+            }
+            _excludeFromLayout = value;
+            SetNeedsLayout();
+        }
+    }
+
     /// <summary>
     ///     Controls how the View's <see cref="Frame"/> is computed during <see cref="LayoutSubviews"/>. If the style is
     ///     set to <see cref="LayoutStyle.Absolute"/>, LayoutSubviews does not change the <see cref="Frame"/>. If the style is

+ 1 - 0
Terminal.Gui/View/View.cs

@@ -252,6 +252,7 @@ public partial class View : Responder, ISupportInitializeNotification
         }
 
         Initialized?.Invoke (this, EventArgs.Empty);
+        LayoutSubviews();
     }
 
     #endregion Constructors and Initialization

+ 3 - 4
UICatalog/Scenarios/AdornmentEditor.cs

@@ -85,6 +85,9 @@ public class AdornmentEditor : View
 
     public AdornmentEditor ()
     {
+        Width = Dim.Auto (DimAutoStyle.Content);
+        Height = Dim.Auto (DimAutoStyle.Content);
+
         BorderStyle = LineStyle.Dashed;
         Initialized += AdornmentEditor_Initialized;
     }
@@ -166,10 +169,6 @@ public class AdornmentEditor : View
         {
             subview.Enabled = AdornmentToEdit is { };
         }
-
-        Width = Dim.Auto (minimumContentDim: Dim.Func (() => expandButton.Widest));
-        Height = Dim.Auto ();
-        LayoutSubviews ();
     }
 
     private EventHandler<ColorEventArgs> ColorPickerColorChanged ()

+ 3 - 2
UICatalog/Scenarios/AdornmentsEditor.cs

@@ -27,8 +27,8 @@ public class AdornmentsEditor : View
         ColorScheme = Colors.ColorSchemes ["Dialog"];
         Title = $"AdornmentsEditor";
 
-        Width = Dim.Auto ();
-        Height = Dim.Fill ();
+        Width = Dim.Auto (DimAutoStyle.Content);
+        Height = Dim.Auto (DimAutoStyle.Content);
 
         BorderStyle = LineStyle.Double;
         //SuperViewRendersLineCanvas = true;
@@ -123,6 +123,7 @@ public class AdornmentsEditor : View
 
         Add (_diagRulerCheckBox);
         _diagRulerCheckBox.Y = Pos.Bottom (_diagPaddingCheckBox);
+
     }
 
     private void Application_MouseEvent (object sender, MouseEvent e)

+ 2 - 2
UICatalog/Scenarios/ContentScrolling.cs

@@ -355,8 +355,8 @@ public class ContentScrolling : Scenario
         {
             X = Pos.Center (),
             Y = Pos.Bottom (textView) + 1,
-            Width = 30,
-            Height = 10
+            Width = Dim.Auto(DimAutoStyle.Content, maximumContentDim: Dim.Func (() => view.ContentSize.Width)),
+            Height = Dim.Auto (DimAutoStyle.Content, maximumContentDim: Dim.Percent(20)),
         };
 
         charMap.Accept += (s, e) =>

+ 50 - 44
UICatalog/Scenarios/ExpanderButton.cs

@@ -45,7 +45,10 @@ public class ExpanderButton : Button
         Initialized += ExpanderButton_Initialized;
     }
 
-    private void ExpanderButton_Initialized (object sender, EventArgs e) { Orient (); }
+    private void ExpanderButton_Initialized (object sender, EventArgs e)
+    {
+        ExpandOrCollapse (Collapsed);
+    }
 
     private Orientation _orientation = Orientation.Horizontal;
 
@@ -76,7 +79,24 @@ public class ExpanderButton : Button
         {
             _orientation = newOrientation;
 
-            Orient ();
+            if (Orientation == Orientation.Vertical)
+            {
+                X = Pos.AnchorEnd ();
+                Y = 0;
+                CollapsedGlyph = new ('\u21d1'); // ⇑
+                ExpandedGlyph = new ('\u21d3'); // ⇓
+            }
+            else
+            {
+                X = 0;
+                Y = Pos.AnchorEnd ();
+                CollapsedGlyph = new ('\u21d0'); // ⇐
+                ExpandedGlyph = new ('\u21d2'); // ⇒
+            }
+
+            Text = $"{(Collapsed ? CollapsedGlyph : ExpandedGlyph)}";
+
+            ExpandOrCollapse (Collapsed);
         }
 
         return args.Cancel;
@@ -98,7 +118,7 @@ public class ExpanderButton : Button
     /// </summary>
     public Rune ExpandedGlyph { get; set; }
 
-    private bool _collapsed = true;
+    private bool _collapsed = false;
 
     /// <summary>
     ///     Gets or sets a value indicating whether the view is collapsed.
@@ -121,6 +141,8 @@ public class ExpanderButton : Button
         {
             _collapsed = newValue;
 
+            ExpandOrCollapse (_collapsed);
+
             View superView = SuperView;
 
             if (superView is Adornment adornment)
@@ -128,17 +150,17 @@ public class ExpanderButton : Button
                 superView = adornment.Parent;
             }
 
-            bool expanded = Orientation == Orientation.Vertical ? superView.Viewport.Height > 0 : superView.Viewport.Width > 0;
-            Orient ();
-
             foreach (View subview in superView.Subviews)
             {
-                subview.Visible = !expanded;
+                subview.Visible = !Collapsed;
+                subview.Enabled = !Collapsed;
             }
 
-            Text = $"{(Collapsed ? CollapsedGlyph : ExpandedGlyph)}";
+            // BUGBUG: This should not be needed. There's some bug in the layout system that doesn't update the layout.
+            superView.SuperView?.LayoutSubviews ();
         }
 
+
         return args.Cancel;
     }
 
@@ -147,13 +169,7 @@ public class ExpanderButton : Button
     ///     <see cref="OrientationEventArgs.Cancel"/> to true.
     /// </summary>
     public event EventHandler<StateEventArgs<bool>> CollapsedChanging;
-
-    // TODO: This is a workaround for Dim.Auto() not working as expected.
-    /// <summary>
-    ///     Gets or sets the widest/tallest dimension of the view.
-    /// </summary>
-    public int Widest { get; set; }
-
+    
     /// <summary>
     ///     Collapses or Expands the view.
     /// </summary>
@@ -165,13 +181,10 @@ public class ExpanderButton : Button
         return true;
     }
 
-    private void Orient ()
-    {
-        if (!IsInitialized)
-        {
-            return;
-        }
+    private Dim _previousDim;
 
+    private void ExpandOrCollapse (bool collapse)
+    {
         View superView = SuperView;
 
         if (superView is Adornment adornment)
@@ -179,48 +192,41 @@ public class ExpanderButton : Button
             superView = adornment.Parent;
         }
 
-        bool expanded = Orientation == Orientation.Vertical ? superView.Viewport.Height > 0 : superView.Viewport.Width > 0;
+        if (superView is null)
+        {
+            return;
+        }
 
-        if (expanded)
+        if (collapse)
         {
+            // Collapse
             if (Orientation == Orientation.Vertical)
             {
-                Widest = superView.ContentSize.Width;
+                _previousDim = superView.Height;
                 superView.Height = 1;
             }
             else
             {
-                Widest = superView.ContentSize.Height;
+                _previousDim = superView.Width;
                 superView.Width = 1;
             }
         }
         else
         {
+            if (_previousDim is null)
+            {
+                return;
+            }
+
+            // Expand
             if (Orientation == Orientation.Vertical)
             {
-                superView.Height = Dim.Auto ();
+                superView.Height = _previousDim;
             }
             else
             {
-                superView.Width = Dim.Auto ();
+                superView.Width = _previousDim;
             }
         }
-
-        if (Orientation == Orientation.Vertical)
-        {
-            X = Pos.AnchorEnd ();
-            Y = 0;
-            CollapsedGlyph = new ('\u21d1'); // ⇑
-            ExpandedGlyph = new ('\u21d3'); // ⇓
-        }
-        else
-        {
-            X = 0;
-            Y = Pos.AnchorEnd ();
-            CollapsedGlyph = new ('\u21d0'); // ⇐
-            ExpandedGlyph = new ('\u21d2'); // ⇒
-        }
-
-        Text = $"{(Collapsed ? CollapsedGlyph : ExpandedGlyph)}";
     }
 }