View.Content.cs 18 KB

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