ScrollSlider.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #nullable enable
  2. using System.Diagnostics;
  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 ScrollSlider can be
  6. /// oriented either vertically or horizontally.
  7. /// </summary>
  8. /// <remarks>
  9. /// <para>
  10. /// If <see cref="View.Text"/> is set, it will be displayed centered within the slider. Set
  11. /// <see cref="ShowPercent"/> to automatically have the Text
  12. /// be show what percent the slider is to the Superview's Viewport size.
  13. /// </para>
  14. /// <para>
  15. /// Used to represent the proportion of the visible content to the Viewport in a <see cref="Scroll"/>.
  16. /// </para>
  17. /// </remarks>
  18. public class ScrollSlider : View, IOrientation, IDesignable
  19. {
  20. /// <summary>
  21. /// Initializes a new instance.
  22. /// </summary>
  23. public ScrollSlider ()
  24. {
  25. Id = "scrollSlider";
  26. WantMousePositionReports = true;
  27. _orientationHelper = new (this); // Do not use object initializer!
  28. _orientationHelper.Orientation = Orientation.Vertical;
  29. _orientationHelper.OrientationChanging += (sender, e) => OrientationChanging?.Invoke (this, e);
  30. _orientationHelper.OrientationChanged += (sender, e) => OrientationChanged?.Invoke (this, e);
  31. OnOrientationChanged (Orientation);
  32. HighlightStyle = HighlightStyle.Hover;
  33. // Default size is 1
  34. Size = 1;
  35. }
  36. #region IOrientation members
  37. private readonly OrientationHelper _orientationHelper;
  38. /// <inheritdoc/>
  39. public Orientation Orientation
  40. {
  41. get => _orientationHelper.Orientation;
  42. set => _orientationHelper.Orientation = value;
  43. }
  44. /// <inheritdoc/>
  45. public event EventHandler<CancelEventArgs<Orientation>>? OrientationChanging;
  46. /// <inheritdoc/>
  47. public event EventHandler<EventArgs<Orientation>>? OrientationChanged;
  48. /// <inheritdoc/>
  49. public void OnOrientationChanged (Orientation newOrientation)
  50. {
  51. TextDirection = Orientation == Orientation.Vertical ? TextDirection.TopBottom_LeftRight : TextDirection.LeftRight_TopBottom;
  52. TextAlignment = Alignment.Center;
  53. VerticalTextAlignment = Alignment.Center;
  54. // Reset Position to 0 when changing orientation
  55. X = 0;
  56. Y = 0;
  57. // Reset Size to 1 when changing orientation
  58. if (Orientation == Orientation.Vertical)
  59. {
  60. Width = Dim.Fill ();
  61. Height = 1;
  62. }
  63. else
  64. {
  65. Width = 1;
  66. Height = Dim.Fill ();
  67. }
  68. }
  69. #endregion
  70. /// <inheritdoc/>
  71. protected override bool OnClearingViewport ()
  72. {
  73. FillRect (Viewport, Glyphs.ContinuousMeterSegment);
  74. return true;
  75. }
  76. /// <summary>
  77. /// Gets or sets whether the ScrollSlider will set <see cref="View.Text"/> to show the percentage the slider
  78. /// takes up within the <see cref="View.SuperView"/>'s Viewport.
  79. /// </summary>
  80. public bool ShowPercent { get; set; }
  81. /// <summary>
  82. /// Gets or sets the size of the ScrollSlider. This is a helper that simply gets or sets the Width or Height depending on the
  83. /// <see cref="Orientation"/>. The size will be constrained such that the ScrollSlider will not go outside the Viewport of
  84. /// the <see cref="View.SuperView"/>. The size will never be less than 1.
  85. /// </summary>
  86. /// <remarks>
  87. /// <para>
  88. /// The dimension of the ScrollSlider that is perpendicular to the <see cref="Orientation"/> will be set to <see cref="Dim.Fill()"/>
  89. /// </para>
  90. /// </remarks>
  91. public int Size
  92. {
  93. get
  94. {
  95. if (Orientation == Orientation.Vertical)
  96. {
  97. return Frame.Height;
  98. }
  99. else
  100. {
  101. return Frame.Width;
  102. }
  103. }
  104. set
  105. {
  106. if (Orientation == Orientation.Vertical)
  107. {
  108. Width = Dim.Fill ();
  109. int viewport = Math.Max (1, SuperView?.Viewport.Height ?? 1);
  110. Height = Math.Clamp (value, 1, viewport);
  111. }
  112. else
  113. {
  114. int viewport = Math.Max (1, SuperView?.Viewport.Width ?? 1);
  115. Width = Math.Clamp (value, 1, viewport);
  116. Height = Dim.Fill ();
  117. }
  118. }
  119. }
  120. /// <summary>
  121. /// Gets or sets the position of the ScrollSlider. This is a helper that simply gets or sets the X or Y depending on the
  122. /// <see cref="Orientation"/>. The position will be constrained such that the ScrollSlider will not go outside the Viewport of
  123. /// the <see cref="View.SuperView"/>.
  124. /// </summary>
  125. public int Position
  126. {
  127. get
  128. {
  129. if (Orientation == Orientation.Vertical)
  130. {
  131. return Frame.Y;
  132. }
  133. else
  134. {
  135. return Frame.X;
  136. }
  137. }
  138. set
  139. {
  140. if (Orientation == Orientation.Vertical)
  141. {
  142. int viewport = Math.Max (1, SuperView?.Viewport.Height ?? 1);
  143. Y = Math.Clamp (value, 0, viewport - Frame.Height);
  144. }
  145. else
  146. {
  147. int viewport = Math.Max (1, SuperView?.Viewport.Width ?? 1);
  148. X = Math.Clamp (value, 0, viewport - Frame.Width);
  149. }
  150. }
  151. }
  152. /// <inheritdoc/>
  153. protected override bool OnDrawingText ()
  154. {
  155. if (!ShowPercent)
  156. {
  157. return false;
  158. }
  159. if (Orientation == Orientation.Vertical)
  160. {
  161. Text = $"{(int)Math.Round ((double)Viewport.Height / SuperView!.GetContentSize ().Height * 100)}%";
  162. }
  163. else
  164. {
  165. Text = $"{(int)Math.Round ((double)Viewport.Width / SuperView!.GetContentSize ().Width * 100)}%";
  166. }
  167. return false;
  168. }
  169. /// <inheritdoc/>
  170. public override Attribute GetNormalColor () { return base.GetHotNormalColor (); }
  171. ///// <inheritdoc/>
  172. private int _lastLocation = -1;
  173. /// <inheritdoc/>
  174. protected override bool OnMouseEvent (MouseEventArgs mouseEvent)
  175. {
  176. if (SuperView is null)
  177. {
  178. return false;
  179. }
  180. int location = Orientation == Orientation.Vertical ? mouseEvent.Position.Y : mouseEvent.Position.X;
  181. int offset = _lastLocation > -1 ? location - _lastLocation : 0;
  182. int superViewDimension = Orientation == Orientation.Vertical ? SuperView!.Viewport.Height : SuperView!.Viewport.Width;
  183. if (mouseEvent.IsPressed || mouseEvent.IsReleased)
  184. {
  185. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed) && _lastLocation == -1)
  186. {
  187. if (Application.MouseGrabView != this)
  188. {
  189. Application.GrabMouse (this);
  190. _lastLocation = location;
  191. }
  192. }
  193. else if (mouseEvent.Flags == (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))
  194. {
  195. if (Orientation == Orientation.Vertical)
  196. {
  197. Y = Frame.Y + offset < 0
  198. ? 0
  199. : Frame.Y + offset + Frame.Height > superViewDimension
  200. ? Math.Max (superViewDimension - Frame.Height, 0)
  201. : Frame.Y + offset;
  202. }
  203. else
  204. {
  205. X = Frame.X + offset < 0
  206. ? 0
  207. : Frame.X + offset + Frame.Width > superViewDimension
  208. ? Math.Max (superViewDimension - Frame.Width, 0)
  209. : Frame.X + offset;
  210. }
  211. }
  212. else if (mouseEvent.Flags == MouseFlags.Button1Released)
  213. {
  214. _lastLocation = -1;
  215. if (Application.MouseGrabView == this)
  216. {
  217. Application.UngrabMouse ();
  218. }
  219. }
  220. }
  221. if (mouseEvent.IsWheel)
  222. {
  223. if (mouseEvent.Flags.HasFlag (MouseFlags.WheeledDown) || mouseEvent.Flags.HasFlag (MouseFlags.WheeledRight))
  224. {
  225. offset = 1;
  226. }
  227. else if (mouseEvent.Flags.HasFlag (MouseFlags.WheeledDown) || mouseEvent.Flags.HasFlag (MouseFlags.WheeledLeft))
  228. {
  229. offset = -1;
  230. }
  231. if (Orientation == Orientation.Vertical)
  232. {
  233. Y = Frame.Y + offset < 0
  234. ? 0
  235. : Frame.Y + offset + Frame.Height > superViewDimension
  236. ? Math.Max (superViewDimension - Frame.Height, 0)
  237. : Frame.Y + offset;
  238. }
  239. else
  240. {
  241. X = Frame.X + offset < 0
  242. ? 0
  243. : Frame.X + offset + Frame.Width > superViewDimension
  244. ? Math.Max (superViewDimension - Frame.Width, 0)
  245. : Frame.X + offset;
  246. }
  247. }
  248. return true;
  249. }
  250. /// <inheritdoc />
  251. public bool EnableForDesign ()
  252. {
  253. Orientation = Orientation.Vertical;
  254. Width = 1;
  255. Height = 10;
  256. ShowPercent = true;
  257. return true;
  258. }
  259. }