View.Content.cs 19 KB

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