Browse Source

Moving slider code to his class.

BDisp 11 months ago
parent
commit
3317c504e0
2 changed files with 87 additions and 104 deletions
  1. 15 104
      Terminal.Gui/Views/Scroll/Scroll.cs
  2. 72 0
      Terminal.Gui/Views/Scroll/ScrollSlider.cs

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

@@ -12,14 +12,14 @@ public class Scroll : View
     /// <inheritdoc/>
     /// <inheritdoc/>
     public Scroll ()
     public Scroll ()
     {
     {
+        _slider = new (this);
+        Add (_slider);
+
         WantContinuousButtonPressed = true;
         WantContinuousButtonPressed = true;
         CanFocus = false;
         CanFocus = false;
         Orientation = Orientation.Vertical;
         Orientation = Orientation.Vertical;
         Width = Dim.Auto (DimAutoStyle.Content, 1);
         Width = Dim.Auto (DimAutoStyle.Content, 1);
         Height = Dim.Auto (DimAutoStyle.Content, 1);
         Height = Dim.Auto (DimAutoStyle.Content, 1);
-
-        _slider = new (this);
-        Add (_slider);
     }
     }
 
 
     private readonly ScrollSlider _slider;
     private readonly ScrollSlider _slider;
@@ -35,30 +35,7 @@ public class Scroll : View
     {
     {
         base.EndInit ();
         base.EndInit ();
 
 
-        AdjustSlider ();
-    }
-
-    /// <inheritdoc/>
-    public override void OnAdded (SuperViewChangedEventArgs e)
-    {
-        View parent = (e.Parent is Adornment adornment ? adornment.Parent : e.Parent)!;
-
-        parent.LayoutComplete += SuperView_LayoutComplete!;
-
-        base.OnAdded (e);
-    }
-
-    /// <inheritdoc/>
-    public override void OnRemoved (SuperViewChangedEventArgs e)
-    {
-        if (e.Parent is { })
-        {
-            View parent = (e.Parent is Adornment adornment ? adornment.Parent : e.Parent)!;
-
-            parent.LayoutComplete -= SuperView_LayoutComplete!;
-        }
-
-        base.OnRemoved (e);
+        AdjustAll ();
     }
     }
 
 
     /// <summary>
     /// <summary>
@@ -70,7 +47,7 @@ public class Scroll : View
         set
         set
         {
         {
             _orientation = value;
             _orientation = value;
-            AdjustSlider ();
+            AdjustAll ();
         }
         }
     }
     }
 
 
@@ -105,7 +82,7 @@ public class Scroll : View
 
 
             if (!_slider._wasSliderMouse)
             if (!_slider._wasSliderMouse)
             {
             {
-                AdjustSlider ();
+                AdjustAll ();
             }
             }
 
 
             OnPositionChanged (_position);
             OnPositionChanged (_position);
@@ -131,7 +108,7 @@ public class Scroll : View
         {
         {
             _size = value;
             _size = value;
             OnSizeChanged (_size);
             OnSizeChanged (_size);
-            AdjustSlider ();
+            AdjustAll ();
         }
         }
     }
     }
 
 
@@ -199,68 +176,21 @@ public class Scroll : View
     /// <summary>Virtual method called when <see cref="Size"/> has changed. Raises <see cref="SizeChanged"/>.</summary>
     /// <summary>Virtual method called when <see cref="Size"/> has changed. Raises <see cref="SizeChanged"/>.</summary>
     protected void OnSizeChanged (int size) { SizeChanged?.Invoke (this, new (in size)); }
     protected void OnSizeChanged (int size) { SizeChanged?.Invoke (this, new (in size)); }
 
 
-    private void AdjustSlider ()
+    /// <inheritdoc/>
+    internal override void OnLayoutComplete (LayoutEventArgs args)
     {
     {
-        if (!IsInitialized)
-        {
-            return;
-        }
-
-        (int Location, int Dimension) slider = GetSliderLocationDimensionFromPosition ();
-        _slider.X = Orientation == Orientation.Vertical ? 0 : slider.Location;
-        _slider.Y = Orientation == Orientation.Vertical ? slider.Location : 0;
+        base.OnLayoutComplete (args);
 
 
-        _slider.SetContentSize (
-                                new (
-                                     Orientation == Orientation.Vertical ? GetContentSize ().Width : slider.Dimension,
-                                     Orientation == Orientation.Vertical ? slider.Dimension : GetContentSize ().Height
-                                    ));
-        SetSliderText ();
+        AdjustAll ();
     }
     }
 
 
-    // QUESTION: This method is only called from one place. Should it be inlined? Or, should it be made internal and unit tests be provided?
-    private (int Location, int Dimension) GetSliderLocationDimensionFromPosition ()
+    private void AdjustAll ()
     {
     {
-        if (GetContentSize ().Height == 0 || GetContentSize ().Width == 0)
-        {
-            return new (0, 0);
-        }
-
-        int scrollSize = Orientation == Orientation.Vertical ? GetContentSize ().Height : GetContentSize ().Width;
-        int location;
-        int dimension;
-
-        if (Size > 0)
-        {
-            dimension = Math.Min (Math.Max (scrollSize * scrollSize / Size, 1), scrollSize);
-
-            // Ensure the Position is valid
-            if (Position > 0 && Position + scrollSize > Size)
-            {
-                Position = Size - scrollSize;
-            }
-
-            location = Math.Min ((Position * scrollSize + Position) / Size, scrollSize - dimension);
-
-            if (Position == Size - scrollSize && location + dimension < scrollSize)
-            {
-                location = scrollSize - dimension;
-            }
-        }
-        else
-        {
-            location = 0;
-            dimension = scrollSize;
-        }
-
-        return new (location, dimension);
+        _slider.AdjustSlider ();
+        SetScrollText ();
     }
     }
 
 
-    // TODO: I think you should create a new `internal` view named "ScrollSlider" with an `Orientation` property. It should inherit from View and override GetNormalColor and the mouse events
-    // that can be moved within it's Superview, constrained to move only horizontally or vertically depending on Orientation.
-    // This will really simplify a lot of this.
-
-    private void SetSliderText ()
+    private void SetScrollText ()
     {
     {
         TextDirection = Orientation == Orientation.Vertical ? TextDirection.TopBottom_LeftRight : TextDirection.LeftRight_TopBottom;
         TextDirection = Orientation == Orientation.Vertical ? TextDirection.TopBottom_LeftRight : TextDirection.LeftRight_TopBottom;
 
 
@@ -269,24 +199,5 @@ public class Scroll : View
                               Enumerable.Repeat (
                               Enumerable.Repeat (
                                                  Glyphs.Stipple.ToString (),
                                                  Glyphs.Stipple.ToString (),
                                                  GetContentSize ().Width * GetContentSize ().Height));
                                                  GetContentSize ().Width * GetContentSize ().Height));
-        _slider.TextDirection = Orientation == Orientation.Vertical ? TextDirection.TopBottom_LeftRight : TextDirection.LeftRight_TopBottom;
-
-        _slider.Text = string.Concat (
-                                      Enumerable.Repeat (
-                                                         Glyphs.ContinuousMeterSegment.ToString (),
-                                                         _slider.GetContentSize ().Width * _slider.GetContentSize ().Height));
-    }
-
-    // TODO: This is unnecessary. If Scroll.Width/Height is Dim.Auto, the Superview will get resized automatically.
-    private void SuperView_LayoutComplete (object sender, LayoutEventArgs e)
-    {
-        if (!_slider._wasSliderMouse)
-        {
-            AdjustSlider ();
-        }
-        else
-        {
-            _slider._wasSliderMouse = false;
-        }
     }
     }
 }
 }

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

@@ -19,6 +19,25 @@ internal class ScrollSlider : View
     private int _lastLocation = -1;
     private int _lastLocation = -1;
     private ColorScheme? _savedColorScheme;
     private ColorScheme? _savedColorScheme;
 
 
+    public void AdjustSlider ()
+    {
+        if (!IsInitialized)
+        {
+            return;
+        }
+
+        (int Location, int Dimension) sliderLocationAndDimension = GetSliderLocationDimensionFromPosition ();
+        X = _host.Orientation == Orientation.Vertical ? 0 : sliderLocationAndDimension.Location;
+        Y = _host.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
+                            ));
+        SetSliderText ();
+    }
+
     /// <inheritdoc/>
     /// <inheritdoc/>
     public override Attribute GetNormalColor ()
     public override Attribute GetNormalColor ()
     {
     {
@@ -145,4 +164,57 @@ internal class ScrollSlider : View
 
 
         return Math.Min ((location * _host.Size + location) / scrollSize, _host.Size - scrollSize);
         return Math.Min ((location * _host.Size + location) / scrollSize, _host.Size - scrollSize);
     }
     }
+
+    // QUESTION: This method is only called from one place. Should it be inlined? Or, should it be made internal and unit tests be provided?
+    private (int Location, int Dimension) GetSliderLocationDimensionFromPosition ()
+    {
+        if (_host.GetContentSize ().Height == 0 || _host.GetContentSize ().Width == 0)
+        {
+            return new (0, 0);
+        }
+
+        int scrollSize = _host.Orientation == Orientation.Vertical ? _host.GetContentSize ().Height : _host.GetContentSize ().Width;
+        int location;
+        int dimension;
+
+        if (_host.Size > 0)
+        {
+            dimension = Math.Min (Math.Max (scrollSize * scrollSize / _host.Size, 1), scrollSize);
+
+            // Ensure the Position is valid
+            if (_host.Position > 0 && _host.Position + scrollSize > _host.Size)
+            {
+                _host.Position = _host.Size - scrollSize;
+            }
+
+            location = Math.Min ((_host.Position * scrollSize + _host.Position) / _host.Size, scrollSize - dimension);
+
+            if (_host.Position == _host.Size - scrollSize && location + dimension < scrollSize)
+            {
+                location = scrollSize - dimension;
+            }
+        }
+        else
+        {
+            location = 0;
+            dimension = scrollSize;
+        }
+
+        return new (location, dimension);
+    }
+
+    // TODO: I think you should create a new `internal` view named "ScrollSlider" with an `Orientation` property. It should inherit from View and override GetNormalColor and the mouse events
+    // that can be moved within it's Superview, constrained to move only horizontally or vertically depending on Orientation.
+    // This will really simplify a lot of this.
+
+    private void SetSliderText ()
+    {
+        TextDirection = _host.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));
+    }
 }
 }