ViewScrolling.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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">The Content-relative location.</param>
  67. /// <returns>The Screen-relative location.</returns>
  68. public Point ContentToScreen (in Point location)
  69. {
  70. // Subtract the ViewportOffsetFromFrame to get the Viewport-relative location.
  71. Point viewportOffset = GetViewportOffsetFromFrame ();
  72. Point contentRelativeToViewport = location;
  73. contentRelativeToViewport.Offset (-Viewport.X, -Viewport.Y);
  74. // Translate to Screen-Relative (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, which is
  81. /// always at <c>0, 0</c>.
  82. /// </remarks>
  83. /// <param name="x">Column relative to the left side of the Content.</param>
  84. /// <param name="y">Row relative to the top of the Content</param>
  85. /// <returns>The coordinate relative to this view's Content.</returns>
  86. public Point ScreenToContent (in Point location)
  87. {
  88. Point viewportOffset = GetViewportOffsetFromFrame ();
  89. Point screen = ScreenToFrame (location.X, location.Y);
  90. screen.Offset (Viewport.X - viewportOffset.X, Viewport.Y - viewportOffset.Y);
  91. return screen;
  92. }
  93. #endregion Content Area
  94. #region Viewport
  95. /// <summary>
  96. /// Gets or sets the scrolling behavior of the view.
  97. /// </summary>
  98. public ScrollSettings ScrollSettings { get; set; }
  99. /// <summary>
  100. /// The location of the viewport.in the view's content (0,0) is the top-left corner of the content. It's size
  101. /// is <see cref="ContentSize"/>.
  102. /// </summary>
  103. private Point _viewportLocation;
  104. /// <summary>
  105. /// Gets or sets the rectangle describing the portion of the View's content that is visible to the user.
  106. /// The viewport Location is relative to the top-left corner of the inner rectangle of <see cref="Padding"/>s.
  107. /// If the viewport Size is the sames as the <see cref="ContentSize"/> the Location will be <c>0, 0</c>.
  108. /// Positive values for the location indicate the visible area is offset into the View's virtual
  109. /// <see cref="ContentSize"/>.
  110. /// </summary>
  111. /// <value>
  112. /// The rectangle describing the location and size of the viewport into the View's virtual content, described by
  113. /// <see cref="ContentSize"/>.
  114. /// </value>
  115. /// <remarks>
  116. /// <para>
  117. /// If <see cref="LayoutStyle"/> is <see cref="LayoutStyle.Computed"/> the value of Viewport is indeterminate until
  118. /// the view has been initialized ( <see cref="IsInitialized"/> is true) and <see cref="LayoutSubviews"/> has been
  119. /// called.
  120. /// </para>
  121. /// <para>
  122. /// Updates to the Viewport Size updates <see cref="Frame"/>, and has the same impact as updating the
  123. /// <see cref="Frame"/>.
  124. /// </para>
  125. /// <para>
  126. /// Altering the Viewport Size will eventually (when the view is next laid out) cause the
  127. /// <see cref="LayoutSubview(View, Size)"/> and <see cref="OnDrawContent(Rectangle)"/> methods to be called.
  128. /// </para>
  129. /// </remarks>
  130. public virtual Rectangle Viewport
  131. {
  132. get
  133. {
  134. #if DEBUG
  135. if (LayoutStyle == LayoutStyle.Computed && !IsInitialized)
  136. {
  137. Debug.WriteLine (
  138. $"WARNING: Viewport is being accessed before the View has been initialized. This is likely a bug in {this}"
  139. );
  140. }
  141. #endif // DEBUG
  142. if (Margin is null || Border is null || Padding is null)
  143. {
  144. // CreateAdornments has not been called yet.
  145. return new (_viewportLocation, Frame.Size);
  146. }
  147. Thickness thickness = GetAdornmentsThickness ();
  148. return new (_viewportLocation, new (
  149. Math.Max (0, Frame.Size.Width - thickness.Horizontal),
  150. Math.Max (0, Frame.Size.Height - thickness.Vertical)
  151. ));
  152. }
  153. set
  154. {
  155. if (!ScrollSettings.HasFlag (ScrollSettings.NoRestrictVertical))
  156. {
  157. if (value.Y + Viewport.Height > ContentSize.Height)
  158. {
  159. value.Y = ContentSize.Height - Viewport.Height;
  160. }
  161. if (value.Y < 0)
  162. {
  163. value.Y = 0;
  164. }
  165. }
  166. if (!ScrollSettings.HasFlag (ScrollSettings.NoRestrictHorizontal))
  167. {
  168. if (value.X + Viewport.Width > ContentSize.Width)
  169. {
  170. value.X = ContentSize.Width - Viewport.Width;
  171. }
  172. if (value.X < 0)
  173. {
  174. value.X = 0;
  175. }
  176. }
  177. Thickness thickness = GetAdornmentsThickness ();
  178. Size newSize = new (value.Size.Width + thickness.Horizontal,
  179. value.Size.Height + thickness.Vertical);
  180. if (newSize == Frame.Size)
  181. {
  182. // The change is not changing the Frame, so we don't need to update it.
  183. // Just call SetNeedsLayout to update the layout.
  184. if (_viewportLocation != value.Location)
  185. {
  186. _viewportLocation = value.Location;
  187. SetNeedsLayout ();
  188. }
  189. return;
  190. }
  191. _viewportLocation = value.Location;
  192. // Update the Frame because we made it bigger or smaller which impacts subviews.
  193. Frame = Frame with
  194. {
  195. Size = newSize
  196. };
  197. }
  198. }
  199. /// <summary>
  200. /// Converts a <see cref="Viewport"/>-relative location to a screen-relative location.
  201. /// </summary>
  202. /// <remarks>
  203. /// Viewport-relative means relative to the top-left corner of the inner rectangle of the <see cref="Padding"/>.
  204. /// </remarks>
  205. public Rectangle ViewportToScreen (in Rectangle location)
  206. {
  207. // Translate bounds to Frame (our SuperView's Viewport-relative coordinates)
  208. Rectangle screen = FrameToScreen ();
  209. Point viewportOffset = GetViewportOffsetFromFrame ();
  210. screen.Offset (viewportOffset.X + location.X, viewportOffset.Y + location.Y);
  211. return new (screen.Location, location.Size);
  212. }
  213. /// <summary>Converts a screen-relative coordinate to a Viewport-relative coordinate.</summary>
  214. /// <returns>The coordinate relative to this view's <see cref="Viewport"/>.</returns>
  215. /// <remarks>
  216. /// Viewport-relative means relative to the top-left corner of the inner rectangle of the <see cref="Padding"/>.
  217. /// </remarks>
  218. /// <param name="x">Column relative to the left side of the Viewport.</param>
  219. /// <param name="y">Row relative to the top of the Viewport</param>
  220. public Point ScreenToViewport (int x, int y)
  221. {
  222. Point viewportOffset = GetViewportOffsetFromFrame ();
  223. Point screen = ScreenToFrame (x, y);
  224. screen.Offset (-viewportOffset.X, -viewportOffset.Y);
  225. return screen;
  226. }
  227. /// <summary>
  228. /// Helper to get the X and Y offset of the Viewport from the Frame. This is the sum of the Left and Top properties
  229. /// of <see cref="Margin"/>, <see cref="Border"/> and <see cref="Padding"/>.
  230. /// </summary>
  231. public Point GetViewportOffsetFromFrame () { return Padding is null ? Point.Empty : Padding.Thickness.GetInside (Padding.Frame).Location; }
  232. /// <summary>
  233. /// Scrolls the view vertically by the specified number of rows.
  234. /// </summary>
  235. /// <remarks>
  236. /// <para>
  237. /// </para>
  238. /// </remarks>
  239. /// <param name="rows"></param>
  240. /// <returns><see langword="true"/> if the <see cref="Viewport"/> was changed.</returns>
  241. public bool? ScrollVertical (int rows)
  242. {
  243. if (ContentSize == Size.Empty || ContentSize == Viewport.Size)
  244. {
  245. return false;
  246. }
  247. Viewport = Viewport with { Y = Viewport.Y + rows };
  248. return true;
  249. }
  250. /// <summary>
  251. /// Scrolls the view horizontally by the specified number of columns.
  252. /// </summary>
  253. /// <remarks>
  254. /// <para>
  255. /// </para>
  256. /// </remarks>
  257. /// <param name="cols"></param>
  258. /// <returns><see langword="true"/> if the <see cref="Viewport"/> was changed.</returns>
  259. public bool? ScrollHorizontal (int cols)
  260. {
  261. if (ContentSize == Size.Empty || ContentSize == Viewport.Size)
  262. {
  263. return false;
  264. }
  265. Viewport = Viewport with { X = Viewport.X + cols };
  266. return true;
  267. }
  268. #endregion Viewport
  269. }