ViewContent.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. using System.Diagnostics;
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// Settings for how the <see cref="View.Viewport"/> behaves relative to the View's Content area.
  5. /// </summary>
  6. [Flags]
  7. public enum ViewportSettings
  8. {
  9. /// <summary>
  10. /// No settings.
  11. /// </summary>
  12. None = 0,
  13. /// <summary>
  14. /// If set, <see cref="View.Viewport"/><c>.X</c> can be set to negative values enabling scrolling beyond the left of
  15. /// the
  16. /// content area.
  17. /// </summary>
  18. /// <remarks>
  19. /// <para>
  20. /// When not set, <see cref="View.Viewport"/><c>.X</c> is constrained to positive values.
  21. /// </para>
  22. /// </remarks>
  23. AllowNegativeX = 1,
  24. /// <summary>
  25. /// If set, <see cref="View.Viewport"/><c>.Y</c> can be set to negative values enabling scrolling beyond the top of the
  26. /// content area.
  27. /// </summary>
  28. /// <remarks>
  29. /// <para>
  30. /// When not set, <see cref="View.Viewport"/><c>.Y</c> is constrained to positive values.
  31. /// </para>
  32. /// </remarks>
  33. AllowNegativeY = 2,
  34. /// <summary>
  35. /// If set, <see cref="View.Viewport"/><c>.Size</c> can be set to negative coordinates enabling scrolling beyond the
  36. /// top-left of the
  37. /// content area.
  38. /// </summary>
  39. /// <remarks>
  40. /// <para>
  41. /// When not set, <see cref="View.Viewport"/><c>.Size</c> is constrained to positive coordinates.
  42. /// </para>
  43. /// </remarks>
  44. AllowNegativeLocation = AllowNegativeX | AllowNegativeY,
  45. /// <summary>
  46. /// If set, <see cref="View.Viewport"/><c>.X</c> can be set values greater than <see cref="View.ContentSize"/>
  47. /// <c>.Width</c> enabling scrolling beyond the right
  48. /// of the content area.
  49. /// </summary>
  50. /// <remarks>
  51. /// <para>
  52. /// When not set, <see cref="View.Viewport"/><c>.X</c> is constrained to <see cref="View.ContentSize"/>
  53. /// <c>.Width - 1</c>.
  54. /// This means the last column of the content will remain visible even if there is an attempt to scroll the
  55. /// Viewport past the last column.
  56. /// </para>
  57. /// <para>
  58. /// The practical effect of this is that the last column of the content will always be visible.
  59. /// </para>
  60. /// </remarks>
  61. AllowXGreaterThanContentWidth = 4,
  62. /// <summary>
  63. /// If set, <see cref="View.Viewport"/><c>.Y</c> can be set values greater than <see cref="View.ContentSize"/>
  64. /// <c>.Height</c> enabling scrolling beyond the right
  65. /// of the content area.
  66. /// </summary>
  67. /// <remarks>
  68. /// <para>
  69. /// When not set, <see cref="View.Viewport"/><c>.Y</c> is constrained to <see cref="View.ContentSize"/>
  70. /// <c>.Height - 1</c>.
  71. /// This means the last row of the content will remain visible even if there is an attempt to scroll the Viewport
  72. /// past the last row.
  73. /// </para>
  74. /// <para>
  75. /// The practical effect of this is that the last row of the content will always be visible.
  76. /// </para>
  77. /// </remarks>
  78. AllowYGreaterThanContentHeight = 8,
  79. /// <summary>
  80. /// If set, <see cref="View.Viewport"/><c>.Size</c> can be set values greater than <see cref="View.ContentSize"/>
  81. /// enabling scrolling beyond the bottom-right
  82. /// of the content area.
  83. /// </summary>
  84. /// <remarks>
  85. /// <para>
  86. /// When not set, <see cref="View.Viewport"/> is constrained to <see cref="View.ContentSize"/><c> -1</c>.
  87. /// This means the last column and row of the content will remain visible even if there is an attempt to
  88. /// scroll the Viewport past the last column or row.
  89. /// </para>
  90. /// </remarks>
  91. AllowLocationGreaterThanContentSize = AllowXGreaterThanContentWidth | AllowYGreaterThanContentHeight,
  92. /// <summary>
  93. /// By default, clipping is applied to the <see cref="View.Viewport"/>. Setting this flag will cause clipping to be
  94. /// applied to the visible content area.
  95. /// </summary>
  96. ClipContentOnly = 16,
  97. /// <summary>
  98. /// If set <see cref="View.Clear()"/> will clear only the portion of the content
  99. /// area that is visible within the <see cref="View.Viewport"/>. This is useful for views that have a
  100. /// content area larger than the Viewport and want the area outside the content to be visually distinct.
  101. /// </summary>
  102. /// <remarks>
  103. /// <see cref="ClipContentOnly"/> must be set for this setting to work (clipping beyond the visible area must be
  104. /// disabled).
  105. /// </remarks>
  106. ClearContentOnly = 32
  107. }
  108. public partial class View
  109. {
  110. #region Content Area
  111. internal Size? _contentSize;
  112. /// <summary>
  113. /// Sets the size of the View's content.
  114. /// </summary>
  115. /// <remarks>
  116. /// <para>
  117. /// By default, the content size is set to <see langword="null"/>.
  118. /// </para>
  119. /// </remarks>
  120. /// <param name="contentSize">
  121. /// <para>
  122. /// If <see langword="null"/>, and the View has no visible subviews, <see cref="ContentSize"/> will track the size of <see cref="Viewport"/>.
  123. /// </para>
  124. /// <para>
  125. /// If <see langword="null"/>, and the View has visible subviews, <see cref="ContentSize"/> will track the maximum position plus size of any
  126. /// visible Subviews
  127. /// and <c>Viewport.Location</c> will track the minimum position and size of any visible Subviews.
  128. /// </para>
  129. /// <para>
  130. /// If not <see langword="null"/>, <see cref="ContentSize"/> is set to the passed value and <see cref="Viewport"/> describes the portion of the content currently visible
  131. /// to the user. This enables virtual scrolling.
  132. /// </para>
  133. /// <para>
  134. /// If not <see langword="null"/>, <see cref="ContentSize"/> is set to the passed value and the behavior of <see cref="Dim.DimAutoStyle.Content"/> will be to use the ContentSize
  135. /// to determine the size of the view.
  136. /// </para>
  137. /// <para>
  138. /// Negative sizes are not supported.
  139. /// </para>
  140. /// </param>
  141. public void SetContentSize (Size? contentSize)
  142. {
  143. if (contentSize?.Width < 0 || contentSize?.Height < 0)
  144. {
  145. throw new ArgumentException (@"ContentSize cannot be negative.", nameof (contentSize));
  146. }
  147. if (contentSize == _contentSize)
  148. {
  149. return;
  150. }
  151. _contentSize = contentSize;
  152. OnContentSizeChanged (new (_contentSize));
  153. }
  154. /// <summary>
  155. /// Gets or sets the size of the View's content.
  156. /// </summary>
  157. /// <remarks>
  158. /// <para>
  159. /// If set to <see langword="null"/>, the value will be the same as the size of <see cref="Viewport"/>,
  160. /// and <c>Viewport.Location</c> will always be <c>0, 0</c>.
  161. /// </para>
  162. /// <para>
  163. /// If explicitly set, <see cref="Viewport"/> describes the portion of the content currently visible
  164. /// to the view. This enables virtual scrolling.
  165. /// </para>
  166. /// <para>
  167. /// If explicitly set, the behavior of <see cref="Dim.DimAutoStyle.Content"/> will be to use the ContentSize
  168. /// to determine the size of the view.
  169. /// </para>
  170. /// <para>
  171. /// Negative sizes are not supported.
  172. /// </para>
  173. /// </remarks>
  174. public Size? ContentSize
  175. {
  176. get => _contentSize ?? Viewport.Size;
  177. //set
  178. //{
  179. // if (value?.Width < 0 || value?.Height < 0)
  180. // {
  181. // throw new ArgumentException (@"ContentSize cannot be negative.", nameof (value));
  182. // }
  183. // if (value == _contentSize)
  184. // {
  185. // return;
  186. // }
  187. // _contentSize = value;
  188. // OnContentSizeChanged (new (_contentSize));
  189. //}
  190. }
  191. /// <summary>
  192. /// Called when <see cref="ContentSize"/> changes. Invokes the <see cref="ContentSizeChanged"/> event.
  193. /// </summary>
  194. /// <param name="e"></param>
  195. /// <returns></returns>
  196. protected bool? OnContentSizeChanged (SizeChangedEventArgs e)
  197. {
  198. ContentSizeChanged?.Invoke (this, e);
  199. if (e.Cancel != true)
  200. {
  201. OnResizeNeeded ();
  202. //SetNeedsLayout ();
  203. //SetNeedsDisplay ();
  204. }
  205. return e.Cancel;
  206. }
  207. /// <summary>
  208. /// Event raised when the <see cref="ContentSize"/> changes.
  209. /// </summary>
  210. public event EventHandler<SizeChangedEventArgs> ContentSizeChanged;
  211. /// <summary>
  212. /// Converts a Content-relative location to a Screen-relative location.
  213. /// </summary>
  214. /// <param name="location">The Content-relative location.</param>
  215. /// <returns>The Screen-relative location.</returns>
  216. public Point ContentToScreen (in Point location)
  217. {
  218. // Subtract the ViewportOffsetFromFrame to get the Viewport-relative location.
  219. Point viewportOffset = GetViewportOffsetFromFrame ();
  220. Point contentRelativeToViewport = location;
  221. contentRelativeToViewport.Offset (-Viewport.X, -Viewport.Y);
  222. // Translate to Screen-Relative (our SuperView's Viewport-relative coordinates)
  223. return ViewportToScreen (contentRelativeToViewport);
  224. }
  225. /// <summary>Converts a Screen-relative coordinate to a Content-relative coordinate.</summary>
  226. /// <remarks>
  227. /// Content-relative means relative to the top-left corner of the view's Content, which is
  228. /// always at <c>0, 0</c>.
  229. /// </remarks>
  230. /// <param name="location">The Screen-relative location.</param>
  231. /// <returns>The coordinate relative to this view's Content.</returns>
  232. public Point ScreenToContent (in Point location)
  233. {
  234. Point viewportOffset = GetViewportOffsetFromFrame ();
  235. Point screen = ScreenToFrame (location);
  236. screen.Offset (Viewport.X - viewportOffset.X, Viewport.Y - viewportOffset.Y);
  237. return screen;
  238. }
  239. #endregion Content Area
  240. #region Viewport
  241. private ViewportSettings _viewportSettings;
  242. /// <summary>
  243. /// Gets or sets how scrolling the <see cref="View.Viewport"/> on the View's Content Area is handled.
  244. /// </summary>
  245. public ViewportSettings ViewportSettings
  246. {
  247. get => _viewportSettings;
  248. set
  249. {
  250. if (_viewportSettings == value)
  251. {
  252. return;
  253. }
  254. _viewportSettings = value;
  255. if (IsInitialized)
  256. {
  257. // Force set Viewport to cause settings to be applied as needed
  258. SetViewport (Viewport);
  259. }
  260. }
  261. }
  262. /// <summary>
  263. /// The location of the viewport into the view's content (0,0) is the top-left corner of the content. The Content
  264. /// area's size
  265. /// is <see cref="ContentSize"/>.
  266. /// </summary>
  267. private Point _viewportLocation;
  268. /// <summary>
  269. /// Gets or sets the rectangle describing the portion of the View's content that is visible to the user.
  270. /// The viewport Location is relative to the top-left corner of the inner rectangle of <see cref="Padding"/>.
  271. /// If the viewport Size is the same as <see cref="ContentSize"/>, or <see cref="ContentSize"/> is
  272. /// <see langword="null"/> the Location will be <c>0, 0</c>.
  273. /// </summary>
  274. /// <value>
  275. /// The rectangle describing the location and size of the viewport into the View's virtual content, described by
  276. /// <see cref="ContentSize"/>.
  277. /// </value>
  278. /// <remarks>
  279. /// <para>
  280. /// Positive values for the location indicate the visible area is offset into (down-and-right) the View's virtual
  281. /// <see cref="ContentSize"/>. This enables scrolling down and to the right (e.g. in a <see cref="ListView"/>.
  282. /// </para>
  283. /// <para>
  284. /// Negative values for the location indicate the visible area is offset above (up-and-left) the View's virtual
  285. /// <see cref="ContentSize"/>. This enables scrolling up and to the left (e.g. in an image viewer that supports zoom
  286. /// where the image stays centered).
  287. /// </para>
  288. /// <para>
  289. /// The <see cref="ViewportSettings"/> property controls how scrolling is handled.
  290. /// </para>
  291. /// <para>
  292. /// If <see cref="LayoutStyle"/> is <see cref="LayoutStyle.Computed"/> the value of Viewport is indeterminate until
  293. /// the view has been initialized ( <see cref="IsInitialized"/> is true) and <see cref="LayoutSubviews"/> has been
  294. /// called.
  295. /// </para>
  296. /// <para>
  297. /// Updates to the Viewport Size updates <see cref="Frame"/>, and has the same impact as updating the
  298. /// <see cref="Frame"/>.
  299. /// </para>
  300. /// <para>
  301. /// Altering the Viewport Size will eventually (when the view is next laid out) cause the
  302. /// <see cref="LayoutSubview(View, Size)"/> and <see cref="OnDrawContent(Rectangle)"/> methods to be called.
  303. /// </para>
  304. /// </remarks>
  305. public virtual Rectangle Viewport
  306. {
  307. get
  308. {
  309. #if DEBUG
  310. if ((_width.ReferencesOtherViews () || _height.ReferencesOtherViews ()) && !IsInitialized)
  311. {
  312. Debug.WriteLine (
  313. $"WARNING: The dimensions of {this} are dependent on other views and Viewport is being accessed before the View has been initialized. This is likely a bug."
  314. );
  315. }
  316. #endif // DEBUG
  317. if (Margin is null || Border is null || Padding is null)
  318. {
  319. // CreateAdornments has not been called yet.
  320. return new (_viewportLocation, Frame.Size);
  321. }
  322. //// BUGBUG: This is a hack. Viewport_get should not have side effects.
  323. //if (Frame.Size == Size.Empty)
  324. //{
  325. // // The Frame has not been set yet (e.g. the view has not been added to a SuperView yet).
  326. // //
  327. // // Use _width & _height instead of Width & Height to avoid debug spew
  328. // Dim.DimAuto widthAuto = _width as Dim.DimAuto;
  329. // Dim.DimAuto heightAuto = _height as Dim.DimAuto;
  330. // if ((widthAuto is { } && widthAuto._style.HasFlag (Dim.DimAutoStyle.Text))
  331. // || (heightAuto is { } && heightAuto._style.HasFlag (Dim.DimAutoStyle.Text)))
  332. // {
  333. // //if (TextFormatter.NeedsFormat)
  334. // {
  335. // // We always use TF in autosize = false mode
  336. // TextFormatter.AutoSize = false;
  337. // var size = TextFormatter.GetAutoSize ();
  338. // if (widthAuto is null || !widthAuto._style.HasFlag (Dim.DimAutoStyle.Text))
  339. // {
  340. // size.Width = _width.Anchor (0);
  341. // }
  342. // if (heightAuto is null || !heightAuto._style.HasFlag (Dim.DimAutoStyle.Text))
  343. // {
  344. // size.Height = _height.Anchor (0);
  345. // }
  346. // // Whenever DimAutoStyle.Text is set, ContentSize will match TextFormatter.Size.
  347. // //ContentSize = size;//TextFormatter.Size == Size.Empty ? null : TextFormatter.Size;
  348. // TextFormatter.Size = size; ;
  349. // // Whenever DimAutoStyle.Text is set, ContentSize will match TextFormatter.Size.
  350. // ContentSize = TextFormatter.Size == Size.Empty ? null : TextFormatter.Size;
  351. // }
  352. // }
  353. //}
  354. Thickness thickness = GetAdornmentsThickness ();
  355. return new (
  356. _viewportLocation,
  357. new (
  358. Math.Max (0, Frame.Size.Width - thickness.Horizontal),
  359. Math.Max (0, Frame.Size.Height - thickness.Vertical)
  360. ));
  361. }
  362. set => SetViewport (value);
  363. }
  364. private void SetViewport (Rectangle viewport)
  365. {
  366. Rectangle oldViewport = viewport;
  367. ApplySettings (ref viewport);
  368. Thickness thickness = GetAdornmentsThickness ();
  369. Size newSize = new (
  370. viewport.Size.Width + thickness.Horizontal,
  371. viewport.Size.Height + thickness.Vertical);
  372. if (newSize == Frame.Size)
  373. {
  374. // The change is not changing the Frame, so we don't need to update it.
  375. // Just call SetNeedsLayout to update the layout.
  376. if (_viewportLocation != viewport.Location)
  377. {
  378. _viewportLocation = viewport.Location;
  379. SetNeedsLayout ();
  380. }
  381. OnViewportChanged (new (IsInitialized ? Viewport : Rectangle.Empty, oldViewport));
  382. return;
  383. }
  384. _viewportLocation = viewport.Location;
  385. // Update the Frame because we made it bigger or smaller which impacts subviews.
  386. Frame = Frame with
  387. {
  388. Size = newSize
  389. };
  390. void ApplySettings (ref Rectangle newViewport)
  391. {
  392. if (!ViewportSettings.HasFlag (ViewportSettings.AllowXGreaterThanContentWidth))
  393. {
  394. if (newViewport.X >= ContentSize.GetValueOrDefault ().Width)
  395. {
  396. newViewport.X = ContentSize.GetValueOrDefault ().Width - 1;
  397. }
  398. }
  399. // IMPORTANT: Check for negative location AFTER checking for location greater than content width
  400. if (!ViewportSettings.HasFlag (ViewportSettings.AllowNegativeX))
  401. {
  402. if (newViewport.X < 0)
  403. {
  404. newViewport.X = 0;
  405. }
  406. }
  407. if (!ViewportSettings.HasFlag (ViewportSettings.AllowYGreaterThanContentHeight))
  408. {
  409. if (newViewport.Y >= ContentSize.GetValueOrDefault().Height)
  410. {
  411. newViewport.Y = ContentSize.GetValueOrDefault ().Height - 1;
  412. }
  413. }
  414. // IMPORTANT: Check for negative location AFTER checking for location greater than content width
  415. if (!ViewportSettings.HasFlag (ViewportSettings.AllowNegativeY))
  416. {
  417. if (newViewport.Y < 0)
  418. {
  419. newViewport.Y = 0;
  420. }
  421. }
  422. }
  423. }
  424. /// <summary>
  425. /// Fired when the <see cref="Viewport"/> changes. This event is fired after the <see cref="Viewport"/> has been updated.
  426. /// </summary>
  427. [CanBeNull]
  428. public event EventHandler<DrawEventArgs> ViewportChanged;
  429. /// <summary>
  430. /// Called when the <see cref="Viewport"/> changes. Invokes the <see cref="ViewportChanged"/> event.
  431. /// </summary>
  432. /// <param name="e"></param>
  433. protected virtual void OnViewportChanged (DrawEventArgs e)
  434. {
  435. ViewportChanged?.Invoke (this, e);
  436. }
  437. /// <summary>
  438. /// Converts a <see cref="Viewport"/>-relative location and size to a screen-relative location and size.
  439. /// </summary>
  440. /// <remarks>
  441. /// Viewport-relative means relative to the top-left corner of the inner rectangle of the <see cref="Padding"/>.
  442. /// </remarks>
  443. /// <param name="viewport">Viewport-relative location and size.</param>
  444. /// <returns>Screen-relative location and size.</returns>
  445. public Rectangle ViewportToScreen (in Rectangle viewport)
  446. {
  447. return viewport with { Location = ViewportToScreen (viewport.Location) };
  448. }
  449. /// <summary>
  450. /// Converts a <see cref="Viewport"/>-relative location to a screen-relative location.
  451. /// </summary>
  452. /// <remarks>
  453. /// Viewport-relative means relative to the top-left corner of the inner rectangle of the <see cref="Padding"/>.
  454. /// </remarks>
  455. /// <param name="viewportLocation">Viewport-relative location.</param>
  456. /// <returns>Screen-relative location.</returns>
  457. public Point ViewportToScreen (in Point viewportLocation)
  458. {
  459. // Translate bounds to Frame (our SuperView's Viewport-relative coordinates)
  460. Rectangle screen = FrameToScreen ();
  461. Point viewportOffset = GetViewportOffsetFromFrame ();
  462. screen.Offset (viewportOffset.X + viewportLocation.X, viewportOffset.Y + viewportLocation.Y);
  463. return screen.Location;
  464. }
  465. /// <summary>Converts a screen-relative coordinate to a Viewport-relative coordinate.</summary>
  466. /// <returns>The coordinate relative to this view's <see cref="Viewport"/>.</returns>
  467. /// <remarks>
  468. /// Viewport-relative means relative to the top-left corner of the inner rectangle of the <see cref="Padding"/>.
  469. /// </remarks>
  470. /// <param name="location">Screen-Relative Coordinate.</param>
  471. /// <returns>Viewport-relative location.</returns>
  472. public Point ScreenToViewport (in Point location)
  473. {
  474. Point viewportOffset = GetViewportOffsetFromFrame ();
  475. Point frame = ScreenToFrame (location);
  476. frame.Offset (-viewportOffset.X, -viewportOffset.Y);
  477. return frame;
  478. }
  479. /// <summary>
  480. /// Helper to get the X and Y offset of the Viewport from the Frame. This is the sum of the Left and Top properties
  481. /// of <see cref="Margin"/>, <see cref="Border"/> and <see cref="Padding"/>.
  482. /// </summary>
  483. public Point GetViewportOffsetFromFrame () { return Padding is null ? Point.Empty : Padding.Thickness.GetInside (Padding.Frame).Location; }
  484. /// <summary>
  485. /// Scrolls the view vertically by the specified number of rows.
  486. /// </summary>
  487. /// <remarks>
  488. /// <para>
  489. /// </para>
  490. /// </remarks>
  491. /// <param name="rows"></param>
  492. /// <returns><see langword="true"/> if the <see cref="Viewport"/> was changed.</returns>
  493. public bool? ScrollVertical (int rows)
  494. {
  495. if (ContentSize == Size.Empty || ContentSize == Viewport.Size)
  496. {
  497. return false;
  498. }
  499. Viewport = Viewport with { Y = Viewport.Y + rows };
  500. return true;
  501. }
  502. /// <summary>
  503. /// Scrolls the view horizontally by the specified number of columns.
  504. /// </summary>
  505. /// <remarks>
  506. /// <para>
  507. /// </para>
  508. /// </remarks>
  509. /// <param name="cols"></param>
  510. /// <returns><see langword="true"/> if the <see cref="Viewport"/> was changed.</returns>
  511. public bool? ScrollHorizontal (int cols)
  512. {
  513. if (ContentSize == Size.Empty || ContentSize == Viewport.Size)
  514. {
  515. return false;
  516. }
  517. Viewport = Viewport with { X = Viewport.X + cols };
  518. return true;
  519. }
  520. #endregion Viewport
  521. }