Added AllowNegativeXWhenWidthGreaterThanContentWidth

This commit is contained in:
Tig
2024-11-18 14:20:19 -07:00
parent 2f2076ddb7
commit 7777668d11
6 changed files with 110 additions and 32 deletions

View File

@@ -348,6 +348,14 @@ public partial class View
}
}
if (!ViewportSettings.HasFlag (ViewportSettings.AllowNegativeXWhenWidthGreaterThanContentWidth))
{
if (Viewport.Width > GetContentSize ().Width)
{
newViewport.X = 0;
}
}
if (!ViewportSettings.HasFlag (ViewportSettings.AllowYGreaterThanContentHeight))
{
if (newViewport.Y >= GetContentSize ().Height)
@@ -356,6 +364,14 @@ public partial class View
}
}
if (!ViewportSettings.HasFlag (ViewportSettings.AllowNegativeYWhenHeightGreaterThanContentHeight))
{
if (Viewport.Height > GetContentSize ().Height)
{
newViewport.Y = 0;
}
}
// IMPORTANT: Check for negative location AFTER checking for location greater than content width
if (!ViewportSettings.HasFlag (ViewportSettings.AllowNegativeY))
{

View File

@@ -76,7 +76,7 @@ public enum ViewportSettings
AllowYGreaterThanContentHeight = 8,
/// <summary>
/// If set, <see cref="View.Viewport"/><c>.Size</c> can be set values greater than <see cref="View.GetContentSize ()"/>
/// If set, <see cref="View.Viewport"/><c>.Location</c> can be set values greater than <see cref="View.GetContentSize ()"/>
/// enabling scrolling beyond the bottom-right
/// of the content area.
/// <para>
@@ -87,11 +87,42 @@ public enum ViewportSettings
/// </summary>
AllowLocationGreaterThanContentSize = AllowXGreaterThanContentWidth | AllowYGreaterThanContentHeight,
/// <summary>
/// If set and <see cref="View.Viewport"/><c>.Width</c> is greater than <see cref="View.GetContentSize ()"/>
/// <c>.Width</c> <see cref="View.Viewport"/><c>.X</c> can be negative.
/// <para>
/// When not set, <see cref="View.Viewport"/><c>.X</c> will be constrained to non-negative values, preventing
/// scrolling beyond the left of the Viewport.
/// </para>
/// <para>
/// This can be useful in infinite scrolling scenarios.
/// </para>
/// </summary>
AllowNegativeXWhenWidthGreaterThanContentWidth = 16,
/// <summary>
/// If set and <see cref="View.Viewport"/><c>.Height</c> is greater than <see cref="View.GetContentSize ()"/>
/// <c>.Height</c> <see cref="View.Viewport"/><c>.Y</c> can be negative.
/// <para>
/// When not set, <see cref="View.Viewport"/><c>.Y</c> will be constrained to non-negative values, preventing
/// scrolling above the top of the Viewport.
/// </para>
/// <para>
/// This can be useful in infinite scrolling scenarios.
/// </para>
/// </summary>
AllowNegativeYWhenHeightGreaterThanContentHeight = 32,
/// <summary>
/// The combination of <see cref="AllowNegativeXWhenWidthGreaterThanContentWidth"/> and <see cref="AllowNegativeYWhenHeightGreaterThanContentHeight"/>.
/// </summary>
AllowNegativeLocationWhenSizeGreaterThanContentSize = AllowNegativeXWhenWidthGreaterThanContentWidth | AllowNegativeYWhenHeightGreaterThanContentHeight,
/// <summary>
/// By default, clipping is applied to the <see cref="View.Viewport"/>. Setting this flag will cause clipping to be
/// applied to the visible content area.
/// </summary>
ClipContentOnly = 16,
ClipContentOnly = 64,
/// <summary>
/// If set <see cref="View.ClearViewport"/> will clear only the portion of the content
@@ -100,5 +131,5 @@ public enum ViewportSettings
/// <see cref="ClipContentOnly"/> must be set for this setting to work (clipping beyond the visible area must be
/// disabled).
/// </summary>
ClearContentOnly = 32,
ClearContentOnly = 128,
}

View File

@@ -17,7 +17,8 @@ namespace Terminal.Gui;
/// See the <see href="https://gui-cs.github.io/Terminal.GuiV2Docs/docs/scrolling.html">Scrolling Deep Dive</see>.
/// </para>
/// <para>
/// By default, the built-in View scrollbars (<see cref="View.VerticalScrollBar"/>/<see cref="View.HorizontalScrollBar"/>) have both <see cref="View.Visible"/> and <see cref="AutoShow"/> set to
/// By default, the built-in View scrollbars (<see cref="View.VerticalScrollBar"/>/
/// <see cref="View.HorizontalScrollBar"/>) have both <see cref="View.Visible"/> and <see cref="AutoShow"/> set to
/// <see langword="false"/>.
/// To enable them, either set <see cref="AutoShow"/> set to <see langword="true"/> or explicitly set
/// <see cref="View.Visible"/>
@@ -184,7 +185,8 @@ public class ScrollBar : View, IOrientation, IDesignable
#endregion
/// <summary>
/// Gets or sets the amount each mouse wheel event, or click on the increment/decrement buttons, will incremenet/decrement the <see cref="Position"/>.
/// Gets or sets the amount each mouse wheel event, or click on the increment/decrement buttons, will
/// incremenet/decrement the <see cref="Position"/>.
/// </summary>
/// <remarks>
/// The default is 1.

View File

@@ -1,8 +1,6 @@
#nullable enable
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
namespace Terminal.Gui;
@@ -13,11 +11,6 @@ namespace Terminal.Gui;
/// </summary>
/// <remarks>
/// <para>
/// If <see cref="View.Text"/> is set, it will be displayed centered within the slider. Set
/// <see cref="ShowPercent"/> to automatically have the Text
/// be show what percent the slider is to the Superview's Viewport size.
/// </para>
/// <para>
/// Used to represent the proportion of the visible content to the Viewport in a <see cref="Scrolled"/>.
/// </para>
/// </remarks>
@@ -105,7 +98,7 @@ public class ScrollSlider : View, IOrientation, IDesignable
/// <summary>
/// Gets or sets the size of the ScrollSlider. This is a helper that gets or sets Width or Height depending
/// on <see cref="Orientation"/>. The size will be clamped between 1 and the dimension of
/// the <see cref="View.SuperView"/>'s Viewport.
/// the <see cref="View.SuperView"/>'s Viewport.
/// </summary>
/// <remarks>
/// <para>

View File

@@ -134,7 +134,6 @@ public class CharMap : View, IDesignable
KeyBindings.Add (Key.End, Command.End);
MouseClick += Handle_MouseClick;
MouseEvent += Handle_MouseEvent;
SetContentSize (new (COLUMN_WIDTH * 16 + RowLabelWidth, MAX_CODE_POINT / 16 * _rowHeight + HEADER_HEIGHT));
@@ -468,39 +467,36 @@ public class CharMap : View, IDesignable
// TODO: Use this to demonstrate using a popover to show glyph info on hover
public event EventHandler<ListViewItemEventArgs>? Hover;
private void Handle_MouseEvent (object? sender, MouseEventArgs e)
/// <inheritdoc />
protected override bool OnMouseEvent (MouseEventArgs mouseEvent)
{
if (e.Flags == MouseFlags.WheeledDown)
if (mouseEvent.Flags == MouseFlags.WheeledDown)
{
ScrollVertical (1);
e.Handled = true;
return;
return mouseEvent.Handled = true;
}
if (e.Flags == MouseFlags.WheeledUp)
if (mouseEvent.Flags == MouseFlags.WheeledUp)
{
ScrollVertical (-1);
e.Handled = true;
return;
return mouseEvent.Handled = true;
}
if (e.Flags == MouseFlags.WheeledRight)
if (mouseEvent.Flags == MouseFlags.WheeledRight)
{
ScrollHorizontal (1);
e.Handled = true;
return;
return mouseEvent.Handled = true;
}
if (e.Flags == MouseFlags.WheeledLeft)
if (mouseEvent.Flags == MouseFlags.WheeledLeft)
{
ScrollHorizontal (-1);
e.Handled = true;
return mouseEvent.Handled = true;
}
}
return false;
}
private void Handle_MouseClick (object? sender, MouseEventArgs me)
{
if (me.Flags != MouseFlags.ReportMousePosition && me.Flags != MouseFlags.Button1Clicked && me.Flags != MouseFlags.Button1DoubleClicked)

View File

@@ -29,7 +29,6 @@ public class Scrolling : Scenario
Width = 60,
Height = 20
};
demoView.SetContentSize (new (80, 25));
label.Text =
$"{demoView}\nContentSize: {demoView.GetContentSize ()}\nViewport.Location: {demoView.Viewport.Location}";
@@ -149,6 +148,8 @@ public class DemoView : View
private void OnInitialized (object sender, EventArgs e)
{
SetContentSize (new (80, 25));
var rulerView = new View
{
Height = Dim.Fill (),
@@ -161,6 +162,16 @@ public class DemoView : View
Add (rulerView);
var centeredLabel = new Label ()
{
X = Pos.Center (),
Y = Pos.Center (),
TextAlignment = Alignment.Center,
VerticalTextAlignment = Alignment.Center,
Text = $"This label is centred.\nContentSize is {GetContentSize ()}"
};
Add (centeredLabel);
var pressMeButton = new Button
{
X = 1,
@@ -209,7 +220,7 @@ public class DemoView : View
Y = 40,
Width = 50,
ColorScheme = Colors.ColorSchemes ["Error"],
Text = "Last line"
Text = "Last line - Beyond content area @ Y = 40"
}
);
@@ -229,4 +240,33 @@ public class DemoView : View
};
Add (anchorButton);
}
protected override bool OnMouseEvent (MouseEventArgs mouseEvent)
{
if (mouseEvent.Flags == MouseFlags.WheeledDown)
{
ScrollVertical (1);
return mouseEvent.Handled = true;
}
if (mouseEvent.Flags == MouseFlags.WheeledUp)
{
ScrollVertical (-1);
return mouseEvent.Handled = true;
}
if (mouseEvent.Flags == MouseFlags.WheeledRight)
{
ScrollHorizontal (1);
return mouseEvent.Handled = true;
}
if (mouseEvent.Flags == MouseFlags.WheeledLeft)
{
ScrollHorizontal (-1);
return mouseEvent.Handled = true;
}
return false;
}
}