ViewContent.cs 16 KB

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