Browse Source

Merge pull request #180 from tig/BDisp-v2_2489-tig-1

Tweaks and suggestions
BDisp 1 year ago
parent
commit
d6a183cebd
2 changed files with 46 additions and 31 deletions
  1. 38 24
      Terminal.Gui/Views/Scroll.cs
  2. 8 7
      UICatalog/Scenarios/ScrollDemo.cs

+ 38 - 24
Terminal.Gui/Views/Scroll.cs

@@ -17,12 +17,18 @@ public class Scroll : View
     public Scroll ()
     {
         WantContinuousButtonPressed = true;
-        ClearOnVisibleFalse = false;
+        //ClearOnVisibleFalse = false;
         CanFocus = false;
         Orientation = Orientation.Vertical;
-        Width = 1;
+        Width = Dim.Auto (DimAutoStyle.Content, 1);
+        Height = Dim.Auto (DimAutoStyle.Content, 1);
 
-        _slider = new () { Id = "slider" };
+        _slider = new ()
+        {
+            Id = "slider",
+            Width = Dim.Auto (DimAutoStyle.Content),
+            Height = Dim.Auto (DimAutoStyle.Content)
+        };
         Add (_slider);
 
         Added += Scroll_Added;
@@ -35,14 +41,12 @@ public class Scroll : View
     }
 
     private readonly View _slider;
-    private int _lastLocation = -1;
 
-    private Orientation _orientation;
-    private int _size;
-    private int _position;
+    private int _lastLocation = -1;
 
     private bool _wasSliderMouse;
 
+    private Orientation _orientation;
     /// <summary>
     ///     Determines the Orientation of the scroll.
     /// </summary>
@@ -52,10 +56,11 @@ public class Scroll : View
         set
         {
             _orientation = value;
-            SetWidthHeight ();
+            AdjustSlider();
         }
     }
 
+    private int _position;
     /// <summary>
     ///     The position, relative to <see cref="Size"/>, to set the scrollbar at.
     /// </summary>
@@ -64,7 +69,7 @@ public class Scroll : View
         get => _position;
         set
         {
-            int barSize = Orientation == Orientation.Vertical ? Frame.Height : Frame.Width;
+            int barSize = Orientation == Orientation.Vertical ? ContentSize.Height : ContentSize.Width;
 
             if (value < 0 || (value > 0 && value + barSize > Size))
             {
@@ -84,7 +89,7 @@ public class Scroll : View
 
             if (!_wasSliderMouse)
             {
-                SetWidthHeight ();
+                AdjustSlider ();
             }
         }
     }
@@ -95,6 +100,7 @@ public class Scroll : View
     /// <summary>This event is raised when the position on the scrollbar is changing.</summary>
     public event EventHandler<StateEventArgs<int>> PositionChanging;
 
+    private int _size;
     /// <summary>
     ///     The size of content the scroll represents.
     /// </summary>
@@ -106,7 +112,7 @@ public class Scroll : View
             int oldSize = _size;
             _size = value;
             OnSizeChanged (oldSize);
-            SetWidthHeight ();
+            AdjustSlider ();
         }
     }
 
@@ -143,24 +149,24 @@ public class Scroll : View
 
     private int GetPositionFromSliderLocation (int location)
     {
-        if (Frame.Height == 0 || Frame.Width == 0)
+        if (ContentSize.Height == 0 || ContentSize.Width == 0)
         {
             return 0;
         }
 
-        int barSize = Orientation == Orientation.Vertical ? Frame.Height : Frame.Width;
+        int barSize = Orientation == Orientation.Vertical ? ContentSize.Height : ContentSize.Width;
 
         return Math.Min (location * Size / barSize, Size - barSize);
     }
 
     private (int Location, int Dimension) GetSliderLocationDimensionFromPosition ()
     {
-        if (Frame.Height == 0 || Frame.Width == 0)
+        if (ContentSize.Height == 0 || ContentSize.Width == 0)
         {
             return new (0, 0);
         }
 
-        int barSize = Orientation == Orientation.Vertical ? Frame.Height : Frame.Width;
+        int barSize = Orientation == Orientation.Vertical ? ContentSize.Height : ContentSize.Width;
         int location;
         int dimension;
 
@@ -194,7 +200,7 @@ public class Scroll : View
     {
         if (!_wasSliderMouse)
         {
-            SetWidthHeight ();
+            AdjustSlider ();
         }
         else
         {
@@ -217,13 +223,16 @@ public class Scroll : View
 
     private void Scroll_DrawContent (object sender, DrawEventArgs e) { SetColorSchemeWithSuperview (sender as View); }
 
-    private void Scroll_Initialized (object sender, EventArgs e) { SetWidthHeight (); }
+    private void Scroll_Initialized (object sender, EventArgs e)
+    {
+        AdjustSlider ();
+    }
 
     private void Scroll_MouseEvent (object sender, MouseEventEventArgs e)
     {
         MouseEvent me = e.MouseEvent;
         int location = Orientation == Orientation.Vertical ? me.Position.Y : me.Position.X;
-        int barSize = Orientation == Orientation.Vertical ? Frame.Height : Frame.Width;
+        int barSize = Orientation == Orientation.Vertical ? ContentSize.Height : ContentSize.Width;
 
         (int topLeft, int bottomRight) sliderPos = _orientation == Orientation.Vertical
                                                        ? new (_slider.Frame.Y, _slider.Frame.Bottom - 1)
@@ -251,6 +260,7 @@ public class Scroll : View
         }
     }
 
+    // TODO: Just override GetNormalColor instead of having this method
     private static void SetColorSchemeWithSuperview (View view)
     {
         if (view.SuperView is { })
@@ -275,16 +285,16 @@ public class Scroll : View
         Text = string.Concat (
                               Enumerable.Repeat (
                                                  Glyphs.Stipple.ToString (),
-                                                 Frame.Width * Frame.Height));
+                                                 ContentSize.Width * ContentSize.Height));
         _slider.TextDirection = Orientation == Orientation.Vertical ? TextDirection.TopBottom_LeftRight : TextDirection.LeftRight_TopBottom;
 
         _slider.Text = string.Concat (
                                       Enumerable.Repeat (
                                                          Glyphs.ContinuousMeterSegment.ToString (),
-                                                         _slider.Frame.Width * _slider.Frame.Height));
+                                                         _slider.ContentSize.Width * _slider.ContentSize.Height));
     }
 
-    private void SetWidthHeight ()
+    private void AdjustSlider ()
     {
         if (!IsInitialized)
         {
@@ -294,8 +304,12 @@ public class Scroll : View
         (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 ? Dim.Fill () : slider.Dimension;
-        _slider.Height = Orientation == Orientation.Vertical ? slider.Dimension : Dim.Fill ();
+
+        _slider.SetContentSize (
+                                new (
+                                     Orientation == Orientation.Vertical ? ContentSize.Width : slider.Dimension,
+                                     Orientation == Orientation.Vertical ? slider.Dimension : ContentSize.Height
+                                    ));
 
         SetSliderText ();
     }
@@ -304,7 +318,7 @@ public class Scroll : View
     {
         MouseEvent me = e.MouseEvent;
         int location = Orientation == Orientation.Vertical ? me.Position.Y : me.Position.X;
-        int barSize = Orientation == Orientation.Vertical ? Frame.Height : Frame.Width;
+        int barSize = Orientation == Orientation.Vertical ? ContentSize.Height : ContentSize.Width;
         int offset = _lastLocation > -1 ? location - _lastLocation : 0;
 
         if (me.Flags == MouseFlags.Button1Pressed)

+ 8 - 7
UICatalog/Scenarios/ScrollDemo.cs

@@ -36,8 +36,7 @@ public class ScrollDemo : Scenario
         var scroll = new Scroll
         {
             X = Pos.AnchorEnd (),
-            Width = 1,
-            Height = Dim.Fill ()
+            Height = Dim.Fill (),
         };
         view.Add (scroll);
 
@@ -47,7 +46,7 @@ public class ScrollDemo : Scenario
         };
         view.Add (lblWidthHeight);
 
-        Buttons.NumericUpDown<int> scrollWidthHeight = new()
+        Buttons.NumericUpDown<int> scrollWidthHeight = new ()
         {
             Value = scroll.Frame.Width,
             X = Pos.Right (lblWidthHeight) + 1,
@@ -93,6 +92,7 @@ public class ScrollDemo : Scenario
                                                  {
                                                      scroll.Orientation = Orientation.Vertical;
                                                      scroll.X = Pos.AnchorEnd ();
+                                                     scroll.Y = 0;
                                                      scroll.Width = scrollWidthHeight.Value;
                                                      scroll.Height = Dim.Fill ();
                                                      scroll.Size /= 3;
@@ -100,6 +100,7 @@ public class ScrollDemo : Scenario
                                                  else
                                                  {
                                                      scroll.Orientation = Orientation.Horizontal;
+                                                     scroll.X = 0;
                                                      scroll.Y = Pos.AnchorEnd ();
                                                      scroll.Width = Dim.Fill ();
                                                      scroll.Height = scrollWidthHeight.Value;
@@ -114,7 +115,7 @@ public class ScrollDemo : Scenario
         };
         view.Add (lblSize);
 
-        Buttons.NumericUpDown<int> scrollSize = new()
+        Buttons.NumericUpDown<int> scrollSize = new ()
         {
             Value = scroll.Size,
             X = Pos.Right (lblSize) + 1,
@@ -144,7 +145,7 @@ public class ScrollDemo : Scenario
         };
         view.Add (lblPosition);
 
-        Buttons.NumericUpDown<int> scrollPosition = new()
+        Buttons.NumericUpDown<int> scrollPosition = new ()
         {
             Value = scroll.Position,
             X = Pos.Right (lblPosition) + 1,
@@ -218,8 +219,8 @@ public class ScrollDemo : Scenario
 
         editor.Initialized += (s, e) =>
                               {
-                                  scroll.Size = 40;
-                                  editor.ViewToEdit = view;
+                                  scroll.Size = int.Max (app.ContentSize.Height * 2, app.ContentSize.Width * 2);
+                                  editor.ViewToEdit = scroll;
                               };
 
         app.Closed += (s, e) => View.Diagnostics = _diagnosticFlags;