ViewContent.cs 16 KB

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