using System.Diagnostics; namespace Terminal.Gui; /// /// Controls the scrolling behavior of a view. /// [Flags] public enum ScrollSettings { /// /// Default settings. /// Default = 0, /// /// If set, does not restrict vertical scrolling to the content size. /// NoRestrictVertical = 1, /// /// If set, does not restrict horizontal scrolling to the content size. /// NoRestrictHorizontal = 2, /// /// If set, does not restrict either vertical or horizontal scrolling to the content size. /// NoRestrict = NoRestrictVertical | NoRestrictHorizontal } public partial class View { #region Content Area private Size _contentSize; /// /// Gets or sets the size of the View's content. If the value is Size.Empty the size of the content is /// the same as the size of the , 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. /// public Size ContentSize { get => _contentSize == Size.Empty ? Viewport.Size : _contentSize; set { _contentSize = value; OnContentSizeChanged (new (_contentSize)); } } /// /// Called when the changes. Invokes the event. /// /// /// protected bool? OnContentSizeChanged (SizeChangedEventArgs e) { ContentSizeChanged?.Invoke (this, e); if (e.Cancel != true) { SetNeedsDisplay (); } return e.Cancel == true; } /// /// Event that is raised when the changes. /// public event EventHandler ContentSizeChanged; /// /// Converts a content-relative location to a screen-relative location. /// /// /// The screen-relative location. public Point ContentToScreen (in Point location) { // Translate to Viewport Point viewportOffset = GetViewportOffsetFromFrame (); Point contentRelativeToViewport = location; contentRelativeToViewport.Offset (-Viewport.X, -Viewport.Y); // Translate to Frame (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. /// /// Column relative to the left side of the Content. /// Row relative to the top of the Content /// The coordinate relative to this view's Content. public Point ScreenToContent (int x, int y) { Point viewportOffset = GetViewportOffsetFromFrame (); Point screen = ScreenToFrame (x, y); screen.Offset (Viewport.X - viewportOffset.X, Viewport.Y - viewportOffset.Y); return screen; } #endregion Content Area #region Viewport /// /// Gets or sets the scrolling behavior of the view. /// public ScrollSettings ScrollSettings { get; set; } private Point _viewportOffset; /// /// 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 s. /// If the viewport Size is the sames as the the Location will be 0, 0. /// Positive values for the location indicate the visible area is offset into the View's virtual /// . /// /// /// The rectangle describing the location and size of the viewport into the View's virtual content, described by /// . /// /// /// /// 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 (LayoutStyle == LayoutStyle.Computed && !IsInitialized) { Debug.WriteLine ( $"WARNING: Viewport is being accessed before the View has been initialized. This is likely a bug in {this}" ); } #endif // DEBUG if (Margin is null || Border is null || Padding is null) { // CreateAdornments has not been called yet. return new (_viewportOffset, Frame.Size); } Thickness totalThickness = GetAdornmentsThickness (); return new ( _viewportOffset, new ( Math.Max (0, Frame.Size.Width - totalThickness.Horizontal), Math.Max (0, Frame.Size.Height - totalThickness.Vertical))); } set { _viewportOffset = value.Location; Thickness totalThickness = GetAdornmentsThickness (); Size newSize = new (value.Size.Width + totalThickness.Horizontal, value.Size.Height + totalThickness.Vertical); if (newSize == Frame.Size) { SetNeedsLayout (); return; } Frame = Frame with { Size = newSize }; } } /// /// 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; } if (!ScrollSettings.HasFlag (ScrollSettings.NoRestrictVertical) && (Viewport.Y + rows > ContentSize.Height - Viewport.Height || Viewport.Y + rows < 0)) { 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; } if (!ScrollSettings.HasFlag (ScrollSettings.NoRestrictHorizontal) && (Viewport.X + cols > ContentSize.Width - Viewport.Width || Viewport.X + cols < 0)) { return false; } Viewport = Viewport with { X = Viewport.X + cols }; return true; } #endregion Viewport }