2
0
Tig 10 сар өмнө
parent
commit
a87d4359d4

+ 15 - 29
Terminal.Gui/Views/ScrollBar/ScrollBar.cs

@@ -26,6 +26,13 @@ public class ScrollBar : View, IOrientation, IDesignable
     /// <inheritdoc/>
     public ScrollBar ()
     {
+        // Set the default width and height based on the orientation - fill Viewport
+        Width = Dim.Auto (DimAutoStyle.Content,
+                          minimumContentDim: Dim.Func (() => Orientation == Orientation.Vertical ? 1 : SuperView?.Viewport.Width ?? 0));
+
+        Height = Dim.Auto (DimAutoStyle.Content,
+                           minimumContentDim: Dim.Func (() => Orientation == Orientation.Vertical ? SuperView?.Viewport.Height ?? 0 : 1));
+
         _decreaseButton = new ()
         {
             CanFocus = false,
@@ -85,11 +92,11 @@ public class ScrollBar : View, IOrientation, IDesignable
     {
         if (Orientation == Orientation.Vertical)
         {
-            Visible = Frame.Height < ScrollableContentSize;
+            Visible = VisibleContentSize < ScrollableContentSize;
         }
         else
         {
-            Visible = Frame.Width < ScrollableContentSize;
+            Visible = VisibleContentSize < ScrollableContentSize;
         }
 
         if (!AutoHide)
@@ -110,13 +117,7 @@ public class ScrollBar : View, IOrientation, IDesignable
         _sliderPosition = CalculateSliderPositionFromContentPosition (_position, NavigationDirection.Forward);
         _slider.Position = _sliderPosition.Value;
     }
-
-    /// <inheritdoc/>
-    protected override void OnSubviewLayout (LayoutEventArgs args)
-    {
-
-    }
-
+    
     private void PositionSubviews ()
     {
         if (Orientation == Orientation.Vertical)
@@ -179,25 +180,6 @@ public class ScrollBar : View, IOrientation, IDesignable
         TextDirection = Orientation == Orientation.Vertical ? TextDirection.TopBottom_LeftRight : TextDirection.LeftRight_TopBottom;
         TextAlignment = Alignment.Center;
         VerticalTextAlignment = Alignment.Center;
-
-        X = 0;
-        Y = 0;
-
-        if (Orientation == Orientation.Vertical)
-        {
-            Height = Dim.Func (() => SuperView?.Viewport.Height ?? 0);
-            Width = 1;
-            X = Pos.AnchorEnd ();
-            Y = 0;
-        }
-        else
-        {
-            Width = Dim.Func (() => SuperView?.Viewport.Width ?? 0);
-            Height = 1;
-            X = 0;
-            Y = Pos.AnchorEnd ();
-        }
-
         _slider.Orientation = newOrientation;
         PositionSubviews ();
 
@@ -214,12 +196,16 @@ public class ScrollBar : View, IOrientation, IDesignable
     /// </remarks>
     public int Increment { get; set; } = 1;
 
-    private bool _autoHide = true;
+    // AutoHide should be false by default. Views should not be hidden by default.
+    private bool _autoHide = false;
 
     /// <summary>
     ///     Gets or sets whether <see cref="View.Visible"/> will be set to <see langword="false"/> if the dimension of the
     ///     scroll bar is greater than or equal to <see cref="ScrollableContentSize"/>.
     /// </summary>
+    /// <remarks>
+    ///     The default is <see langword="false"/>.
+    /// </remarks>
     public bool AutoHide
     {
         get => _autoHide;

+ 35 - 30
UnitTests/Views/ScrollBarTests.cs

@@ -15,13 +15,13 @@ public class ScrollBarTests (ITestOutputHelper output)
         Assert.Equal (0, scrollBar.VisibleContentSize);
         Assert.Equal (0, scrollBar.GetSliderPosition ());
         Assert.Equal (0, scrollBar.Position);
-        Assert.True (scrollBar.AutoHide);
+        Assert.False (scrollBar.AutoHide);
     }
 
     #region AutoHide
     [Fact]
     [AutoInitShutdown]
-    public void AutoHide_True_Is_Default_CorrectlyHidesAndShows ()
+    public void AutoHide_False_Is_Default_CorrectlyHidesAndShows ()
     {
         var super = new Toplevel ()
         {
@@ -34,8 +34,12 @@ public class ScrollBarTests (ITestOutputHelper output)
         {
         };
         super.Add (scrollBar);
+        Assert.False (scrollBar.AutoHide);
+        Assert.True (scrollBar.Visible);
+
+        scrollBar.AutoHide = true;
         Assert.True (scrollBar.AutoHide);
-        Assert.True (scrollBar.Visible); // Before Init
+        Assert.True (scrollBar.Visible);
 
         RunState rs = Application.Begin (super);
 
@@ -82,7 +86,7 @@ public class ScrollBarTests (ITestOutputHelper output)
 
     [Fact]
     [AutoInitShutdown]
-    public void AutoHide_Change_AutoSize_CorrectlyHidesAndShows ()
+    public void AutoHide_True_Changing_ScrollableContentSize_CorrectlyHidesAndShows ()
     {
         var super = new Toplevel ()
         {
@@ -96,8 +100,10 @@ public class ScrollBarTests (ITestOutputHelper output)
             ScrollableContentSize = 20,
         };
         super.Add (scrollBar);
-        Assert.True (scrollBar.AutoHide);
-        Assert.True (scrollBar.Visible); // Before Init
+        Assert.False (scrollBar.AutoHide);
+        Assert.True (scrollBar.Visible);
+
+        scrollBar.AutoHide = true;
 
         RunState rs = Application.Begin (super);
 
@@ -110,12 +116,15 @@ public class ScrollBarTests (ITestOutputHelper output)
         Assert.False (scrollBar.Visible);
 
         scrollBar.ScrollableContentSize = 30;
+        Application.RunIteration (ref rs);
         Assert.True (scrollBar.Visible);
 
         scrollBar.AutoHide = false;
+        Application.RunIteration (ref rs);
         Assert.True (scrollBar.Visible);
 
         scrollBar.ScrollableContentSize = 10;
+        Application.RunIteration (ref rs);
         Assert.True (scrollBar.Visible);
 
         super.Dispose ();
@@ -123,7 +132,7 @@ public class ScrollBarTests (ITestOutputHelper output)
 
     [Fact]
     [AutoInitShutdown]
-    public void AutoHide_Change_Size_CorrectlyHidesAndShows ()
+    public void AutoHide_Change_VisibleContentSize_CorrectlyHidesAndShows ()
     {
         var super = new Toplevel ()
         {
@@ -135,42 +144,42 @@ public class ScrollBarTests (ITestOutputHelper output)
         var scrollBar = new ScrollBar
         {
             ScrollableContentSize = 20,
+            VisibleContentSize = 20
         };
         super.Add (scrollBar);
+        Assert.False (scrollBar.AutoHide);
+        Assert.True (scrollBar.Visible);
+
+        scrollBar.AutoHide = true;
 
         RunState rs = Application.Begin (super);
 
         Assert.Equal (Orientation.Vertical, scrollBar.Orientation);
         Assert.Equal (20, scrollBar.VisibleContentSize);
-        //Assert.True (scrollBar.ShowScrollIndicator);
         Assert.False (scrollBar.Visible);
-        Assert.Equal (1, scrollBar.Frame.Width);
-        Assert.Equal (20, scrollBar.Frame.Height);
 
-        scrollBar.ScrollableContentSize = 10;
+        scrollBar.VisibleContentSize = 10;
         Application.RunIteration (ref rs);
-        //Assert.False (scrollBar.ShowScrollIndicator);
-        Assert.False (scrollBar.Visible);
-
-        scrollBar.ScrollableContentSize = 30;
-        //Assert.True (scrollBar.ShowScrollIndicator);
         Assert.True (scrollBar.Visible);
 
-        scrollBar.ScrollableContentSize = 10;
+        scrollBar.VisibleContentSize = 30;
         Application.RunIteration (ref rs);
-        //Assert.False (scrollBar.ShowScrollIndicator);
         Assert.False (scrollBar.Visible);
 
-        scrollBar.ScrollableContentSize = 21;
-        //Assert.True (scrollBar.ShowScrollIndicator);
+        scrollBar.VisibleContentSize = 10;
+        Application.RunIteration (ref rs);
         Assert.True (scrollBar.Visible);
 
+        scrollBar.VisibleContentSize = 21;
+        Application.RunIteration (ref rs);
+        Assert.False (scrollBar.Visible);
+
         scrollBar.AutoHide = false;
-        //Assert.True (scrollBar.ShowScrollIndicator);
+        Application.RunIteration (ref rs);
         Assert.True (scrollBar.Visible);
 
-        scrollBar.ScrollableContentSize = 10;
-        //Assert.True (scrollBar.ShowScrollIndicator);
+        scrollBar.VisibleContentSize = 10;
+        Application.RunIteration (ref rs);
         Assert.True (scrollBar.Visible);
 
         super.Dispose ();
@@ -833,19 +842,15 @@ public class ScrollBarTests (ITestOutputHelper output)
         int initialPos = scrollBar.Position;
 
         Point btnPoint = orientation == Orientation.Vertical
-                             ? new (scrollBar.Frame.X, 0)
-                             : new (0, scrollBar.Frame.Y);
+                             ? new (0, 0)
+                             : new (0, 0);
 
         Application.RaiseMouseEvent (new ()
         {
             ScreenPosition = btnPoint,
             Flags = MouseFlags.Button1Clicked
         });
-        Application.RaiseMouseEvent (new ()
-        {
-            ScreenPosition = new (0, 0),
-            Flags = MouseFlags.Button1Clicked
-        });
+
         Application.RunIteration (ref rs);
 
         Assert.Equal (initialPos - increment, scrollBar.Position);