ViewContent.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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="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 the size of the View's content.
  156. /// </summary>
  157. /// <remarks>
  158. /// <para>
  159. /// Use <see cref="SetContentSize"/> to change to change the content size.
  160. /// </para>
  161. /// <para>
  162. /// If the content size has not been explicitly set with <see cref="SetContentSize"/>, the value tracks
  163. /// <see cref="Viewport"/>.
  164. /// </para>
  165. /// </remarks>
  166. public Size ContentSize => _contentSize ?? Viewport.Size;
  167. /// <summary>
  168. /// Called when <see cref="ContentSize"/> has changed.
  169. /// </summary>
  170. /// <param name="e"></param>
  171. /// <returns></returns>
  172. protected bool? OnContentSizeChanged (SizeChangedEventArgs e)
  173. {
  174. ContentSizeChanged?.Invoke (this, e);
  175. if (e.Cancel != true)
  176. {
  177. OnResizeNeeded ();
  178. //SetNeedsLayout ();
  179. //SetNeedsDisplay ();
  180. }
  181. return e.Cancel;
  182. }
  183. /// <summary>
  184. /// Event raised when the <see cref="ContentSize"/> changes.
  185. /// </summary>
  186. public event EventHandler<SizeChangedEventArgs> ContentSizeChanged;
  187. /// <summary>
  188. /// Converts a Content-relative location to a Screen-relative location.
  189. /// </summary>
  190. /// <param name="location">The Content-relative location.</param>
  191. /// <returns>The Screen-relative location.</returns>
  192. public Point ContentToScreen (in Point location)
  193. {
  194. // Subtract the ViewportOffsetFromFrame to get the Viewport-relative location.
  195. Point viewportOffset = GetViewportOffsetFromFrame ();
  196. Point contentRelativeToViewport = location;
  197. contentRelativeToViewport.Offset (-Viewport.X, -Viewport.Y);
  198. // Translate to Screen-Relative (our SuperView's Viewport-relative coordinates)
  199. return ViewportToScreen (contentRelativeToViewport);
  200. }
  201. /// <summary>Converts a Screen-relative coordinate to a Content-relative coordinate.</summary>
  202. /// <remarks>
  203. /// Content-relative means relative to the top-left corner of the view's Content, which is
  204. /// always at <c>0, 0</c>.
  205. /// </remarks>
  206. /// <param name="location">The Screen-relative location.</param>
  207. /// <returns>The coordinate relative to this view's Content.</returns>
  208. public Point ScreenToContent (in Point location)
  209. {
  210. Point viewportOffset = GetViewportOffsetFromFrame ();
  211. Point screen = ScreenToFrame (location);
  212. screen.Offset (Viewport.X - viewportOffset.X, Viewport.Y - viewportOffset.Y);
  213. return screen;
  214. }
  215. #endregion Content Area
  216. #region Viewport
  217. private ViewportSettings _viewportSettings;
  218. /// <summary>
  219. /// Gets or sets how scrolling the <see cref="View.Viewport"/> on the View's Content Area is handled.
  220. /// </summary>
  221. public ViewportSettings ViewportSettings
  222. {
  223. get => _viewportSettings;
  224. set
  225. {
  226. if (_viewportSettings == value)
  227. {
  228. return;
  229. }
  230. _viewportSettings = value;
  231. if (IsInitialized)
  232. {
  233. // Force set Viewport to cause settings to be applied as needed
  234. SetViewport (Viewport);
  235. }
  236. }
  237. }
  238. /// <summary>
  239. /// The location of the viewport into the view's content (0,0) is the top-left corner of the content. The Content
  240. /// area's size
  241. /// is <see cref="ContentSize"/>.
  242. /// </summary>
  243. private Point _viewportLocation;
  244. /// <summary>
  245. /// Gets or sets the rectangle describing the portion of the View's content that is visible to the user.
  246. /// The viewport Location is relative to the top-left corner of the inner rectangle of <see cref="Padding"/>.
  247. /// If the viewport Size is the same as <see cref="ContentSize"/>, or <see cref="ContentSize"/> is
  248. /// <see langword="null"/> the Location will be <c>0, 0</c>.
  249. /// </summary>
  250. /// <value>
  251. /// The rectangle describing the location and size of the viewport into the View's virtual content, described by
  252. /// <see cref="ContentSize"/>.
  253. /// </value>
  254. /// <remarks>
  255. /// <para>
  256. /// Positive values for the location indicate the visible area is offset into (down-and-right) the View's virtual
  257. /// <see cref="ContentSize"/>. This enables scrolling down and to the right (e.g. in a <see cref="ListView"/>.
  258. /// </para>
  259. /// <para>
  260. /// Negative values for the location indicate the visible area is offset above (up-and-left) the View's virtual
  261. /// <see cref="ContentSize"/>. This enables scrolling up and to the left (e.g. in an image viewer that supports zoom
  262. /// where the image stays centered).
  263. /// </para>
  264. /// <para>
  265. /// The <see cref="ViewportSettings"/> property controls how scrolling is handled.
  266. /// </para>
  267. /// <para>
  268. /// If <see cref="LayoutStyle"/> is <see cref="LayoutStyle.Computed"/> the value of Viewport is indeterminate until
  269. /// the view has been initialized ( <see cref="IsInitialized"/> is true) and <see cref="LayoutSubviews"/> has been
  270. /// called.
  271. /// </para>
  272. /// <para>
  273. /// Updates to the Viewport Size updates <see cref="Frame"/>, and has the same impact as updating the
  274. /// <see cref="Frame"/>.
  275. /// </para>
  276. /// <para>
  277. /// Altering the Viewport Size will eventually (when the view is next laid out) cause the
  278. /// <see cref="LayoutSubview(View, Size)"/> and <see cref="OnDrawContent(Rectangle)"/> methods to be called.
  279. /// </para>
  280. /// </remarks>
  281. public virtual Rectangle Viewport
  282. {
  283. get
  284. {
  285. #if DEBUG
  286. if ((_width.ReferencesOtherViews () || _height.ReferencesOtherViews ()) && !IsInitialized)
  287. {
  288. Debug.WriteLine (
  289. $"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."
  290. );
  291. }
  292. #endif // DEBUG
  293. if (Margin is null || Border is null || Padding is null)
  294. {
  295. // CreateAdornments has not been called yet.
  296. return new (_viewportLocation, Frame.Size);
  297. }
  298. //// BUGBUG: This is a hack. Viewport_get should not have side effects.
  299. //if (Frame.Size == Size.Empty)
  300. //{
  301. // // The Frame has not been set yet (e.g. the view has not been added to a SuperView yet).
  302. // //
  303. // // Use _width & _height instead of Width & Height to avoid debug spew
  304. // DimAuto widthAuto = _width as DimAuto;
  305. // DimAuto heightAuto = _height as DimAuto;
  306. // if ((widthAuto is { } && widthAuto._style.HasFlag (DimAutoStyle.Text))
  307. // || (heightAuto is { } && heightAuto._style.HasFlag (DimAutoStyle.Text)))
  308. // {
  309. // //if (TextFormatter.NeedsFormat)
  310. // {
  311. // // We always use TF in autosize = false mode
  312. // TextFormatter.AutoSize = false;
  313. // var size = TextFormatter.GetAutoSize ();
  314. // if (widthAuto is null || !widthAuto._style.HasFlag (DimAutoStyle.Text))
  315. // {
  316. // size.Width = _width.Anchor (0);
  317. // }
  318. // if (heightAuto is null || !heightAuto._style.HasFlag (DimAutoStyle.Text))
  319. // {
  320. // size.Height = _height.Anchor (0);
  321. // }
  322. // // Whenever DimAutoStyle.Text is set, ContentSize will match TextFormatter.Size.
  323. // //ContentSize = size;//TextFormatter.Size == Size.Empty ? null : TextFormatter.Size;
  324. // TextFormatter.Size = size; ;
  325. // // Whenever DimAutoStyle.Text is set, ContentSize will match TextFormatter.Size.
  326. // ContentSize = TextFormatter.Size == Size.Empty ? null : TextFormatter.Size;
  327. // }
  328. // }
  329. //}
  330. Thickness thickness = GetAdornmentsThickness ();
  331. return new (
  332. _viewportLocation,
  333. new (
  334. Math.Max (0, Frame.Size.Width - thickness.Horizontal),
  335. Math.Max (0, Frame.Size.Height - thickness.Vertical)
  336. ));
  337. }
  338. set => SetViewport (value);
  339. }
  340. private void SetViewport (Rectangle viewport)
  341. {
  342. Rectangle oldViewport = viewport;
  343. ApplySettings (ref viewport);
  344. Thickness thickness = GetAdornmentsThickness ();
  345. Size newSize = new (
  346. viewport.Size.Width + thickness.Horizontal,
  347. viewport.Size.Height + thickness.Vertical);
  348. if (newSize == Frame.Size)
  349. {
  350. // The change is not changing the Frame, so we don't need to update it.
  351. // Just call SetNeedsLayout to update the layout.
  352. if (_viewportLocation != viewport.Location)
  353. {
  354. _viewportLocation = viewport.Location;
  355. SetNeedsLayout ();
  356. }
  357. OnViewportChanged (new (IsInitialized ? Viewport : Rectangle.Empty, oldViewport));
  358. return;
  359. }
  360. _viewportLocation = viewport.Location;
  361. // Update the Frame because we made it bigger or smaller which impacts subviews.
  362. Frame = Frame with
  363. {
  364. Size = newSize
  365. };
  366. void ApplySettings (ref Rectangle newViewport)
  367. {
  368. if (!ViewportSettings.HasFlag (ViewportSettings.AllowXGreaterThanContentWidth))
  369. {
  370. if (newViewport.X >= ContentSize.Width)
  371. {
  372. newViewport.X = ContentSize.Width - 1;
  373. }
  374. }
  375. // IMPORTANT: Check for negative location AFTER checking for location greater than content width
  376. if (!ViewportSettings.HasFlag (ViewportSettings.AllowNegativeX))
  377. {
  378. if (newViewport.X < 0)
  379. {
  380. newViewport.X = 0;
  381. }
  382. }
  383. if (!ViewportSettings.HasFlag (ViewportSettings.AllowYGreaterThanContentHeight))
  384. {
  385. if (newViewport.Y >= ContentSize.Height)
  386. {
  387. newViewport.Y = ContentSize.Height - 1;
  388. }
  389. }
  390. // IMPORTANT: Check for negative location AFTER checking for location greater than content width
  391. if (!ViewportSettings.HasFlag (ViewportSettings.AllowNegativeY))
  392. {
  393. if (newViewport.Y < 0)
  394. {
  395. newViewport.Y = 0;
  396. }
  397. }
  398. }
  399. }
  400. /// <summary>
  401. /// Fired when the <see cref="Viewport"/> changes. This event is fired after the <see cref="Viewport"/> has been updated.
  402. /// </summary>
  403. [CanBeNull]
  404. public event EventHandler<DrawEventArgs> ViewportChanged;
  405. /// <summary>
  406. /// Called when the <see cref="Viewport"/> changes. Invokes the <see cref="ViewportChanged"/> event.
  407. /// </summary>
  408. /// <param name="e"></param>
  409. protected virtual void OnViewportChanged (DrawEventArgs e)
  410. {
  411. ViewportChanged?.Invoke (this, e);
  412. }
  413. /// <summary>
  414. /// Converts a <see cref="Viewport"/>-relative location and size to a screen-relative location and size.
  415. /// </summary>
  416. /// <remarks>
  417. /// Viewport-relative means relative to the top-left corner of the inner rectangle of the <see cref="Padding"/>.
  418. /// </remarks>
  419. /// <param name="viewport">Viewport-relative location and size.</param>
  420. /// <returns>Screen-relative location and size.</returns>
  421. public Rectangle ViewportToScreen (in Rectangle viewport)
  422. {
  423. return viewport with { Location = ViewportToScreen (viewport.Location) };
  424. }
  425. /// <summary>
  426. /// Converts a <see cref="Viewport"/>-relative location to a screen-relative location.
  427. /// </summary>
  428. /// <remarks>
  429. /// Viewport-relative means relative to the top-left corner of the inner rectangle of the <see cref="Padding"/>.
  430. /// </remarks>
  431. /// <param name="viewportLocation">Viewport-relative location.</param>
  432. /// <returns>Screen-relative location.</returns>
  433. public Point ViewportToScreen (in Point viewportLocation)
  434. {
  435. // Translate bounds to Frame (our SuperView's Viewport-relative coordinates)
  436. Rectangle screen = FrameToScreen ();
  437. Point viewportOffset = GetViewportOffsetFromFrame ();
  438. screen.Offset (viewportOffset.X + viewportLocation.X, viewportOffset.Y + viewportLocation.Y);
  439. return screen.Location;
  440. }
  441. /// <summary>Converts a screen-relative coordinate to a Viewport-relative coordinate.</summary>
  442. /// <returns>The coordinate relative to this view's <see cref="Viewport"/>.</returns>
  443. /// <remarks>
  444. /// Viewport-relative means relative to the top-left corner of the inner rectangle of the <see cref="Padding"/>.
  445. /// </remarks>
  446. /// <param name="location">Screen-Relative Coordinate.</param>
  447. /// <returns>Viewport-relative location.</returns>
  448. public Point ScreenToViewport (in Point location)
  449. {
  450. Point viewportOffset = GetViewportOffsetFromFrame ();
  451. Point frame = ScreenToFrame (location);
  452. frame.Offset (-viewportOffset.X, -viewportOffset.Y);
  453. return frame;
  454. }
  455. /// <summary>
  456. /// Helper to get the X and Y offset of the Viewport from the Frame. This is the sum of the Left and Top properties
  457. /// of <see cref="Margin"/>, <see cref="Border"/> and <see cref="Padding"/>.
  458. /// </summary>
  459. public Point GetViewportOffsetFromFrame () { return Padding is null ? Point.Empty : Padding.Thickness.GetInside (Padding.Frame).Location; }
  460. /// <summary>
  461. /// Scrolls the view vertically by the specified number of rows.
  462. /// </summary>
  463. /// <remarks>
  464. /// <para>
  465. /// </para>
  466. /// </remarks>
  467. /// <param name="rows"></param>
  468. /// <returns><see langword="true"/> if the <see cref="Viewport"/> was changed.</returns>
  469. public bool? ScrollVertical (int rows)
  470. {
  471. if (ContentSize == Size.Empty || ContentSize == Viewport.Size)
  472. {
  473. return false;
  474. }
  475. Viewport = Viewport with { Y = Viewport.Y + rows };
  476. return true;
  477. }
  478. /// <summary>
  479. /// Scrolls the view horizontally by the specified number of columns.
  480. /// </summary>
  481. /// <remarks>
  482. /// <para>
  483. /// </para>
  484. /// </remarks>
  485. /// <param name="cols"></param>
  486. /// <returns><see langword="true"/> if the <see cref="Viewport"/> was changed.</returns>
  487. public bool? ScrollHorizontal (int cols)
  488. {
  489. if (ContentSize == Size.Empty || ContentSize == Viewport.Size)
  490. {
  491. return false;
  492. }
  493. Viewport = Viewport with { X = Viewport.X + cols };
  494. return true;
  495. }
  496. #endregion Viewport
  497. }