From ddce5a4f780b65e16be18a84daf28fe73c6cc09a Mon Sep 17 00:00:00 2001 From: Tig Date: Sat, 16 Nov 2024 09:32:07 -0700 Subject: [PATCH] Refactored View.ScrollBar --- Terminal.Gui/View/View.ScrollBars.cs | 267 +++++++++++----------- Terminal.Gui/View/View.cs | 1 - Terminal.Gui/Views/ScrollBar/ScrollBar.cs | 9 +- 3 files changed, 137 insertions(+), 140 deletions(-) diff --git a/Terminal.Gui/View/View.ScrollBars.cs b/Terminal.Gui/View/View.ScrollBars.cs index 8dcfe8d92..39a254443 100644 --- a/Terminal.Gui/View/View.ScrollBars.cs +++ b/Terminal.Gui/View/View.ScrollBars.cs @@ -11,145 +11,145 @@ public partial class View /// private void SetupScrollBars () { - _horizontalScrollBar = new Lazy ( - () => - { - 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; - } + if (this is Adornment) + { + return; + } - 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 ( - () => - { - var scrollBar = new ScrollBar - { - 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; - }); + _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 (_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; - } - }; + 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 { - if (_verticalScrollBar.IsValueCreated) - { - _verticalScrollBar.Value.ScrollableContentSize = GetContentSize ().Height; - } - if (_horizontalScrollBar.IsValueCreated) - { - _horizontalScrollBar.Value.ScrollableContentSize = GetContentSize ().Width; - } + 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 ( + () => + { + if (_verticalScrollBar.IsValueCreated) + { + return _verticalScrollBar.Value.Visible ? 1 : 0; + } + + return 0; + })); + scrollBar.ScrollableContentSize = GetContentSize ().Width; + } + + Padding?.Add (scrollBar); + + scrollBar.Initialized += OnScrollBarOnInitialized; + + return scrollBar; + + void OnScrollBarOnInitialized (object? o, EventArgs eventArgs) + { + if (orientation == Orientation.Vertical) + { + 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 + { + 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 + }; + }; + } + } } /// @@ -165,6 +165,11 @@ public partial class View /// private void DisposeScrollBars () { + if (this is Adornment) + { + return; + } + if (_horizontalScrollBar.IsValueCreated) { Padding?.Remove (_horizontalScrollBar.Value); diff --git a/Terminal.Gui/View/View.cs b/Terminal.Gui/View/View.cs index 7af099f79..d36a358ed 100644 --- a/Terminal.Gui/View/View.cs +++ b/Terminal.Gui/View/View.cs @@ -150,7 +150,6 @@ public partial class View : IDisposable, ISupportInitializeNotification //SetupMouse (); SetupText (); - SetupScrollBars (); } diff --git a/Terminal.Gui/Views/ScrollBar/ScrollBar.cs b/Terminal.Gui/Views/ScrollBar/ScrollBar.cs index 93f079d9c..9d73c0ab6 100644 --- a/Terminal.Gui/Views/ScrollBar/ScrollBar.cs +++ b/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) {