View.Content.cs 19 KB

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