ViewScrolling.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. using System.Diagnostics;
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// Controls the scrolling behavior of a view.
  5. /// </summary>
  6. [Flags]
  7. public enum ScrollSettings
  8. {
  9. /// <summary>
  10. /// Default settings.
  11. /// </summary>
  12. Default = 0,
  13. /// <summary>
  14. /// If set, does not restrict vertical scrolling to the content size.
  15. /// </summary>
  16. NoRestrictVertical = 1,
  17. /// <summary>
  18. /// If set, does not restrict horizontal scrolling to the content size.
  19. /// </summary>
  20. NoRestrictHorizontal = 2,
  21. /// <summary>
  22. /// If set, does not restrict either vertical or horizontal scrolling to the content size.
  23. /// </summary>
  24. NoRestrict = NoRestrictVertical | NoRestrictHorizontal
  25. }
  26. public partial class View
  27. {
  28. #region Content Area
  29. private Size _contentSize;
  30. /// <summary>
  31. /// Gets or sets the size of the View's content. If the value is <c>Size.Empty</c> the size of the content is
  32. /// the same as the size of the <see cref="Viewport"/>, and <c>Viewport.Location</c> will always be <c>0, 0</c>.
  33. /// If a positive size is provided, <see cref="Viewport"/> describes the portion of the content currently visible
  34. /// to the view. This enables virtual scrolling.
  35. /// </summary>
  36. public Size ContentSize
  37. {
  38. get => _contentSize == Size.Empty ? Viewport.Size : _contentSize;
  39. set
  40. {
  41. _contentSize = value;
  42. OnContentSizeChanged (new (_contentSize));
  43. }
  44. }
  45. /// <summary>
  46. /// Called when the <see cref="ContentSize"/> changes. Invokes the <see cref="ContentSizeChanged"/> event.
  47. /// </summary>
  48. /// <param name="e"></param>
  49. /// <returns></returns>
  50. protected bool? OnContentSizeChanged (SizeChangedEventArgs e)
  51. {
  52. ContentSizeChanged?.Invoke (this, e);
  53. if (e.Cancel != true)
  54. {
  55. SetNeedsDisplay ();
  56. }
  57. return e.Cancel == true;
  58. }
  59. /// <summary>
  60. /// Event that is raised when the <see cref="ContentSize"/> changes.
  61. /// </summary>
  62. public event EventHandler<SizeChangedEventArgs> ContentSizeChanged;
  63. /// <summary>
  64. /// Converts a content-relative location to a screen-relative location.
  65. /// </summary>
  66. /// <param name="location"></param>
  67. /// <returns>The screen-relative location.</returns>
  68. public Point ContentToScreen (in Point location)
  69. {
  70. // Translate to Viewport
  71. Point viewportOffset = GetViewportOffsetFromFrame ();
  72. Point contentRelativeToViewport = location;
  73. contentRelativeToViewport.Offset (-Viewport.X, -Viewport.Y);
  74. // Translate to Frame (our SuperView's Viewport-relative coordinates)
  75. Rectangle screen = ViewportToScreen (new (contentRelativeToViewport, Size.Empty));
  76. return screen.Location;
  77. }
  78. /// <summary>Converts a screen-relative coordinate to a Content-relative coordinate.</summary>
  79. /// <remarks>
  80. /// Content-relative means relative to the top-left corner of the view's Content.
  81. /// </remarks>
  82. /// <param name="x">Column relative to the left side of the Content.</param>
  83. /// <param name="y">Row relative to the top of the Content</param>
  84. /// <returns>The coordinate relative to this view's Content.</returns>
  85. public Point ScreenToContent (int x, int y)
  86. {
  87. Point viewportOffset = GetViewportOffsetFromFrame ();
  88. Point screen = ScreenToFrame (x, y);
  89. screen.Offset (Viewport.X - viewportOffset.X, Viewport.Y - viewportOffset.Y);
  90. return screen;
  91. }
  92. #endregion Content Area
  93. #region Viewport
  94. /// <summary>
  95. /// Gets or sets the scrolling behavior of the view.
  96. /// </summary>
  97. public ScrollSettings ScrollSettings { get; set; }
  98. /// <summary>
  99. /// The location of the viewport.in the view's content (0,0) is the top-left corner of the content. It's size
  100. /// is <see cref="ContentSize"/>.
  101. /// </summary>
  102. private Point _viewportLocation;
  103. /// <summary>
  104. /// Gets or sets the rectangle describing the portion of the View's content that is visible to the user.
  105. /// The viewport Location is relative to the top-left corner of the inner rectangle of <see cref="Padding"/>s.
  106. /// If the viewport Size is the sames as the <see cref="ContentSize"/> the Location will be <c>0, 0</c>.
  107. /// Positive values for the location indicate the visible area is offset into the View's virtual
  108. /// <see cref="ContentSize"/>.
  109. /// </summary>
  110. /// <value>
  111. /// The rectangle describing the location and size of the viewport into the View's virtual content, described by
  112. /// <see cref="ContentSize"/>.
  113. /// </value>
  114. /// <remarks>
  115. /// <para>
  116. /// If <see cref="LayoutStyle"/> is <see cref="LayoutStyle.Computed"/> the value of Viewport is indeterminate until
  117. /// the view has been initialized ( <see cref="IsInitialized"/> is true) and <see cref="LayoutSubviews"/> has been
  118. /// called.
  119. /// </para>
  120. /// <para>
  121. /// Updates to the Viewport Size updates <see cref="Frame"/>, and has the same impact as updating the
  122. /// <see cref="Frame"/>.
  123. /// </para>
  124. /// <para>
  125. /// Altering the Viewport Size will eventually (when the view is next laid out) cause the
  126. /// <see cref="LayoutSubview(View, Size)"/> and <see cref="OnDrawContent(Rectangle)"/> methods to be called.
  127. /// </para>
  128. /// </remarks>
  129. public virtual Rectangle Viewport
  130. {
  131. get
  132. {
  133. #if DEBUG
  134. if (LayoutStyle == LayoutStyle.Computed && !IsInitialized)
  135. {
  136. Debug.WriteLine (
  137. $"WARNING: Viewport is being accessed before the View has been initialized. This is likely a bug in {this}"
  138. );
  139. }
  140. #endif // DEBUG
  141. if (Margin is null || Border is null || Padding is null)
  142. {
  143. // CreateAdornments has not been called yet.
  144. return new (_viewportLocation, Frame.Size);
  145. }
  146. Thickness thickness = GetAdornmentsThickness ();
  147. return new (_viewportLocation, new (
  148. Math.Max (0, Frame.Size.Width - thickness.Horizontal),
  149. Math.Max (0, Frame.Size.Height - thickness.Vertical)
  150. ));
  151. }
  152. set
  153. {
  154. _viewportLocation = value.Location;
  155. Thickness thickness = GetAdornmentsThickness ();
  156. Size newSize = new (value.Size.Width + thickness.Horizontal,
  157. value.Size.Height + thickness.Vertical);
  158. if (newSize == Frame.Size)
  159. {
  160. // The change is not changing the Frame, so we don't need to update it.
  161. // Just call SetRelativeLayout6 to update the layout.
  162. SetNeedsLayout ();
  163. return;
  164. }
  165. // Update the Frame because we made it bigger or smaller which impacts subviews.
  166. Frame = Frame with
  167. {
  168. Size = newSize
  169. };
  170. }
  171. }
  172. /// <summary>
  173. /// Converts a <see cref="Viewport"/>-relative location to a screen-relative location.
  174. /// </summary>
  175. /// <remarks>
  176. /// Viewport-relative means relative to the top-left corner of the inner rectangle of the <see cref="Padding"/>.
  177. /// </remarks>
  178. public Rectangle ViewportToScreen (in Rectangle location)
  179. {
  180. // Translate bounds to Frame (our SuperView's Viewport-relative coordinates)
  181. Rectangle screen = FrameToScreen ();
  182. Point viewportOffset = GetViewportOffsetFromFrame ();
  183. screen.Offset (viewportOffset.X + location.X, viewportOffset.Y + location.Y);
  184. return new (screen.Location, location.Size);
  185. }
  186. /// <summary>Converts a screen-relative coordinate to a Viewport-relative coordinate.</summary>
  187. /// <returns>The coordinate relative to this view's <see cref="Viewport"/>.</returns>
  188. /// <remarks>
  189. /// Viewport-relative means relative to the top-left corner of the inner rectangle of the <see cref="Padding"/>.
  190. /// </remarks>
  191. /// <param name="x">Column relative to the left side of the Viewport.</param>
  192. /// <param name="y">Row relative to the top of the Viewport</param>
  193. public Point ScreenToViewport (int x, int y)
  194. {
  195. Point viewportOffset = GetViewportOffsetFromFrame ();
  196. Point screen = ScreenToFrame (x, y);
  197. screen.Offset (-viewportOffset.X, -viewportOffset.Y);
  198. return screen;
  199. }
  200. /// <summary>
  201. /// Helper to get the X and Y offset of the Viewport from the Frame. This is the sum of the Left and Top properties
  202. /// of <see cref="Margin"/>, <see cref="Border"/> and <see cref="Padding"/>.
  203. /// </summary>
  204. public Point GetViewportOffsetFromFrame () { return Padding is null ? Point.Empty : Padding.Thickness.GetInside (Padding.Frame).Location; }
  205. /// <summary>
  206. /// Scrolls the view vertically by the specified number of rows.
  207. /// </summary>
  208. /// <remarks>
  209. /// <para>
  210. /// </para>
  211. /// </remarks>
  212. /// <param name="rows"></param>
  213. /// <returns><see langword="true"/> if the <see cref="Viewport"/> was changed.</returns>
  214. public bool? ScrollVertical (int rows)
  215. {
  216. if (ContentSize == Size.Empty || ContentSize == Viewport.Size)
  217. {
  218. return false;
  219. }
  220. if (!ScrollSettings.HasFlag (ScrollSettings.NoRestrictVertical)
  221. && (Viewport.Y + rows > ContentSize.Height - Viewport.Height || Viewport.Y + rows < 0))
  222. {
  223. return false;
  224. }
  225. Viewport = Viewport with { Y = Viewport.Y + rows };
  226. return true;
  227. }
  228. /// <summary>
  229. /// Scrolls the view horizontally by the specified number of columns.
  230. /// </summary>
  231. /// <remarks>
  232. /// <para>
  233. /// </para>
  234. /// </remarks>
  235. /// <param name="cols"></param>
  236. /// <returns><see langword="true"/> if the <see cref="Viewport"/> was changed.</returns>
  237. public bool? ScrollHorizontal (int cols)
  238. {
  239. if (ContentSize == Size.Empty || ContentSize == Viewport.Size)
  240. {
  241. return false;
  242. }
  243. if (!ScrollSettings.HasFlag (ScrollSettings.NoRestrictHorizontal)
  244. && (Viewport.X + cols > ContentSize.Width - Viewport.Width || Viewport.X + cols < 0))
  245. {
  246. return false;
  247. }
  248. Viewport = Viewport with { X = Viewport.X + cols };
  249. return true;
  250. }
  251. #endregion Viewport
  252. }