From 861dfee008604f9ba9da96ab4e26755f68420409 Mon Sep 17 00:00:00 2001 From: Tig Date: Sat, 16 Nov 2024 15:08:10 -0700 Subject: [PATCH] Code cleanup --- Terminal.Gui/View/View.ScrollBars.cs | 200 +++++++++---------- Terminal.Gui/Views/ScrollBar/ScrollBar.cs | 4 + Terminal.Gui/Views/ScrollBar/ScrollSlider.cs | 67 ++----- 3 files changed, 109 insertions(+), 162 deletions(-) diff --git a/Terminal.Gui/View/View.ScrollBars.cs b/Terminal.Gui/View/View.ScrollBars.cs index a0563aafb..6aa7e5f65 100644 --- a/Terminal.Gui/View/View.ScrollBars.cs +++ b/Terminal.Gui/View/View.ScrollBars.cs @@ -7,7 +7,7 @@ public partial class View private Lazy _verticalScrollBar; /// - /// Initializes the ScrollBars of the View. Called by the constructor. + /// Initializes the ScrollBars of the View. Called by the View constructor. /// private void SetupScrollBars () { @@ -16,39 +16,11 @@ public partial class View 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 { @@ -58,98 +30,108 @@ public partial class View 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 { - 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; + ConfigureHorizontalScrollBar (scrollBar); } Padding?.Add (scrollBar); - - scrollBar.Initialized += OnScrollBarOnInitialized; + scrollBar.Initialized += OnScrollBarInitialized; return scrollBar; + } - void OnScrollBarOnInitialized (object? o, EventArgs eventArgs) + 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; + + ViewportChanged += (_, _) => { - if (orientation == Orientation.Vertical) - { - Padding!.Thickness = Padding.Thickness with { Right = scrollBar.Visible ? Padding.Thickness.Right + 1 : 0 }; + scrollBar.VisibleContentSize = Viewport.Height; + scrollBar.Position = Viewport.Y; + }; - scrollBar.PositionChanged += (_, args) => - { - Viewport = Viewport with - { - Y = Math.Min ( - args.CurrentValue, - GetContentSize ().Height - Viewport.Height) - }; - }; + ContentSizeChanged += (_, _) => + { + scrollBar.ScrollableContentSize = GetContentSize ().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 }; + 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; - scrollBar.PositionChanged += (_, args) => - { - Viewport = Viewport with - { - X = Math.Min ( - args.CurrentValue, - GetContentSize ().Width - Viewport.Width) - }; - }; + ViewportChanged += (_, _) => + { + scrollBar.VisibleContentSize = Viewport.Width; + scrollBar.Position = Viewport.X; + }; - scrollBar.VisibleChanged += (_, _) => - { - Padding.Thickness = Padding.Thickness with - { - Bottom = scrollBar.Visible - ? Padding.Thickness.Bottom + 1 - : Padding.Thickness.Bottom - 1 - }; - }; - } + ContentSizeChanged += (_, _) => + { + 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); + } + } + + private void ConfigureVerticalScrollBarEvents (ScrollBar scrollBar) + { + 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 + }; + }; + } + + private void ConfigureHorizontalScrollBarEvents (ScrollBar scrollBar) + { + 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 + }; + }; } /// diff --git a/Terminal.Gui/Views/ScrollBar/ScrollBar.cs b/Terminal.Gui/Views/ScrollBar/ScrollBar.cs index ec0802c41..c73726048 100644 --- a/Terminal.Gui/Views/ScrollBar/ScrollBar.cs +++ b/Terminal.Gui/Views/ScrollBar/ScrollBar.cs @@ -16,6 +16,10 @@ namespace Terminal.Gui; /// /// /// +/// ScrollBars can be enabled on any View calling . By default, the built-in View scrollbars have +/// see set to thus will only be visible if the content size (see is +/// larger than see +/// /// /// /// By default, this view cannot be focused and does not support keyboard input. diff --git a/Terminal.Gui/Views/ScrollBar/ScrollSlider.cs b/Terminal.Gui/Views/ScrollBar/ScrollSlider.cs index a18ff0d87..0456ac09d 100644 --- a/Terminal.Gui/Views/ScrollBar/ScrollSlider.cs +++ b/Terminal.Gui/Views/ScrollBar/ScrollSlider.cs @@ -39,27 +39,6 @@ public class ScrollSlider : View, IOrientation, IDesignable OnOrientationChanged (Orientation); 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 @@ -393,26 +372,8 @@ public class ScrollSlider : View, IOrientation, IDesignable } currentLocation -= SliderPadding / 2; - - // location does not account for the ShrinkBy - int sliderLowerBound = SliderPadding / 2; - int sliderUpperBound = superViewDimension - SliderPadding / 2 - Size; - int newLocation = currentLocation + offsetFromLastLocation; 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) { @@ -430,16 +391,6 @@ public class ScrollSlider : View, IOrientation, IDesignable return false; } - /// - public bool EnableForDesign () - { - // Orientation = Orientation.Horizontal; - ShowPercent = true; - Size = 5; - - return true; - } - /// /// Gets the slider size. /// @@ -470,14 +421,14 @@ public class ScrollSlider : View, IOrientation, IDesignable } /// - /// Gets the slider position. + /// Calculates the slider position. /// /// The size of the content. /// The size of the visible content. /// The position in the content (between 0 and ). /// The bounds of the area the slider moves in (e.g. the size of the minus 2). /// The direction the slider is moving. - public static int CalculatePosition ( + internal static int CalculatePosition ( int scrollableContentSize, int visibleContentSize, int contentPosition, @@ -498,13 +449,13 @@ public class ScrollSlider : View, IOrientation, IDesignable } /// - /// Gets the content position. + /// Calculates the content position. /// /// The size of the content. /// The size of the visible content. /// The position of the slider. /// The bounds of the area the slider moves in (e.g. the size of the minus 2). - public static int CalculateContentPosition ( + internal static int CalculateContentPosition ( int scrollableContentSize, int visibleContentSize, int sliderPosition, @@ -523,4 +474,14 @@ public class ScrollSlider : View, IOrientation, IDesignable return (int)Math.Clamp (rounded, 0, Math.Max (0, scrollableContentSize - sliderSize)); } + + /// + public bool EnableForDesign () + { + ShowPercent = true; + Size = 5; + + return true; + } + }