using System.Diagnostics; namespace Terminal.Gui; public partial class View { #region Content Area internal Size? _contentSize; /// /// Sets the size of the View's content. /// /// /// /// By default, the content size is set to . /// /// /// /// /// If , and the View has no visible subviews, will track the size of . /// /// /// If , and the View has visible subviews, will track the maximum position plus size of any /// visible Subviews /// and Viewport.Location will track the minimum position and size of any visible Subviews. /// /// /// If not , is set to the passed value and describes the portion of the content currently visible /// to the user. This enables virtual scrolling. /// /// /// If not , is set to the passed value and the behavior of will be to use the ContentSize /// to determine the size of the view. /// /// /// Negative sizes are not supported. /// /// public void SetContentSize (Size? contentSize) { if (ContentSize.Width < 0 || ContentSize.Height < 0) { throw new ArgumentException (@"ContentSize cannot be negative.", nameof (contentSize)); } if (contentSize == _contentSize) { return; } _contentSize = contentSize; OnContentSizeChanged (new (_contentSize)); } /// /// Gets the size of the View's content. /// /// /// /// Use to change to change the content size. /// /// /// If the content size has not been explicitly set with , the value tracks /// . /// /// public Size ContentSize => _contentSize ?? Viewport.Size; /// /// Called when has changed. /// /// /// protected bool? OnContentSizeChanged (SizeChangedEventArgs e) { ContentSizeChanged?.Invoke (this, e); if (e.Cancel != true) { OnResizeNeeded (); //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) return ViewportToScreen (contentRelativeToViewport); } /// 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); 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 , or is /// 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 (Margin is null || Border is null || Padding is null) { // CreateAdornments has not been called yet. return new (_viewportLocation, Frame.Size); } Thickness thickness = GetAdornmentsThickness (); 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 and size to a screen-relative location and size. /// /// /// Viewport-relative means relative to the top-left corner of the inner rectangle of the . /// /// Viewport-relative location and size. /// Screen-relative location and size. public Rectangle ViewportToScreen (in Rectangle viewport) { return viewport with { Location = ViewportToScreen (viewport.Location) }; } /// /// Converts a -relative location to a screen-relative location. /// /// /// Viewport-relative means relative to the top-left corner of the inner rectangle of the . /// /// Viewport-relative location. /// Screen-relative location. public Point ViewportToScreen (in Point viewportLocation) { // Translate bounds to Frame (our SuperView's Viewport-relative coordinates) Rectangle screen = FrameToScreen (); Point viewportOffset = GetViewportOffsetFromFrame (); screen.Offset (viewportOffset.X + viewportLocation.X, viewportOffset.Y + viewportLocation.Y); return screen.Location; } /// 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 . /// /// Screen-Relative Coordinate. /// Viewport-relative location. public Point ScreenToViewport (in Point location) { Point viewportOffset = GetViewportOffsetFromFrame (); Point frame = ScreenToFrame (location); frame.Offset (-viewportOffset.X, -viewportOffset.Y); return frame; } /// /// 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 }