ScrollSlider.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. #nullable enable
  2. using System.ComponentModel;
  3. namespace Terminal.Gui;
  4. /// <summary>
  5. /// The ScrollSlider can be dragged with the mouse, constrained by the size of the Viewport of it's superview. The
  6. /// ScrollSlider can be
  7. /// oriented either vertically or horizontally.
  8. /// </summary>
  9. /// <remarks>
  10. /// <para>
  11. /// Used to represent the proportion of the visible content to the Viewport in a <see cref="Scrolled"/>.
  12. /// </para>
  13. /// </remarks>
  14. public class ScrollSlider : View, IOrientation, IDesignable
  15. {
  16. /// <summary>
  17. /// Initializes a new instance.
  18. /// </summary>
  19. public ScrollSlider ()
  20. {
  21. Id = "scrollSlider";
  22. WantMousePositionReports = true;
  23. _orientationHelper = new (this); // Do not use object initializer!
  24. _orientationHelper.Orientation = Orientation.Vertical;
  25. _orientationHelper.OrientationChanging += (sender, e) => OrientationChanging?.Invoke (this, e);
  26. _orientationHelper.OrientationChanged += (sender, e) => OrientationChanged?.Invoke (this, e);
  27. OnOrientationChanged (Orientation);
  28. HighlightStyle = HighlightStyle.Hover;
  29. }
  30. #region IOrientation members
  31. private readonly OrientationHelper _orientationHelper;
  32. /// <inheritdoc/>
  33. public Orientation Orientation
  34. {
  35. get => _orientationHelper.Orientation;
  36. set => _orientationHelper.Orientation = value;
  37. }
  38. /// <inheritdoc/>
  39. public event EventHandler<CancelEventArgs<Orientation>>? OrientationChanging;
  40. /// <inheritdoc/>
  41. public event EventHandler<EventArgs<Orientation>>? OrientationChanged;
  42. /// <inheritdoc/>
  43. public void OnOrientationChanged (Orientation newOrientation)
  44. {
  45. TextDirection = Orientation == Orientation.Vertical ? TextDirection.TopBottom_LeftRight : TextDirection.LeftRight_TopBottom;
  46. TextAlignment = Alignment.Center;
  47. VerticalTextAlignment = Alignment.Center;
  48. // Reset Position to 0 when changing orientation
  49. X = 0;
  50. Y = 0;
  51. Position = 0;
  52. // Reset opposite dim to Dim.Fill ()
  53. if (Orientation == Orientation.Vertical)
  54. {
  55. Height = Width;
  56. Width = Dim.Fill ();
  57. }
  58. else
  59. {
  60. Width = Height;
  61. Height = Dim.Fill ();
  62. }
  63. SetNeedsLayout ();
  64. }
  65. #endregion
  66. /// <inheritdoc/>
  67. protected override bool OnClearingViewport ()
  68. {
  69. if (Orientation == Orientation.Vertical)
  70. {
  71. FillRect (Viewport with { Height = Size }, Glyphs.ContinuousMeterSegment);
  72. }
  73. else
  74. {
  75. FillRect (Viewport with { Width = Size }, Glyphs.ContinuousMeterSegment);
  76. }
  77. return true;
  78. }
  79. private int? _size;
  80. /// <summary>
  81. /// Gets or sets the size of the ScrollSlider. This is a helper that gets or sets Width or Height depending
  82. /// on <see cref="Orientation"/>. The size will be clamped between 1 and the dimension of
  83. /// the <see cref="View.SuperView"/>'s Viewport.
  84. /// </summary>
  85. /// <remarks>
  86. /// <para>
  87. /// The dimension of the ScrollSlider that is perpendicular to the <see cref="Orientation"/> will be set to
  88. /// <see cref="Dim.Fill()"/>
  89. /// </para>
  90. /// </remarks>
  91. public int Size
  92. {
  93. get => _size ?? 1;
  94. set
  95. {
  96. if (value == _size)
  97. {
  98. return;
  99. }
  100. _size = Math.Clamp (value, 1, VisibleContentSize);
  101. if (Orientation == Orientation.Vertical)
  102. {
  103. Height = _size;
  104. }
  105. else
  106. {
  107. Width = _size;
  108. }
  109. SetNeedsLayout ();
  110. }
  111. }
  112. private int? _visibleContentSize;
  113. /// <summary>
  114. /// Gets or sets the size of the viewport into the content being scrolled. If not explicitly set, will be the
  115. /// greater of 1 and the dimension of the <see cref="View.SuperView"/>.
  116. /// </summary>
  117. public int VisibleContentSize
  118. {
  119. get
  120. {
  121. if (_visibleContentSize.HasValue)
  122. {
  123. return _visibleContentSize.Value;
  124. }
  125. return Math.Max (1, Orientation == Orientation.Vertical ? SuperView?.Viewport.Height ?? 2048 : SuperView?.Viewport.Width ?? 2048);
  126. }
  127. set
  128. {
  129. if (value == _visibleContentSize)
  130. {
  131. return;
  132. }
  133. _visibleContentSize = int.Max (1, value);
  134. if (_position >= _visibleContentSize - _size)
  135. {
  136. Position = _position;
  137. }
  138. SetNeedsLayout ();
  139. }
  140. }
  141. private int _position;
  142. /// <summary>
  143. /// Gets or sets the position of the ScrollSlider relative to the size of the ScrollSlider's Frame.
  144. /// The position will be constrained such that the ScrollSlider will not go outside the Viewport of
  145. /// the <see cref="View.SuperView"/>.
  146. /// </summary>
  147. public int Position
  148. {
  149. get => _position;
  150. set
  151. {
  152. int clampedPosition = ClampPosition (value);
  153. if (_position == clampedPosition)
  154. {
  155. return;
  156. }
  157. RaisePositionChangeEvents (clampedPosition);
  158. SetNeedsLayout ();
  159. }
  160. }
  161. /// <summary>
  162. /// Moves the scroll slider to the specified position. Does not clamp.
  163. /// </summary>
  164. /// <param name="position"></param>
  165. internal void MoveToPosition (int position)
  166. {
  167. if (Orientation == Orientation.Vertical)
  168. {
  169. Y = _position + SliderPadding / 2;
  170. }
  171. else
  172. {
  173. X = _position + SliderPadding / 2;
  174. }
  175. }
  176. /// <summary>
  177. /// INTERNAL API (for unit tests) - Clamps the position such that the right side of the slider
  178. /// never goes past the edge of the Viewport.
  179. /// </summary>
  180. /// <param name="newPosition"></param>
  181. /// <returns></returns>
  182. internal int ClampPosition (int newPosition)
  183. {
  184. return Math.Clamp (newPosition, 0, Math.Max (SliderPadding / 2, VisibleContentSize - SliderPadding - Size));
  185. }
  186. private void RaisePositionChangeEvents (int newPosition)
  187. {
  188. if (OnPositionChanging (_position, newPosition))
  189. {
  190. return;
  191. }
  192. CancelEventArgs<int> args = new (ref _position, ref newPosition);
  193. PositionChanging?.Invoke (this, args);
  194. if (args.Cancel)
  195. {
  196. return;
  197. }
  198. int distance = newPosition - _position;
  199. _position = ClampPosition (newPosition);
  200. MoveToPosition (_position);
  201. OnPositionChanged (_position);
  202. PositionChanged?.Invoke (this, new (in _position));
  203. OnScrolled (distance);
  204. Scrolled?.Invoke (this, new (in distance));
  205. RaiseSelecting (new (Command.Select, null, null, distance));
  206. }
  207. /// <summary>
  208. /// Called when <see cref="Position"/> is changing. Return true to cancel the change.
  209. /// </summary>
  210. protected virtual bool OnPositionChanging (int currentPos, int newPos) { return false; }
  211. /// <summary>
  212. /// Raised when the <see cref="Position"/> is changing. Set <see cref="CancelEventArgs.Cancel"/> to
  213. /// <see langword="true"/> to prevent the position from being changed.
  214. /// </summary>
  215. public event EventHandler<CancelEventArgs<int>>? PositionChanging;
  216. /// <summary>Called when <see cref="Position"/> has changed.</summary>
  217. protected virtual void OnPositionChanged (int position) { }
  218. /// <summary>Raised when the <see cref="Position"/> has changed.</summary>
  219. public event EventHandler<EventArgs<int>>? PositionChanged;
  220. /// <summary>Called when <see cref="Position"/> has changed. Indicates how much to scroll.</summary>
  221. protected virtual void OnScrolled (int distance) { }
  222. /// <summary>Raised when the <see cref="Position"/> has changed. Indicates how much to scroll.</summary>
  223. public event EventHandler<EventArgs<int>>? Scrolled;
  224. /// <inheritdoc/>
  225. public override Attribute GetNormalColor () { return base.GetHotNormalColor (); }
  226. ///// <inheritdoc/>
  227. private int _lastLocation = -1;
  228. /// <summary>
  229. /// Gets or sets the amount to pad the start and end of the scroll slider. The default is 0.
  230. /// </summary>
  231. /// <remarks>
  232. /// When the scroll slider is used by <see cref="ScrollBar"/>, which has increment and decrement buttons, the
  233. /// SliderPadding should be set to the size of the buttons (typically 2).
  234. /// </remarks>
  235. public int SliderPadding { get; set; }
  236. /// <inheritdoc/>
  237. protected override bool OnMouseEvent (MouseEventArgs mouseEvent)
  238. {
  239. if (SuperView is null)
  240. {
  241. return false;
  242. }
  243. if (mouseEvent.IsSingleDoubleOrTripleClicked)
  244. {
  245. return true;
  246. }
  247. int location = (Orientation == Orientation.Vertical ? mouseEvent.Position.Y : mouseEvent.Position.X);
  248. int offsetFromLastLocation = _lastLocation > -1 ? location - _lastLocation : 0;
  249. int superViewDimension = VisibleContentSize;
  250. if (mouseEvent.IsPressed || mouseEvent.IsReleased)
  251. {
  252. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed) && _lastLocation == -1)
  253. {
  254. if (Application.MouseGrabView != this)
  255. {
  256. Application.GrabMouse (this);
  257. _lastLocation = location;
  258. }
  259. }
  260. else if (mouseEvent.Flags == (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))
  261. {
  262. int currentLocation;
  263. if (Orientation == Orientation.Vertical)
  264. {
  265. currentLocation = Frame.Y;
  266. }
  267. else
  268. {
  269. currentLocation = Frame.X;
  270. }
  271. currentLocation -= SliderPadding / 2;
  272. int newLocation = currentLocation + offsetFromLastLocation;
  273. Position = newLocation;
  274. }
  275. else if (mouseEvent.Flags == MouseFlags.Button1Released)
  276. {
  277. _lastLocation = -1;
  278. if (Application.MouseGrabView == this)
  279. {
  280. Application.UngrabMouse ();
  281. }
  282. }
  283. return true;
  284. }
  285. return false;
  286. }
  287. /// <summary>
  288. /// Gets the slider size.
  289. /// </summary>
  290. /// <param name="scrollableContentSize">The size of the content.</param>
  291. /// <param name="visibleContentSize">The size of the visible content.</param>
  292. /// <param name="sliderBounds">The bounds of the area the slider moves in (e.g. the size of the <see cref="ScrollBar"/> minus 2).</param>
  293. public static int CalculateSize (
  294. int scrollableContentSize,
  295. int visibleContentSize,
  296. int sliderBounds
  297. )
  298. {
  299. if (scrollableContentSize <= 0 || sliderBounds <= 0)
  300. {
  301. return 1; // Slider must be at least 1
  302. }
  303. if (visibleContentSize <= 0 || scrollableContentSize <= visibleContentSize)
  304. {
  305. return sliderBounds;
  306. }
  307. double sliderSizeD = ((double)visibleContentSize / scrollableContentSize) * sliderBounds;
  308. int sliderSize = (int)Math.Floor (sliderSizeD);
  309. return Math.Clamp (sliderSize, 1, sliderBounds);
  310. }
  311. /// <summary>
  312. /// Calculates the slider position.
  313. /// </summary>
  314. /// <param name="scrollableContentSize">The size of the content.</param>
  315. /// <param name="visibleContentSize">The size of the visible content.</param>
  316. /// <param name="contentPosition">The position in the content (between 0 and <paramref name="scrollableContentSize"/>).</param>
  317. /// <param name="sliderBounds">The bounds of the area the slider moves in (e.g. the size of the <see cref="ScrollBar"/> minus 2).</param>
  318. /// <param name="direction">The direction the slider is moving.</param>
  319. internal static int CalculatePosition (
  320. int scrollableContentSize,
  321. int visibleContentSize,
  322. int contentPosition,
  323. int sliderBounds,
  324. NavigationDirection direction
  325. )
  326. {
  327. if (scrollableContentSize - visibleContentSize <= 0 || sliderBounds <= 0)
  328. {
  329. return 0;
  330. }
  331. int calculatedSliderSize = CalculateSize (scrollableContentSize, visibleContentSize, sliderBounds);
  332. double newSliderPosition = ((double)contentPosition / (scrollableContentSize - visibleContentSize)) * (sliderBounds - calculatedSliderSize);
  333. return Math.Clamp ((int)Math.Round (newSliderPosition), 0, sliderBounds - calculatedSliderSize);
  334. }
  335. /// <summary>
  336. /// Calculates the content position.
  337. /// </summary>
  338. /// <param name="scrollableContentSize">The size of the content.</param>
  339. /// <param name="visibleContentSize">The size of the visible content.</param>
  340. /// <param name="sliderPosition">The position of the slider.</param>
  341. /// <param name="sliderBounds">The bounds of the area the slider moves in (e.g. the size of the <see cref="ScrollBar"/> minus 2).</param>
  342. internal static int CalculateContentPosition (
  343. int scrollableContentSize,
  344. int visibleContentSize,
  345. int sliderPosition,
  346. int sliderBounds
  347. )
  348. {
  349. int sliderSize = CalculateSize (scrollableContentSize, visibleContentSize, sliderBounds);
  350. double pos = ((double)(sliderPosition) / (sliderBounds - sliderSize)) * (scrollableContentSize - visibleContentSize);
  351. if (pos is double.NaN)
  352. {
  353. return 0;
  354. }
  355. double rounded = Math.Ceiling (pos);
  356. return (int)Math.Clamp (rounded, 0, Math.Max (0, scrollableContentSize - sliderSize));
  357. }
  358. /// <inheritdoc/>
  359. public bool EnableForDesign ()
  360. {
  361. Size = 5;
  362. return true;
  363. }
  364. }