Browse Source

Code cleanup

Tig 8 months ago
parent
commit
861dfee008

+ 92 - 110
Terminal.Gui/View/View.ScrollBars.cs

@@ -7,7 +7,7 @@ public partial class View
     private Lazy<ScrollBar> _verticalScrollBar;
     private Lazy<ScrollBar> _verticalScrollBar;
 
 
     /// <summary>
     /// <summary>
-    ///     Initializes the ScrollBars of the View. Called by the constructor.
+    ///     Initializes the ScrollBars of the View. Called by the View constructor.
     /// </summary>
     /// </summary>
     private void SetupScrollBars ()
     private void SetupScrollBars ()
     {
     {
@@ -16,39 +16,11 @@ public partial class View
             return;
             return;
         }
         }
 
 
-        _verticalScrollBar = new (() => ScrollBarFactory (Orientation.Vertical));
-        _horizontalScrollBar = new (() => ScrollBarFactory (Orientation.Horizontal));
-
-        ViewportChanged += (_, _) =>
-                           {
-                               if (_verticalScrollBar.IsValueCreated)
-                               {
-                                   _verticalScrollBar.Value.VisibleContentSize = Viewport.Height;
-                                   _verticalScrollBar.Value.Position = Viewport.Y;
-                               }
-
-                               if (_horizontalScrollBar.IsValueCreated)
-                               {
-                                   _horizontalScrollBar.Value.VisibleContentSize = Viewport.Width;
-                                   _horizontalScrollBar.Value.Position = Viewport.X;
-                               }
-                           };
-
-        ContentSizeChanged += (_, _) =>
-                              {
-                                  if (_verticalScrollBar.IsValueCreated)
-                                  {
-                                      _verticalScrollBar.Value.ScrollableContentSize = GetContentSize ().Height;
-                                  }
-
-                                  if (_horizontalScrollBar.IsValueCreated)
-                                  {
-                                      _horizontalScrollBar.Value.ScrollableContentSize = GetContentSize ().Width;
-                                  }
-                              };
+        _verticalScrollBar = new (() => CreateScrollBar (Orientation.Vertical));
+        _horizontalScrollBar = new (() => CreateScrollBar (Orientation.Horizontal));
     }
     }
 
 
-    private ScrollBar ScrollBarFactory (Orientation orientation)
+    private ScrollBar CreateScrollBar (Orientation orientation)
     {
     {
         var scrollBar = new ScrollBar
         var scrollBar = new ScrollBar
         {
         {
@@ -58,98 +30,108 @@ public partial class View
 
 
         if (orientation == Orientation.Vertical)
         if (orientation == Orientation.Vertical)
         {
         {
-            scrollBar.X = Pos.AnchorEnd ();
-
-            // Ensure the scrollbar's length accomodates for the opposite scrollbar's visibility
-            scrollBar.Height = Dim.Fill (
-                                         Dim.Func (
-                                                   () =>
-                                                   {
-                                                       if (_horizontalScrollBar.IsValueCreated)
-                                                       {
-                                                           return _horizontalScrollBar.Value.Visible ? 1 : 0;
-                                                       }
-
-                                                       return 0;
-                                                   }));
-            scrollBar.ScrollableContentSize = GetContentSize ().Height;
+            ConfigureVerticalScrollBar (scrollBar);
         }
         }
         else
         else
         {
         {
-            scrollBar.Y = Pos.AnchorEnd ();
+            ConfigureHorizontalScrollBar (scrollBar);
+        }
+
+        Padding?.Add (scrollBar);
+        scrollBar.Initialized += OnScrollBarInitialized;
+
+        return scrollBar;
+    }
+
+    private void ConfigureVerticalScrollBar (ScrollBar scrollBar)
+    {
+        scrollBar.X = Pos.AnchorEnd ();
+        scrollBar.Height = Dim.Fill (Dim.Func (() => _horizontalScrollBar is { IsValueCreated: true, Value.Visible: true } ? 1 : 0));
+        scrollBar.ScrollableContentSize = GetContentSize ().Height;
 
 
-            // Ensure the scrollbar's length accomodates for the opposite scrollbar's visibility
-            scrollBar.Width = Dim.Fill (
-                                        Dim.Func (
-                                                  () =>
-                                                  {
-                                                      if (_verticalScrollBar.IsValueCreated)
-                                                      {
-                                                          return _verticalScrollBar.Value.Visible ? 1 : 0;
-                                                      }
+        ViewportChanged += (_, _) =>
+        {
+            scrollBar.VisibleContentSize = Viewport.Height;
+            scrollBar.Position = Viewport.Y;
+        };
+
+        ContentSizeChanged += (_, _) =>
+        {
+            scrollBar.ScrollableContentSize = GetContentSize ().Height;
+        };
+    }
+
+    private void ConfigureHorizontalScrollBar (ScrollBar scrollBar)
+    {
+        scrollBar.Y = Pos.AnchorEnd ();
+        scrollBar.Width = Dim.Fill (Dim.Func (() => _verticalScrollBar is { IsValueCreated: true, Value.Visible: true } ? 1 : 0));
+        scrollBar.ScrollableContentSize = GetContentSize ().Width;
+
+        ViewportChanged += (_, _) =>
+        {
+            scrollBar.VisibleContentSize = Viewport.Width;
+            scrollBar.Position = Viewport.X;
+        };
 
 
-                                                      return 0;
-                                                  }));
+        ContentSizeChanged += (_, _) =>
+        {
             scrollBar.ScrollableContentSize = GetContentSize ().Width;
             scrollBar.ScrollableContentSize = GetContentSize ().Width;
+        };
+    }
+
+    private void OnScrollBarInitialized (object? sender, EventArgs e)
+    {
+        var scrollBar = (ScrollBar)sender!;
+        if (scrollBar.Orientation == Orientation.Vertical)
+        {
+            ConfigureVerticalScrollBarEvents (scrollBar);
         }
         }
+        else
+        {
+            ConfigureHorizontalScrollBarEvents (scrollBar);
+        }
+    }
 
 
-        Padding?.Add (scrollBar);
+    private void ConfigureVerticalScrollBarEvents (ScrollBar scrollBar)
+    {
+        Padding!.Thickness = Padding.Thickness with { Right = scrollBar.Visible ? Padding.Thickness.Right + 1 : 0 };
 
 
-        scrollBar.Initialized += OnScrollBarOnInitialized;
+        scrollBar.PositionChanged += (_, args) =>
+        {
+            Viewport = Viewport with
+            {
+                Y = Math.Min (args.CurrentValue, GetContentSize ().Height - Viewport.Height)
+            };
+        };
 
 
-        return scrollBar;
+        scrollBar.VisibleChanged += (_, _) =>
+        {
+            Padding.Thickness = Padding.Thickness with
+            {
+                Right = scrollBar.Visible ? Padding.Thickness.Right + 1 : Padding.Thickness.Right - 1
+            };
+        };
+    }
 
 
-        void OnScrollBarOnInitialized (object? o, EventArgs eventArgs)
+    private void ConfigureHorizontalScrollBarEvents (ScrollBar scrollBar)
+    {
+        Padding!.Thickness = Padding.Thickness with { Bottom = scrollBar.Visible ? Padding.Thickness.Bottom + 1 : 0 };
+
+        scrollBar.PositionChanged += (_, args) =>
         {
         {
-            if (orientation == Orientation.Vertical)
+            Viewport = Viewport with
             {
             {
-                Padding!.Thickness = Padding.Thickness with { Right = scrollBar.Visible ? Padding.Thickness.Right + 1 : 0 };
-
-                scrollBar.PositionChanged += (_, args) =>
-                                             {
-                                                 Viewport = Viewport with
-                                                 {
-                                                     Y = Math.Min (
-                                                                   args.CurrentValue,
-                                                                   GetContentSize ().Height - Viewport.Height)
-                                                 };
-                                             };
-
-                scrollBar.VisibleChanged += (_, _) =>
-                                            {
-                                                Padding.Thickness = Padding.Thickness with
-                                                {
-                                                    Right = scrollBar.Visible
-                                                                ? Padding.Thickness.Right + 1
-                                                                : Padding.Thickness.Right - 1
-                                                };
-                                            };
-            }
-            else
+                X = Math.Min (args.CurrentValue, GetContentSize ().Width - Viewport.Width)
+            };
+        };
+
+        scrollBar.VisibleChanged += (_, _) =>
+        {
+            Padding.Thickness = Padding.Thickness with
             {
             {
-                Padding!.Thickness = Padding.Thickness with { Bottom = scrollBar.Visible ? Padding.Thickness.Bottom + 1 : 0 };
-
-                scrollBar.PositionChanged += (_, args) =>
-                                             {
-                                                 Viewport = Viewport with
-                                                 {
-                                                     X = Math.Min (
-                                                                   args.CurrentValue,
-                                                                   GetContentSize ().Width - Viewport.Width)
-                                                 };
-                                             };
-
-                scrollBar.VisibleChanged += (_, _) =>
-                                            {
-                                                Padding.Thickness = Padding.Thickness with
-                                                {
-                                                    Bottom = scrollBar.Visible
-                                                                 ? Padding.Thickness.Bottom + 1
-                                                                 : Padding.Thickness.Bottom - 1
-                                                };
-                                            };
-            }
-        }
+                Bottom = scrollBar.Visible ? Padding.Thickness.Bottom + 1 : Padding.Thickness.Bottom - 1
+            };
+        };
     }
     }
 
 
     /// <summary>
     /// <summary>

+ 4 - 0
Terminal.Gui/Views/ScrollBar/ScrollBar.cs

@@ -16,6 +16,10 @@ namespace Terminal.Gui;
 /// </summary>
 /// </summary>
 /// <remarks>
 /// <remarks>
 ///     <para>
 ///     <para>
+///         ScrollBars can be enabled on any View calling <see cref="View.EnableScrollBar"/>. By default, the built-in View scrollbars have
+///         see <see cref="AutoHide"/> set to <see langword="true"/> thus will only be visible if the content size (see <see cref="View.SetContentSize"/> is
+///         larger than see <see cref="View.Viewport"/>
+///      
 ///     </para>
 ///     </para>
 ///     <para>
 ///     <para>
 ///         By default, this view cannot be focused and does not support keyboard input.
 ///         By default, this view cannot be focused and does not support keyboard input.

+ 14 - 53
Terminal.Gui/Views/ScrollBar/ScrollSlider.cs

@@ -39,27 +39,6 @@ public class ScrollSlider : View, IOrientation, IDesignable
         OnOrientationChanged (Orientation);
         OnOrientationChanged (Orientation);
 
 
         HighlightStyle = HighlightStyle.Hover;
         HighlightStyle = HighlightStyle.Hover;
-
-        FrameChanged += (sender, args) =>
-                        {
-                            //if (Orientation == Orientation.Vertical)
-                            //{
-                            //    Size = Frame.Height;
-                            //}
-                            //else
-                            //{
-                            //    Size = Frame.Width;
-                            //}
-                        };
-
-        SubviewLayout += (sender, args) =>
-                         {
-                         };
-
-        SubviewsLaidOut += (sender, args) =>
-                           {
-
-                           };
     }
     }
 
 
     #region IOrientation members
     #region IOrientation members
@@ -393,26 +372,8 @@ public class ScrollSlider : View, IOrientation, IDesignable
                 }
                 }
 
 
                 currentLocation -= SliderPadding / 2;
                 currentLocation -= SliderPadding / 2;
-
-                // location does not account for the ShrinkBy
-                int sliderLowerBound = SliderPadding / 2;
-                int sliderUpperBound = superViewDimension - SliderPadding / 2 - Size;
-
                 int newLocation = currentLocation + offsetFromLastLocation;
                 int newLocation = currentLocation + offsetFromLastLocation;
                 Position = newLocation;
                 Position = newLocation;
-
-                //if (location > 0 && location < sliderLowerBound)
-                //{
-                //    Position = 0;
-                //}
-                //else if (location > sliderUpperBound)
-                //{
-                //    Position = superViewDimension - Size;
-                //}
-                //else
-                //{
-                //    Position = currentLocation + offsetFromLastLocation;
-                //}
             }
             }
             else if (mouseEvent.Flags == MouseFlags.Button1Released)
             else if (mouseEvent.Flags == MouseFlags.Button1Released)
             {
             {
@@ -430,16 +391,6 @@ public class ScrollSlider : View, IOrientation, IDesignable
         return false;
         return false;
     }
     }
 
 
-    /// <inheritdoc/>
-    public bool EnableForDesign ()
-    {
-        //        Orientation = Orientation.Horizontal;
-        ShowPercent = true;
-        Size = 5;
-
-        return true;
-    }
-
     /// <summary>
     /// <summary>
     ///     Gets the slider size.
     ///     Gets the slider size.
     /// </summary>
     /// </summary>
@@ -470,14 +421,14 @@ public class ScrollSlider : View, IOrientation, IDesignable
     }
     }
 
 
     /// <summary>
     /// <summary>
-    ///     Gets the slider position.
+    ///     Calculates the slider position.
     /// </summary>
     /// </summary>
     /// <param name="scrollableContentSize">The size of the content.</param>
     /// <param name="scrollableContentSize">The size of the content.</param>
     /// <param name="visibleContentSize">The size of the visible content.</param>
     /// <param name="visibleContentSize">The size of the visible content.</param>
     /// <param name="contentPosition">The position in the content (between 0 and <paramref name="scrollableContentSize"/>).</param>
     /// <param name="contentPosition">The position in the content (between 0 and <paramref name="scrollableContentSize"/>).</param>
     /// <param name="sliderBounds">The bounds of the area the slider moves in (e.g. the size of the <see cref="ScrollBar"/> minus 2).</param>
     /// <param name="sliderBounds">The bounds of the area the slider moves in (e.g. the size of the <see cref="ScrollBar"/> minus 2).</param>
     /// <param name="direction">The direction the slider is moving.</param>
     /// <param name="direction">The direction the slider is moving.</param>
-    public static int CalculatePosition (
+    internal static int CalculatePosition (
         int scrollableContentSize,
         int scrollableContentSize,
         int visibleContentSize,
         int visibleContentSize,
         int contentPosition,
         int contentPosition,
@@ -498,13 +449,13 @@ public class ScrollSlider : View, IOrientation, IDesignable
     }
     }
 
 
     /// <summary>
     /// <summary>
-    ///     Gets the content position.
+    ///     Calculates the content position.
     /// </summary>
     /// </summary>
     /// <param name="scrollableContentSize">The size of the content.</param>
     /// <param name="scrollableContentSize">The size of the content.</param>
     /// <param name="visibleContentSize">The size of the visible content.</param>
     /// <param name="visibleContentSize">The size of the visible content.</param>
     /// <param name="sliderPosition">The position of the slider.</param>
     /// <param name="sliderPosition">The position of the slider.</param>
     /// <param name="sliderBounds">The bounds of the area the slider moves in (e.g. the size of the <see cref="ScrollBar"/> minus 2).</param>
     /// <param name="sliderBounds">The bounds of the area the slider moves in (e.g. the size of the <see cref="ScrollBar"/> minus 2).</param>
-    public static int CalculateContentPosition (
+    internal static int CalculateContentPosition (
         int scrollableContentSize,
         int scrollableContentSize,
         int visibleContentSize,
         int visibleContentSize,
         int sliderPosition,
         int sliderPosition,
@@ -523,4 +474,14 @@ public class ScrollSlider : View, IOrientation, IDesignable
 
 
         return (int)Math.Clamp (rounded, 0, Math.Max (0, scrollableContentSize - sliderSize));
         return (int)Math.Clamp (rounded, 0, Math.Max (0, scrollableContentSize - sliderSize));
     }
     }
+
+    /// <inheritdoc/>
+    public bool EnableForDesign ()
+    {
+        ShowPercent = true;
+        Size = 5;
+
+        return true;
+    }
+
 }
 }