ViewScrolling.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. private Point _viewportOffset;
  99. /// <summary>
  100. /// Gets or sets the rectangle describing the portion of the View's content that is visible to the user.
  101. /// The viewport Location is relative to the top-left corner of the inner rectangle of <see cref="Padding"/>s.
  102. /// If the viewport Size is the sames as the <see cref="ContentSize"/> the Location will be <c>0, 0</c>.
  103. /// Positive values for the location indicate the visible area is offset into the View's virtual
  104. /// <see cref="ContentSize"/>.
  105. /// </summary>
  106. /// <value>
  107. /// The rectangle describing the location and size of the viewport into the View's virtual content, described by
  108. /// <see cref="ContentSize"/>.
  109. /// </value>
  110. /// <remarks>
  111. /// <para>
  112. /// If <see cref="LayoutStyle"/> is <see cref="LayoutStyle.Computed"/> the value of Viewport is indeterminate until
  113. /// the view has been initialized ( <see cref="IsInitialized"/> is true) and <see cref="LayoutSubviews"/> has been
  114. /// called.
  115. /// </para>
  116. /// <para>
  117. /// Updates to the Viewport Size updates <see cref="Frame"/>, and has the same impact as updating the
  118. /// <see cref="Frame"/>.
  119. /// </para>
  120. /// <para>
  121. /// Altering the Viewport Size will eventually (when the view is next laid out) cause the
  122. /// <see cref="LayoutSubview(View, Rectangle)"/> and <see cref="OnDrawContent(Rectangle)"/> methods to be called.
  123. /// </para>
  124. /// </remarks>
  125. public virtual Rectangle Viewport
  126. {
  127. get
  128. {
  129. #if DEBUG
  130. if (LayoutStyle == LayoutStyle.Computed && !IsInitialized)
  131. {
  132. Debug.WriteLine (
  133. $"WARNING: Viewport is being accessed before the View has been initialized. This is likely a bug in {this}"
  134. );
  135. }
  136. #endif // DEBUG
  137. if (Margin is null || Border is null || Padding is null)
  138. {
  139. // CreateAdornments has not been called yet.
  140. return new (_viewportOffset, Frame.Size);
  141. }
  142. Thickness totalThickness = GetAdornmentsThickness ();
  143. return new (
  144. _viewportOffset,
  145. new (
  146. Math.Max (0, Frame.Size.Width - totalThickness.Horizontal),
  147. Math.Max (0, Frame.Size.Height - totalThickness.Vertical)));
  148. }
  149. set
  150. {
  151. _viewportOffset = value.Location;
  152. Thickness totalThickness = GetAdornmentsThickness ();
  153. Size newSize = new (value.Size.Width + totalThickness.Horizontal,
  154. value.Size.Height + totalThickness.Vertical);
  155. if (newSize == Frame.Size)
  156. {
  157. SetNeedsLayout ();
  158. return;
  159. }
  160. Frame = Frame with
  161. {
  162. Size = newSize
  163. };
  164. }
  165. }
  166. /// <summary>
  167. /// Converts a <see cref="Viewport"/>-relative location to a screen-relative location.
  168. /// </summary>
  169. /// <remarks>
  170. /// Viewport-relative means relative to the top-left corner of the inner rectangle of the <see cref="Padding"/>.
  171. /// </remarks>
  172. public Rectangle ViewportToScreen (in Rectangle location)
  173. {
  174. // Translate bounds to Frame (our SuperView's Viewport-relative coordinates)
  175. Rectangle screen = FrameToScreen ();
  176. Point viewportOffset = GetViewportOffsetFromFrame ();
  177. screen.Offset (viewportOffset.X + location.X, viewportOffset.Y + location.Y);
  178. return new (screen.Location, location.Size);
  179. }
  180. /// <summary>Converts a screen-relative coordinate to a Viewport-relative coordinate.</summary>
  181. /// <returns>The coordinate relative to this view's <see cref="Viewport"/>.</returns>
  182. /// <remarks>
  183. /// Viewport-relative means relative to the top-left corner of the inner rectangle of the <see cref="Padding"/>.
  184. /// </remarks>
  185. /// <param name="x">Column relative to the left side of the Viewport.</param>
  186. /// <param name="y">Row relative to the top of the Viewport</param>
  187. public Point ScreenToViewport (int x, int y)
  188. {
  189. Point viewportOffset = GetViewportOffsetFromFrame ();
  190. Point screen = ScreenToFrame (x, y);
  191. screen.Offset (-viewportOffset.X, -viewportOffset.Y);
  192. return screen;
  193. }
  194. /// <summary>
  195. /// Helper to get the X and Y offset of the Viewport from the Frame. This is the sum of the Left and Top properties
  196. /// of <see cref="Margin"/>, <see cref="Border"/> and <see cref="Padding"/>.
  197. /// </summary>
  198. public Point GetViewportOffsetFromFrame () { return Padding is null ? Point.Empty : Padding.Thickness.GetInside (Padding.Frame).Location; }
  199. /// <summary>
  200. /// Scrolls the view vertically by the specified number of rows.
  201. /// </summary>
  202. /// <remarks>
  203. /// <para>
  204. /// </para>
  205. /// </remarks>
  206. /// <param name="rows"></param>
  207. /// <returns><see langword="true"/> if the <see cref="Viewport"/> was changed.</returns>
  208. public bool? ScrollVertical (int rows)
  209. {
  210. if (ContentSize == Size.Empty || ContentSize == Viewport.Size)
  211. {
  212. return false;
  213. }
  214. if (!ScrollSettings.HasFlag (ScrollSettings.NoRestrictVertical)
  215. && (Viewport.Y + rows > ContentSize.Height - Viewport.Height || Viewport.Y + rows < 0))
  216. {
  217. return false;
  218. }
  219. Viewport = Viewport with { Y = Viewport.Y + rows };
  220. return true;
  221. }
  222. /// <summary>
  223. /// Scrolls the view horizontally by the specified number of columns.
  224. /// </summary>
  225. /// <remarks>
  226. /// <para>
  227. /// </para>
  228. /// </remarks>
  229. /// <param name="cols"></param>
  230. /// <returns><see langword="true"/> if the <see cref="Viewport"/> was changed.</returns>
  231. public bool? ScrollHorizontal (int cols)
  232. {
  233. if (ContentSize == Size.Empty || ContentSize == Viewport.Size)
  234. {
  235. return false;
  236. }
  237. if (!ScrollSettings.HasFlag (ScrollSettings.NoRestrictHorizontal)
  238. && (Viewport.X + cols > ContentSize.Width - Viewport.Width || Viewport.X + cols < 0))
  239. {
  240. return false;
  241. }
  242. Viewport = Viewport with { X = Viewport.X + cols };
  243. return true;
  244. }
  245. #endregion Viewport
  246. }