ViewScrolling.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 => _contentSize = value;
  40. }
  41. /// <summary>
  42. /// Converts a content-relative location to a screen-relative location.
  43. /// </summary>
  44. /// <param name="location"></param>
  45. /// <returns>The screen-relative location.</returns>
  46. public Point ContentToScreen (in Point location)
  47. {
  48. // Translate to Viewport
  49. Point viewportOffset = GetViewportOffsetFromFrame ();
  50. Point contentRelativeToViewport = location;
  51. contentRelativeToViewport.Offset (-Viewport.X, -Viewport.Y);
  52. // Translate to Frame (our SuperView's Viewport-relative coordinates)
  53. Rectangle screen = ViewportToScreen (new (contentRelativeToViewport, Size.Empty));
  54. return screen.Location;
  55. }
  56. /// <summary>Converts a screen-relative coordinate to a Content-relative coordinate.</summary>
  57. /// <remarks>
  58. /// Content-relative means relative to the top-left corner of the view's Content.
  59. /// </remarks>
  60. /// <param name="x">Column relative to the left side of the Content.</param>
  61. /// <param name="y">Row relative to the top of the Content</param>
  62. /// <returns>The coordinate relative to this view's Content.</returns>
  63. public Point ScreenToContent (int x, int y)
  64. {
  65. Point viewportOffset = GetViewportOffsetFromFrame ();
  66. Point screen = ScreenToFrame (x, y);
  67. screen.Offset (Viewport.X - viewportOffset.X, Viewport.Y - viewportOffset.Y);
  68. return screen;
  69. }
  70. #endregion Content Area
  71. #region Viewport
  72. /// <summary>
  73. /// Gets or sets the scrolling behavior of the view.
  74. /// </summary>
  75. public ScrollSettings ScrollSettings { get; set; }
  76. private Point _viewportOffset;
  77. /// <summary>
  78. /// Gets or sets the rectangle describing the portion of the View's content that is visible to the user.
  79. /// The viewport Location is relative to the top-left corner of the inner rectangle of <see cref="Padding"/>s.
  80. /// If the viewport Size is the sames as the <see cref="ContentSize"/> the Location will be <c>0, 0</c>.
  81. /// Positive values for the location indicate the visible area is offset into the View's virtual
  82. /// <see cref="ContentSize"/>.
  83. /// </summary>
  84. /// <value>
  85. /// The rectangle describing the location and size of the viewport into the View's virtual content, described by
  86. /// <see cref="ContentSize"/>.
  87. /// </value>
  88. /// <remarks>
  89. /// <para>
  90. /// If <see cref="LayoutStyle"/> is <see cref="LayoutStyle.Computed"/> the value of Viewport is indeterminate until
  91. /// the view has been initialized ( <see cref="IsInitialized"/> is true) and <see cref="LayoutSubviews"/> has been
  92. /// called.
  93. /// </para>
  94. /// <para>
  95. /// Updates to the Viewport Size updates <see cref="Frame"/>, and has the same impact as updating the
  96. /// <see cref="Frame"/>.
  97. /// </para>
  98. /// <para>
  99. /// Altering the Viewport Size will eventually (when the view is next laid out) cause the
  100. /// <see cref="LayoutSubview(View, Rectangle)"/> and <see cref="OnDrawContent(Rectangle)"/> methods to be called.
  101. /// </para>
  102. /// </remarks>
  103. public virtual Rectangle Viewport
  104. {
  105. get
  106. {
  107. #if DEBUG
  108. if (LayoutStyle == LayoutStyle.Computed && !IsInitialized)
  109. {
  110. Debug.WriteLine (
  111. $"WARNING: Viewport is being accessed before the View has been initialized. This is likely a bug in {this}"
  112. );
  113. }
  114. #endif // DEBUG
  115. if (Margin is null || Border is null || Padding is null)
  116. {
  117. // CreateAdornments has not been called yet.
  118. return new (_viewportOffset, Frame.Size);
  119. }
  120. Thickness totalThickness = GetAdornmentsThickness ();
  121. return new (
  122. _viewportOffset,
  123. new (
  124. Math.Max (0, Frame.Size.Width - totalThickness.Horizontal),
  125. Math.Max (0, Frame.Size.Height - totalThickness.Vertical)));
  126. }
  127. set
  128. {
  129. _viewportOffset = value.Location;
  130. Thickness totalThickness = GetAdornmentsThickness ();
  131. Size newSize = new (value.Size.Width + totalThickness.Horizontal,
  132. value.Size.Height + totalThickness.Vertical);
  133. if (newSize == Frame.Size)
  134. {
  135. SetNeedsLayout ();
  136. return;
  137. }
  138. Frame = Frame with
  139. {
  140. Size = newSize
  141. };
  142. }
  143. }
  144. /// <summary>
  145. /// Converts a <see cref="Viewport"/>-relative location to a screen-relative location.
  146. /// </summary>
  147. /// <remarks>
  148. /// Viewport-relative means relative to the top-left corner of the inner rectangle of the <see cref="Padding"/>.
  149. /// </remarks>
  150. public Rectangle ViewportToScreen (in Rectangle location)
  151. {
  152. // Translate bounds to Frame (our SuperView's Viewport-relative coordinates)
  153. Rectangle screen = FrameToScreen ();
  154. Point viewportOffset = GetViewportOffsetFromFrame ();
  155. screen.Offset (viewportOffset.X + location.X, viewportOffset.Y + location.Y);
  156. return new (screen.Location, location.Size);
  157. }
  158. /// <summary>Converts a screen-relative coordinate to a Viewport-relative coordinate.</summary>
  159. /// <returns>The coordinate relative to this view's <see cref="Viewport"/>.</returns>
  160. /// <remarks>
  161. /// Viewport-relative means relative to the top-left corner of the inner rectangle of the <see cref="Padding"/>.
  162. /// </remarks>
  163. /// <param name="x">Column relative to the left side of the Viewport.</param>
  164. /// <param name="y">Row relative to the top of the Viewport</param>
  165. public Point ScreenToViewport (int x, int y)
  166. {
  167. Point viewportOffset = GetViewportOffsetFromFrame ();
  168. Point screen = ScreenToFrame (x, y);
  169. screen.Offset (-viewportOffset.X, -viewportOffset.Y);
  170. return screen;
  171. }
  172. /// <summary>
  173. /// Helper to get the X and Y offset of the Viewport from the Frame. This is the sum of the Left and Top properties
  174. /// of <see cref="Margin"/>, <see cref="Border"/> and <see cref="Padding"/>.
  175. /// </summary>
  176. public Point GetViewportOffsetFromFrame () { return Padding is null ? Point.Empty : Padding.Thickness.GetInside (Padding.Frame).Location; }
  177. /// <summary>
  178. /// Scrolls the view vertically by the specified number of rows.
  179. /// </summary>
  180. /// <remarks>
  181. /// <para>
  182. /// </para>
  183. /// </remarks>
  184. /// <param name="rows"></param>
  185. /// <returns><see langword="true"/> if the <see cref="Viewport"/> was changed.</returns>
  186. public bool? ScrollVertical (int rows)
  187. {
  188. if (ContentSize == Size.Empty || ContentSize == Viewport.Size)
  189. {
  190. return false;
  191. }
  192. if (!ScrollSettings.HasFlag (ScrollSettings.NoRestrictVertical)
  193. && (Viewport.Y + rows > ContentSize.Height - Viewport.Height || Viewport.Y + rows < 0))
  194. {
  195. return false;
  196. }
  197. Viewport = Viewport with { Y = Viewport.Y + rows };
  198. return true;
  199. }
  200. /// <summary>
  201. /// Scrolls the view horizontally by the specified number of columns.
  202. /// </summary>
  203. /// <remarks>
  204. /// <para>
  205. /// </para>
  206. /// </remarks>
  207. /// <param name="cols"></param>
  208. /// <returns><see langword="true"/> if the <see cref="Viewport"/> was changed.</returns>
  209. public bool? ScrollHorizontal (int cols)
  210. {
  211. if (ContentSize == Size.Empty || ContentSize == Viewport.Size)
  212. {
  213. return false;
  214. }
  215. if (!ScrollSettings.HasFlag (ScrollSettings.NoRestrictHorizontal)
  216. && (Viewport.X + cols > ContentSize.Width - Viewport.Width || Viewport.X + cols < 0))
  217. {
  218. return false;
  219. }
  220. Viewport = Viewport with { X = Viewport.X + cols };
  221. return true;
  222. }
  223. #endregion Viewport
  224. }