using System.Diagnostics;
namespace Terminal.Gui;
///
/// Settings for how the behaves relative to the View's Content area.
///
[Flags]
public enum ViewportSettings
{
///
/// No settings.
///
None = 0,
///
/// If set, .X can be set to negative values enabling scrolling beyond the left of
/// the
/// content area.
///
///
///
/// When not set, .X is constrained to positive values.
///
///
AllowNegativeX = 1,
///
/// If set, .Y can be set to negative values enabling scrolling beyond the top of the
/// content area.
///
///
///
/// When not set, .Y is constrained to positive values.
///
///
AllowNegativeY = 2,
///
/// If set, .Size can be set to negative coordinates enabling scrolling beyond the
/// top-left of the
/// content area.
///
///
///
/// When not set, .Size is constrained to positive coordinates.
///
///
AllowNegativeLocation = AllowNegativeX | AllowNegativeY,
///
/// If set, .X can be set values greater than
/// .Width enabling scrolling beyond the right
/// of the content area.
///
///
///
/// When not set, .X is constrained to
/// .Width - 1.
/// This means the last column of the content will remain visible even if there is an attempt to scroll the
/// Viewport past the last column.
///
///
/// The practical effect of this is that the last column of the content will always be visible.
///
///
AllowXGreaterThanContentWidth = 4,
///
/// If set, .Y can be set values greater than
/// .Height enabling scrolling beyond the right
/// of the content area.
///
///
///
/// When not set, .Y is constrained to
/// .Height - 1.
/// This means the last row of the content will remain visible even if there is an attempt to scroll the Viewport
/// past the last row.
///
///
/// The practical effect of this is that the last row of the content will always be visible.
///
///
AllowYGreaterThanContentHeight = 8,
///
/// If set, .Size can be set values greater than
/// enabling scrolling beyond the bottom-right
/// of the content area.
///
///
///
/// When not set, is constrained to -1.
/// This means the last column and row of the content will remain visible even if there is an attempt to
/// scroll the Viewport past the last column or row.
///
///
AllowLocationGreaterThanContentSize = AllowXGreaterThanContentWidth | AllowYGreaterThanContentHeight,
///
/// By default, clipping is applied to the . Setting this flag will cause clipping to be
/// applied to the visible content area.
///
ClipContentOnly = 16,
///
/// If set will clear only the portion of the content
/// area that is visible within the . This is useful for views that have a
/// content area larger than the Viewport and want the area outside the content to be visually distinct.
///
///
/// must be set for this setting to work (clipping beyond the visible area must be
/// disabled).
///
ClearContentOnly = 32
}
public partial class View
{
#region Content Area
private Size _contentSize;
///
/// Gets or sets the size of the View's content. If not set, the value will be the same as the size of ,
/// and Viewport.Location will always be 0, 0.
///
///
///
/// If a positive size is provided, describes the portion of the content currently visible
/// to the view. This enables virtual scrolling.
///
///
/// Negative sizes are not supported.
///
///
public Size ContentSize
{
get => _contentSize == Size.Empty ? Viewport.Size : _contentSize;
set
{
if (value.Width < 0 || value.Height < 0)
{
throw new ArgumentException (@"ContentSize cannot be negative.", nameof (value));
}
if (value == _contentSize)
{
return;
}
_contentSize = value;
OnContentSizeChanged (new (_contentSize));
}
}
///
/// Called when changes. Invokes the event.
///
///
///
protected bool? OnContentSizeChanged (SizeChangedEventArgs e)
{
ContentSizeChanged?.Invoke (this, e);
if (e.Cancel != true)
{
SetNeedsLayout ();
SetNeedsDisplay ();
}
return e.Cancel;
}
///
/// Event raised when the changes.
///
public event EventHandler ContentSizeChanged;
///
/// Converts a Content-relative location to a Screen-relative location.
///
/// The Content-relative location.
/// The Screen-relative location.
public Point ContentToScreen (in Point location)
{
// Subtract the ViewportOffsetFromFrame to get the Viewport-relative location.
Point viewportOffset = GetViewportOffsetFromFrame ();
Point contentRelativeToViewport = location;
contentRelativeToViewport.Offset (-Viewport.X, -Viewport.Y);
// Translate to Screen-Relative (our SuperView's Viewport-relative coordinates)
Rectangle screen = ViewportToScreen (new (contentRelativeToViewport, Size.Empty));
return screen.Location;
}
/// Converts a Screen-relative coordinate to a Content-relative coordinate.
///
/// Content-relative means relative to the top-left corner of the view's Content, which is
/// always at 0, 0.
///
/// The Screen-relative location.
/// The coordinate relative to this view's Content.
public Point ScreenToContent (in Point location)
{
Point viewportOffset = GetViewportOffsetFromFrame ();
Point screen = ScreenToFrame (location.X, location.Y);
screen.Offset (Viewport.X - viewportOffset.X, Viewport.Y - viewportOffset.Y);
return screen;
}
#endregion Content Area
#region Viewport
private ViewportSettings _viewportSettings;
///
/// Gets or sets how scrolling the on the View's Content Area is handled.
///
public ViewportSettings ViewportSettings
{
get => _viewportSettings;
set
{
if (_viewportSettings == value)
{
return;
}
_viewportSettings = value;
if (IsInitialized)
{
// Force set Viewport to cause settings to be applied as needed
SetViewport (Viewport);
}
}
}
///
/// The location of the viewport into the view's content (0,0) is the top-left corner of the content. The Content
/// area's size
/// is .
///
private Point _viewportLocation;
///
/// Gets or sets the rectangle describing the portion of the View's content that is visible to the user.
/// The viewport Location is relative to the top-left corner of the inner rectangle of .
/// If the viewport Size is the same as the Location will be 0, 0.
///
///
/// The rectangle describing the location and size of the viewport into the View's virtual content, described by
/// .
///
///
///
/// Positive values for the location indicate the visible area is offset into (down-and-right) the View's virtual
/// . This enables scrolling down and to the right (e.g. in a .
///
///
/// Negative values for the location indicate the visible area is offset above (up-and-left) the View's virtual
/// . This enables scrolling up and to the left (e.g. in an image viewer that supports zoom
/// where the image stays centered).
///
///
/// The property controls how scrolling is handled.
///
///
/// If is the value of Viewport is indeterminate until
/// the view has been initialized ( is true) and has been
/// called.
///
///
/// Updates to the Viewport Size updates , and has the same impact as updating the
/// .
///
///
/// Altering the Viewport Size will eventually (when the view is next laid out) cause the
/// and methods to be called.
///
///
public virtual Rectangle Viewport
{
get
{
#if DEBUG
if ((_width.ReferencesOtherViews () || _height.ReferencesOtherViews ()) && !IsInitialized)
{
Debug.WriteLine (
$"WARNING: The dimensions of {this} are dependent on other views and Viewport is being accessed before the View has been initialized. This is likely a bug."
);
}
#endif // DEBUG
if (Margin is null || Border is null || Padding is null)
{
// CreateAdornments has not been called yet.
return new (_viewportLocation, Frame.Size);
}
Thickness thickness = GetAdornmentsThickness ();
if (Frame.Size == Size.Empty)
{
// The Frame has not been set yet (e.g. the view has not been added to a SuperView yet).
//
if ((Width is Dim.DimAuto widthAuto && widthAuto._style != Dim.DimAutoStyle.Subviews)
|| (Height is Dim.DimAuto heightAuto && heightAuto._style != Dim.DimAutoStyle.Subviews))
{
if (TextFormatter.NeedsFormat)
{
// This updates TextFormatter.Size to the text size
TextFormatter.AutoSize = true;
// Whenever DimAutoStyle.Text is set, ContentSize will match TextFormatter.Size.
ContentSize = TextFormatter.Size;
}
}
//SetRelativeLayout (SuperView?.ContentSize ?? new Size (int.MaxValue, int.MaxValue));
}
return new (
_viewportLocation,
new (
Math.Max (0, Frame.Size.Width - thickness.Horizontal),
Math.Max (0, Frame.Size.Height - thickness.Vertical)
));
}
set => SetViewport (value);
}
private void SetViewport (Rectangle viewport)
{
Rectangle oldViewport = viewport;
ApplySettings (ref viewport);
Thickness thickness = GetAdornmentsThickness ();
Size newSize = new (
viewport.Size.Width + thickness.Horizontal,
viewport.Size.Height + thickness.Vertical);
if (newSize == Frame.Size)
{
// The change is not changing the Frame, so we don't need to update it.
// Just call SetNeedsLayout to update the layout.
if (_viewportLocation != viewport.Location)
{
_viewportLocation = viewport.Location;
SetNeedsLayout ();
}
OnViewportChanged (new (IsInitialized ? Viewport : Rectangle.Empty, oldViewport));
return;
}
_viewportLocation = viewport.Location;
// Update the Frame because we made it bigger or smaller which impacts subviews.
Frame = Frame with
{
Size = newSize
};
void ApplySettings (ref Rectangle newViewport)
{
if (!ViewportSettings.HasFlag (ViewportSettings.AllowXGreaterThanContentWidth))
{
if (newViewport.X >= ContentSize.Width)
{
newViewport.X = ContentSize.Width - 1;
}
}
// IMPORTANT: Check for negative location AFTER checking for location greater than content width
if (!ViewportSettings.HasFlag (ViewportSettings.AllowNegativeX))
{
if (newViewport.X < 0)
{
newViewport.X = 0;
}
}
if (!ViewportSettings.HasFlag (ViewportSettings.AllowYGreaterThanContentHeight))
{
if (newViewport.Y >= ContentSize.Height)
{
newViewport.Y = ContentSize.Height - 1;
}
}
// IMPORTANT: Check for negative location AFTER checking for location greater than content width
if (!ViewportSettings.HasFlag (ViewportSettings.AllowNegativeY))
{
if (newViewport.Y < 0)
{
newViewport.Y = 0;
}
}
}
}
///
/// Fired when the changes. This event is fired after the has been updated.
///
[CanBeNull]
public event EventHandler ViewportChanged;
///
/// Called when the changes. Invokes the event.
///
///
protected virtual void OnViewportChanged (DrawEventArgs e) { ViewportChanged?.Invoke (this, e); }
///
/// Converts a -relative location to a screen-relative location.
///
///
/// Viewport-relative means relative to the top-left corner of the inner rectangle of the .
///
public Rectangle ViewportToScreen (in Rectangle location)
{
// Translate bounds to Frame (our SuperView's Viewport-relative coordinates)
Rectangle screen = FrameToScreen ();
Point viewportOffset = GetViewportOffsetFromFrame ();
screen.Offset (viewportOffset.X + location.X, viewportOffset.Y + location.Y);
return new (screen.Location, location.Size);
}
/// Converts a screen-relative coordinate to a Viewport-relative coordinate.
/// The coordinate relative to this view's .
///
/// Viewport-relative means relative to the top-left corner of the inner rectangle of the .
///
/// Column relative to the left side of the Viewport.
/// Row relative to the top of the Viewport
public Point ScreenToViewport (int x, int y)
{
Point viewportOffset = GetViewportOffsetFromFrame ();
Point screen = ScreenToFrame (x, y);
screen.Offset (-viewportOffset.X, -viewportOffset.Y);
return screen;
}
///
/// Helper to get the X and Y offset of the Viewport from the Frame. This is the sum of the Left and Top properties
/// of , and .
///
public Point GetViewportOffsetFromFrame () { return Padding is null ? Point.Empty : Padding.Thickness.GetInside (Padding.Frame).Location; }
///
/// Scrolls the view vertically by the specified number of rows.
///
///
///
///
///
///
/// if the was changed.
public bool? ScrollVertical (int rows)
{
if (ContentSize == Size.Empty || ContentSize == Viewport.Size)
{
return false;
}
Viewport = Viewport with { Y = Viewport.Y + rows };
return true;
}
///
/// Scrolls the view horizontally by the specified number of columns.
///
///
///
///
///
///
/// if the was changed.
public bool? ScrollHorizontal (int cols)
{
if (ContentSize == Size.Empty || ContentSize == Viewport.Size)
{
return false;
}
Viewport = Viewport with { X = Viewport.X + cols };
return true;
}
#endregion Viewport
}