Browse Source

Unit tests pass

Tig 8 months ago
parent
commit
701d592913

+ 16 - 16
Terminal.Gui/Views/Scroll/Scroll.cs

@@ -48,7 +48,7 @@ public class Scroll : View, IOrientation, IDesignable
             return;
         }
 
-        _slider.Size = (int)Math.Clamp (Math.Floor ((double)ViewportDimension * ViewportDimension / (Size - 2)), 1, ViewportDimension);
+        _slider.Size = (int)Math.Clamp (Math.Floor ((double)ViewportDimension * ViewportDimension / (Size)), 1, ViewportDimension);
     }
 
     #region IOrientation members
@@ -124,6 +124,7 @@ public class Scroll : View, IOrientation, IDesignable
             _size = value;
             OnSizeChanged (_size);
             SizeChanged?.Invoke (this, new (in _size));
+            SetNeedsLayout ();
         }
     }
 
@@ -158,15 +159,13 @@ public class Scroll : View, IOrientation, IDesignable
     {
         int currentSliderPosition = CalculateSliderPosition (_contentPosition);
 
-        if (newSliderPosition > Size - ViewportDimension)
+        if (newSliderPosition > Size - ViewportDimension || currentSliderPosition == newSliderPosition)
         {
             return;
         }
 
         if (OnSliderPositionChanging (currentSliderPosition, newSliderPosition))
         {
-            _slider.Position = currentSliderPosition;
-
             return;
         }
 
@@ -175,19 +174,12 @@ public class Scroll : View, IOrientation, IDesignable
 
         if (args.Cancel)
         {
-            _slider.Position = currentSliderPosition;
-
             return;
         }
 
         // This sets the slider position and clamps the value
         _slider.Position = newSliderPosition;
 
-        if (_slider.Position == currentSliderPosition)
-        {
-            return;
-        }
-
         ContentPosition = (int)Math.Round ((double)newSliderPosition / (ViewportDimension - _slider.Size) * (Size - ViewportDimension));
 
         OnSliderPositionChanged (newSliderPosition);
@@ -247,7 +239,7 @@ public class Scroll : View, IOrientation, IDesignable
     private void RaiseContentPositionChangeEvents (int newContentPosition)
     {
         // Clamp the value between 0 and Size - ViewportDimension
-        newContentPosition = (int)Math.Clamp (newContentPosition, 0, Size - ViewportDimension);
+        newContentPosition = (int)Math.Clamp (newContentPosition, 0, Math.Max (0, Size - ViewportDimension));
 
         if (OnContentPositionChanging (_contentPosition, newContentPosition))
         {
@@ -329,6 +321,14 @@ public class Scroll : View, IOrientation, IDesignable
         return true;
     }
 
+    /// <summary>
+    ///     Gets or sets the amount each mouse hweel event will incremenet/decrement the <see cref="ContentPosition"/>.
+    /// </summary>
+    /// <remarks>
+    ///     The default is 1.
+    /// </remarks>
+    public int Increment { get; set; } = 1;
+
     /// <inheritdoc/>
     protected override bool OnMouseEvent (MouseEventArgs mouseEvent)
     {
@@ -346,24 +346,24 @@ public class Scroll : View, IOrientation, IDesignable
         {
             if (mouseEvent.Flags.HasFlag (MouseFlags.WheeledDown))
             {
-                ContentPosition++;
+                ContentPosition += Increment;
             }
 
             if (mouseEvent.Flags.HasFlag (MouseFlags.WheeledUp))
             {
-                ContentPosition--;
+                ContentPosition -= Increment;
             }
         }
         else
         {
             if (mouseEvent.Flags.HasFlag (MouseFlags.WheeledRight))
             {
-                ContentPosition++;
+                ContentPosition += Increment;
             }
 
             if (mouseEvent.Flags.HasFlag (MouseFlags.WheeledLeft))
             {
-                ContentPosition--;
+                ContentPosition -= Increment;
             }
         }
 

+ 4 - 3
Terminal.Gui/Views/Scroll/ScrollBar.cs

@@ -243,12 +243,13 @@ public class ScrollBar : View, IOrientation, IDesignable
     }
 
     /// <summary>
-    ///     Gets or sets the amount each click of the increment/decrement buttons will incremenet/decrement the <see cref="ContentPosition"/>.
+    ///     Gets or sets the amount each click of the increment/decrement buttons and each
+    ///     mouse wheel event will incremenet/decrement the <see cref="ContentPosition"/>.
     /// </summary>
     /// <remarks>
     ///     The default is 1.
     /// </remarks>
-    public int Increment { get; set; } = 1;
+    public int Increment { get => _scroll.Increment; set => _scroll.Increment = value; }
 
     /// <inheritdoc/>
     protected override void OnSubviewLayout (LayoutEventArgs args) { PositionSubviews (); }
@@ -285,7 +286,7 @@ public class ScrollBar : View, IOrientation, IDesignable
             _increaseButton.Height = Dim.Fill ();
             _increaseButton.Title = Glyphs.RightArrow.ToString ();
             _scroll.Y = 0;
-            _scroll.X = Pos.Bottom (_decreaseButton);
+            _scroll.X = Pos.Right (_decreaseButton);
             _scroll.Width = Dim.Fill (1);
             _scroll.Height = Dim.Fill ();
         }

+ 5 - 0
Terminal.Gui/Views/Scroll/ScrollSlider.cs

@@ -190,6 +190,11 @@ public class ScrollSlider : View, IOrientation, IDesignable
             return false;
         }
 
+        if (SuperView is null)
+        {
+            return false;
+        }
+
         if (Orientation == Orientation.Vertical)
         {
             Text = $"{(int)Math.Round ((double)Viewport.Height / SuperView!.GetContentSize ().Height * 100)}%";

File diff suppressed because it is too large
+ 96 - 924
UnitTests/Views/ScrollBarTests.cs


+ 22 - 0
UnitTests/Views/ScrollSliderTests.cs

@@ -445,4 +445,26 @@ public class ScrollSliderTests (ITestOutputHelper output)
 
         _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
     }
+
+    [Fact]
+    public void ShowPercent_True_ShowsPercentage ()
+    {
+        View super = new ()
+        {
+            Id = "super",
+            Width = 10,
+            Height = 10
+        };
+        ScrollSlider scrollSlider = new ()
+        {
+            Id = "scrollSlider",
+            Height = 10,
+            Width = 10,
+        };
+        super.Add (scrollSlider);
+        scrollSlider.ShowPercent = true;
+        Assert.True (scrollSlider.ShowPercent);
+        super.Draw ();
+        Assert.Contains ("0%", scrollSlider.Text);
+    }
 }

File diff suppressed because it is too large
+ 124 - 809
UnitTests/Views/ScrollTests.cs


Some files were not shown because too many files changed in this diff