View.Content.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. public partial class View
  4. {
  5. #region Content Area
  6. internal Size? _contentSize;
  7. /// <summary>
  8. /// Sets the size of the View's content.
  9. /// </summary>
  10. /// <remarks>
  11. /// <para>
  12. /// See the View Layout Deep Dive for more information: <see href="https://gui-cs.github.io/Terminal.GuiV2Docs/docs/layout.html"/>
  13. /// </para>
  14. /// <para>
  15. /// Negative sizes are not supported.
  16. /// </para>
  17. /// <para>
  18. /// If not explicitly set, and the View has no visible subviews, <see cref="GetContentSize ()"/> will return the
  19. /// size of
  20. /// <see cref="Viewport"/>.
  21. /// </para>
  22. /// <para>
  23. /// If not explicitly set, and the View has visible subviews, <see cref="GetContentSize ()"/> will return the
  24. /// maximum
  25. /// position + dimension of the Subviews, supporting <see cref="Dim.Auto"/> with the
  26. /// <see cref="DimAutoStyle.Content"/> flag set.
  27. /// </para>
  28. /// <para>
  29. /// If set <see cref="Viewport"/> describes the portion of the content currently visible to the user. This enables
  30. /// virtual scrolling.
  31. /// </para>
  32. /// <para>
  33. /// If set the behavior of <see cref="DimAutoStyle.Content"/> will be to use the ContentSize to determine the size
  34. /// of the view.
  35. /// </para>
  36. /// </remarks>
  37. public void SetContentSize (Size? contentSize)
  38. {
  39. if (contentSize is { } && (contentSize.Value.Width < 0 || contentSize.Value.Height < 0))
  40. {
  41. throw new ArgumentException (@"ContentSize cannot be negative.", nameof (contentSize));
  42. }
  43. if (contentSize == _contentSize)
  44. {
  45. return;
  46. }
  47. _contentSize = contentSize;
  48. OnContentSizeChanged (new (_contentSize));
  49. }
  50. /// <summary>
  51. /// Gets the size of the View's content.
  52. /// </summary>
  53. /// <remarks>a>
  54. /// <para>
  55. /// See the View Layout Deep Dive for more information: <see href="https://gui-cs.github.io/Terminal.GuiV2Docs/docs/layout.html"/>
  56. /// </para>
  57. /// <para>
  58. /// If the content size was not explicitly set by <see cref="SetContentSize"/>, and the View has no visible subviews, <see cref="GetContentSize ()"/> will return the
  59. /// size of
  60. /// <see cref="Viewport"/>.
  61. /// </para>
  62. /// <para>
  63. /// If the content size was not explicitly set by <see cref="SetContentSize"/>, and the View has visible subviews, <see cref="GetContentSize ()"/> will return the
  64. /// maximum
  65. /// position + dimension of the Subviews, supporting <see cref="Dim.Auto"/> with the
  66. /// <see cref="DimAutoStyle.Content"/> flag set.
  67. /// </para>
  68. /// <para>
  69. /// If set <see cref="Viewport"/> describes the portion of the content currently visible to the user. This enables
  70. /// virtual scrolling.
  71. /// </para>
  72. /// <para>
  73. /// If set the behavior of <see cref="DimAutoStyle.Content"/> will be to use the ContentSize to determine the size
  74. /// of the view.
  75. /// </para>
  76. /// </remarks>
  77. /// <returns>
  78. /// If the content size was not explicitly set by <see cref="SetContentSize"/>, <see cref="GetContentSize ()"/> will
  79. /// return the size of the <see cref="Viewport"/> and <see cref="ContentSizeTracksViewport"/> will be <see langword="true"/>.
  80. /// </returns>
  81. public Size GetContentSize () { return _contentSize ?? Viewport.Size; }
  82. /// <summary>
  83. /// Gets or sets a value indicating whether the view's content size tracks the <see cref="Viewport"/>'s
  84. /// size or not.
  85. /// </summary>
  86. /// <remarks>
  87. /// <para>
  88. /// See the View Layout Deep Dive for more information: <see href="https://gui-cs.github.io/Terminal.GuiV2Docs/docs/layout.html"/>
  89. /// </para>
  90. /// <list type="bullet">
  91. /// <listheader>
  92. /// <term>Value</term> <description>Result</description>
  93. /// </listheader>
  94. /// <item>
  95. /// <term>
  96. /// <see langword="true"/>
  97. /// </term>
  98. /// <description>
  99. /// <para>
  100. /// <see cref="GetContentSize ()"/> will return the <see cref="Viewport"/>'s size. Content scrolling
  101. /// will be
  102. /// disabled.
  103. /// </para>
  104. /// <para>
  105. /// The behavior of <see cref="DimAutoStyle.Content"/> will be to use position and size of the Subviews
  106. /// to
  107. /// determine the size of the view, ignoring <see cref="GetContentSize ()"/>.
  108. /// </para>
  109. /// </description>
  110. /// </item>
  111. /// <item>
  112. /// <term>
  113. /// <see langword="false"/>
  114. /// </term>
  115. /// <description>
  116. /// <para>
  117. /// The return value of <see cref="GetContentSize ()"/> is independent of <see cref="Viewport"/> and <see cref="Viewport"/>
  118. /// describes the portion of the content currently visible to the user enabling content scrolling.
  119. /// </para>
  120. /// <para>
  121. /// The behavior of <see cref="DimAutoStyle.Content"/> will be to use <see cref="GetContentSize ()"/>
  122. /// to
  123. /// determine the
  124. /// size of the view, ignoring the position and size of the Subviews.
  125. /// </para>
  126. /// </description>
  127. /// </item>
  128. /// </list>
  129. /// </remarks>
  130. public bool ContentSizeTracksViewport
  131. {
  132. get => _contentSize is null;
  133. set => _contentSize = value ? null : _contentSize;
  134. }
  135. /// <summary>
  136. /// Called when <see cref="GetContentSize ()"/> has changed.
  137. /// </summary>
  138. /// <param name="e"></param>
  139. /// <returns></returns>
  140. protected bool? OnContentSizeChanged (SizeChangedEventArgs e)
  141. {
  142. ContentSizeChanged?.Invoke (this, e);
  143. if (e.Cancel != true)
  144. {
  145. SetNeedsLayout ();
  146. }
  147. return e.Cancel;
  148. }
  149. /// <summary>
  150. /// Event raised when the <see cref="GetContentSize ()"/> changes.
  151. /// </summary>
  152. public event EventHandler<SizeChangedEventArgs>? ContentSizeChanged;
  153. /// <summary>
  154. /// Converts a Content-relative location to a Screen-relative location.
  155. /// </summary>
  156. /// <param name="location">The Content-relative location.</param>
  157. /// <returns>The Screen-relative location.</returns>
  158. public Point ContentToScreen (in Point location)
  159. {
  160. // Subtract the ViewportOffsetFromFrame to get the Viewport-relative location.
  161. Point viewportOffset = GetViewportOffsetFromFrame ();
  162. Point contentRelativeToViewport = location;
  163. contentRelativeToViewport.Offset (-Viewport.X, -Viewport.Y);
  164. // Translate to Screen-Relative (our SuperView's Viewport-relative coordinates)
  165. return ViewportToScreen (contentRelativeToViewport);
  166. }
  167. /// <summary>Converts a Screen-relative coordinate to a Content-relative coordinate.</summary>
  168. /// <remarks>
  169. /// Content-relative means relative to the top-left corner of the view's Content, which is
  170. /// always at <c>0, 0</c>.
  171. /// </remarks>
  172. /// <param name="location">The Screen-relative location.</param>
  173. /// <returns>The coordinate relative to this view's Content.</returns>
  174. public Point ScreenToContent (in Point location)
  175. {
  176. Point viewportOffset = GetViewportOffsetFromFrame ();
  177. Point screen = ScreenToFrame (location);
  178. screen.Offset (Viewport.X - viewportOffset.X, Viewport.Y - viewportOffset.Y);
  179. return screen;
  180. }
  181. #endregion Content Area
  182. #region Viewport
  183. private ViewportSettings _viewportSettings;
  184. /// <summary>
  185. /// Gets or sets how scrolling the <see cref="View.Viewport"/> on the View's Content Area is handled.
  186. /// </summary>
  187. public ViewportSettings ViewportSettings
  188. {
  189. get => _viewportSettings;
  190. set
  191. {
  192. if (_viewportSettings == value)
  193. {
  194. return;
  195. }
  196. _viewportSettings = value;
  197. if (IsInitialized)
  198. {
  199. // Force set Viewport to cause settings to be applied as needed
  200. SetViewport (Viewport);
  201. }
  202. }
  203. }
  204. /// <summary>
  205. /// The location of the viewport into the view's content (0,0) is the top-left corner of the content. The Content
  206. /// area's size
  207. /// is <see cref="GetContentSize ()"/>.
  208. /// </summary>
  209. private Point _viewportLocation;
  210. /// <summary>
  211. /// Gets or sets the rectangle describing the portion of the View's content that is visible to the user.
  212. /// The viewport Location is relative to the top-left corner of the inner rectangle of <see cref="Padding"/>.
  213. /// If the viewport Size is the same as <see cref="GetContentSize ()"/>, or <see cref="GetContentSize ()"/> is
  214. /// <see langword="null"/> the Location will be <c>0, 0</c>.
  215. /// </summary>
  216. /// <value>
  217. /// The rectangle describing the location and size of the viewport into the View's virtual content, described by
  218. /// <see cref="GetContentSize ()"/>.
  219. /// </value>
  220. /// <remarks>
  221. /// <para>
  222. /// See the View Layout Deep Dive for more information: <see href="https://gui-cs.github.io/Terminal.GuiV2Docs/docs/layout.html"/>
  223. /// </para>
  224. /// <para>
  225. /// Positive values for the location indicate the visible area is offset into (down-and-right) the View's virtual
  226. /// <see cref="GetContentSize ()"/>. This enables scrolling down and to the right (e.g. in a <see cref="ListView"/>
  227. /// .
  228. /// </para>
  229. /// <para>
  230. /// Negative values for the location indicate the visible area is offset above (up-and-left) the View's virtual
  231. /// <see cref="GetContentSize ()"/>. This enables scrolling up and to the left (e.g. in an image viewer that
  232. /// supports
  233. /// zoom
  234. /// where the image stays centered).
  235. /// </para>
  236. /// <para>
  237. /// The <see cref="ViewportSettings"/> property controls how scrolling is handled.
  238. /// </para>
  239. /// <para>
  240. /// Updates to the Viewport Size updates <see cref="Frame"/>, and has the same impact as updating the
  241. /// <see cref="Frame"/>.
  242. /// </para>
  243. /// <para>
  244. /// Altering the Viewport Size will eventually (when the view is next laid out) cause the
  245. /// <see cref="Layout"/> and <see cref="OnDrawingContent"/> methods to be called.
  246. /// </para>
  247. /// </remarks>
  248. public virtual Rectangle Viewport
  249. {
  250. get
  251. {
  252. if (Margin is null || Border is null || Padding is null)
  253. {
  254. // CreateAdornments has not been called yet.
  255. return new (_viewportLocation, Frame.Size);
  256. }
  257. Thickness thickness = GetAdornmentsThickness ();
  258. return new (
  259. _viewportLocation,
  260. new (
  261. Math.Max (0, Frame.Size.Width - thickness.Horizontal),
  262. Math.Max (0, Frame.Size.Height - thickness.Vertical)
  263. ));
  264. }
  265. set => SetViewport (value);
  266. }
  267. private void SetViewport (Rectangle viewport)
  268. {
  269. Rectangle oldViewport = viewport;
  270. ApplySettings (ref viewport);
  271. Thickness thickness = GetAdornmentsThickness ();
  272. Size newSize = new (
  273. viewport.Size.Width + thickness.Horizontal,
  274. viewport.Size.Height + thickness.Vertical);
  275. if (newSize == Frame.Size)
  276. {
  277. // The change is not changing the Frame, so we don't need to update it.
  278. // Just call SetNeedsLayout to update the layout.
  279. if (_viewportLocation != viewport.Location)
  280. {
  281. _viewportLocation = viewport.Location;
  282. SetNeedsLayout ();
  283. }
  284. OnViewportChanged (new (IsInitialized ? Viewport : Rectangle.Empty, oldViewport));
  285. return;
  286. }
  287. _viewportLocation = viewport.Location;
  288. // Update the Frame because we made it bigger or smaller which impacts subviews.
  289. Frame = Frame with
  290. {
  291. Size = newSize
  292. };
  293. void ApplySettings (ref Rectangle newViewport)
  294. {
  295. if (!ViewportSettings.HasFlag (ViewportSettings.AllowXGreaterThanContentWidth))
  296. {
  297. if (newViewport.X >= GetContentSize ().Width)
  298. {
  299. newViewport.X = GetContentSize ().Width - 1;
  300. }
  301. }
  302. // IMPORTANT: Check for negative location AFTER checking for location greater than content width
  303. if (!ViewportSettings.HasFlag (ViewportSettings.AllowNegativeX))
  304. {
  305. if (newViewport.X < 0)
  306. {
  307. newViewport.X = 0;
  308. }
  309. }
  310. if (!ViewportSettings.HasFlag (ViewportSettings.AllowYGreaterThanContentHeight))
  311. {
  312. if (newViewport.Y >= GetContentSize ().Height)
  313. {
  314. newViewport.Y = GetContentSize ().Height - 1;
  315. }
  316. }
  317. // IMPORTANT: Check for negative location AFTER checking for location greater than content width
  318. if (!ViewportSettings.HasFlag (ViewportSettings.AllowNegativeY))
  319. {
  320. if (newViewport.Y < 0)
  321. {
  322. newViewport.Y = 0;
  323. }
  324. }
  325. }
  326. }
  327. /// <summary>
  328. /// Fired when the <see cref="Viewport"/> changes. This event is fired after the <see cref="Viewport"/> has been
  329. /// updated.
  330. /// </summary>
  331. public event EventHandler<DrawEventArgs>? ViewportChanged;
  332. /// <summary>
  333. /// Called when the <see cref="Viewport"/> changes. Invokes the <see cref="ViewportChanged"/> event.
  334. /// </summary>
  335. /// <param name="e"></param>
  336. protected virtual void OnViewportChanged (DrawEventArgs e) { ViewportChanged?.Invoke (this, e); }
  337. /// <summary>
  338. /// Converts a <see cref="Viewport"/>-relative location and size to a screen-relative location and size.
  339. /// </summary>
  340. /// <remarks>
  341. /// Viewport-relative means relative to the top-left corner of the inner rectangle of the <see cref="Padding"/>.
  342. /// </remarks>
  343. /// <param name="viewport">Viewport-relative location and size.</param>
  344. /// <returns>Screen-relative location and size.</returns>
  345. public Rectangle ViewportToScreen (in Rectangle viewport) { return viewport with { Location = ViewportToScreen (viewport.Location) }; }
  346. /// <summary>
  347. /// Converts a <see cref="Viewport"/>-relative location to a screen-relative location.
  348. /// </summary>
  349. /// <remarks>
  350. /// Viewport-relative means relative to the top-left corner of the inner rectangle of the <see cref="Padding"/>.
  351. /// </remarks>
  352. /// <param name="viewportLocation">Viewport-relative location.</param>
  353. /// <returns>Screen-relative location.</returns>
  354. public Point ViewportToScreen (in Point viewportLocation)
  355. {
  356. // Translate bounds to Frame (our SuperView's Viewport-relative coordinates)
  357. Rectangle screen = FrameToScreen ();
  358. Point viewportOffset = GetViewportOffsetFromFrame ();
  359. screen.Offset (viewportOffset.X + viewportLocation.X, viewportOffset.Y + viewportLocation.Y);
  360. return screen.Location;
  361. }
  362. /// <summary>Converts a screen-relative coordinate to a Viewport-relative coordinate.</summary>
  363. /// <returns>The coordinate relative to this view's <see cref="Viewport"/>.</returns>
  364. /// <remarks>
  365. /// Viewport-relative means relative to the top-left corner of the inner rectangle of the <see cref="Padding"/>.
  366. /// </remarks>
  367. /// <param name="location">Screen-Relative Coordinate.</param>
  368. /// <returns>Viewport-relative location.</returns>
  369. public Point ScreenToViewport (in Point location)
  370. {
  371. Point viewportOffset = GetViewportOffsetFromFrame ();
  372. Point frame = ScreenToFrame (location);
  373. frame.Offset (-viewportOffset.X, -viewportOffset.Y);
  374. return frame;
  375. }
  376. /// <summary>
  377. /// Helper to get the X and Y offset of the Viewport from the Frame. This is the sum of the Left and Top properties
  378. /// of <see cref="Margin"/>, <see cref="Border"/> and <see cref="Padding"/>.
  379. /// </summary>
  380. public Point GetViewportOffsetFromFrame () { return Padding is null ? Point.Empty : Padding.Thickness.GetInside (Padding.Frame).Location; }
  381. /// <summary>
  382. /// Scrolls the view vertically by the specified number of rows.
  383. /// </summary>
  384. /// <remarks>
  385. /// <para>
  386. /// </para>
  387. /// </remarks>
  388. /// <param name="rows"></param>
  389. /// <returns><see langword="true"/> if the <see cref="Viewport"/> was changed.</returns>
  390. public bool? ScrollVertical (int rows)
  391. {
  392. if (GetContentSize () == Size.Empty || GetContentSize () == Viewport.Size)
  393. {
  394. return false;
  395. }
  396. Viewport = Viewport with { Y = Viewport.Y + rows };
  397. return true;
  398. }
  399. /// <summary>
  400. /// Scrolls the view horizontally by the specified number of columns.
  401. /// </summary>
  402. /// <remarks>
  403. /// <para>
  404. /// </para>
  405. /// </remarks>
  406. /// <param name="cols"></param>
  407. /// <returns><see langword="true"/> if the <see cref="Viewport"/> was changed.</returns>
  408. public bool? ScrollHorizontal (int cols)
  409. {
  410. if (GetContentSize () == Size.Empty || GetContentSize () == Viewport.Size)
  411. {
  412. return false;
  413. }
  414. Viewport = Viewport with { X = Viewport.X + cols };
  415. return true;
  416. }
  417. #endregion Viewport
  418. }