View.Content.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. OnResizeNeeded ();
  146. //SetNeedsLayout ();
  147. //SetNeedsDisplay ();
  148. }
  149. return e.Cancel;
  150. }
  151. /// <summary>
  152. /// Event raised when the <see cref="GetContentSize ()"/> changes.
  153. /// </summary>
  154. public event EventHandler<SizeChangedEventArgs>? ContentSizeChanged;
  155. /// <summary>
  156. /// Converts a Content-relative location to a Screen-relative location.
  157. /// </summary>
  158. /// <param name="location">The Content-relative location.</param>
  159. /// <returns>The Screen-relative location.</returns>
  160. public Point ContentToScreen (in Point location)
  161. {
  162. // Subtract the ViewportOffsetFromFrame to get the Viewport-relative location.
  163. Point viewportOffset = GetViewportOffsetFromFrame ();
  164. Point contentRelativeToViewport = location;
  165. contentRelativeToViewport.Offset (-Viewport.X, -Viewport.Y);
  166. // Translate to Screen-Relative (our SuperView's Viewport-relative coordinates)
  167. return ViewportToScreen (contentRelativeToViewport);
  168. }
  169. /// <summary>Converts a Screen-relative coordinate to a Content-relative coordinate.</summary>
  170. /// <remarks>
  171. /// Content-relative means relative to the top-left corner of the view's Content, which is
  172. /// always at <c>0, 0</c>.
  173. /// </remarks>
  174. /// <param name="location">The Screen-relative location.</param>
  175. /// <returns>The coordinate relative to this view's Content.</returns>
  176. public Point ScreenToContent (in Point location)
  177. {
  178. Point viewportOffset = GetViewportOffsetFromFrame ();
  179. Point screen = ScreenToFrame (location);
  180. screen.Offset (Viewport.X - viewportOffset.X, Viewport.Y - viewportOffset.Y);
  181. return screen;
  182. }
  183. #endregion Content Area
  184. #region Viewport
  185. private ViewportSettings _viewportSettings;
  186. /// <summary>
  187. /// Gets or sets how scrolling the <see cref="View.Viewport"/> on the View's Content Area is handled.
  188. /// </summary>
  189. public ViewportSettings ViewportSettings
  190. {
  191. get => _viewportSettings;
  192. set
  193. {
  194. if (_viewportSettings == value)
  195. {
  196. return;
  197. }
  198. _viewportSettings = value;
  199. if (IsInitialized)
  200. {
  201. // Force set Viewport to cause settings to be applied as needed
  202. SetViewport (Viewport);
  203. }
  204. }
  205. }
  206. /// <summary>
  207. /// The location of the viewport into the view's content (0,0) is the top-left corner of the content. The Content
  208. /// area's size
  209. /// is <see cref="GetContentSize ()"/>.
  210. /// </summary>
  211. private Point _viewportLocation;
  212. /// <summary>
  213. /// Gets or sets the rectangle describing the portion of the View's content that is visible to the user.
  214. /// The viewport Location is relative to the top-left corner of the inner rectangle of <see cref="Padding"/>.
  215. /// If the viewport Size is the same as <see cref="GetContentSize ()"/>, or <see cref="GetContentSize ()"/> is
  216. /// <see langword="null"/> the Location will be <c>0, 0</c>.
  217. /// </summary>
  218. /// <value>
  219. /// The rectangle describing the location and size of the viewport into the View's virtual content, described by
  220. /// <see cref="GetContentSize ()"/>.
  221. /// </value>
  222. /// <remarks>
  223. /// <para>
  224. /// See the View Layout Deep Dive for more information: <see href="https://gui-cs.github.io/Terminal.GuiV2Docs/docs/layout.html"/>
  225. /// </para>
  226. /// <para>
  227. /// Positive values for the location indicate the visible area is offset into (down-and-right) the View's virtual
  228. /// <see cref="GetContentSize ()"/>. This enables scrolling down and to the right (e.g. in a <see cref="ListView"/>
  229. /// .
  230. /// </para>
  231. /// <para>
  232. /// Negative values for the location indicate the visible area is offset above (up-and-left) the View's virtual
  233. /// <see cref="GetContentSize ()"/>. This enables scrolling up and to the left (e.g. in an image viewer that
  234. /// supports
  235. /// zoom
  236. /// where the image stays centered).
  237. /// </para>
  238. /// <para>
  239. /// The <see cref="ViewportSettings"/> property controls how scrolling is handled.
  240. /// </para>
  241. /// <para>
  242. /// Updates to the Viewport Size updates <see cref="Frame"/>, and has the same impact as updating the
  243. /// <see cref="Frame"/>.
  244. /// </para>
  245. /// <para>
  246. /// Altering the Viewport Size will eventually (when the view is next laid out) cause the
  247. /// <see cref="LayoutSubview(View, Size)"/> and <see cref="OnDrawContent(Rectangle)"/> methods to be called.
  248. /// </para>
  249. /// </remarks>
  250. public virtual Rectangle Viewport
  251. {
  252. get
  253. {
  254. if (Margin is null || Border is null || Padding is null)
  255. {
  256. // CreateAdornments has not been called yet.
  257. return new (_viewportLocation, Frame.Size);
  258. }
  259. Thickness thickness = GetAdornmentsThickness ();
  260. return new (
  261. _viewportLocation,
  262. new (
  263. Math.Max (0, Frame.Size.Width - thickness.Horizontal),
  264. Math.Max (0, Frame.Size.Height - thickness.Vertical)
  265. ));
  266. }
  267. set => SetViewport (value);
  268. }
  269. private void SetViewport (Rectangle viewport)
  270. {
  271. Rectangle oldViewport = viewport;
  272. ApplySettings (ref viewport);
  273. Thickness thickness = GetAdornmentsThickness ();
  274. Size newSize = new (
  275. viewport.Size.Width + thickness.Horizontal,
  276. viewport.Size.Height + thickness.Vertical);
  277. if (newSize == Frame.Size)
  278. {
  279. // The change is not changing the Frame, so we don't need to update it.
  280. // Just call SetNeedsLayout to update the layout.
  281. if (_viewportLocation != viewport.Location)
  282. {
  283. _viewportLocation = viewport.Location;
  284. SetNeedsLayout ();
  285. }
  286. OnViewportChanged (new (IsInitialized ? Viewport : Rectangle.Empty, oldViewport));
  287. return;
  288. }
  289. _viewportLocation = viewport.Location;
  290. // Update the Frame because we made it bigger or smaller which impacts subviews.
  291. Frame = Frame with
  292. {
  293. Size = newSize
  294. };
  295. void ApplySettings (ref Rectangle newViewport)
  296. {
  297. if (!ViewportSettings.HasFlag (ViewportSettings.AllowXGreaterThanContentWidth))
  298. {
  299. if (newViewport.X >= GetContentSize ().Width)
  300. {
  301. newViewport.X = GetContentSize ().Width - 1;
  302. }
  303. }
  304. // IMPORTANT: Check for negative location AFTER checking for location greater than content width
  305. if (!ViewportSettings.HasFlag (ViewportSettings.AllowNegativeX))
  306. {
  307. if (newViewport.X < 0)
  308. {
  309. newViewport.X = 0;
  310. }
  311. }
  312. if (!ViewportSettings.HasFlag (ViewportSettings.AllowYGreaterThanContentHeight))
  313. {
  314. if (newViewport.Y >= GetContentSize ().Height)
  315. {
  316. newViewport.Y = GetContentSize ().Height - 1;
  317. }
  318. }
  319. // IMPORTANT: Check for negative location AFTER checking for location greater than content width
  320. if (!ViewportSettings.HasFlag (ViewportSettings.AllowNegativeY))
  321. {
  322. if (newViewport.Y < 0)
  323. {
  324. newViewport.Y = 0;
  325. }
  326. }
  327. }
  328. }
  329. /// <summary>
  330. /// Fired when the <see cref="Viewport"/> changes. This event is fired after the <see cref="Viewport"/> has been
  331. /// updated.
  332. /// </summary>
  333. public event EventHandler<DrawEventArgs>? ViewportChanged;
  334. /// <summary>
  335. /// Called when the <see cref="Viewport"/> changes. Invokes the <see cref="ViewportChanged"/> event.
  336. /// </summary>
  337. /// <param name="e"></param>
  338. protected virtual void OnViewportChanged (DrawEventArgs e) { ViewportChanged?.Invoke (this, e); }
  339. /// <summary>
  340. /// Converts a <see cref="Viewport"/>-relative location and size to a screen-relative location and size.
  341. /// </summary>
  342. /// <remarks>
  343. /// Viewport-relative means relative to the top-left corner of the inner rectangle of the <see cref="Padding"/>.
  344. /// </remarks>
  345. /// <param name="viewport">Viewport-relative location and size.</param>
  346. /// <returns>Screen-relative location and size.</returns>
  347. public Rectangle ViewportToScreen (in Rectangle viewport) { return viewport with { Location = ViewportToScreen (viewport.Location) }; }
  348. /// <summary>
  349. /// Converts a <see cref="Viewport"/>-relative location to a screen-relative location.
  350. /// </summary>
  351. /// <remarks>
  352. /// Viewport-relative means relative to the top-left corner of the inner rectangle of the <see cref="Padding"/>.
  353. /// </remarks>
  354. /// <param name="viewportLocation">Viewport-relative location.</param>
  355. /// <returns>Screen-relative location.</returns>
  356. public Point ViewportToScreen (in Point viewportLocation)
  357. {
  358. // Translate bounds to Frame (our SuperView's Viewport-relative coordinates)
  359. Rectangle screen = FrameToScreen ();
  360. Point viewportOffset = GetViewportOffsetFromFrame ();
  361. screen.Offset (viewportOffset.X + viewportLocation.X, viewportOffset.Y + viewportLocation.Y);
  362. return screen.Location;
  363. }
  364. /// <summary>Converts a screen-relative coordinate to a Viewport-relative coordinate.</summary>
  365. /// <returns>The coordinate relative to this view's <see cref="Viewport"/>.</returns>
  366. /// <remarks>
  367. /// Viewport-relative means relative to the top-left corner of the inner rectangle of the <see cref="Padding"/>.
  368. /// </remarks>
  369. /// <param name="location">Screen-Relative Coordinate.</param>
  370. /// <returns>Viewport-relative location.</returns>
  371. public Point ScreenToViewport (in Point location)
  372. {
  373. Point viewportOffset = GetViewportOffsetFromFrame ();
  374. Point frame = ScreenToFrame (location);
  375. frame.Offset (-viewportOffset.X, -viewportOffset.Y);
  376. return frame;
  377. }
  378. /// <summary>
  379. /// Helper to get the X and Y offset of the Viewport from the Frame. This is the sum of the Left and Top properties
  380. /// of <see cref="Margin"/>, <see cref="Border"/> and <see cref="Padding"/>.
  381. /// </summary>
  382. public Point GetViewportOffsetFromFrame () { return Padding is null ? Point.Empty : Padding.Thickness.GetInside (Padding.Frame).Location; }
  383. /// <summary>
  384. /// Scrolls the view vertically by the specified number of rows.
  385. /// </summary>
  386. /// <remarks>
  387. /// <para>
  388. /// </para>
  389. /// </remarks>
  390. /// <param name="rows"></param>
  391. /// <returns><see langword="true"/> if the <see cref="Viewport"/> was changed.</returns>
  392. public bool? ScrollVertical (int rows)
  393. {
  394. if (GetContentSize () == Size.Empty || GetContentSize () == Viewport.Size)
  395. {
  396. return false;
  397. }
  398. Viewport = Viewport with { Y = Viewport.Y + rows };
  399. return true;
  400. }
  401. /// <summary>
  402. /// Scrolls the view horizontally by the specified number of columns.
  403. /// </summary>
  404. /// <remarks>
  405. /// <para>
  406. /// </para>
  407. /// </remarks>
  408. /// <param name="cols"></param>
  409. /// <returns><see langword="true"/> if the <see cref="Viewport"/> was changed.</returns>
  410. public bool? ScrollHorizontal (int cols)
  411. {
  412. if (GetContentSize () == Size.Empty || GetContentSize () == Viewport.Size)
  413. {
  414. return false;
  415. }
  416. Viewport = Viewport with { X = Viewport.X + cols };
  417. return true;
  418. }
  419. #endregion Viewport
  420. }