ViewContent.cs 17 KB

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