2
0

View.Content.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. namespace Terminal.Gui;
  2. public partial class View
  3. {
  4. #region Content Area
  5. internal Size? _contentSize;
  6. /// <summary>
  7. /// Sets the size of the View's content.
  8. /// </summary>
  9. /// <remarks>
  10. /// <para>
  11. /// Negative sizes are not supported.
  12. /// </para>
  13. /// <para>
  14. /// If not explicitly set, and the View has no visible subviews, <see cref="GetContentSize ()"/> will return the
  15. /// size of
  16. /// <see cref="Viewport"/>.
  17. /// </para>
  18. /// <para>
  19. /// If not explicitly set, and the View has visible subviews, <see cref="GetContentSize ()"/> will return the
  20. /// maximum
  21. /// position + dimension of the Subviews, supporting <see cref="Dim.Auto"/> with the
  22. /// <see cref="DimAutoStyle.Content"/> flag set.
  23. /// </para>
  24. /// <para>
  25. /// If set <see cref="Viewport"/> describes the portion of the content currently visible to the user. This enables
  26. /// virtual scrolling.
  27. /// </para>
  28. /// <para>
  29. /// If set the behavior of <see cref="DimAutoStyle.Content"/> will be to use the ContentSize 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. /// 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
  52. /// size of
  53. /// <see cref="Viewport"/>.
  54. /// </para>
  55. /// <para>
  56. /// If the content size was not explicitly set by <see cref="SetContentSize"/>, and the View has visible subviews, <see cref="GetContentSize ()"/> will return the
  57. /// maximum
  58. /// position + dimension of the Subviews, supporting <see cref="Dim.Auto"/> with the
  59. /// <see cref="DimAutoStyle.Content"/> flag set.
  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. /// <list type="bullet">
  81. /// <listheader>
  82. /// <term>Value</term> <description>Result</description>
  83. /// </listheader>
  84. /// <item>
  85. /// <term>
  86. /// <see langword="true"/>
  87. /// </term>
  88. /// <description>
  89. /// <para>
  90. /// <see cref="GetContentSize ()"/> will return the <see cref="Viewport"/>'s size. Content scrolling
  91. /// will be
  92. /// disabled.
  93. /// </para>
  94. /// <para>
  95. /// The behavior of <see cref="DimAutoStyle.Content"/> will be to use position and size of the Subviews
  96. /// to
  97. /// determine the size of the view, ignoring <see cref="GetContentSize ()"/>.
  98. /// </para>
  99. /// </description>
  100. /// </item>
  101. /// <item>
  102. /// <term>
  103. /// <see langword="false"/>
  104. /// </term>
  105. /// <description>
  106. /// <para>
  107. /// The return value of <see cref="GetContentSize ()"/> is independent of <see cref="Viewport"/> and <see cref="Viewport"/>
  108. /// describes the portion of the content currently visible to the user enabling content scrolling.
  109. /// </para>
  110. /// <para>
  111. /// The behavior of <see cref="DimAutoStyle.Content"/> will be to use <see cref="GetContentSize ()"/>
  112. /// to
  113. /// determine the
  114. /// size of the view, ignoring the position and size of the Subviews.
  115. /// </para>
  116. /// </description>
  117. /// </item>
  118. /// </list>
  119. /// </remarks>
  120. public bool ContentSizeTracksViewport
  121. {
  122. get => _contentSize is null;
  123. set => _contentSize = value ? null : _contentSize;
  124. }
  125. /// <summary>
  126. /// Called when <see cref="GetContentSize ()"/> has changed.
  127. /// </summary>
  128. /// <param name="e"></param>
  129. /// <returns></returns>
  130. protected bool? OnContentSizeChanged (SizeChangedEventArgs e)
  131. {
  132. ContentSizeChanged?.Invoke (this, e);
  133. if (e.Cancel != true)
  134. {
  135. OnResizeNeeded ();
  136. //SetNeedsLayout ();
  137. //SetNeedsDisplay ();
  138. }
  139. return e.Cancel;
  140. }
  141. /// <summary>
  142. /// Event raised when the <see cref="GetContentSize ()"/> changes.
  143. /// </summary>
  144. public event EventHandler<SizeChangedEventArgs> ContentSizeChanged;
  145. /// <summary>
  146. /// Converts a Content-relative location to a Screen-relative location.
  147. /// </summary>
  148. /// <param name="location">The Content-relative location.</param>
  149. /// <returns>The Screen-relative location.</returns>
  150. public Point ContentToScreen (in Point location)
  151. {
  152. // Subtract the ViewportOffsetFromFrame to get the Viewport-relative location.
  153. Point viewportOffset = GetViewportOffsetFromFrame ();
  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. /// Positive values for the location indicate the visible area is offset into (down-and-right) the View's virtual
  215. /// <see cref="GetContentSize ()"/>. This enables scrolling down and to the right (e.g. in a <see cref="ListView"/>
  216. /// .
  217. /// </para>
  218. /// <para>
  219. /// Negative values for the location indicate the visible area is offset above (up-and-left) the View's virtual
  220. /// <see cref="GetContentSize ()"/>. This enables scrolling up and to the left (e.g. in an image viewer that
  221. /// supports
  222. /// zoom
  223. /// where the image stays centered).
  224. /// </para>
  225. /// <para>
  226. /// The <see cref="ViewportSettings"/> property controls how scrolling is handled.
  227. /// </para>
  228. /// <para>
  229. /// Updates to the Viewport Size updates <see cref="Frame"/>, and has the same impact as updating the
  230. /// <see cref="Frame"/>.
  231. /// </para>
  232. /// <para>
  233. /// Altering the Viewport Size will eventually (when the view is next laid out) cause the
  234. /// <see cref="LayoutSubview(View, Size)"/> and <see cref="OnDrawContent(Rectangle)"/> methods to be called.
  235. /// </para>
  236. /// </remarks>
  237. public virtual Rectangle Viewport
  238. {
  239. get
  240. {
  241. if (Margin is null || Border is null || Padding is null)
  242. {
  243. // CreateAdornments has not been called yet.
  244. return new (_viewportLocation, Frame.Size);
  245. }
  246. Thickness thickness = GetAdornmentsThickness ();
  247. return new (
  248. _viewportLocation,
  249. new (
  250. Math.Max (0, Frame.Size.Width - thickness.Horizontal),
  251. Math.Max (0, Frame.Size.Height - thickness.Vertical)
  252. ));
  253. }
  254. set => SetViewport (value);
  255. }
  256. private void SetViewport (Rectangle viewport)
  257. {
  258. Rectangle oldViewport = viewport;
  259. ApplySettings (ref viewport);
  260. Thickness thickness = GetAdornmentsThickness ();
  261. Size newSize = new (
  262. viewport.Size.Width + thickness.Horizontal,
  263. viewport.Size.Height + thickness.Vertical);
  264. if (newSize == Frame.Size)
  265. {
  266. // The change is not changing the Frame, so we don't need to update it.
  267. // Just call SetNeedsLayout to update the layout.
  268. if (_viewportLocation != viewport.Location)
  269. {
  270. _viewportLocation = viewport.Location;
  271. SetNeedsLayout ();
  272. }
  273. OnViewportChanged (new (IsInitialized ? Viewport : Rectangle.Empty, oldViewport));
  274. return;
  275. }
  276. _viewportLocation = viewport.Location;
  277. // Update the Frame because we made it bigger or smaller which impacts subviews.
  278. Frame = Frame with
  279. {
  280. Size = newSize
  281. };
  282. void ApplySettings (ref Rectangle newViewport)
  283. {
  284. if (!ViewportSettings.HasFlag (ViewportSettings.AllowXGreaterThanContentWidth))
  285. {
  286. if (newViewport.X >= GetContentSize ().Width)
  287. {
  288. newViewport.X = GetContentSize ().Width - 1;
  289. }
  290. }
  291. // IMPORTANT: Check for negative location AFTER checking for location greater than content width
  292. if (!ViewportSettings.HasFlag (ViewportSettings.AllowNegativeX))
  293. {
  294. if (newViewport.X < 0)
  295. {
  296. newViewport.X = 0;
  297. }
  298. }
  299. if (!ViewportSettings.HasFlag (ViewportSettings.AllowYGreaterThanContentHeight))
  300. {
  301. if (newViewport.Y >= GetContentSize ().Height)
  302. {
  303. newViewport.Y = GetContentSize ().Height - 1;
  304. }
  305. }
  306. // IMPORTANT: Check for negative location AFTER checking for location greater than content width
  307. if (!ViewportSettings.HasFlag (ViewportSettings.AllowNegativeY))
  308. {
  309. if (newViewport.Y < 0)
  310. {
  311. newViewport.Y = 0;
  312. }
  313. }
  314. }
  315. }
  316. /// <summary>
  317. /// Fired when the <see cref="Viewport"/> changes. This event is fired after the <see cref="Viewport"/> has been
  318. /// updated.
  319. /// </summary>
  320. [CanBeNull]
  321. public event EventHandler<DrawEventArgs> ViewportChanged;
  322. /// <summary>
  323. /// Called when the <see cref="Viewport"/> changes. Invokes the <see cref="ViewportChanged"/> event.
  324. /// </summary>
  325. /// <param name="e"></param>
  326. protected virtual void OnViewportChanged (DrawEventArgs e) { ViewportChanged?.Invoke (this, e); }
  327. /// <summary>
  328. /// Converts a <see cref="Viewport"/>-relative location and size to a screen-relative location and size.
  329. /// </summary>
  330. /// <remarks>
  331. /// Viewport-relative means relative to the top-left corner of the inner rectangle of the <see cref="Padding"/>.
  332. /// </remarks>
  333. /// <param name="viewport">Viewport-relative location and size.</param>
  334. /// <returns>Screen-relative location and size.</returns>
  335. public Rectangle ViewportToScreen (in Rectangle viewport) { return viewport with { Location = ViewportToScreen (viewport.Location) }; }
  336. /// <summary>
  337. /// Converts a <see cref="Viewport"/>-relative location to a screen-relative location.
  338. /// </summary>
  339. /// <remarks>
  340. /// Viewport-relative means relative to the top-left corner of the inner rectangle of the <see cref="Padding"/>.
  341. /// </remarks>
  342. /// <param name="viewportLocation">Viewport-relative location.</param>
  343. /// <returns>Screen-relative location.</returns>
  344. public Point ViewportToScreen (in Point viewportLocation)
  345. {
  346. // Translate bounds to Frame (our SuperView's Viewport-relative coordinates)
  347. Rectangle screen = FrameToScreen ();
  348. Point viewportOffset = GetViewportOffsetFromFrame ();
  349. screen.Offset (viewportOffset.X + viewportLocation.X, viewportOffset.Y + viewportLocation.Y);
  350. return screen.Location;
  351. }
  352. /// <summary>Converts a screen-relative coordinate to a Viewport-relative coordinate.</summary>
  353. /// <returns>The coordinate relative to this view's <see cref="Viewport"/>.</returns>
  354. /// <remarks>
  355. /// Viewport-relative means relative to the top-left corner of the inner rectangle of the <see cref="Padding"/>.
  356. /// </remarks>
  357. /// <param name="location">Screen-Relative Coordinate.</param>
  358. /// <returns>Viewport-relative location.</returns>
  359. public Point ScreenToViewport (in Point location)
  360. {
  361. Point viewportOffset = GetViewportOffsetFromFrame ();
  362. Point frame = ScreenToFrame (location);
  363. frame.Offset (-viewportOffset.X, -viewportOffset.Y);
  364. return frame;
  365. }
  366. /// <summary>
  367. /// Helper to get the X and Y offset of the Viewport from the Frame. This is the sum of the Left and Top properties
  368. /// of <see cref="Margin"/>, <see cref="Border"/> and <see cref="Padding"/>.
  369. /// </summary>
  370. public Point GetViewportOffsetFromFrame () { return Padding is null ? Point.Empty : Padding.Thickness.GetInside (Padding.Frame).Location; }
  371. /// <summary>
  372. /// Scrolls the view vertically by the specified number of rows.
  373. /// </summary>
  374. /// <remarks>
  375. /// <para>
  376. /// </para>
  377. /// </remarks>
  378. /// <param name="rows"></param>
  379. /// <returns><see langword="true"/> if the <see cref="Viewport"/> was changed.</returns>
  380. public bool? ScrollVertical (int rows)
  381. {
  382. if (GetContentSize () == Size.Empty || GetContentSize () == Viewport.Size)
  383. {
  384. return false;
  385. }
  386. Viewport = Viewport with { Y = Viewport.Y + rows };
  387. return true;
  388. }
  389. /// <summary>
  390. /// Scrolls the view horizontally by the specified number of columns.
  391. /// </summary>
  392. /// <remarks>
  393. /// <para>
  394. /// </para>
  395. /// </remarks>
  396. /// <param name="cols"></param>
  397. /// <returns><see langword="true"/> if the <see cref="Viewport"/> was changed.</returns>
  398. public bool? ScrollHorizontal (int cols)
  399. {
  400. if (GetContentSize () == Size.Empty || GetContentSize () == Viewport.Size)
  401. {
  402. return false;
  403. }
  404. Viewport = Viewport with { X = Viewport.X + cols };
  405. return true;
  406. }
  407. #endregion Viewport
  408. }