Code cleanup

This commit is contained in:
Tig
2024-11-16 15:08:10 -07:00
parent 0313e8fe8c
commit 861dfee008
3 changed files with 109 additions and 162 deletions

View File

@@ -7,7 +7,7 @@ public partial class View
private Lazy<ScrollBar> _verticalScrollBar;
/// <summary>
/// Initializes the ScrollBars of the View. Called by the constructor.
/// Initializes the ScrollBars of the View. Called by the View constructor.
/// </summary>
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
};
};
}
/// <summary>

View File

@@ -16,6 +16,10 @@ namespace Terminal.Gui;
/// </summary>
/// <remarks>
/// <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>
/// By default, this view cannot be focused and does not support keyboard input.

View File

@@ -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;
}
/// <inheritdoc/>
public bool EnableForDesign ()
{
// Orientation = Orientation.Horizontal;
ShowPercent = true;
Size = 5;
return true;
}
/// <summary>
/// Gets the slider size.
/// </summary>
@@ -470,14 +421,14 @@ public class ScrollSlider : View, IOrientation, IDesignable
}
/// <summary>
/// Gets the slider position.
/// Calculates the slider position.
/// </summary>
/// <param name="scrollableContentSize">The size of the 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="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>
public static int CalculatePosition (
internal static int CalculatePosition (
int scrollableContentSize,
int visibleContentSize,
int contentPosition,
@@ -498,13 +449,13 @@ public class ScrollSlider : View, IOrientation, IDesignable
}
/// <summary>
/// Gets the content position.
/// Calculates the content position.
/// </summary>
/// <param name="scrollableContentSize">The size of the content.</param>
/// <param name="visibleContentSize">The size of the visible content.</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>
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));
}
/// <inheritdoc/>
public bool EnableForDesign ()
{
ShowPercent = true;
Size = 5;
return true;
}
}