ViewContent.cs 15 KB

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