Tig 9 months ago
parent
commit
70c30ac9e5
2 changed files with 45 additions and 45 deletions
  1. 14 6
      Terminal.Gui/Views/ScrollBar/ScrollBar.cs
  2. 31 39
      UICatalog/Scenarios/ScrollBarDemo.cs

+ 14 - 6
Terminal.Gui/Views/ScrollBar/ScrollBar.cs

@@ -78,8 +78,6 @@ public class ScrollBar : View, IOrientation, IDesignable
     /// <inheritdoc/>
     /// <inheritdoc/>
     protected override void OnFrameChanged (in Rectangle frame)
     protected override void OnFrameChanged (in Rectangle frame)
     {
     {
-
-
         ShowHide ();
         ShowHide ();
     }
     }
 
 
@@ -235,7 +233,7 @@ public class ScrollBar : View, IOrientation, IDesignable
                 {
                 {
                     Visible = true;
                     Visible = true;
                 }
                 }
-
+                ShowHide ();
                 SetNeedsLayout ();
                 SetNeedsLayout ();
             }
             }
         }
         }
@@ -299,7 +297,7 @@ public class ScrollBar : View, IOrientation, IDesignable
             {
             {
                 return _scrollableContentSize.Value;
                 return _scrollableContentSize.Value;
             }
             }
-            return Orientation == Orientation.Vertical ? SuperView?.GetContentSize().Height ?? 0 : SuperView?.GetContentSize ().Width ?? 0;
+            return Orientation == Orientation.Vertical ? SuperView?.GetContentSize ().Height ?? 0 : SuperView?.GetContentSize ().Width ?? 0;
 
 
         }
         }
         set
         set
@@ -311,7 +309,12 @@ public class ScrollBar : View, IOrientation, IDesignable
 
 
             _scrollableContentSize = value;
             _scrollableContentSize = value;
             _slider.Size = CalculateSliderSize ();
             _slider.Size = CalculateSliderSize ();
-            ShowHide();
+            ShowHide ();
+
+            if (!Visible)
+            {
+                return;
+            }
             OnSizeChanged (value);
             OnSizeChanged (value);
             ScrollableContentSizeChanged?.Invoke (this, new (in value));
             ScrollableContentSizeChanged?.Invoke (this, new (in value));
             SetNeedsLayout ();
             SetNeedsLayout ();
@@ -345,7 +348,7 @@ public class ScrollBar : View, IOrientation, IDesignable
         get => _position;
         get => _position;
         set
         set
         {
         {
-            if (value == _position)
+            if (value == _position || !Visible)
             {
             {
                 return;
                 return;
             }
             }
@@ -369,6 +372,11 @@ public class ScrollBar : View, IOrientation, IDesignable
 
 
             int distance = newContentPosition - _position;
             int distance = newContentPosition - _position;
 
 
+            if (_position == newContentPosition)
+            {
+                return;
+
+            }
             _position = newContentPosition;
             _position = newContentPosition;
 
 
             _sliderPosition = CalculateSliderPositionFromContentPosition (_position, direction);
             _sliderPosition = CalculateSliderPositionFromContentPosition (_position, direction);

+ 31 - 39
UICatalog/Scenarios/ScrollBarDemo.cs

@@ -79,9 +79,9 @@ public class ScrollBarDemo : Scenario
 
 
         NumericUpDown<int> scrollWidthHeight = new ()
         NumericUpDown<int> scrollWidthHeight = new ()
         {
         {
-            Value = scrollBar.Frame.Width,
+            Value = 1,
             X = Pos.Right (lblWidthHeight) + 1,
             X = Pos.Right (lblWidthHeight) + 1,
-            Y = Pos.Top (lblWidthHeight)
+            Y = Pos.Top (lblWidthHeight),
         };
         };
         demoFrame.Add (scrollWidthHeight);
         demoFrame.Add (scrollWidthHeight);
 
 
@@ -110,19 +110,19 @@ public class ScrollBarDemo : Scenario
                                            };
                                            };
 
 
 
 
-        var lblOrientationabel = new Label
+        var lblOrientationLabel = new Label
         {
         {
             Text = "_Orientation:",
             Text = "_Orientation:",
             TextAlignment = Alignment.End,
             TextAlignment = Alignment.End,
             Y = Pos.Align (Alignment.Start, groupId: 1),
             Y = Pos.Align (Alignment.Start, groupId: 1),
             Width = Dim.Func (() => GetMaxLabelWidth (1))
             Width = Dim.Func (() => GetMaxLabelWidth (1))
         };
         };
-        demoFrame.Add (lblOrientationabel);
+        demoFrame.Add (lblOrientationLabel);
 
 
         var rgOrientation = new RadioGroup
         var rgOrientation = new RadioGroup
         {
         {
-            X = Pos.Right (lblOrientationabel) + 1,
-            Y = Pos.Top (lblOrientationabel),
+            X = Pos.Right (lblOrientationLabel) + 1,
+            Y = Pos.Top (lblOrientationLabel),
             RadioLabels = ["Vertical", "Horizontal"],
             RadioLabels = ["Vertical", "Horizontal"],
             Orientation = Orientation.Horizontal
             Orientation = Orientation.Horizontal
         };
         };
@@ -140,7 +140,7 @@ public class ScrollBarDemo : Scenario
                                                      scrollBar.Orientation = Orientation.Vertical;
                                                      scrollBar.Orientation = Orientation.Vertical;
                                                      scrollBar.X = Pos.AnchorEnd () - 5;
                                                      scrollBar.X = Pos.AnchorEnd () - 5;
                                                      scrollBar.Y = 0;
                                                      scrollBar.Y = 0;
-                                                     scrollWidthHeight.Value = Math.Min (scrollWidthHeight.Value, scrollBar.SuperView!.GetContentSize ().Width);
+                                                     //scrollWidthHeight.Value = Math.Min (scrollWidthHeight.Value, scrollBar.SuperView!.GetContentSize ().Width);
                                                      scrollBar.Width = scrollWidthHeight.Value;
                                                      scrollBar.Width = scrollWidthHeight.Value;
                                                      controlledList.Visible = true;
                                                      controlledList.Visible = true;
                                                  }
                                                  }
@@ -149,7 +149,7 @@ public class ScrollBarDemo : Scenario
                                                      scrollBar.Orientation = Orientation.Horizontal;
                                                      scrollBar.Orientation = Orientation.Horizontal;
                                                      scrollBar.X = 0;
                                                      scrollBar.X = 0;
                                                      scrollBar.Y = Pos.AnchorEnd ();
                                                      scrollBar.Y = Pos.AnchorEnd ();
-                                                     scrollWidthHeight.Value = Math.Min (scrollWidthHeight.Value, scrollBar.SuperView!.GetContentSize ().Height);
+                                                     //scrollWidthHeight.Value = Math.Min (scrollWidthHeight.Value, scrollBar.SuperView!.GetContentSize ().Height);
                                                      scrollBar.Height = scrollWidthHeight.Value;
                                                      scrollBar.Height = scrollWidthHeight.Value;
                                                      controlledList.Visible = false;
                                                      controlledList.Visible = false;
 
 
@@ -158,22 +158,22 @@ public class ScrollBarDemo : Scenario
 
 
         var lblSize = new Label
         var lblSize = new Label
         {
         {
-            Text = "_Size:",
+            Text = "_Content Size:",
             TextAlignment = Alignment.End,
             TextAlignment = Alignment.End,
             Y = Pos.Align (Alignment.Start, groupId: 1),
             Y = Pos.Align (Alignment.Start, groupId: 1),
             Width = Dim.Func (() => GetMaxLabelWidth (1))
             Width = Dim.Func (() => GetMaxLabelWidth (1))
         };
         };
         demoFrame.Add (lblSize);
         demoFrame.Add (lblSize);
 
 
-        NumericUpDown<int> scrollSize = new ()
+        NumericUpDown<int> scrollContentSize = new ()
         {
         {
             Value = scrollBar.ScrollableContentSize,
             Value = scrollBar.ScrollableContentSize,
             X = Pos.Right (lblSize) + 1,
             X = Pos.Right (lblSize) + 1,
             Y = Pos.Top (lblSize)
             Y = Pos.Top (lblSize)
         };
         };
-        demoFrame.Add (scrollSize);
+        demoFrame.Add (scrollContentSize);
 
 
-        scrollSize.ValueChanging += (s, e) =>
+        scrollContentSize.ValueChanging += (s, e) =>
                                     {
                                     {
                                         if (e.NewValue < 0)
                                         if (e.NewValue < 0)
                                         {
                                         {
@@ -185,6 +185,7 @@ public class ScrollBarDemo : Scenario
                                         if (scrollBar.ScrollableContentSize != e.NewValue)
                                         if (scrollBar.ScrollableContentSize != e.NewValue)
                                         {
                                         {
                                             scrollBar.ScrollableContentSize = e.NewValue;
                                             scrollBar.ScrollableContentSize = e.NewValue;
+                                            controlledList.SetSource (new ObservableCollection<string> (Enumerable.Range (0, scrollBar.ScrollableContentSize).Select (n => $"{n:00000}")));
                                         }
                                         }
                                     };
                                     };
 
 
@@ -197,15 +198,15 @@ public class ScrollBarDemo : Scenario
         };
         };
         demoFrame.Add (lblVisibleContentSize);
         demoFrame.Add (lblVisibleContentSize);
 
 
-        NumericUpDown<int> VisibleContentSize = new ()
+        NumericUpDown<int> visibleContentSize = new ()
         {
         {
             Value = scrollBar.VisibleContentSize,
             Value = scrollBar.VisibleContentSize,
             X = Pos.Right (lblVisibleContentSize) + 1,
             X = Pos.Right (lblVisibleContentSize) + 1,
             Y = Pos.Top (lblVisibleContentSize)
             Y = Pos.Top (lblVisibleContentSize)
         };
         };
-        demoFrame.Add (VisibleContentSize);
+        demoFrame.Add (visibleContentSize);
 
 
-        VisibleContentSize.ValueChanging += (s, e) =>
+        visibleContentSize.ValueChanging += (s, e) =>
                                            {
                                            {
                                                if (e.NewValue < 0)
                                                if (e.NewValue < 0)
                                                {
                                                {
@@ -226,7 +227,6 @@ public class ScrollBarDemo : Scenario
             TextAlignment = Alignment.End,
             TextAlignment = Alignment.End,
             Y = Pos.Align (Alignment.Start, groupId: 1),
             Y = Pos.Align (Alignment.Start, groupId: 1),
             Width = Dim.Func (() => GetMaxLabelWidth (1))
             Width = Dim.Func (() => GetMaxLabelWidth (1))
-
         };
         };
         demoFrame.Add (lblSliderPosition);
         demoFrame.Add (lblSliderPosition);
 
 
@@ -254,25 +254,25 @@ public class ScrollBarDemo : Scenario
         };
         };
         demoFrame.Add (scrolled);
         demoFrame.Add (scrolled);
 
 
-        var lblContentPosition = new Label
+        var lblPosition = new Label
         {
         {
-            Text = "_ContentPosition:",
+            Text = "_Position:",
             TextAlignment = Alignment.End,
             TextAlignment = Alignment.End,
             Y = Pos.Align (Alignment.Start, groupId: 1),
             Y = Pos.Align (Alignment.Start, groupId: 1),
             Width = Dim.Func (() => GetMaxLabelWidth (1))
             Width = Dim.Func (() => GetMaxLabelWidth (1))
 
 
         };
         };
-        demoFrame.Add (lblContentPosition);
+        demoFrame.Add (lblPosition);
 
 
-        NumericUpDown<int> scrollContentPosition = new ()
+        NumericUpDown<int> scrollPosition = new ()
         {
         {
             Value = scrollBar.GetSliderPosition (),
             Value = scrollBar.GetSliderPosition (),
-            X = Pos.Right (lblContentPosition) + 1,
-            Y = Pos.Top (lblContentPosition)
+            X = Pos.Right (lblPosition) + 1,
+            Y = Pos.Top (lblPosition)
         };
         };
-        demoFrame.Add (scrollContentPosition);
+        demoFrame.Add (scrollPosition);
 
 
-        scrollContentPosition.ValueChanging += (s, e) =>
+        scrollPosition.ValueChanging += (s, e) =>
                                                {
                                                {
                                                    if (e.NewValue < 0)
                                                    if (e.NewValue < 0)
                                                    {
                                                    {
@@ -304,7 +304,7 @@ public class ScrollBarDemo : Scenario
         {
         {
             Y = Pos.Top (lblOptions),
             Y = Pos.Top (lblOptions),
             X = Pos.Right (lblOptions) + 1,
             X = Pos.Right (lblOptions) + 1,
-            Text = "Auto_HideScrollBar",
+            Text = $"Auto_Hide",
             CheckedState = scrollBar.AutoHide ? CheckState.Checked : CheckState.UnChecked
             CheckedState = scrollBar.AutoHide ? CheckState.Checked : CheckState.UnChecked
         };
         };
         ckbAutoHide.CheckedStateChanging += (s, e) => scrollBar.AutoHide = e.NewValue == CheckState.Checked;
         ckbAutoHide.CheckedStateChanging += (s, e) => scrollBar.AutoHide = e.NewValue == CheckState.Checked;
@@ -320,14 +320,6 @@ public class ScrollBarDemo : Scenario
         ckbShowPercent.CheckedStateChanging += (s, e) => scrollBar.ShowPercent = e.NewValue == CheckState.Checked;
         ckbShowPercent.CheckedStateChanging += (s, e) => scrollBar.ShowPercent = e.NewValue == CheckState.Checked;
         demoFrame.Add (ckbShowPercent);
         demoFrame.Add (ckbShowPercent);
 
 
-        //var ckbKeepContentInAllViewport = new CheckBox
-        //{
-        //    X = Pos.Right (ckbShowScrollIndicator) + 1, Y = Pos.Bottom (scrollPosition), Text = "KeepContentInAllViewport",
-        //    CheckedState = scrollBar.KeepContentInAllViewport ? CheckState.Checked : CheckState.UnChecked
-        //};
-        //ckbKeepContentInAllViewport.CheckedStateChanging += (s, e) => scrollBar.KeepContentInAllViewport = e.NewValue == CheckState.Checked;
-        //view.Add (ckbKeepContentInAllViewport);
-
         var lblScrollFrame = new Label
         var lblScrollFrame = new Label
         {
         {
             Y = Pos.Bottom (lblOptions) + 1
             Y = Pos.Bottom (lblOptions) + 1
@@ -351,7 +343,7 @@ public class ScrollBarDemo : Scenario
                                          lblScrollFrame.Text = $"Scroll Frame: {scrollBar.Frame.ToString ()}";
                                          lblScrollFrame.Text = $"Scroll Frame: {scrollBar.Frame.ToString ()}";
                                          lblScrollViewport.Text = $"Scroll Viewport: {scrollBar.Viewport.ToString ()}";
                                          lblScrollViewport.Text = $"Scroll Viewport: {scrollBar.Viewport.ToString ()}";
                                          lblScrollContentSize.Text = $"Scroll ContentSize: {scrollBar.GetContentSize ().ToString ()}";
                                          lblScrollContentSize.Text = $"Scroll ContentSize: {scrollBar.GetContentSize ().ToString ()}";
-                                         VisibleContentSize.Value = scrollBar.VisibleContentSize;
+                                         visibleContentSize.Value = scrollBar.VisibleContentSize;
                                      };
                                      };
 
 
         EventLog eventLog = new ()
         EventLog eventLog = new ()
@@ -372,16 +364,16 @@ public class ScrollBarDemo : Scenario
                                   {
                                   {
                                       eventLog.Log ($"SizeChanged: {e.CurrentValue}");
                                       eventLog.Log ($"SizeChanged: {e.CurrentValue}");
 
 
-                                      if (scrollSize.Value != e.CurrentValue)
+                                      if (scrollContentSize.Value != e.CurrentValue)
                                       {
                                       {
-                                          scrollSize.Value = e.CurrentValue;
+                                          scrollContentSize.Value = e.CurrentValue;
                                       }
                                       }
                                   };
                                   };
 
 
             scrollBar.SliderPositionChanged += (s, e) =>
             scrollBar.SliderPositionChanged += (s, e) =>
                                             {
                                             {
                                                 eventLog.Log ($"SliderPositionChanged: {e.CurrentValue}");
                                                 eventLog.Log ($"SliderPositionChanged: {e.CurrentValue}");
-                                                eventLog.Log ($"  ContentPosition: {scrollBar.Position}");
+                                                eventLog.Log ($"  Position: {scrollBar.Position}");
                                                 scrollSliderPosition.Text = e.CurrentValue.ToString ();
                                                 scrollSliderPosition.Text = e.CurrentValue.ToString ();
                                             };
                                             };
 
 
@@ -394,8 +386,8 @@ public class ScrollBarDemo : Scenario
 
 
             scrollBar.PositionChanged += (s, e) =>
             scrollBar.PositionChanged += (s, e) =>
                                              {
                                              {
-                                                 eventLog.Log ($"ContentPositionChanged: {e.CurrentValue}");
-                                                 scrollContentPosition.Value = e.CurrentValue;
+                                                 eventLog.Log ($"PositionChanged: {e.CurrentValue}");
+                                                 scrollPosition.Value = e.CurrentValue;
                                                  controlledList.Viewport = controlledList.Viewport with { Y = e.CurrentValue };
                                                  controlledList.Viewport = controlledList.Viewport with { Y = e.CurrentValue };
                                              };
                                              };