ViewContent.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. /// Updates to the Viewport Size updates <see cref="Frame"/>, and has the same impact as updating the
  164. /// <see cref="Frame"/>.
  165. /// </para>
  166. /// <para>
  167. /// Altering the Viewport Size will eventually (when the view is next laid out) cause the
  168. /// <see cref="LayoutSubview(View, Size)"/> and <see cref="OnDrawContent(Rectangle)"/> methods to be called.
  169. /// </para>
  170. /// </remarks>
  171. public virtual Rectangle Viewport
  172. {
  173. get
  174. {
  175. if (Margin is null || Border is null || Padding is null)
  176. {
  177. // CreateAdornments has not been called yet.
  178. return new (_viewportLocation, Frame.Size);
  179. }
  180. Thickness thickness = GetAdornmentsThickness ();
  181. return new (
  182. _viewportLocation,
  183. new (
  184. Math.Max (0, Frame.Size.Width - thickness.Horizontal),
  185. Math.Max (0, Frame.Size.Height - thickness.Vertical)
  186. ));
  187. }
  188. set => SetViewport (value);
  189. }
  190. private void SetViewport (Rectangle viewport)
  191. {
  192. Rectangle oldViewport = viewport;
  193. ApplySettings (ref viewport);
  194. Thickness thickness = GetAdornmentsThickness ();
  195. Size newSize = new (
  196. viewport.Size.Width + thickness.Horizontal,
  197. viewport.Size.Height + thickness.Vertical);
  198. if (newSize == Frame.Size)
  199. {
  200. // The change is not changing the Frame, so we don't need to update it.
  201. // Just call SetNeedsLayout to update the layout.
  202. if (_viewportLocation != viewport.Location)
  203. {
  204. _viewportLocation = viewport.Location;
  205. SetNeedsLayout ();
  206. }
  207. OnViewportChanged (new (IsInitialized ? Viewport : Rectangle.Empty, oldViewport));
  208. return;
  209. }
  210. _viewportLocation = viewport.Location;
  211. // Update the Frame because we made it bigger or smaller which impacts subviews.
  212. Frame = Frame with
  213. {
  214. Size = newSize
  215. };
  216. void ApplySettings (ref Rectangle newViewport)
  217. {
  218. if (!ViewportSettings.HasFlag (ViewportSettings.AllowXGreaterThanContentWidth))
  219. {
  220. if (newViewport.X >= ContentSize.Width)
  221. {
  222. newViewport.X = ContentSize.Width - 1;
  223. }
  224. }
  225. // IMPORTANT: Check for negative location AFTER checking for location greater than content width
  226. if (!ViewportSettings.HasFlag (ViewportSettings.AllowNegativeX))
  227. {
  228. if (newViewport.X < 0)
  229. {
  230. newViewport.X = 0;
  231. }
  232. }
  233. if (!ViewportSettings.HasFlag (ViewportSettings.AllowYGreaterThanContentHeight))
  234. {
  235. if (newViewport.Y >= ContentSize.Height)
  236. {
  237. newViewport.Y = ContentSize.Height - 1;
  238. }
  239. }
  240. // IMPORTANT: Check for negative location AFTER checking for location greater than content width
  241. if (!ViewportSettings.HasFlag (ViewportSettings.AllowNegativeY))
  242. {
  243. if (newViewport.Y < 0)
  244. {
  245. newViewport.Y = 0;
  246. }
  247. }
  248. }
  249. }
  250. /// <summary>
  251. /// Fired when the <see cref="Viewport"/> changes. This event is fired after the <see cref="Viewport"/> has been updated.
  252. /// </summary>
  253. [CanBeNull]
  254. public event EventHandler<DrawEventArgs> ViewportChanged;
  255. /// <summary>
  256. /// Called when the <see cref="Viewport"/> changes. Invokes the <see cref="ViewportChanged"/> event.
  257. /// </summary>
  258. /// <param name="e"></param>
  259. protected virtual void OnViewportChanged (DrawEventArgs e)
  260. {
  261. ViewportChanged?.Invoke (this, e);
  262. }
  263. /// <summary>
  264. /// Converts a <see cref="Viewport"/>-relative location and size to a screen-relative location and size.
  265. /// </summary>
  266. /// <remarks>
  267. /// Viewport-relative means relative to the top-left corner of the inner rectangle of the <see cref="Padding"/>.
  268. /// </remarks>
  269. /// <param name="viewport">Viewport-relative location and size.</param>
  270. /// <returns>Screen-relative location and size.</returns>
  271. public Rectangle ViewportToScreen (in Rectangle viewport)
  272. {
  273. return viewport with { Location = ViewportToScreen (viewport.Location) };
  274. }
  275. /// <summary>
  276. /// Converts a <see cref="Viewport"/>-relative location to a screen-relative location.
  277. /// </summary>
  278. /// <remarks>
  279. /// Viewport-relative means relative to the top-left corner of the inner rectangle of the <see cref="Padding"/>.
  280. /// </remarks>
  281. /// <param name="viewportLocation">Viewport-relative location.</param>
  282. /// <returns>Screen-relative location.</returns>
  283. public Point ViewportToScreen (in Point viewportLocation)
  284. {
  285. // Translate bounds to Frame (our SuperView's Viewport-relative coordinates)
  286. Rectangle screen = FrameToScreen ();
  287. Point viewportOffset = GetViewportOffsetFromFrame ();
  288. screen.Offset (viewportOffset.X + viewportLocation.X, viewportOffset.Y + viewportLocation.Y);
  289. return screen.Location;
  290. }
  291. /// <summary>Converts a screen-relative coordinate to a Viewport-relative coordinate.</summary>
  292. /// <returns>The coordinate relative to this view's <see cref="Viewport"/>.</returns>
  293. /// <remarks>
  294. /// Viewport-relative means relative to the top-left corner of the inner rectangle of the <see cref="Padding"/>.
  295. /// </remarks>
  296. /// <param name="location">Screen-Relative Coordinate.</param>
  297. /// <returns>Viewport-relative location.</returns>
  298. public Point ScreenToViewport (in Point location)
  299. {
  300. Point viewportOffset = GetViewportOffsetFromFrame ();
  301. Point frame = ScreenToFrame (location);
  302. frame.Offset (-viewportOffset.X, -viewportOffset.Y);
  303. return frame;
  304. }
  305. /// <summary>
  306. /// Helper to get the X and Y offset of the Viewport from the Frame. This is the sum of the Left and Top properties
  307. /// of <see cref="Margin"/>, <see cref="Border"/> and <see cref="Padding"/>.
  308. /// </summary>
  309. public Point GetViewportOffsetFromFrame () { return Padding is null ? Point.Empty : Padding.Thickness.GetInside (Padding.Frame).Location; }
  310. /// <summary>
  311. /// Scrolls the view vertically by the specified number of rows.
  312. /// </summary>
  313. /// <remarks>
  314. /// <para>
  315. /// </para>
  316. /// </remarks>
  317. /// <param name="rows"></param>
  318. /// <returns><see langword="true"/> if the <see cref="Viewport"/> was changed.</returns>
  319. public bool? ScrollVertical (int rows)
  320. {
  321. if (ContentSize == Size.Empty || ContentSize == Viewport.Size)
  322. {
  323. return false;
  324. }
  325. Viewport = Viewport with { Y = Viewport.Y + rows };
  326. return true;
  327. }
  328. /// <summary>
  329. /// Scrolls the view horizontally by the specified number of columns.
  330. /// </summary>
  331. /// <remarks>
  332. /// <para>
  333. /// </para>
  334. /// </remarks>
  335. /// <param name="cols"></param>
  336. /// <returns><see langword="true"/> if the <see cref="Viewport"/> was changed.</returns>
  337. public bool? ScrollHorizontal (int cols)
  338. {
  339. if (ContentSize == Size.Empty || ContentSize == Viewport.Size)
  340. {
  341. return false;
  342. }
  343. Viewport = Viewport with { X = Viewport.X + cols };
  344. return true;
  345. }
  346. #endregion Viewport
  347. }