Browse Source

Refixed some stuff. WIP

Tig 9 months ago
parent
commit
808ac9232f
2 changed files with 175 additions and 59 deletions
  1. 12 46
      Terminal.Gui/Views/ScrollBar/ScrollSlider.cs
  2. 163 13
      UnitTests/Views/ScrollSliderTests.cs

+ 12 - 46
Terminal.Gui/Views/ScrollBar/ScrollSlider.cs

@@ -38,7 +38,10 @@ public class ScrollSlider : View, IOrientation, IDesignable
 
         HighlightStyle = HighlightStyle.Hover;
 
-        FrameChanged += OnFrameChanged;
+        FrameChanged += (sender, args) =>
+                        {
+                            
+                        };
 
         SubviewLayout += (sender, args) =>
                          {
@@ -46,26 +49,7 @@ public class ScrollSlider : View, IOrientation, IDesignable
 
         SubviewsLaidOut += (sender, args) =>
                            {
-                               if (Orientation == Orientation.Vertical)
-                               {
-                                   if (SuperView?.Viewport.Height > 0)
-                                   {
-                                       ViewportDimension = SuperView!.Viewport.Height;
-                                   }
-                               }
-                               else
-                               {
-                                   if (SuperView?.Viewport.Width > 0)
-                                   {
-                                       ViewportDimension = SuperView!.Viewport.Width;
-                                   }
-                               }
-
-                               if (NeedsLayout)
-                               {
-                                   Layout ();
-                               }
-
+                               
                            };
     }
 
@@ -98,16 +82,14 @@ public class ScrollSlider : View, IOrientation, IDesignable
         Y = 0;
         Position = 0;
 
-        // Reset Size to 1 when changing orientation
+        // Reset opposite dim to Dim.Fill ()
         if (Orientation == Orientation.Vertical)
         {
             Width = Dim.Fill ();
-            Size = 1;
         }
         else
         {
             Height = Dim.Fill ();
-            Size = 1;
         }
         SetNeedsLayout ();
     }
@@ -144,11 +126,11 @@ public class ScrollSlider : View, IOrientation, IDesignable
         }
     }
 
-    private int _size;
+    private int? _size;
 
     /// <summary>
     ///     Gets or sets the size of the ScrollSlider. This is a helper that gets or sets Width or Height depending
-    ///     on  <see cref="Orientation"/>. The size will be clamed between 1 and the dimension of
+    ///     on  <see cref="Orientation"/>. The size will be clamped between 1 and the dimension of
     ///     the <see cref="View.SuperView"/>'s Viewport. 
     /// </summary>
     /// <remarks>
@@ -159,7 +141,7 @@ public class ScrollSlider : View, IOrientation, IDesignable
     /// </remarks>
     public int Size
     {
-        get => _size;
+        get => _size ?? 1;
         set
         {
             if (value == _size)
@@ -206,12 +188,7 @@ public class ScrollSlider : View, IOrientation, IDesignable
                 return;
             }
             _viewportDimension = int.Max (1, value);
-
-            if (Size > _viewportDimension)
-            {
-                Size = _viewportDimension.Value;
-            }
-
+            
             if (_position > _viewportDimension - _size)
             {
                 Position = _position;
@@ -221,13 +198,6 @@ public class ScrollSlider : View, IOrientation, IDesignable
         }
     }
 
-    private void OnFrameChanged (object? sender, EventArgs<Rectangle> e)
-    {
-
-        //ViewportDimension = (Orientation == Orientation.Vertical ? e.CurrentValue.Height : e.CurrentValue.Width);
-        //Position = (Orientation == Orientation.Vertical ? e.CurrentValue.Y : e.CurrentValue.X) - ShrinkBy / 2;
-    }
-
     private int _position;
 
     /// <summary>
@@ -265,10 +235,6 @@ public class ScrollSlider : View, IOrientation, IDesignable
         {
             X = _position + ShrinkBy / 2;
         }
-
-        //SetNeedsLayout ();
-
-        // Layout ();
     }
 
     /// <summary>
@@ -410,8 +376,8 @@ public class ScrollSlider : View, IOrientation, IDesignable
                 {
                     X = Frame.X + offset < ShrinkBy / 2
                             ? ShrinkBy / 2
-                            : Frame.X + offset + Frame.Height > superViewDimension
-                                ? Math.Max (superViewDimension - Frame.Height + ShrinkBy / 2, 1)
+                            : Frame.X + offset + Frame.Width > superViewDimension
+                                ? Math.Max (superViewDimension - Frame.Width + ShrinkBy / 2, 1)
                                 : Frame.X + offset;
                 }
             }

+ 163 - 13
UnitTests/Views/ScrollSliderTests.cs

@@ -21,16 +21,42 @@ public class ScrollSliderTests (ITestOutputHelper output)
         Assert.Equal (0, scrollSlider.Frame.X);
         Assert.Equal (0, scrollSlider.Frame.Y);
         Assert.Equal (1, scrollSlider.Size);
+        Assert.Equal (2048, scrollSlider.ViewportDimension);
     }
 
     [Fact]
-    public void OnOrientationChanged_Sets_Size_To_1 ()
+    public void Add_To_SuperView_Initializes_Correctly ()
     {
+        View super = new View ()
+        {
+            Id = "super",
+            Width = 10,
+            Height = 10,
+            CanFocus = true
+        };
         var scrollSlider = new ScrollSlider ();
-        scrollSlider.Orientation = Orientation.Horizontal;
+        super.Add (scrollSlider);
+
+        Assert.False (scrollSlider.CanFocus);
+        Assert.Equal (Orientation.Vertical, scrollSlider.Orientation);
+        Assert.Equal (TextDirection.TopBottom_LeftRight, scrollSlider.TextDirection);
+        Assert.Equal (Alignment.Center, scrollSlider.TextAlignment);
+        Assert.Equal (Alignment.Center, scrollSlider.VerticalTextAlignment);
+        scrollSlider.Layout ();
+        Assert.Equal (0, scrollSlider.Frame.X);
+        Assert.Equal (0, scrollSlider.Frame.Y);
         Assert.Equal (1, scrollSlider.Size);
+        Assert.Equal (10, scrollSlider.ViewportDimension);
     }
 
+    //[Fact]
+    //public void OnOrientationChanged_Sets_Size_To_1 ()
+    //{
+    //    var scrollSlider = new ScrollSlider ();
+    //    scrollSlider.Orientation = Orientation.Horizontal;
+    //    Assert.Equal (1, scrollSlider.Size);
+    //}
+
     [Fact]
     public void OnOrientationChanged_Sets_Position_To_0 ()
     {
@@ -87,11 +113,11 @@ public class ScrollSliderTests (ITestOutputHelper output)
         Assert.True (scrollSlider.Size <= 5);
     }
 
+
     [Theory]
     [CombinatorialData]
-    public void Size_Clamps_To_ViewportDimensions ([CombinatorialRange (10, 10, 1)] int dimension, [CombinatorialRange (-1, 12, 1)] int sliderSize, Orientation orientation)
+    public void Size_Clamps_To_ViewportDimensions ([CombinatorialRange (1, 6, 1)] int dimension, [CombinatorialRange (-1, 6, 1)] int sliderSize, Orientation orientation)
     {
-
         var scrollSlider = new ScrollSlider
         {
             Orientation = orientation,
@@ -105,6 +131,52 @@ public class ScrollSliderTests (ITestOutputHelper output)
         Assert.True (scrollSlider.Size <= dimension);
     }
 
+    [Fact]
+    public void ViewportDimension_Not_Set_Uses_SuperView ()
+    {
+        View super = new ()
+        {
+            Id = "super",
+            Height = 5,
+            Width = 5,
+        };
+        var scrollSlider = new ScrollSlider
+        {
+        };
+
+        super.Add (scrollSlider);
+        super.Layout ();
+        Assert.Equal (5, scrollSlider.ViewportDimension);
+    }
+
+    [Fact]
+    public void ViewportDimension_Set_Overrides_SuperView ()
+    {
+        View super = new ()
+        {
+            Id = "super",
+            Height = 5,
+            Width = 5,
+        };
+        var scrollSlider = new ScrollSlider
+        {
+            ViewportDimension = 10,
+        };
+
+        super.Add (scrollSlider);
+        super.Layout ();
+        Assert.Equal (10, scrollSlider.ViewportDimension);
+
+        super.Height = 3;
+        super.Layout ();
+        Assert.Equal (10, scrollSlider.ViewportDimension);
+
+        super.Height = 7;
+        super.Layout ();
+        Assert.Equal (10, scrollSlider.ViewportDimension);
+
+    }
+
     [Theory]
     [CombinatorialData]
     public void ViewportDimensions_Clamps_0_To_Dimension ([CombinatorialRange (0, 10, 1)] int dimension, Orientation orientation)
@@ -140,40 +212,88 @@ public class ScrollSliderTests (ITestOutputHelper output)
 
     [Theory]
     [CombinatorialData]
-    public void ClampPosition_Clamps_To_Viewport_Minus_Size ([CombinatorialRange (10, 10, 1)] int dimension, [CombinatorialRange (1, 5, 1)] int sliderSize, [CombinatorialRange (-2, 6, 2)] int sliderPosition, Orientation orientation)
+    public void ClampPosition_WithSuperView_Clamps_To_ViewPort_Minus_Size_If_ViewportDimension_Not_Set ([CombinatorialRange (10, 10, 1)] int dimension, [CombinatorialRange (1, 5, 1)] int sliderSize, [CombinatorialRange (-1, 10, 2)] int sliderPosition, Orientation orientation)
     {
+        View super = new ()
+        {
+            Id = "super",
+            Height = dimension,
+            Width = dimension,
+        };
         var scrollSlider = new ScrollSlider
         {
             Orientation = orientation,
-            ViewportDimension = dimension,
             Size = sliderSize,
         };
+        super.Add (scrollSlider);
+        super.Layout ();
+
+        Assert.Equal(dimension, scrollSlider.ViewportDimension);
 
         int clampedPosition = scrollSlider.ClampPosition (sliderPosition);
 
         Assert.InRange (clampedPosition, 0, dimension - sliderSize);
+    }
 
+    [Theory]
+    [CombinatorialData]
+    public void ClampPosition_WithSuperView_Clamps_To_ViewportDimension_Minus_Size ([CombinatorialRange (10, 10, 1)] int dimension, [CombinatorialRange (1, 5, 1)] int sliderSize, [CombinatorialRange (-1, 10, 2)] int sliderPosition, Orientation orientation)
+    {
         View super = new ()
         {
             Id = "super",
-            Height = dimension,
-            Width = dimension,
+            Height = dimension + 2,
+            Width = dimension + 2,
         };
-        scrollSlider = new ScrollSlider
+        var scrollSlider = new ScrollSlider
         {
             Orientation = orientation,
+            ViewportDimension = dimension,
             Size = sliderSize,
         };
         super.Add (scrollSlider);
         super.Layout ();
 
-        clampedPosition = scrollSlider.ClampPosition (sliderPosition);
+        int clampedPosition = scrollSlider.ClampPosition (sliderPosition);
+
         Assert.InRange (clampedPosition, 0, dimension - sliderSize);
     }
 
     [Theory]
     [CombinatorialData]
-    public void Position_Clamps_To_SuperView_Viewport ([CombinatorialRange (0, 5, 1)] int sliderSize, [CombinatorialRange (-2, 6, 2)] int sliderPosition, Orientation orientation)
+    public void ClampPosition_NoSuperView_Clamps_To_ViewportDimension_Minus_Size ([CombinatorialRange (10, 10, 1)] int dimension, [CombinatorialRange (1, 5, 1)] int sliderSize, [CombinatorialRange (-1, 10, 2)] int sliderPosition, Orientation orientation)
+    {
+        var scrollSlider = new ScrollSlider
+        {
+            Orientation = orientation,
+            ViewportDimension = dimension,
+            Size = sliderSize,
+        };
+
+        int clampedPosition = scrollSlider.ClampPosition (sliderPosition);
+
+        Assert.InRange (clampedPosition, 0, dimension - sliderSize);
+    }
+
+    [Theory]
+    [CombinatorialData]
+    public void Position_Clamps_To_ViewportDimension ([CombinatorialRange (0, 5, 1)] int dimension, [CombinatorialRange(1, 5, 1)] int sliderSize, [CombinatorialRange (-1, 10, 2)] int sliderPosition, Orientation orientation)
+    {
+        var scrollSlider = new ScrollSlider
+        {
+            Orientation = orientation,
+            ViewportDimension = dimension,
+            Size = sliderSize,
+            Position = sliderPosition
+        };
+
+        Assert.True (scrollSlider.Position <= 5);
+    }
+
+
+    [Theory]
+    [CombinatorialData]
+    public void Position_Clamps_To_SuperView_Viewport ([CombinatorialRange (0, 5, 1)] int sliderSize, [CombinatorialRange (-2, 10, 2)] int sliderPosition, Orientation orientation)
     {
         var super = new View
         {
@@ -195,6 +315,35 @@ public class ScrollSliderTests (ITestOutputHelper output)
         Assert.True (scrollSlider.Position <= 5);
     }
 
+
+    [Theory]
+    [CombinatorialData]
+    public void Position_Clamps_To_ViewportDimension_With_SuperView ([CombinatorialRange (0, 5, 1)] int dimension, [CombinatorialRange (1, 5, 1)] int sliderSize, [CombinatorialRange (-2, 10, 2)] int sliderPosition, Orientation orientation)
+    {
+        var super = new View
+        {
+            Id = "super",
+            Width = 10,
+            Height = 10
+        };
+
+        var scrollSlider = new ScrollSlider
+        {
+            Orientation = orientation,
+            ViewportDimension = dimension,
+            Size = sliderSize,
+            Position = sliderPosition
+        };
+
+        super.Add (scrollSlider);
+        scrollSlider.Size = sliderSize;
+        scrollSlider.Layout ();
+
+        scrollSlider.Position = sliderPosition;
+
+        Assert.True (scrollSlider.Position <= 5);
+    }
+
     [Theory]
     [SetupFakeDriver]
     [InlineData (
@@ -516,11 +665,12 @@ public class ScrollSliderTests (ITestOutputHelper output)
         {
             Orientation = orientation,
             Size = sliderSize,
-            Position = position,
+            //Position = position,
         };
         Assert.Equal (sliderSize, scrollSlider.Size);
         super.Add (scrollSlider);
- 
+        scrollSlider.Position = position;
+
         super.Layout ();
         super.Draw ();