View.Content.cs 19 KB

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