ViewContent.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. using System.Diagnostics;
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// Settings for how scrolling the <see cref="View.Viewport"/> on the View's Content Area is handled.
  5. /// </summary>
  6. [Flags]
  7. public enum ScrollSettings
  8. {
  9. /// <summary>
  10. /// No settings.
  11. /// </summary>
  12. None = 0,
  13. /// <summary>
  14. /// If set, <see cref="View.Viewport"/> can be set to a rectangle that does not perfectly intersect with the Content Area
  15. /// rectangle (<see cref="View.ContentSize"/> in the horizontal direction, enabling scrolling beyond the dimensions of the content area vertically.
  16. /// </summary>
  17. /// <remarks>
  18. /// <para>
  19. /// When not set, <see cref="View.Viewport"/> is constrained to the bounds of the Content Area rectangle in the horizontal direction.
  20. /// </para>
  21. /// </remarks>
  22. AllowViewportOutsideContentHorizontal = 1,
  23. /// <summary>
  24. /// If set, <see cref="View.Viewport"/> can be set to a rectangle that does not perfectly intersect with the Content Area
  25. /// rectangle (<see cref="View.ContentSize"/> in the vertical direction, enabling scrolling beyond the dimensions of the content area vertically.
  26. /// </summary>
  27. /// <remarks>
  28. /// <para>
  29. /// When not set, <see cref="View.Viewport"/> is constrained to the bounds of the Content Area rectangle in the vertical direction.
  30. /// </para>
  31. /// </remarks>
  32. AllowViewportOutsideContentVertical = 2,
  33. /// <summary>
  34. /// If set, <see cref="View.Viewport"/> can be set to a rectangle that does not perfectly intersect with the Content Area
  35. /// rectangle (<see cref="View.ContentSize"/>, enabling scrolling beyond the dimensions of the content area vertically.
  36. /// </summary>
  37. /// <remarks>
  38. /// <para>
  39. /// When not set, <see cref="View.Viewport"/> is constrained to the bounds of the Content Area rectangle.
  40. /// </para>
  41. /// </remarks>
  42. AllowViewportOutsideContent = AllowViewportOutsideContentHorizontal | AllowViewportOutsideContentVertical
  43. }
  44. public partial class View
  45. {
  46. #region Content Area
  47. private Size _contentSize;
  48. /// <summary>
  49. /// Gets or sets the size of the View's content. If the value is <c>Size.Empty</c> the size of the content is
  50. /// the same as the size of <see cref="Viewport"/>, and <c>Viewport.Location</c> will always be <c>0, 0</c>.
  51. /// </summary>
  52. /// <remarks>
  53. /// <para>
  54. /// If a positive size is provided, <see cref="Viewport"/> describes the portion of the content currently visible
  55. /// to the view. This enables virtual scrolling.
  56. /// </para>
  57. /// <para>
  58. /// Negative sizes are not supported.
  59. /// </para>
  60. /// </remarks>
  61. public Size ContentSize
  62. {
  63. get => _contentSize == Size.Empty ? Viewport.Size : _contentSize;
  64. set
  65. {
  66. if (value.Width < 0 || value.Height < 0)
  67. {
  68. throw new ArgumentException (@"ContentSize cannot be negative.", nameof (value));
  69. }
  70. if (value == _contentSize)
  71. {
  72. return;
  73. }
  74. _contentSize = value;
  75. OnContentSizeChanged (new (_contentSize));
  76. }
  77. }
  78. /// <summary>
  79. /// Called when <see cref="ContentSize"/> changes. Invokes the <see cref="ContentSizeChanged"/> event.
  80. /// </summary>
  81. /// <param name="e"></param>
  82. /// <returns></returns>
  83. protected bool? OnContentSizeChanged (SizeChangedEventArgs e)
  84. {
  85. ContentSizeChanged?.Invoke (this, e);
  86. if (e.Cancel != true)
  87. {
  88. SetNeedsDisplay ();
  89. }
  90. return e.Cancel;
  91. }
  92. /// <summary>
  93. /// Event that is raised when the <see cref="ContentSize"/> changes.
  94. /// </summary>
  95. public event EventHandler<SizeChangedEventArgs> ContentSizeChanged;
  96. /// <summary>
  97. /// Converts a Content-relative location to a Screen-relative location.
  98. /// </summary>
  99. /// <param name="location">The Content-relative location.</param>
  100. /// <returns>The Screen-relative location.</returns>
  101. public Point ContentToScreen (in Point location)
  102. {
  103. // Subtract the ViewportOffsetFromFrame to get the Viewport-relative location.
  104. Point viewportOffset = GetViewportOffsetFromFrame ();
  105. Point contentRelativeToViewport = location;
  106. contentRelativeToViewport.Offset (-Viewport.X, -Viewport.Y);
  107. // Translate to Screen-Relative (our SuperView's Viewport-relative coordinates)
  108. Rectangle screen = ViewportToScreen (new (contentRelativeToViewport, Size.Empty));
  109. return screen.Location;
  110. }
  111. /// <summary>Converts a Screen-relative coordinate to a Content-relative coordinate.</summary>
  112. /// <remarks>
  113. /// Content-relative means relative to the top-left corner of the view's Content, which is
  114. /// always at <c>0, 0</c>.
  115. /// </remarks>
  116. /// <param name="x">Column relative to the left side of the Content.</param>
  117. /// <param name="y">Row relative to the top of the Content</param>
  118. /// <returns>The coordinate relative to this view's Content.</returns>
  119. public Point ScreenToContent (in Point location)
  120. {
  121. Point viewportOffset = GetViewportOffsetFromFrame ();
  122. Point screen = ScreenToFrame (location.X, location.Y);
  123. screen.Offset (Viewport.X - viewportOffset.X, Viewport.Y - viewportOffset.Y);
  124. return screen;
  125. }
  126. #endregion Content Area
  127. #region Viewport
  128. private ScrollSettings _scrollSettings;
  129. /// <summary>
  130. /// Gets or sets how scrolling the <see cref="View.Viewport"/> on the View's Content Area is handled.
  131. /// </summary>
  132. public ScrollSettings ScrollSettings
  133. {
  134. get => _scrollSettings;
  135. set
  136. {
  137. if (_scrollSettings == value)
  138. {
  139. return;
  140. }
  141. _scrollSettings = value;
  142. // Force set Viewport to cause settings to be applied as needed
  143. SetViewport (Viewport);
  144. }
  145. }
  146. /// <summary>
  147. /// The location of the viewport into the view's content (0,0) is the top-left corner of the content. The Content
  148. /// area's size
  149. /// is <see cref="ContentSize"/>.
  150. /// </summary>
  151. private Point _viewportLocation;
  152. /// <summary>
  153. /// Gets or sets the rectangle describing the portion of the View's content that is visible to the user.
  154. /// The viewport Location is relative to the top-left corner of the inner rectangle of <see cref="Padding"/>.
  155. /// If the viewport Size is the same as <see cref="ContentSize"/> the Location will be <c>0, 0</c>.
  156. /// </summary>
  157. /// <value>
  158. /// The rectangle describing the location and size of the viewport into the View's virtual content, described by
  159. /// <see cref="ContentSize"/>.
  160. /// </value>
  161. /// <remarks>
  162. /// <para>
  163. /// Positive values for the location indicate the visible area is offset into (down-and-right) the View's virtual
  164. /// <see cref="ContentSize"/>. This enables virtual scrolling.
  165. /// </para>
  166. /// <para>
  167. /// Negative values for the location indicate the visible area is offset above (up-and-left) the View's virtual
  168. /// <see cref="ContentSize"/>. This enables virtual zoom.
  169. /// </para>
  170. /// <para>
  171. /// The <see cref="ScrollSettings"/> property controls how scrolling is handled. If <see cref="ScrollSettings"/> is
  172. /// </para>
  173. /// <para>
  174. /// If <see cref="LayoutStyle"/> is <see cref="LayoutStyle.Computed"/> the value of Viewport is indeterminate until
  175. /// the view has been initialized ( <see cref="IsInitialized"/> is true) and <see cref="LayoutSubviews"/> has been
  176. /// called.
  177. /// </para>
  178. /// <para>
  179. /// Updates to the Viewport Size updates <see cref="Frame"/>, and has the same impact as updating the
  180. /// <see cref="Frame"/>.
  181. /// </para>
  182. /// <para>
  183. /// Altering the Viewport Size will eventually (when the view is next laid out) cause the
  184. /// <see cref="LayoutSubview(View, Size)"/> and <see cref="OnDrawContent(Rectangle)"/> methods to be called.
  185. /// </para>
  186. /// </remarks>
  187. public virtual Rectangle Viewport
  188. {
  189. get
  190. {
  191. #if DEBUG
  192. if (LayoutStyle == LayoutStyle.Computed && !IsInitialized)
  193. {
  194. Debug.WriteLine (
  195. $"WARNING: Viewport is being accessed before the View has been initialized. This is likely a bug in {this}"
  196. );
  197. }
  198. #endif // DEBUG
  199. if (Margin is null || Border is null || Padding is null)
  200. {
  201. // CreateAdornments has not been called yet.
  202. return new (_viewportLocation, Frame.Size);
  203. }
  204. Thickness thickness = GetAdornmentsThickness ();
  205. return new (
  206. _viewportLocation,
  207. new (
  208. Math.Max (0, Frame.Size.Width - thickness.Horizontal),
  209. Math.Max (0, Frame.Size.Height - thickness.Vertical)
  210. ));
  211. }
  212. set => SetViewport (value);
  213. }
  214. private void SetViewport (Rectangle viewport)
  215. {
  216. ApplySettings (ref viewport);
  217. Thickness thickness = GetAdornmentsThickness ();
  218. Size newSize = new (
  219. viewport.Size.Width + thickness.Horizontal,
  220. viewport.Size.Height + thickness.Vertical);
  221. if (newSize == Frame.Size)
  222. {
  223. // The change is not changing the Frame, so we don't need to update it.
  224. // Just call SetNeedsLayout to update the layout.
  225. if (_viewportLocation != viewport.Location)
  226. {
  227. _viewportLocation = viewport.Location;
  228. SetNeedsLayout ();
  229. }
  230. return;
  231. }
  232. _viewportLocation = viewport.Location;
  233. // Update the Frame because we made it bigger or smaller which impacts subviews.
  234. Frame = Frame with
  235. {
  236. Size = newSize
  237. };
  238. void ApplySettings (ref Rectangle location)
  239. {
  240. if (!ScrollSettings.HasFlag (ScrollSettings.AllowViewportOutsideContentHorizontal))
  241. {
  242. if (location.Y + Viewport.Height > ContentSize.Height)
  243. {
  244. location.Y = ContentSize.Height - Viewport.Height;
  245. }
  246. if (location.Y < 0)
  247. {
  248. location.Y = 0;
  249. }
  250. }
  251. if (!ScrollSettings.HasFlag (ScrollSettings.AllowViewportOutsideContentVertical))
  252. {
  253. if (location.X + Viewport.Width > ContentSize.Width)
  254. {
  255. location.X = ContentSize.Width - Viewport.Width;
  256. }
  257. if (location.X < 0)
  258. {
  259. location.X = 0;
  260. }
  261. }
  262. }
  263. }
  264. /// <summary>
  265. /// Converts a <see cref="Viewport"/>-relative location to a screen-relative location.
  266. /// </summary>
  267. /// <remarks>
  268. /// Viewport-relative means relative to the top-left corner of the inner rectangle of the <see cref="Padding"/>.
  269. /// </remarks>
  270. public Rectangle ViewportToScreen (in Rectangle location)
  271. {
  272. // Translate bounds to Frame (our SuperView's Viewport-relative coordinates)
  273. Rectangle screen = FrameToScreen ();
  274. Point viewportOffset = GetViewportOffsetFromFrame ();
  275. screen.Offset (viewportOffset.X + location.X, viewportOffset.Y + location.Y);
  276. return new (screen.Location, location.Size);
  277. }
  278. /// <summary>Converts a screen-relative coordinate to a Viewport-relative coordinate.</summary>
  279. /// <returns>The coordinate relative to this view's <see cref="Viewport"/>.</returns>
  280. /// <remarks>
  281. /// Viewport-relative means relative to the top-left corner of the inner rectangle of the <see cref="Padding"/>.
  282. /// </remarks>
  283. /// <param name="x">Column relative to the left side of the Viewport.</param>
  284. /// <param name="y">Row relative to the top of the Viewport</param>
  285. public Point ScreenToViewport (int x, int y)
  286. {
  287. Point viewportOffset = GetViewportOffsetFromFrame ();
  288. Point screen = ScreenToFrame (x, y);
  289. screen.Offset (-viewportOffset.X, -viewportOffset.Y);
  290. return screen;
  291. }
  292. /// <summary>
  293. /// Helper to get the X and Y offset of the Viewport from the Frame. This is the sum of the Left and Top properties
  294. /// of <see cref="Margin"/>, <see cref="Border"/> and <see cref="Padding"/>.
  295. /// </summary>
  296. public Point GetViewportOffsetFromFrame () { return Padding is null ? Point.Empty : Padding.Thickness.GetInside (Padding.Frame).Location; }
  297. /// <summary>
  298. /// Scrolls the view vertically by the specified number of rows.
  299. /// </summary>
  300. /// <remarks>
  301. /// <para>
  302. /// </para>
  303. /// </remarks>
  304. /// <param name="rows"></param>
  305. /// <returns><see langword="true"/> if the <see cref="Viewport"/> was changed.</returns>
  306. public bool? ScrollVertical (int rows)
  307. {
  308. if (ContentSize == Size.Empty || ContentSize == Viewport.Size)
  309. {
  310. return false;
  311. }
  312. Viewport = Viewport with { Y = Viewport.Y + rows };
  313. return true;
  314. }
  315. /// <summary>
  316. /// Scrolls the view horizontally by the specified number of columns.
  317. /// </summary>
  318. /// <remarks>
  319. /// <para>
  320. /// </para>
  321. /// </remarks>
  322. /// <param name="cols"></param>
  323. /// <returns><see langword="true"/> if the <see cref="Viewport"/> was changed.</returns>
  324. public bool? ScrollHorizontal (int cols)
  325. {
  326. if (ContentSize == Size.Empty || ContentSize == Viewport.Size)
  327. {
  328. return false;
  329. }
  330. Viewport = Viewport with { X = Viewport.X + cols };
  331. return true;
  332. }
  333. #endregion Viewport
  334. }