Przeglądaj źródła

Refactored View.ScrollBar

Tig 8 miesięcy temu
rodzic
commit
ddce5a4f78

+ 133 - 128
Terminal.Gui/View/View.ScrollBars.cs

@@ -11,145 +11,145 @@ public partial class View
     /// </summary>
     private void SetupScrollBars ()
     {
-        _horizontalScrollBar = new Lazy<ScrollBar> (
-                                                    () =>
-                                                    {
-                                                        var scrollBar = new ScrollBar
-                                                        {
-                                                            Orientation = Orientation.Horizontal,
-                                                            X = 0,
-                                                            Y = Pos.AnchorEnd (),
-                                                            Width = Dim.Fill (
-                                                                              Dim.Func (
-                                                                                        () =>
-                                                                                        {
-                                                                                            if (_verticalScrollBar.IsValueCreated)
-                                                                                            {
-                                                                                                return _verticalScrollBar.Value.Visible ? 1 : 0;
-                                                                                            }
-
-                                                                                            return 0;
-                                                                                        })),
-                                                            ScrollableContentSize = GetContentSize ().Width,
-                                                            Visible = false
-                                                        };
-
-                                                        Padding?.Add (scrollBar);
-
-                                                        scrollBar.Initialized += (_, _) =>
-                                                                                 {
-                                                                                     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
-                                                                                                                     };
-                                                                                                                 };
-                                                                                 };
-
-                                                        return scrollBar;
-                                                    });
-
-        _verticalScrollBar = new Lazy<ScrollBar> (
+        if (this is Adornment)
+        {
+            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;
+                                  }
+                              };
+    }
+
+    private ScrollBar ScrollBarFactory (Orientation orientation)
+    {
+        var scrollBar = new ScrollBar
+        {
+            Orientation = orientation,
+            AutoHide = true
+        };
+
+        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;
+        }
+        else
+        {
+            scrollBar.Y = Pos.AnchorEnd ();
+
+            // Ensure the scrollbar's length accomodates for the opposite scrollbar's visibility
+            scrollBar.Width = Dim.Fill (
+                                        Dim.Func (
                                                   () =>
                                                   {
-                                                      var scrollBar = new ScrollBar
+                                                      if (_verticalScrollBar.IsValueCreated)
                                                       {
-                                                          Orientation = Orientation.Vertical,
-                                                          X = Pos.AnchorEnd (),
-                                                          Y = Pos.Func (() => Padding.Thickness.Top),
-                                                          Height = Dim.Fill (
-                                                                             Dim.Func (
-                                                                                       () =>
-                                                                                       {
-                                                                                           if (_horizontalScrollBar.IsValueCreated)
-                                                                                           {
-                                                                                               return _horizontalScrollBar.Value.Visible ? 1 : 0;
-                                                                                           }
-
-                                                                                           return 0;
-                                                                                       })),
-                                                          ScrollableContentSize = GetContentSize ().Height,
-                                                          Visible = false
-                                                      };
-
-                                                      Padding?.Add (scrollBar);
-
-                                                      scrollBar.Initialized += (_, _) =>
-                                                                               {
-                                                                                   if (Padding is { })
-                                                                                   {
-                                                                                       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 - 1))
-                                                                                                                        };
-                                                                                                                    };
-
-                                                                                       scrollBar.VisibleChanged += (_, _) =>
-                                                                                                                   {
-                                                                                                                       Padding.Thickness = Padding.Thickness with
-                                                                                                                       {
-                                                                                                                           Right = scrollBar.Visible
-                                                                                                                                       ? Padding.Thickness.Right + 1
-                                                                                                                                       : Padding.Thickness.Right - 1
-                                                                                                                       };
-                                                                                                                   };
-                                                                                   }
-                                                                               };
-
-                                                      return scrollBar;
-                                                  });
+                                                          return _verticalScrollBar.Value.Visible ? 1 : 0;
+                                                      }
 
-        ViewportChanged += (_, _) =>
-        {
-            if (_verticalScrollBar.IsValueCreated)
-            {
-                _verticalScrollBar.Value.VisibleContentSize = Viewport.Height;
-                _verticalScrollBar.Value.Position = Viewport.Y;
-            }
+                                                      return 0;
+                                                  }));
+            scrollBar.ScrollableContentSize = GetContentSize ().Width;
+        }
 
-            if (_horizontalScrollBar.IsValueCreated)
-            {
-                _horizontalScrollBar.Value.VisibleContentSize = Viewport.Width;
-                _horizontalScrollBar.Value.Position = Viewport.X;
-            }
-        };
+        Padding?.Add (scrollBar);
 
-        ContentSizeChanged += (_, _) =>
+        scrollBar.Initialized += OnScrollBarOnInitialized;
+
+        return scrollBar;
+
+        void OnScrollBarOnInitialized (object? o, EventArgs eventArgs)
         {
-            if (_verticalScrollBar.IsValueCreated)
+            if (orientation == Orientation.Vertical)
             {
-                _verticalScrollBar.Value.ScrollableContentSize = GetContentSize ().Height;
+                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
+                                                };
+                                            };
             }
-            if (_horizontalScrollBar.IsValueCreated)
+            else
             {
-                _horizontalScrollBar.Value.ScrollableContentSize = GetContentSize ().Width;
+                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
+                                                };
+                                            };
             }
-        };
+        }
     }
 
     /// <summary>
@@ -165,6 +165,11 @@ public partial class View
     /// </summary>
     private void DisposeScrollBars ()
     {
+        if (this is Adornment)
+        {
+            return;
+        }
+
         if (_horizontalScrollBar.IsValueCreated)
         {
             Padding?.Remove (_horizontalScrollBar.Value);

+ 0 - 1
Terminal.Gui/View/View.cs

@@ -150,7 +150,6 @@ public partial class View : IDisposable, ISupportInitializeNotification
         //SetupMouse ();
 
         SetupText ();
-
         SetupScrollBars ();
     }
 

+ 1 - 8
Terminal.Gui/Views/ScrollBar/ScrollBar.cs

@@ -90,14 +90,7 @@ public class ScrollBar : View, IOrientation, IDesignable
 
     private void ShowHide ()
     {
-        if (Orientation == Orientation.Vertical)
-        {
-            Visible = VisibleContentSize < ScrollableContentSize;
-        }
-        else
-        {
-            Visible = VisibleContentSize < ScrollableContentSize;
-        }
+        Visible = VisibleContentSize < ScrollableContentSize;
 
         if (!AutoHide)
         {