Browse Source

Remove unnecessary _sliderContainer view.

BDisp 1 year ago
parent
commit
84c69f0ffe
3 changed files with 106 additions and 43 deletions
  1. 15 33
      Terminal.Gui/Views/Scroll.cs
  2. 39 3
      UICatalog/Scenarios/ScrollDemo.cs
  3. 52 7
      UnitTests/Views/ScrollTests.cs

+ 15 - 33
Terminal.Gui/Views/Scroll.cs

@@ -20,22 +20,20 @@ public class Scroll : View
         ClearOnVisibleFalse = false;
         CanFocus = false;
         Orientation = Orientation.Vertical;
+        Width = 1;
 
-        _sliderContainer = new () { Width = Dim.Fill (), Height = Dim.Fill (), WantContinuousButtonPressed = true };
         _slider = new () { Id = "slider" };
-        _sliderContainer.Add (_slider);
-        Add (_sliderContainer);
+        Add (_slider);
 
         Added += Scroll_Added;
         Removed += Scroll_Removed;
         Initialized += Scroll_Initialized;
-        _sliderContainer.DrawContent += SubViews_DrawContent;
-        _sliderContainer.MouseEvent += SliderContainer_MouseEvent;
+        DrawContent += SubViews_DrawContent;
+        MouseEvent += SliderContainer_MouseEvent;
         _slider.DrawContent += SubViews_DrawContent;
         _slider.MouseEvent += Slider_MouseEvent;
     }
 
-    private readonly View _sliderContainer;
     private readonly View _slider;
     private int _lastLocation = -1;
 
@@ -120,8 +118,8 @@ public class Scroll : View
     {
         Added -= Scroll_Added;
         Initialized -= Scroll_Initialized;
-        _sliderContainer.DrawContent -= SubViews_DrawContent;
-        _sliderContainer.MouseEvent -= SliderContainer_MouseEvent;
+        DrawContent -= SubViews_DrawContent;
+        MouseEvent -= SliderContainer_MouseEvent;
         _slider.DrawContent -= SubViews_DrawContent;
         _slider.MouseEvent -= Slider_MouseEvent;
 
@@ -250,48 +248,32 @@ public class Scroll : View
 
     private void SetSliderText ()
     {
-        _sliderContainer.TextDirection = Orientation == Orientation.Vertical ? TextDirection.TopBottom_LeftRight : TextDirection.LeftRight_TopBottom;
-
-        _sliderContainer.Text = string.Concat (
-                                               Enumerable.Repeat (
-                                                                  Glyphs.Stipple.ToString (),
-                                                                  Orientation == Orientation.Vertical
-                                                                      ? _sliderContainer.Frame.Height
-                                                                      : _sliderContainer.Frame.Width));
+        TextDirection = Orientation == Orientation.Vertical ? TextDirection.TopBottom_LeftRight : TextDirection.LeftRight_TopBottom;
+
+        Text = string.Concat (
+                              Enumerable.Repeat (
+                                                 Glyphs.Stipple.ToString (),
+                                                 Frame.Width * Frame.Height));
         _slider.TextDirection = Orientation == Orientation.Vertical ? TextDirection.TopBottom_LeftRight : TextDirection.LeftRight_TopBottom;
 
         _slider.Text = string.Concat (
                                       Enumerable.Repeat (
                                                          Glyphs.ContinuousMeterSegment.ToString (),
-                                                         Orientation == Orientation.Vertical
-                                                             ? _sliderContainer.Frame.Height
-                                                             : _slider.Frame.Width));
+                                                         _slider.Frame.Width * _slider.Frame.Height));
     }
 
     private void SetWidthHeight ()
     {
-        if (Orientation == Orientation.Vertical)
-        {
-            Width = 1;
-        }
-        else
-        {
-            Height = 1;
-        }
-
         if (!IsInitialized)
         {
             return;
         }
 
-        _sliderContainer.Width = Orientation == Orientation.Vertical ? 1 : Dim.Fill ();
-        _sliderContainer.Height = Orientation == Orientation.Vertical ? Dim.Fill () : 1;
-
         (int Location, int Dimension) slider = GetSliderLocationDimensionFromPosition ();
         _slider.X = Orientation == Orientation.Vertical ? 0 : slider.Location;
         _slider.Y = Orientation == Orientation.Vertical ? slider.Location : 0;
-        _slider.Width = Orientation == Orientation.Vertical ? 1 : slider.Dimension;
-        _slider.Height = Orientation == Orientation.Vertical ? slider.Dimension : 1;
+        _slider.Width = Orientation == Orientation.Vertical ? Dim.Fill () : slider.Dimension;
+        _slider.Height = Orientation == Orientation.Vertical ? slider.Dimension : Dim.Fill ();
 
         SetSliderText ();
     }

+ 39 - 3
UICatalog/Scenarios/ScrollDemo.cs

@@ -36,13 +36,47 @@ public class ScrollDemo : Scenario
         var scroll = new Scroll
         {
             X = Pos.AnchorEnd (),
-            Width = Dim.Fill (),
+            Width = 1,
             Height = Dim.Fill ()
         };
         view.Add (scroll);
 
+        var lblWidthHeight = new Label
+        {
+            Text = "Width/Height:"
+        };
+        view.Add (lblWidthHeight);
+
+        Buttons.NumericUpDown<int> scrollWidthHeight = new()
+        {
+            Value = scroll.Frame.Width,
+            X = Pos.Right (lblWidthHeight) + 1,
+            Y = Pos.Top (lblWidthHeight)
+        };
+        view.Add (scrollWidthHeight);
+
+        scrollWidthHeight.ValueChanging += (s, e) =>
+                                           {
+                                               if (e.NewValue < 1)
+                                               {
+                                                   e.Cancel = true;
+
+                                                   return;
+                                               }
+
+                                               if (scroll.Orientation == Orientation.Vertical)
+                                               {
+                                                   scroll.Width = e.NewValue;
+                                               }
+                                               else
+                                               {
+                                                   scroll.Height = e.NewValue;
+                                               }
+                                           };
+
         var rgOrientation = new RadioGroup
         {
+            Y = Pos.Bottom (lblWidthHeight),
             RadioLabels = ["Vertical", "Horizontal"],
             Orientation = Orientation.Horizontal
         };
@@ -59,6 +93,7 @@ public class ScrollDemo : Scenario
                                                  {
                                                      scroll.Orientation = Orientation.Vertical;
                                                      scroll.X = Pos.AnchorEnd ();
+                                                     scroll.Width = scrollWidthHeight.Value;
                                                      scroll.Height = Dim.Fill ();
                                                      scroll.Size /= 3;
                                                  }
@@ -67,6 +102,7 @@ public class ScrollDemo : Scenario
                                                      scroll.Orientation = Orientation.Horizontal;
                                                      scroll.Y = Pos.AnchorEnd ();
                                                      scroll.Width = Dim.Fill ();
+                                                     scroll.Height = scrollWidthHeight.Value;
                                                      scroll.Size *= 3;
                                                  }
                                              };
@@ -78,7 +114,7 @@ public class ScrollDemo : Scenario
         };
         view.Add (lblSize);
 
-        Buttons.NumericUpDown<int> scrollSize = new Buttons.NumericUpDown<int>
+        Buttons.NumericUpDown<int> scrollSize = new()
         {
             Value = scroll.Size,
             X = Pos.Right (lblSize) + 1,
@@ -108,7 +144,7 @@ public class ScrollDemo : Scenario
         };
         view.Add (lblPosition);
 
-        Buttons.NumericUpDown<int> scrollPosition = new Buttons.NumericUpDown<int>
+        Buttons.NumericUpDown<int> scrollPosition = new()
         {
             Value = scroll.Position,
             X = Pos.Right (lblPosition) + 1,

+ 52 - 7
UnitTests/Views/ScrollTests.cs

@@ -158,6 +158,7 @@ public class ScrollTests
 
         scroll.Orientation = Orientation.Horizontal;
         scroll.Width = 10;
+        scroll.Height = 1;
         scroll.Position = 0;
         scroll.Size = size;
         Application.Refresh ();
@@ -272,7 +273,13 @@ public class ScrollTests
 ░░░░░██░░░")]
     public void Mouse_On_The_Container (Orientation orientation, int size, int position, int location, string output, int expectedPos, string expectedOut)
     {
-        var scroll = new Scroll { Width = 10, Height = 10, Orientation = orientation, Size = size, Position = position };
+        var scroll = new Scroll
+        {
+            Width = orientation == Orientation.Vertical ? 1 : 10,
+            Height = orientation == Orientation.Vertical ? 10 : 1,
+            Orientation = orientation, Size = size,
+            Position = position
+        };
         var top = new Toplevel ();
         top.Add (scroll);
         Application.Begin (top);
@@ -641,7 +648,13 @@ public class ScrollTests
         string expectedOut
     )
     {
-        var scroll = new Scroll { Width = 10, Height = 10, Orientation = orientation, Size = size, Position = position };
+        var scroll = new Scroll
+        {
+            Width = orientation == Orientation.Vertical ? 1 : 10,
+            Height = orientation == Orientation.Vertical ? 10 : 1,
+            Orientation = orientation,
+            Size = size, Position = position
+        };
         var top = new Toplevel ();
         top.Add (scroll);
         Application.Begin (top);
@@ -759,6 +772,7 @@ public class ScrollTests
     [InlineData (
                     3,
                     10,
+                    1,
                     Orientation.Vertical,
                     @"
 ┌─┐
@@ -774,12 +788,40 @@ public class ScrollTests
     [InlineData (
                     10,
                     3,
+                    1,
                     Orientation.Horizontal,
                     @"
 ┌────────┐
 │███░░░░░│
 └────────┘")]
-    public void Vertical_Horizontal_Draws_Correctly (int width, int height, Orientation orientation, string expected)
+    [InlineData (
+                    3,
+                    10,
+                    3,
+                    Orientation.Vertical,
+                    @"
+┌───┐
+│███│
+│███│
+│███│
+│░░░│
+│░░░│
+│░░░│
+│░░░│
+│░░░│
+└───┘")]
+    [InlineData (
+                    10,
+                    3,
+                    3,
+                    Orientation.Horizontal,
+                    @"
+┌────────┐
+│███░░░░░│
+│███░░░░░│
+│███░░░░░│
+└────────┘")]
+    public void Vertical_Horizontal_Draws_Correctly (int sizeWidth, int sizeHeight, int widthHeight, Orientation orientation, string expected)
     {
         var super = new Window { Id = "super", Width = Dim.Fill (), Height = Dim.Fill () };
         var top = new Toplevel ();
@@ -788,14 +830,17 @@ public class ScrollTests
         var scroll = new Scroll
         {
             Orientation = orientation,
-            Size = orientation == Orientation.Vertical ? height * 2 : width * 2,
-            Width = orientation == Orientation.Vertical ? 1 : Dim.Fill (),
-            Height = orientation == Orientation.Vertical ? Dim.Fill () : 1
+            Size = orientation == Orientation.Vertical ? sizeHeight * 2 : sizeWidth * 2,
+            Width = orientation == Orientation.Vertical ? widthHeight : Dim.Fill (),
+            Height = orientation == Orientation.Vertical ? Dim.Fill () : widthHeight
         };
         super.Add (scroll);
 
         Application.Begin (top);
-        ((FakeDriver)Application.Driver).SetBufferSize (width, height);
+
+        ((FakeDriver)Application.Driver).SetBufferSize (
+                                                        sizeWidth + (orientation == Orientation.Vertical ? widthHeight - 1 : 0),
+                                                        sizeHeight + (orientation == Orientation.Vertical ? 0 : widthHeight - 1));
 
         _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
     }