Explorar el Código

Remove host parameter from scroll classes.

BDisp hace 11 meses
padre
commit
ae3fcef17a

+ 9 - 21
Terminal.Gui/Views/Scroll/Scroll.cs

@@ -10,33 +10,19 @@ namespace Terminal.Gui;
 public class Scroll : View
 {
     /// <inheritdoc/>
-    public Scroll () : this (null) { }
-
-    public Scroll (ScrollBar? host)
+    public Scroll ()
     {
-        _host = host;
-        _slider = new (this);
+        _slider = new ();
         Add (_slider);
 
         WantContinuousButtonPressed = true;
         CanFocus = false;
         Orientation = Orientation.Vertical;
 
-        if (_host is { })
-        {
-            Y = 1;
-            Width = Dim.Fill ();
-            Height = Dim.Fill (1);
-        }
-        else
-        {
-            Width = Dim.Auto (DimAutoStyle.Content, 1);
-            Height = Dim.Auto (DimAutoStyle.Content, 1);
-        }
+        Width = Dim.Auto (DimAutoStyle.Content, 1);
+        Height = Dim.Auto (DimAutoStyle.Content, 1);
     }
 
-    internal readonly ScrollBar? _host;
-
     internal readonly ScrollSlider _slider;
     private Orientation _orientation;
     private int _position;
@@ -76,10 +62,10 @@ public class Scroll : View
                 return;
             }
 
-            if (_host is { IsInitialized: false })
+            if (SupView is { IsInitialized: false })
             {
                 // Ensures a more exactly calculation
-                SetRelativeLayout (_host.Frame.Size);
+                SetRelativeLayout (SupView.Frame.Size);
             }
 
             int barSize = Orientation == Orientation.Vertical ? GetContentSize ().Height : GetContentSize ().Width;
@@ -193,7 +179,7 @@ public class Scroll : View
 
     internal void AdjustScroll ()
     {
-        if (_host is { })
+        if (SupView is { })
         {
             X = Orientation == Orientation.Vertical ? 0 : 1;
             Y = Orientation == Orientation.Vertical ? 1 : 0;
@@ -213,6 +199,8 @@ public class Scroll : View
         AdjustScroll ();
     }
 
+    internal ScrollBar? SupView => SuperView as ScrollBar;
+
     private void SetScrollText ()
     {
         TextDirection = Orientation == Orientation.Vertical ? TextDirection.TopBottom_LeftRight : TextDirection.LeftRight_TopBottom;

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

@@ -17,9 +17,9 @@ public class ScrollBar : View
     /// <inheritdoc/>
     public ScrollBar ()
     {
-        _scroll = new (this);
-        _decrease = new (this);
-        _increase = new (this, VariationMode.Increase);
+        _scroll = new ();
+        _decrease = new ();
+        _increase = new () { VariationMode = VariationMode.Increase };
         Add (_scroll, _decrease, _increase);
 
         CanFocus = false;
@@ -68,6 +68,9 @@ public class ScrollBar : View
     /// </summary>
     public event EventHandler<CancelEventArgs<int>>? PositionChanging;
 
+    /// <summary>
+    ///     Gets or sets the size of the Scroll. This is the total size of the content that can be scrolled through.
+    /// </summary>
     public int Size
     {
         get => _scroll.Size;

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

@@ -10,10 +10,8 @@ internal enum VariationMode
 
 internal class ScrollButton : View
 {
-    public ScrollButton (ScrollBar host, VariationMode variation = VariationMode.Decrease)
+    public ScrollButton ()
     {
-        _host = host;
-        VariationMode = variation;
         TextAlignment = Alignment.Center;
         VerticalTextAlignment = Alignment.Center;
         Id = "scrollButton";
@@ -23,7 +21,6 @@ internal class ScrollButton : View
         WantContinuousButtonPressed = true;
     }
 
-    private readonly ScrollBar _host;
     private ColorScheme? _savedColorScheme;
 
     public void AdjustButton ()
@@ -33,8 +30,8 @@ internal class ScrollButton : View
             return;
         }
 
-        Width = _host.Orientation == Orientation.Vertical ? Dim.Fill () : 1;
-        Height = _host.Orientation == Orientation.Vertical ? 1 : Dim.Fill ();
+        Width = SupView.Orientation == Orientation.Vertical ? Dim.Fill () : 1;
+        Height = SupView.Orientation == Orientation.Vertical ? 1 : Dim.Fill ();
 
         switch (VariationMode)
         {
@@ -44,8 +41,8 @@ internal class ScrollButton : View
 
                 break;
             case VariationMode.Increase:
-                X = _host.Orientation == Orientation.Vertical ? 0 : Pos.AnchorEnd (1);
-                Y = _host.Orientation == Orientation.Vertical ? Pos.AnchorEnd (1) : 0;
+                X = SupView.Orientation == Orientation.Vertical ? 0 : Pos.AnchorEnd (1);
+                Y = SupView.Orientation == Orientation.Vertical ? Pos.AnchorEnd (1) : 0;
 
                 break;
             default:
@@ -60,22 +57,22 @@ internal class ScrollButton : View
     {
         if (_savedColorScheme is null)
         {
-            ColorScheme = new () { Normal = new (_host.ColorScheme.HotNormal.Foreground, _host.ColorScheme.HotNormal.Background) };
+            ColorScheme = new () { Normal = new (SupView.ColorScheme.HotNormal.Foreground, SupView.ColorScheme.HotNormal.Background) };
         }
         else
         {
-            ColorScheme = new () { Normal = new (_host.ColorScheme.Normal.Background, _host.ColorScheme.Normal.Foreground) };
+            ColorScheme = new () { Normal = new (SupView.ColorScheme.Normal.Background, SupView.ColorScheme.Normal.Foreground) };
         }
 
         return base.GetNormalColor ();
     }
 
-    public VariationMode VariationMode { get; }
+    public VariationMode VariationMode { get; init; }
 
     /// <inheritdoc/>
     protected internal override bool? OnMouseEnter (MouseEvent mouseEvent)
     {
-        _savedColorScheme ??= _host.ColorScheme;
+        _savedColorScheme ??= SupView.ColorScheme;
 
         ColorScheme = new ()
         {
@@ -97,11 +94,11 @@ internal class ScrollButton : View
             switch (VariationMode)
             {
                 case VariationMode.Decrease:
-                    _host.Position--;
+                    SupView.Position--;
 
                     return true;
                 case VariationMode.Increase:
-                    _host.Position++;
+                    SupView.Position++;
 
                     return true;
                 default:
@@ -129,15 +126,17 @@ internal class ScrollButton : View
         switch (VariationMode)
         {
             case VariationMode.Decrease:
-                Text = _host.Orientation == Orientation.Vertical ? Glyphs.UpArrow.ToString () : Glyphs.LeftArrow.ToString ();
+                Text = SupView.Orientation == Orientation.Vertical ? Glyphs.UpArrow.ToString () : Glyphs.LeftArrow.ToString ();
 
                 break;
             case VariationMode.Increase:
-                Text = _host.Orientation == Orientation.Vertical ? Glyphs.DownArrow.ToString () : Glyphs.RightArrow.ToString ();
+                Text = SupView.Orientation == Orientation.Vertical ? Glyphs.DownArrow.ToString () : Glyphs.RightArrow.ToString ();
 
                 break;
             default:
                 throw new ArgumentOutOfRangeException ();
         }
     }
+
+    private ScrollBar SupView => (SuperView as ScrollBar)!;
 }

+ 37 - 37
Terminal.Gui/Views/Scroll/ScrollSlider.cs

@@ -4,16 +4,14 @@ namespace Terminal.Gui;
 
 internal class ScrollSlider : View
 {
-    public ScrollSlider (Scroll host)
+    public ScrollSlider ()
     {
-        _host = host;
         Id = "scrollSlider";
         Width = Dim.Auto (DimAutoStyle.Content);
         Height = Dim.Auto (DimAutoStyle.Content);
         WantMousePositionReports = true;
     }
 
-    private readonly Scroll _host;
     private int _lastLocation = -1;
     private ColorScheme? _savedColorScheme;
 
@@ -25,13 +23,13 @@ internal class ScrollSlider : View
         }
 
         (int Location, int Dimension) sliderLocationAndDimension = GetSliderLocationDimensionFromPosition ();
-        X = _host.Orientation == Orientation.Vertical ? 0 : sliderLocationAndDimension.Location;
-        Y = _host.Orientation == Orientation.Vertical ? sliderLocationAndDimension.Location : 0;
+        X = SupView.Orientation == Orientation.Vertical ? 0 : sliderLocationAndDimension.Location;
+        Y = SupView.Orientation == Orientation.Vertical ? sliderLocationAndDimension.Location : 0;
 
         SetContentSize (
                         new (
-                             _host.Orientation == Orientation.Vertical ? _host.GetContentSize ().Width : sliderLocationAndDimension.Dimension,
-                             _host.Orientation == Orientation.Vertical ? sliderLocationAndDimension.Dimension : _host.GetContentSize ().Height
+                             SupView.Orientation == Orientation.Vertical ? SupView.GetContentSize ().Width : sliderLocationAndDimension.Dimension,
+                             SupView.Orientation == Orientation.Vertical ? sliderLocationAndDimension.Dimension : SupView.GetContentSize ().Height
                             ));
         SetSliderText ();
     }
@@ -41,11 +39,11 @@ internal class ScrollSlider : View
     {
         if (_savedColorScheme is null)
         {
-            ColorScheme = new () { Normal = new (_host.ColorScheme.HotNormal.Foreground, _host.ColorScheme.HotNormal.Foreground) };
+            ColorScheme = new () { Normal = new (SupView.ColorScheme.HotNormal.Foreground, SupView.ColorScheme.HotNormal.Foreground) };
         }
         else
         {
-            ColorScheme = new () { Normal = new (_host.ColorScheme.Normal.Foreground, _host.ColorScheme.Normal.Foreground) };
+            ColorScheme = new () { Normal = new (SupView.ColorScheme.Normal.Foreground, SupView.ColorScheme.Normal.Foreground) };
         }
 
         return base.GetNormalColor ();
@@ -54,7 +52,7 @@ internal class ScrollSlider : View
     /// <inheritdoc/>
     protected internal override bool? OnMouseEnter (MouseEvent mouseEvent)
     {
-        _savedColorScheme ??= _host.ColorScheme;
+        _savedColorScheme ??= SupView.ColorScheme;
 
         ColorScheme = new ()
         {
@@ -71,9 +69,9 @@ internal class ScrollSlider : View
     /// <inheritdoc/>
     protected internal override bool OnMouseEvent (MouseEvent mouseEvent)
     {
-        int location = _host.Orientation == Orientation.Vertical ? mouseEvent.Position.Y : mouseEvent.Position.X;
+        int location = SupView.Orientation == Orientation.Vertical ? mouseEvent.Position.Y : mouseEvent.Position.X;
         int offset = _lastLocation > -1 ? location - _lastLocation : 0;
-        int barSize = _host.Orientation == Orientation.Vertical ? _host.GetContentSize ().Height : _host.GetContentSize ().Width;
+        int barSize = SupView.Orientation == Orientation.Vertical ? SupView.GetContentSize ().Height : SupView.GetContentSize ().Width;
 
         if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed) && _lastLocation == -1)
         {
@@ -85,19 +83,19 @@ internal class ScrollSlider : View
         }
         else if (mouseEvent.Flags == (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))
         {
-            if (_host.Orientation == Orientation.Vertical)
+            if (SupView.Orientation == Orientation.Vertical)
             {
                 Y = Frame.Y + offset < 0 ? 0 :
                     Frame.Y + offset + Frame.Height > barSize ? Math.Max (barSize - Frame.Height, 0) : Frame.Y + offset;
 
-                _host.Position = GetPositionFromSliderLocation (Frame.Y);
+                SupView.Position = GetPositionFromSliderLocation (Frame.Y);
             }
             else
             {
                 X = Frame.X + offset < 0 ? 0 :
                     Frame.X + offset + Frame.Width > barSize ? Math.Max (barSize - Frame.Width, 0) : Frame.X + offset;
 
-                _host.Position = GetPositionFromSliderLocation (Frame.X);
+                SupView.Position = GetPositionFromSliderLocation (Frame.X);
             }
         }
         else if (mouseEvent.Flags == MouseFlags.Button1Released)
@@ -109,15 +107,15 @@ internal class ScrollSlider : View
                 Application.UngrabMouse ();
             }
         }
-        else if ((mouseEvent.Flags == MouseFlags.WheeledDown && _host.Orientation == Orientation.Vertical)
-                 || (mouseEvent.Flags == MouseFlags.WheeledRight && _host.Orientation == Orientation.Horizontal))
+        else if ((mouseEvent.Flags == MouseFlags.WheeledDown && SupView.Orientation == Orientation.Vertical)
+                 || (mouseEvent.Flags == MouseFlags.WheeledRight && SupView.Orientation == Orientation.Horizontal))
         {
-            _host.Position = Math.Min (_host.Position + 1, _host.Size - barSize);
+            SupView.Position = Math.Min (SupView.Position + 1, SupView.Size - barSize);
         }
-        else if ((mouseEvent.Flags == MouseFlags.WheeledUp && _host.Orientation == Orientation.Vertical)
-                 || (mouseEvent.Flags == MouseFlags.WheeledLeft && _host.Orientation == Orientation.Horizontal))
+        else if ((mouseEvent.Flags == MouseFlags.WheeledUp && SupView.Orientation == Orientation.Vertical)
+                 || (mouseEvent.Flags == MouseFlags.WheeledLeft && SupView.Orientation == Orientation.Horizontal))
         {
-            _host.Position = Math.Max (_host.Position - 1, 0);
+            SupView.Position = Math.Max (SupView.Position - 1, 0);
         }
         else if (mouseEvent.Flags != MouseFlags.ReportMousePosition)
         {
@@ -141,48 +139,48 @@ internal class ScrollSlider : View
 
     internal int GetPositionFromSliderLocation (int location)
     {
-        if (_host.GetContentSize ().Height == 0 || _host.GetContentSize ().Width == 0)
+        if (SupView.GetContentSize ().Height == 0 || SupView.GetContentSize ().Width == 0)
         {
             return 0;
         }
 
-        int scrollSize = _host.Orientation == Orientation.Vertical ? _host.GetContentSize ().Height : _host.GetContentSize ().Width;
+        int scrollSize = SupView.Orientation == Orientation.Vertical ? SupView.GetContentSize ().Height : SupView.GetContentSize ().Width;
 
         // Ensure the Position is valid if the slider is at end
         // We use Frame here instead of ContentSize because even if the slider has a margin or border, Frame indicates the actual size
-        if ((_host.Orientation == Orientation.Vertical && location + Frame.Height >= scrollSize)
-            || (_host.Orientation == Orientation.Horizontal && location + Frame.Width >= scrollSize))
+        if ((SupView.Orientation == Orientation.Vertical && location + Frame.Height >= scrollSize)
+            || (SupView.Orientation == Orientation.Horizontal && location + Frame.Width >= scrollSize))
         {
-            return _host.Size - scrollSize;
+            return SupView.Size - scrollSize;
         }
 
-        return (int)Math.Min (Math.Round ((double)(location * _host.Size + location) / scrollSize), _host.Size - scrollSize);
+        return (int)Math.Min (Math.Round ((double)(location * SupView.Size + location) / scrollSize), SupView.Size - scrollSize);
     }
 
     internal (int Location, int Dimension) GetSliderLocationDimensionFromPosition ()
     {
-        if (_host.GetContentSize ().Height == 0 || _host.GetContentSize ().Width == 0)
+        if (SupView.GetContentSize ().Height == 0 || SupView.GetContentSize ().Width == 0)
         {
             return new (0, 0);
         }
 
-        int scrollSize = _host.Orientation == Orientation.Vertical ? _host.GetContentSize ().Height : _host.GetContentSize ().Width;
+        int scrollSize = SupView.Orientation == Orientation.Vertical ? SupView.GetContentSize ().Height : SupView.GetContentSize ().Width;
         int location;
         int dimension;
 
-        if (_host.Size > 0)
+        if (SupView.Size > 0)
         {
-            dimension = (int)Math.Min (Math.Max (Math.Ceiling ((double)scrollSize * scrollSize / _host.Size), 1), scrollSize);
+            dimension = (int)Math.Min (Math.Max (Math.Ceiling ((double)scrollSize * scrollSize / SupView.Size), 1), scrollSize);
 
             // Ensure the Position is valid
-            if (_host.Position > 0 && _host.Position + scrollSize > _host.Size)
+            if (SupView.Position > 0 && SupView.Position + scrollSize > SupView.Size)
             {
-                _host.Position = _host.Size - scrollSize;
+                SupView.Position = SupView.Size - scrollSize;
             }
 
-            location = (int)Math.Min (Math.Round ((double)_host.Position * scrollSize / (_host.Size + 1)), scrollSize - dimension);
+            location = (int)Math.Min (Math.Round ((double)SupView.Position * scrollSize / (SupView.Size + 1)), scrollSize - dimension);
 
-            if (_host.Position == _host.Size - scrollSize && location + dimension < scrollSize)
+            if (SupView.Position == SupView.Size - scrollSize && location + dimension < scrollSize)
             {
                 location = scrollSize - dimension;
             }
@@ -202,12 +200,14 @@ internal class ScrollSlider : View
 
     private void SetSliderText ()
     {
-        TextDirection = _host.Orientation == Orientation.Vertical ? TextDirection.TopBottom_LeftRight : TextDirection.LeftRight_TopBottom;
+        TextDirection = SupView.Orientation == Orientation.Vertical ? TextDirection.TopBottom_LeftRight : TextDirection.LeftRight_TopBottom;
 
         // QUESTION: Should these Glyphs be configurable via CM?
         Text = string.Concat (
                               Enumerable.Repeat (
                                                  Glyphs.ContinuousMeterSegment.ToString (),
-                                                 _host.GetContentSize ().Width * _host.GetContentSize ().Height));
+                                                 SupView.GetContentSize ().Width * SupView.GetContentSize ().Height));
     }
+
+    private Scroll SupView => (SuperView as Scroll)!;
 }

+ 17 - 17
UnitTests/Views/ScrollSliderTests.cs

@@ -17,7 +17,7 @@ public class ScrollSliderTests
     public void Test_Position_Location_Consistency (Orientation orientation, int scrollLength, int size, int location, int expectedPosition)
     {
         // Arrange
-        Scroll host = new ()
+        Scroll scroll = new ()
         {
             Orientation = orientation,
             Width = orientation == Orientation.Vertical ? 1 : scrollLength,
@@ -25,18 +25,18 @@ public class ScrollSliderTests
             Size = size
         };
 
-        host.BeginInit ();
-        host.EndInit ();
+        scroll.BeginInit ();
+        scroll.EndInit ();
 
         // Act
-        host.Position = host._slider.GetPositionFromSliderLocation (location);
-        (int calculatedLocation, int calculatedDimension) = host._slider.GetSliderLocationDimensionFromPosition ();
-        int calculatedPosition = host._slider.GetPositionFromSliderLocation (calculatedLocation);
+        scroll.Position = scroll._slider.GetPositionFromSliderLocation (location);
+        (int calculatedLocation, int calculatedDimension) = scroll._slider.GetSliderLocationDimensionFromPosition ();
+        int calculatedPosition = scroll._slider.GetPositionFromSliderLocation (calculatedLocation);
 
         // Assert
         AssertLocation (scrollLength, location, calculatedLocation, calculatedDimension);
 
-        Assert.Equal (host.Position, expectedPosition);
+        Assert.Equal (scroll.Position, expectedPosition);
         Assert.Equal (calculatedPosition, expectedPosition);
     }
 
@@ -47,7 +47,7 @@ public class ScrollSliderTests
     {
         var random = new Random ();
 
-        Scroll host = new ()
+        Scroll scroll = new ()
         {
             Orientation = orientation,
             Width = orientation == Orientation.Vertical ? 1 : scrollLength,
@@ -55,8 +55,8 @@ public class ScrollSliderTests
             Size = size
         };
 
-        host.BeginInit ();
-        host.EndInit ();
+        scroll.BeginInit ();
+        scroll.EndInit ();
 
         // Number of random tests to run
         for (var i = 0; i < testCount; i++)
@@ -65,21 +65,21 @@ public class ScrollSliderTests
             int randomScrollLength = random.Next (0, 60); // Random content size length
             int randomLocation = random.Next (0, randomScrollLength); // Random location
 
-            host.Width = host.Orientation == Orientation.Vertical ? 1 : randomScrollLength;
-            host.Height = host.Orientation == Orientation.Vertical ? randomScrollLength : 1;
+            scroll.Width = scroll.Orientation == Orientation.Vertical ? 1 : randomScrollLength;
+            scroll.Height = scroll.Orientation == Orientation.Vertical ? randomScrollLength : 1;
 
             // Slider may have changed content size
-            host.LayoutSubviews ();
+            scroll.LayoutSubviews ();
 
             // Act
-            host.Position = host._slider.GetPositionFromSliderLocation (randomLocation);
-            (int calculatedLocation, int calculatedDimension) = host._slider.GetSliderLocationDimensionFromPosition ();
-            int calculatedPosition = host._slider.GetPositionFromSliderLocation (calculatedLocation);
+            scroll.Position = scroll._slider.GetPositionFromSliderLocation (randomLocation);
+            (int calculatedLocation, int calculatedDimension) = scroll._slider.GetSliderLocationDimensionFromPosition ();
+            int calculatedPosition = scroll._slider.GetPositionFromSliderLocation (calculatedLocation);
 
             // Assert
             AssertLocation (randomScrollLength, randomLocation, calculatedLocation, calculatedDimension);
 
-            Assert.Equal (host.Position, calculatedPosition);
+            Assert.Equal (scroll.Position, calculatedPosition);
         }
     }