ViewContent.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. using System.Diagnostics;
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// Settings for how scrolling the <see cref="View.Viewport"/> on the View's Content Area is handled.
  5. /// </summary>
  6. [Flags]
  7. public enum ScrollSettings
  8. {
  9. /// <summary>
  10. /// Default settings.
  11. /// </summary>
  12. Default = 0,
  13. /// <summary>
  14. /// If set, does not restrict vertical scrolling to <see cref="View.ContentSize"/>.<c>Height</c>.
  15. /// </summary>
  16. NoRestrictVertical = 1,
  17. /// <summary>
  18. /// If set, does not restrict horizontal scrolling to <see cref="View.ContentSize"/>.<c>Width</c>.
  19. /// </summary>
  20. NoRestrictHorizontal = 2,
  21. /// <summary>
  22. /// If set, does not restrict either vertical or horizontal scrolling to <see cref="View.ContentSize"/>.
  23. /// </summary>
  24. NoRestrict = NoRestrictVertical | NoRestrictHorizontal
  25. }
  26. public partial class View
  27. {
  28. #region Content Area
  29. private Size _contentSize;
  30. /// <summary>
  31. /// Gets or sets the size of the View's content. If the value is <c>Size.Empty</c> the size of the content is
  32. /// the same as the size of <see cref="Viewport"/>, and <c>Viewport.Location</c> will always be <c>0, 0</c>.
  33. /// </summary>
  34. /// <remarks>
  35. /// <para>
  36. /// If a positive size is provided, <see cref="Viewport"/> describes the portion of the content currently visible
  37. /// to the view. This enables virtual scrolling.
  38. /// </para>
  39. /// <para>
  40. /// Negative sizes are not supported.
  41. /// </para>
  42. /// </remarks>
  43. public Size ContentSize
  44. {
  45. get => _contentSize == Size.Empty ? Viewport.Size : _contentSize;
  46. set
  47. {
  48. if (value.Width < 0 || value.Height < 0)
  49. {
  50. throw new ArgumentException (@"ContentSize cannot be negative.", nameof (value));
  51. }
  52. if (value == _contentSize)
  53. {
  54. return;
  55. }
  56. _contentSize = value;
  57. OnContentSizeChanged (new (_contentSize));
  58. }
  59. }
  60. /// <summary>
  61. /// Called when <see cref="ContentSize"/> changes. Invokes the <see cref="ContentSizeChanged"/> event.
  62. /// </summary>
  63. /// <param name="e"></param>
  64. /// <returns></returns>
  65. protected bool? OnContentSizeChanged (SizeChangedEventArgs e)
  66. {
  67. ContentSizeChanged?.Invoke (this, e);
  68. if (e.Cancel != true)
  69. {
  70. SetNeedsDisplay ();
  71. }
  72. return e.Cancel;
  73. }
  74. /// <summary>
  75. /// Event that is raised when the <see cref="ContentSize"/> changes.
  76. /// </summary>
  77. public event EventHandler<SizeChangedEventArgs> ContentSizeChanged;
  78. /// <summary>
  79. /// Converts a Content-relative location to a Screen-relative location.
  80. /// </summary>
  81. /// <param name="location">The Content-relative location.</param>
  82. /// <returns>The Screen-relative location.</returns>
  83. public Point ContentToScreen (in Point location)
  84. {
  85. // Subtract the ViewportOffsetFromFrame to get the Viewport-relative location.
  86. Point viewportOffset = GetViewportOffsetFromFrame ();
  87. Point contentRelativeToViewport = location;
  88. contentRelativeToViewport.Offset (-Viewport.X, -Viewport.Y);
  89. // Translate to Screen-Relative (our SuperView's Viewport-relative coordinates)
  90. Rectangle screen = ViewportToScreen (new (contentRelativeToViewport, Size.Empty));
  91. return screen.Location;
  92. }
  93. /// <summary>Converts a Screen-relative coordinate to a Content-relative coordinate.</summary>
  94. /// <remarks>
  95. /// Content-relative means relative to the top-left corner of the view's Content, which is
  96. /// always at <c>0, 0</c>.
  97. /// </remarks>
  98. /// <param name="x">Column relative to the left side of the Content.</param>
  99. /// <param name="y">Row relative to the top of the Content</param>
  100. /// <returns>The coordinate relative to this view's Content.</returns>
  101. public Point ScreenToContent (in Point location)
  102. {
  103. Point viewportOffset = GetViewportOffsetFromFrame ();
  104. Point screen = ScreenToFrame (location.X, location.Y);
  105. screen.Offset (Viewport.X - viewportOffset.X, Viewport.Y - viewportOffset.Y);
  106. return screen;
  107. }
  108. #endregion Content Area
  109. #region Viewport
  110. /// <summary>
  111. /// Gets or sets how scrolling the <see cref="View.Viewport"/> on the View's Content Area is handled.
  112. /// </summary>
  113. public ScrollSettings ScrollSettings { get; set; }
  114. /// <summary>
  115. /// The location of the viewport into the view's content (0,0) is the top-left corner of the content. The Content
  116. /// area's size
  117. /// is <see cref="ContentSize"/>.
  118. /// </summary>
  119. private Point _viewportLocation;
  120. /// <summary>
  121. /// Gets or sets the rectangle describing the portion of the View's content that is visible to the user.
  122. /// The viewport Location is relative to the top-left corner of the inner rectangle of <see cref="Padding"/>.
  123. /// If the viewport Size is the same as <see cref="ContentSize"/> the Location will be <c>0, 0</c>.
  124. /// </summary>
  125. /// <value>
  126. /// The rectangle describing the location and size of the viewport into the View's virtual content, described by
  127. /// <see cref="ContentSize"/>.
  128. /// </value>
  129. /// <remarks>
  130. /// <para>
  131. /// Positive values for the location indicate the visible area is offset into (down-and-right) the View's virtual
  132. /// <see cref="ContentSize"/>. This enables virtual scrolling.
  133. /// </para>
  134. /// <para>
  135. /// Negative values for the location indicate the visible area is offset above (up-and-left) the View's virtual
  136. /// <see cref="ContentSize"/>. This enables virtual zoom.
  137. /// </para>
  138. /// <para>
  139. /// The <see cref="ScrollSettings"/> property controls how scrolling is handled. If <see cref="ScrollSettings"/> is
  140. /// </para>
  141. /// <para>
  142. /// If <see cref="LayoutStyle"/> is <see cref="LayoutStyle.Computed"/> the value of Viewport is indeterminate until
  143. /// the view has been initialized ( <see cref="IsInitialized"/> is true) and <see cref="LayoutSubviews"/> has been
  144. /// called.
  145. /// </para>
  146. /// <para>
  147. /// Updates to the Viewport Size updates <see cref="Frame"/>, and has the same impact as updating the
  148. /// <see cref="Frame"/>.
  149. /// </para>
  150. /// <para>
  151. /// Altering the Viewport Size will eventually (when the view is next laid out) cause the
  152. /// <see cref="LayoutSubview(View, Size)"/> and <see cref="OnDrawContent(Rectangle)"/> methods to be called.
  153. /// </para>
  154. /// </remarks>
  155. public virtual Rectangle Viewport
  156. {
  157. get
  158. {
  159. #if DEBUG
  160. if (LayoutStyle == LayoutStyle.Computed && !IsInitialized)
  161. {
  162. Debug.WriteLine (
  163. $"WARNING: Viewport is being accessed before the View has been initialized. This is likely a bug in {this}"
  164. );
  165. }
  166. #endif // DEBUG
  167. if (Margin is null || Border is null || Padding is null)
  168. {
  169. // CreateAdornments has not been called yet.
  170. return new (_viewportLocation, Frame.Size);
  171. }
  172. Thickness thickness = GetAdornmentsThickness ();
  173. return new (
  174. _viewportLocation,
  175. new (
  176. Math.Max (0, Frame.Size.Width - thickness.Horizontal),
  177. Math.Max (0, Frame.Size.Height - thickness.Vertical)
  178. ));
  179. }
  180. set
  181. {
  182. if (!ScrollSettings.HasFlag (ScrollSettings.NoRestrictVertical))
  183. {
  184. if (value.Y + Viewport.Height > ContentSize.Height)
  185. {
  186. value.Y = ContentSize.Height - Viewport.Height;
  187. }
  188. if (value.Y < 0)
  189. {
  190. value.Y = 0;
  191. }
  192. }
  193. if (!ScrollSettings.HasFlag (ScrollSettings.NoRestrictHorizontal))
  194. {
  195. if (value.X + Viewport.Width > ContentSize.Width)
  196. {
  197. value.X = ContentSize.Width - Viewport.Width;
  198. }
  199. if (value.X < 0)
  200. {
  201. value.X = 0;
  202. }
  203. }
  204. Thickness thickness = GetAdornmentsThickness ();
  205. Size newSize = new (
  206. value.Size.Width + thickness.Horizontal,
  207. value.Size.Height + thickness.Vertical);
  208. if (newSize == Frame.Size)
  209. {
  210. // The change is not changing the Frame, so we don't need to update it.
  211. // Just call SetNeedsLayout to update the layout.
  212. if (_viewportLocation != value.Location)
  213. {
  214. _viewportLocation = value.Location;
  215. SetNeedsLayout ();
  216. }
  217. return;
  218. }
  219. _viewportLocation = value.Location;
  220. // Update the Frame because we made it bigger or smaller which impacts subviews.
  221. Frame = Frame with
  222. {
  223. Size = newSize
  224. };
  225. }
  226. }
  227. /// <summary>
  228. /// Converts a <see cref="Viewport"/>-relative location to a screen-relative location.
  229. /// </summary>
  230. /// <remarks>
  231. /// Viewport-relative means relative to the top-left corner of the inner rectangle of the <see cref="Padding"/>.
  232. /// </remarks>
  233. public Rectangle ViewportToScreen (in Rectangle location)
  234. {
  235. // Translate bounds to Frame (our SuperView's Viewport-relative coordinates)
  236. Rectangle screen = FrameToScreen ();
  237. Point viewportOffset = GetViewportOffsetFromFrame ();
  238. screen.Offset (viewportOffset.X + location.X, viewportOffset.Y + location.Y);
  239. return new (screen.Location, location.Size);
  240. }
  241. /// <summary>Converts a screen-relative coordinate to a Viewport-relative coordinate.</summary>
  242. /// <returns>The coordinate relative to this view's <see cref="Viewport"/>.</returns>
  243. /// <remarks>
  244. /// Viewport-relative means relative to the top-left corner of the inner rectangle of the <see cref="Padding"/>.
  245. /// </remarks>
  246. /// <param name="x">Column relative to the left side of the Viewport.</param>
  247. /// <param name="y">Row relative to the top of the Viewport</param>
  248. public Point ScreenToViewport (int x, int y)
  249. {
  250. Point viewportOffset = GetViewportOffsetFromFrame ();
  251. Point screen = ScreenToFrame (x, y);
  252. screen.Offset (-viewportOffset.X, -viewportOffset.Y);
  253. return screen;
  254. }
  255. /// <summary>
  256. /// Helper to get the X and Y offset of the Viewport from the Frame. This is the sum of the Left and Top properties
  257. /// of <see cref="Margin"/>, <see cref="Border"/> and <see cref="Padding"/>.
  258. /// </summary>
  259. public Point GetViewportOffsetFromFrame () { return Padding is null ? Point.Empty : Padding.Thickness.GetInside (Padding.Frame).Location; }
  260. /// <summary>
  261. /// Scrolls the view vertically by the specified number of rows.
  262. /// </summary>
  263. /// <remarks>
  264. /// <para>
  265. /// </para>
  266. /// </remarks>
  267. /// <param name="rows"></param>
  268. /// <returns><see langword="true"/> if the <see cref="Viewport"/> was changed.</returns>
  269. public bool? ScrollVertical (int rows)
  270. {
  271. if (ContentSize == Size.Empty || ContentSize == Viewport.Size)
  272. {
  273. return false;
  274. }
  275. Viewport = Viewport with { Y = Viewport.Y + rows };
  276. return true;
  277. }
  278. /// <summary>
  279. /// Scrolls the view horizontally by the specified number of columns.
  280. /// </summary>
  281. /// <remarks>
  282. /// <para>
  283. /// </para>
  284. /// </remarks>
  285. /// <param name="cols"></param>
  286. /// <returns><see langword="true"/> if the <see cref="Viewport"/> was changed.</returns>
  287. public bool? ScrollHorizontal (int cols)
  288. {
  289. if (ContentSize == Size.Empty || ContentSize == Viewport.Size)
  290. {
  291. return false;
  292. }
  293. Viewport = Viewport with { X = Viewport.X + cols };
  294. return true;
  295. }
  296. #endregion Viewport
  297. }