View.Content.cs 21 KB

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