ScrollSlider.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. #nullable enable
  2. using System.ComponentModel;
  3. using System.Diagnostics;
  4. namespace Terminal.Gui;
  5. /// <summary>
  6. /// The ScrollSlider can be dragged with the mouse, constrained by the size of the Viewport of it's superview. The ScrollSlider can be
  7. /// oriented either vertically or horizontally.
  8. /// </summary>
  9. /// <remarks>
  10. /// <para>
  11. /// If <see cref="View.Text"/> is set, it will be displayed centered within the slider. Set
  12. /// <see cref="ShowPercent"/> to automatically have the Text
  13. /// be show what percent the slider is to the Superview's Viewport size.
  14. /// </para>
  15. /// <para>
  16. /// Used to represent the proportion of the visible content to the Viewport in a <see cref="Scroll"/>.
  17. /// </para>
  18. /// </remarks>
  19. public class ScrollSlider : View, IOrientation, IDesignable
  20. {
  21. /// <summary>
  22. /// Initializes a new instance.
  23. /// </summary>
  24. public ScrollSlider ()
  25. {
  26. Id = "scrollSlider";
  27. WantMousePositionReports = true;
  28. _orientationHelper = new (this); // Do not use object initializer!
  29. _orientationHelper.Orientation = Orientation.Vertical;
  30. _orientationHelper.OrientationChanging += (sender, e) => OrientationChanging?.Invoke (this, e);
  31. _orientationHelper.OrientationChanged += (sender, e) => OrientationChanged?.Invoke (this, e);
  32. OnOrientationChanged (Orientation);
  33. HighlightStyle = HighlightStyle.Hover;
  34. // Default size is 1
  35. Size = 1;
  36. FrameChanged += OnFrameChanged;
  37. }
  38. #region IOrientation members
  39. private readonly OrientationHelper _orientationHelper;
  40. /// <inheritdoc/>
  41. public Orientation Orientation
  42. {
  43. get => _orientationHelper.Orientation;
  44. set => _orientationHelper.Orientation = value;
  45. }
  46. /// <inheritdoc/>
  47. public event EventHandler<CancelEventArgs<Orientation>>? OrientationChanging;
  48. /// <inheritdoc/>
  49. public event EventHandler<EventArgs<Orientation>>? OrientationChanged;
  50. /// <inheritdoc/>
  51. public void OnOrientationChanged (Orientation newOrientation)
  52. {
  53. TextDirection = Orientation == Orientation.Vertical ? TextDirection.TopBottom_LeftRight : TextDirection.LeftRight_TopBottom;
  54. TextAlignment = Alignment.Center;
  55. VerticalTextAlignment = Alignment.Center;
  56. // Reset Position to 0 when changing orientation
  57. X = 0;
  58. Y = 0;
  59. //Position = 0;
  60. // Reset Size to 1 when changing orientation
  61. if (Orientation == Orientation.Vertical)
  62. {
  63. Width = Dim.Fill ();
  64. Height = 1;
  65. }
  66. else
  67. {
  68. Width = 1;
  69. Height = Dim.Fill ();
  70. }
  71. }
  72. #endregion
  73. /// <inheritdoc/>
  74. protected override bool OnClearingViewport ()
  75. {
  76. FillRect (Viewport, Glyphs.ContinuousMeterSegment);
  77. return true;
  78. }
  79. private bool _showPercent;
  80. /// <summary>
  81. /// Gets or sets whether the ScrollSlider will set <see cref="View.Text"/> to show the percentage the slider
  82. /// takes up within the <see cref="View.SuperView"/>'s Viewport.
  83. /// </summary>
  84. public bool ShowPercent
  85. {
  86. get => _showPercent;
  87. set
  88. {
  89. _showPercent = value;
  90. SetNeedsDraw ();
  91. }
  92. }
  93. /// <summary>
  94. /// Gets or sets the size of the ScrollSlider. This is a helper that simply gets or sets the Width or Height depending on the
  95. /// <see cref="Orientation"/>. The size will be constrained such that the ScrollSlider will not go outside the Viewport of
  96. /// the <see cref="View.SuperView"/>. The size will never be less than 1.
  97. /// </summary>
  98. /// <remarks>
  99. /// <para>
  100. /// The dimension of the ScrollSlider that is perpendicular to the <see cref="Orientation"/> will be set to <see cref="Dim.Fill()"/>
  101. /// </para>
  102. /// </remarks>
  103. public int Size
  104. {
  105. get
  106. {
  107. if (Orientation == Orientation.Vertical)
  108. {
  109. return Viewport.Height;
  110. }
  111. else
  112. {
  113. return Viewport.Width;
  114. }
  115. }
  116. set
  117. {
  118. if (Orientation == Orientation.Vertical)
  119. {
  120. Width = Dim.Fill ();
  121. int viewport = Math.Max (1, SuperView?.Viewport.Height ?? 1);
  122. Height = Math.Clamp (value, 1, viewport);
  123. }
  124. else
  125. {
  126. int viewport = Math.Max (1, SuperView?.Viewport.Width ?? 1);
  127. Width = Math.Clamp (value, 1, viewport);
  128. Height = Dim.Fill ();
  129. }
  130. }
  131. }
  132. /// <summary>
  133. /// Gets the size of the viewport into the content being scrolled, bounded by <see cref="Size"/>.
  134. /// </summary>
  135. /// <remarks>
  136. /// This is the SuperView's Viewport demension.
  137. /// </remarks>
  138. public int ViewportDimension => Orientation == Orientation.Vertical ? SuperView?.Viewport.Height ?? 0 : SuperView?.Viewport.Width ?? 0;
  139. private void OnFrameChanged (object? sender, EventArgs<Rectangle> e)
  140. {
  141. Position = Orientation == Orientation.Vertical ? e.CurrentValue.Y : e.CurrentValue.X;
  142. }
  143. private int _position;
  144. /// <summary>
  145. /// Gets or sets the position of the ScrollSlider relative to the size of the ScrollSlider's Frame.
  146. /// The position will be constrained such that the ScrollSlider will not go outside the Viewport of
  147. /// the <see cref="View.SuperView"/>.
  148. /// </summary>
  149. public int Position
  150. {
  151. get => _position;
  152. set
  153. {
  154. if (_position == value)
  155. {
  156. return;
  157. }
  158. RaisePositionChangeEvents (ClampPosition (value));
  159. SetNeedsLayout ();
  160. }
  161. }
  162. public void SetPosition (int position)
  163. {
  164. _position = ClampPosition (position);
  165. if (Orientation == Orientation.Vertical)
  166. {
  167. Y = _position;
  168. }
  169. else
  170. {
  171. X = _position;
  172. }
  173. }
  174. private int ClampPosition (int newPosittion)
  175. {
  176. if (SuperView is null || !IsInitialized)
  177. {
  178. return 1;
  179. }
  180. if (Orientation == Orientation.Vertical)
  181. {
  182. return Math.Clamp (newPosittion, 0, Math.Max (0, ViewportDimension - Viewport.Height));
  183. }
  184. else
  185. {
  186. return Math.Clamp (newPosittion, 0, Math.Max (0, ViewportDimension - Viewport.Width));
  187. }
  188. }
  189. private void RaisePositionChangeEvents (int newPosition)
  190. {
  191. if (OnPositionChanging (_position, newPosition))
  192. {
  193. return;
  194. }
  195. CancelEventArgs<int> args = new (ref _position, ref newPosition);
  196. PositionChanging?.Invoke (this, args);
  197. if (args.Cancel)
  198. {
  199. return;
  200. }
  201. int scrollAmount = newPosition - _position;
  202. _position = newPosition;
  203. OnPositionChanged (_position);
  204. PositionChanged?.Invoke (this, new (in _position));
  205. OnScroll (scrollAmount);
  206. Scroll?.Invoke (this, new (in scrollAmount));
  207. RaiseSelecting (new CommandContext (Command.Select, null, null, scrollAmount));
  208. }
  209. /// <summary>
  210. /// Called when <see cref="Position"/> is changing. Return true to cancel the change.
  211. /// </summary>
  212. protected virtual bool OnPositionChanging (int currentPos, int newPos) { return false; }
  213. /// <summary>
  214. /// Raised when the <see cref="Position"/> is changing. Set <see cref="CancelEventArgs.Cancel"/> to
  215. /// <see langword="true"/> to prevent the position from being changed.
  216. /// </summary>
  217. public event EventHandler<CancelEventArgs<int>>? PositionChanging;
  218. /// <summary>Called when <see cref="Position"/> has changed.</summary>
  219. protected virtual void OnPositionChanged (int position) { }
  220. /// <summary>Raised when the <see cref="Position"/> has changed.</summary>
  221. public event EventHandler<EventArgs<int>>? PositionChanged;
  222. /// <summary>Called when <see cref="Position"/> has changed. Indicates how much to scroll.</summary>
  223. protected virtual void OnScroll (int scrollAmount) { }
  224. /// <summary>Raised when the <see cref="Position"/> has changed. Indicates how much to scroll.</summary>
  225. public event EventHandler<EventArgs<int>>? Scroll;
  226. /// <inheritdoc/>
  227. protected override bool OnDrawingText ()
  228. {
  229. if (!ShowPercent)
  230. {
  231. Text = string.Empty;
  232. return false;
  233. }
  234. if (SuperView is null)
  235. {
  236. return false;
  237. }
  238. if (Orientation == Orientation.Vertical)
  239. {
  240. Text = $"{(int)Math.Round ((double)Viewport.Height / SuperView!.GetContentSize ().Height * 100)}%";
  241. }
  242. else
  243. {
  244. Text = $"{(int)Math.Round ((double)Viewport.Width / SuperView!.GetContentSize ().Width * 100)}%";
  245. }
  246. return false;
  247. }
  248. /// <inheritdoc/>
  249. public override Attribute GetNormalColor () { return base.GetHotNormalColor (); }
  250. ///// <inheritdoc/>
  251. private int _lastLocation = -1;
  252. /// <inheritdoc/>
  253. protected override bool OnMouseEvent (MouseEventArgs mouseEvent)
  254. {
  255. if (SuperView is null)
  256. {
  257. return false;
  258. }
  259. int location = Orientation == Orientation.Vertical ? mouseEvent.Position.Y : mouseEvent.Position.X;
  260. int offset = _lastLocation > -1 ? location - _lastLocation : 0;
  261. int superViewDimension = Orientation == Orientation.Vertical ? SuperView!.Viewport.Height : SuperView!.Viewport.Width;
  262. if (mouseEvent.IsPressed || mouseEvent.IsReleased)
  263. {
  264. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed) && _lastLocation == -1)
  265. {
  266. if (Application.MouseGrabView != this)
  267. {
  268. Application.GrabMouse (this);
  269. _lastLocation = location;
  270. }
  271. }
  272. else if (mouseEvent.Flags == (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))
  273. {
  274. if (Orientation == Orientation.Vertical)
  275. {
  276. Y = Frame.Y + offset < 0
  277. ? 0
  278. : Frame.Y + offset + Frame.Height > superViewDimension
  279. ? Math.Max (superViewDimension - Frame.Height, 0)
  280. : Frame.Y + offset;
  281. }
  282. else
  283. {
  284. X = Frame.X + offset < 0
  285. ? 0
  286. : Frame.X + offset + Frame.Width > superViewDimension
  287. ? Math.Max (superViewDimension - Frame.Width, 0)
  288. : Frame.X + offset;
  289. }
  290. }
  291. else if (mouseEvent.Flags == MouseFlags.Button1Released)
  292. {
  293. _lastLocation = -1;
  294. if (Application.MouseGrabView == this)
  295. {
  296. Application.UngrabMouse ();
  297. }
  298. }
  299. return true;
  300. }
  301. return false;
  302. }
  303. /// <inheritdoc />
  304. public bool EnableForDesign ()
  305. {
  306. OrientationChanged += (sender, args) =>
  307. {
  308. if (args.CurrentValue == Orientation.Vertical)
  309. {
  310. Width = Dim.Fill ();
  311. Height = 5;
  312. }
  313. else
  314. {
  315. Width = 5;
  316. Height = Dim.Fill();
  317. }
  318. };
  319. Orientation = Orientation.Horizontal;
  320. ShowPercent = true;
  321. return true;
  322. }
  323. }