ViewScrolling.cs 11 KB

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