ViewContent.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. private Size _contentSize;
  112. /// <summary>
  113. /// Gets or sets the size of the View's content. If not set, the value will be the same as the size of <see cref="Viewport"/>,
  114. /// and <c>Viewport.Location</c> will always be <c>0, 0</c>.
  115. /// </summary>
  116. /// <remarks>
  117. /// <para>
  118. /// If a positive size is provided, <see cref="Viewport"/> describes the portion of the content currently visible
  119. /// to the view. This enables virtual scrolling.
  120. /// </para>
  121. /// <para>
  122. /// Negative sizes are not supported.
  123. /// </para>
  124. /// </remarks>
  125. public Size ContentSize
  126. {
  127. get => _contentSize == Size.Empty ? Viewport.Size : _contentSize;
  128. set
  129. {
  130. if (value.Width < 0 || value.Height < 0)
  131. {
  132. throw new ArgumentException (@"ContentSize cannot be negative.", nameof (value));
  133. }
  134. if (value == _contentSize)
  135. {
  136. return;
  137. }
  138. _contentSize = value;
  139. OnContentSizeChanged (new (_contentSize));
  140. }
  141. }
  142. /// <summary>
  143. /// Called when <see cref="ContentSize"/> changes. Invokes the <see cref="ContentSizeChanged"/> event.
  144. /// </summary>
  145. /// <param name="e"></param>
  146. /// <returns></returns>
  147. protected bool? OnContentSizeChanged (SizeChangedEventArgs e)
  148. {
  149. ContentSizeChanged?.Invoke (this, e);
  150. if (e.Cancel != true)
  151. {
  152. SetNeedsLayout ();
  153. SetNeedsDisplay ();
  154. }
  155. return e.Cancel;
  156. }
  157. /// <summary>
  158. /// Event raised when the <see cref="ContentSize"/> changes.
  159. /// </summary>
  160. public event EventHandler<SizeChangedEventArgs> ContentSizeChanged;
  161. /// <summary>
  162. /// Converts a Content-relative location to a Screen-relative location.
  163. /// </summary>
  164. /// <param name="location">The Content-relative location.</param>
  165. /// <returns>The Screen-relative location.</returns>
  166. public Point ContentToScreen (in Point location)
  167. {
  168. // Subtract the ViewportOffsetFromFrame to get the Viewport-relative location.
  169. Point viewportOffset = GetViewportOffsetFromFrame ();
  170. Point contentRelativeToViewport = location;
  171. contentRelativeToViewport.Offset (-Viewport.X, -Viewport.Y);
  172. // Translate to Screen-Relative (our SuperView's Viewport-relative coordinates)
  173. Rectangle screen = ViewportToScreen (new (contentRelativeToViewport, Size.Empty));
  174. return screen.Location;
  175. }
  176. /// <summary>Converts a Screen-relative coordinate to a Content-relative coordinate.</summary>
  177. /// <remarks>
  178. /// Content-relative means relative to the top-left corner of the view's Content, which is
  179. /// always at <c>0, 0</c>.
  180. /// </remarks>
  181. /// <param name="location">The Screen-relative location.</param>
  182. /// <returns>The coordinate relative to this view's Content.</returns>
  183. public Point ScreenToContent (in Point location)
  184. {
  185. Point viewportOffset = GetViewportOffsetFromFrame ();
  186. Point screen = ScreenToFrame (location.X, location.Y);
  187. screen.Offset (Viewport.X - viewportOffset.X, Viewport.Y - viewportOffset.Y);
  188. return screen;
  189. }
  190. #endregion Content Area
  191. #region Viewport
  192. private ViewportSettings _viewportSettings;
  193. /// <summary>
  194. /// Gets or sets how scrolling the <see cref="View.Viewport"/> on the View's Content Area is handled.
  195. /// </summary>
  196. public ViewportSettings ViewportSettings
  197. {
  198. get => _viewportSettings;
  199. set
  200. {
  201. if (_viewportSettings == value)
  202. {
  203. return;
  204. }
  205. _viewportSettings = value;
  206. if (IsInitialized)
  207. {
  208. // Force set Viewport to cause settings to be applied as needed
  209. SetViewport (Viewport);
  210. }
  211. }
  212. }
  213. /// <summary>
  214. /// The location of the viewport into the view's content (0,0) is the top-left corner of the content. The Content
  215. /// area's size
  216. /// is <see cref="ContentSize"/>.
  217. /// </summary>
  218. private Point _viewportLocation;
  219. /// <summary>
  220. /// Gets or sets the rectangle describing the portion of the View's content that is visible to the user.
  221. /// The viewport Location is relative to the top-left corner of the inner rectangle of <see cref="Padding"/>.
  222. /// If the viewport Size is the same as <see cref="ContentSize"/> the Location will be <c>0, 0</c>.
  223. /// </summary>
  224. /// <value>
  225. /// The rectangle describing the location and size of the viewport into the View's virtual content, described by
  226. /// <see cref="ContentSize"/>.
  227. /// </value>
  228. /// <remarks>
  229. /// <para>
  230. /// Positive values for the location indicate the visible area is offset into (down-and-right) the View's virtual
  231. /// <see cref="ContentSize"/>. This enables scrolling down and to the right (e.g. in a <see cref="ListView"/>.
  232. /// </para>
  233. /// <para>
  234. /// Negative values for the location indicate the visible area is offset above (up-and-left) the View's virtual
  235. /// <see cref="ContentSize"/>. This enables scrolling up and to the left (e.g. in an image viewer that supports zoom
  236. /// where the image stays centered).
  237. /// </para>
  238. /// <para>
  239. /// The <see cref="ViewportSettings"/> property controls how scrolling is handled.
  240. /// </para>
  241. /// <para>
  242. /// If <see cref="LayoutStyle"/> is <see cref="LayoutStyle.Computed"/> the value of Viewport is indeterminate until
  243. /// the view has been initialized ( <see cref="IsInitialized"/> is true) and <see cref="LayoutSubviews"/> has been
  244. /// called.
  245. /// </para>
  246. /// <para>
  247. /// Updates to the Viewport Size updates <see cref="Frame"/>, and has the same impact as updating the
  248. /// <see cref="Frame"/>.
  249. /// </para>
  250. /// <para>
  251. /// Altering the Viewport Size will eventually (when the view is next laid out) cause the
  252. /// <see cref="LayoutSubview(View, Size)"/> and <see cref="OnDrawContent(Rectangle)"/> methods to be called.
  253. /// </para>
  254. /// </remarks>
  255. public virtual Rectangle Viewport
  256. {
  257. get
  258. {
  259. #if DEBUG
  260. if (LayoutStyle == LayoutStyle.Computed && !IsInitialized)
  261. {
  262. Debug.WriteLine (
  263. $"WARNING: Viewport is being accessed before the View has been initialized. This is likely a bug in {this}"
  264. );
  265. }
  266. #endif // DEBUG
  267. if (Margin is null || Border is null || Padding is null)
  268. {
  269. // CreateAdornments has not been called yet.
  270. return new (_viewportLocation, Frame.Size);
  271. }
  272. Thickness thickness = GetAdornmentsThickness ();
  273. return new (
  274. _viewportLocation,
  275. new (
  276. Math.Max (0, Frame.Size.Width - thickness.Horizontal),
  277. Math.Max (0, Frame.Size.Height - thickness.Vertical)
  278. ));
  279. }
  280. set => SetViewport (value);
  281. }
  282. private void SetViewport (Rectangle viewport)
  283. {
  284. Rectangle oldViewport = viewport;
  285. ApplySettings (ref viewport);
  286. Thickness thickness = GetAdornmentsThickness ();
  287. Size newSize = new (
  288. viewport.Size.Width + thickness.Horizontal,
  289. viewport.Size.Height + thickness.Vertical);
  290. if (newSize == Frame.Size)
  291. {
  292. // The change is not changing the Frame, so we don't need to update it.
  293. // Just call SetNeedsLayout to update the layout.
  294. if (_viewportLocation != viewport.Location)
  295. {
  296. _viewportLocation = viewport.Location;
  297. SetNeedsLayout ();
  298. }
  299. OnViewportChanged (new (IsInitialized ? Viewport : Rectangle.Empty, oldViewport));
  300. return;
  301. }
  302. _viewportLocation = viewport.Location;
  303. // Update the Frame because we made it bigger or smaller which impacts subviews.
  304. Frame = Frame with
  305. {
  306. Size = newSize
  307. };
  308. void ApplySettings (ref Rectangle newViewport)
  309. {
  310. if (!ViewportSettings.HasFlag (ViewportSettings.AllowXGreaterThanContentWidth))
  311. {
  312. if (newViewport.X >= ContentSize.Width)
  313. {
  314. newViewport.X = ContentSize.Width - 1;
  315. }
  316. }
  317. // IMPORTANT: Check for negative location AFTER checking for location greater than content width
  318. if (!ViewportSettings.HasFlag (ViewportSettings.AllowNegativeX))
  319. {
  320. if (newViewport.X < 0)
  321. {
  322. newViewport.X = 0;
  323. }
  324. }
  325. if (!ViewportSettings.HasFlag (ViewportSettings.AllowYGreaterThanContentHeight))
  326. {
  327. if (newViewport.Y >= ContentSize.Height)
  328. {
  329. newViewport.Y = ContentSize.Height - 1;
  330. }
  331. }
  332. // IMPORTANT: Check for negative location AFTER checking for location greater than content width
  333. if (!ViewportSettings.HasFlag (ViewportSettings.AllowNegativeY))
  334. {
  335. if (newViewport.Y < 0)
  336. {
  337. newViewport.Y = 0;
  338. }
  339. }
  340. }
  341. }
  342. /// <summary>
  343. /// Fired when the <see cref="Viewport"/> changes. This event is fired after the <see cref="Viewport"/> has been updated.
  344. /// </summary>
  345. [CanBeNull]
  346. public event EventHandler<DrawEventArgs> ViewportChanged;
  347. /// <summary>
  348. /// Called when the <see cref="Viewport"/> changes. Invokes the <see cref="ViewportChanged"/> event.
  349. /// </summary>
  350. /// <param name="e"></param>
  351. protected virtual void OnViewportChanged (DrawEventArgs e) { ViewportChanged?.Invoke (this, e); }
  352. /// <summary>
  353. /// Converts a <see cref="Viewport"/>-relative location to a screen-relative location.
  354. /// </summary>
  355. /// <remarks>
  356. /// Viewport-relative means relative to the top-left corner of the inner rectangle of the <see cref="Padding"/>.
  357. /// </remarks>
  358. public Rectangle ViewportToScreen (in Rectangle location)
  359. {
  360. // Translate bounds to Frame (our SuperView's Viewport-relative coordinates)
  361. Rectangle screen = FrameToScreen ();
  362. Point viewportOffset = GetViewportOffsetFromFrame ();
  363. screen.Offset (viewportOffset.X + location.X, viewportOffset.Y + location.Y);
  364. return new (screen.Location, location.Size);
  365. }
  366. /// <summary>Converts a screen-relative coordinate to a Viewport-relative coordinate.</summary>
  367. /// <returns>The coordinate relative to this view's <see cref="Viewport"/>.</returns>
  368. /// <remarks>
  369. /// Viewport-relative means relative to the top-left corner of the inner rectangle of the <see cref="Padding"/>.
  370. /// </remarks>
  371. /// <param name="x">Column relative to the left side of the Viewport.</param>
  372. /// <param name="y">Row relative to the top of the Viewport</param>
  373. public Point ScreenToViewport (int x, int y)
  374. {
  375. Point viewportOffset = GetViewportOffsetFromFrame ();
  376. Point screen = ScreenToFrame (x, y);
  377. screen.Offset (-viewportOffset.X, -viewportOffset.Y);
  378. return screen;
  379. }
  380. /// <summary>
  381. /// Helper to get the X and Y offset of the Viewport from the Frame. This is the sum of the Left and Top properties
  382. /// of <see cref="Margin"/>, <see cref="Border"/> and <see cref="Padding"/>.
  383. /// </summary>
  384. public Point GetViewportOffsetFromFrame () { return Padding is null ? Point.Empty : Padding.Thickness.GetInside (Padding.Frame).Location; }
  385. /// <summary>
  386. /// Scrolls the view vertically by the specified number of rows.
  387. /// </summary>
  388. /// <remarks>
  389. /// <para>
  390. /// </para>
  391. /// </remarks>
  392. /// <param name="rows"></param>
  393. /// <returns><see langword="true"/> if the <see cref="Viewport"/> was changed.</returns>
  394. public bool? ScrollVertical (int rows)
  395. {
  396. if (ContentSize == Size.Empty || ContentSize == Viewport.Size)
  397. {
  398. return false;
  399. }
  400. Viewport = Viewport with { Y = Viewport.Y + rows };
  401. return true;
  402. }
  403. /// <summary>
  404. /// Scrolls the view horizontally by the specified number of columns.
  405. /// </summary>
  406. /// <remarks>
  407. /// <para>
  408. /// </para>
  409. /// </remarks>
  410. /// <param name="cols"></param>
  411. /// <returns><see langword="true"/> if the <see cref="Viewport"/> was changed.</returns>
  412. public bool? ScrollHorizontal (int cols)
  413. {
  414. if (ContentSize == Size.Empty || ContentSize == Viewport.Size)
  415. {
  416. return false;
  417. }
  418. Viewport = Viewport with { X = Viewport.X + cols };
  419. return true;
  420. }
  421. #endregion Viewport
  422. }