2
0

ScrollSlider.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. private bool _showPercent;
  77. /// <summary>
  78. /// Gets or sets whether the ScrollSlider will set <see cref="View.Text"/> to show the percentage the slider
  79. /// takes up within the <see cref="View.SuperView"/>'s Viewport.
  80. /// </summary>
  81. public bool ShowPercent
  82. {
  83. get => _showPercent;
  84. set
  85. {
  86. _showPercent = value;
  87. SetNeedsDraw();
  88. }
  89. }
  90. /// <summary>
  91. /// Gets or sets the size of the ScrollSlider. This is a helper that simply gets or sets the Width or Height depending on the
  92. /// <see cref="Orientation"/>. The size will be constrained such that the ScrollSlider will not go outside the Viewport of
  93. /// the <see cref="View.SuperView"/>. The size will never be less than 1.
  94. /// </summary>
  95. /// <remarks>
  96. /// <para>
  97. /// The dimension of the ScrollSlider that is perpendicular to the <see cref="Orientation"/> will be set to <see cref="Dim.Fill()"/>
  98. /// </para>
  99. /// </remarks>
  100. public int Size
  101. {
  102. get
  103. {
  104. if (Orientation == Orientation.Vertical)
  105. {
  106. return Frame.Height;
  107. }
  108. else
  109. {
  110. return Frame.Width;
  111. }
  112. }
  113. set
  114. {
  115. if (Orientation == Orientation.Vertical)
  116. {
  117. Width = Dim.Fill ();
  118. int viewport = Math.Max (1, SuperView?.Viewport.Height ?? 1);
  119. Height = Math.Clamp (value, 1, viewport);
  120. }
  121. else
  122. {
  123. int viewport = Math.Max (1, SuperView?.Viewport.Width ?? 1);
  124. Width = Math.Clamp (value, 1, viewport);
  125. Height = Dim.Fill ();
  126. }
  127. }
  128. }
  129. /// <summary>
  130. /// Gets or sets the position of the ScrollSlider relative to the size of the ScrollSlider's Frame. This is a helper that simply gets or sets the X or Y depending on the
  131. /// <see cref="Orientation"/>. The position will be constrained such that the ScrollSlider will not go outside the Viewport of
  132. /// the <see cref="View.SuperView"/>.
  133. /// </summary>
  134. public int Position
  135. {
  136. get
  137. {
  138. if (Orientation == Orientation.Vertical)
  139. {
  140. return Frame.Y;
  141. }
  142. else
  143. {
  144. return Frame.X;
  145. }
  146. }
  147. set
  148. {
  149. if (Orientation == Orientation.Vertical)
  150. {
  151. int viewport = Math.Max (1, SuperView?.Viewport.Height ?? 1);
  152. Y = Math.Clamp (value, 0, viewport - Frame.Height);
  153. }
  154. else
  155. {
  156. int viewport = Math.Max (1, SuperView?.Viewport.Width ?? 1);
  157. X = Math.Clamp (value, 0, viewport - Frame.Width);
  158. }
  159. }
  160. }
  161. /// <inheritdoc/>
  162. protected override bool OnDrawingText ()
  163. {
  164. if (!ShowPercent)
  165. {
  166. Text = string.Empty;
  167. return false;
  168. }
  169. if (SuperView is null)
  170. {
  171. return false;
  172. }
  173. if (Orientation == Orientation.Vertical)
  174. {
  175. Text = $"{(int)Math.Round ((double)Viewport.Height / SuperView!.GetContentSize ().Height * 100)}%";
  176. }
  177. else
  178. {
  179. Text = $"{(int)Math.Round ((double)Viewport.Width / SuperView!.GetContentSize ().Width * 100)}%";
  180. }
  181. return false;
  182. }
  183. /// <inheritdoc/>
  184. public override Attribute GetNormalColor () { return base.GetHotNormalColor (); }
  185. ///// <inheritdoc/>
  186. private int _lastLocation = -1;
  187. /// <inheritdoc/>
  188. protected override bool OnMouseEvent (MouseEventArgs mouseEvent)
  189. {
  190. if (SuperView is null)
  191. {
  192. return false;
  193. }
  194. int location = Orientation == Orientation.Vertical ? mouseEvent.Position.Y : mouseEvent.Position.X;
  195. int offset = _lastLocation > -1 ? location - _lastLocation : 0;
  196. int superViewDimension = Orientation == Orientation.Vertical ? SuperView!.Viewport.Height : SuperView!.Viewport.Width;
  197. if (mouseEvent.IsPressed || mouseEvent.IsReleased)
  198. {
  199. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed) && _lastLocation == -1)
  200. {
  201. if (Application.MouseGrabView != this)
  202. {
  203. Application.GrabMouse (this);
  204. _lastLocation = location;
  205. }
  206. }
  207. else if (mouseEvent.Flags == (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))
  208. {
  209. if (Orientation == Orientation.Vertical)
  210. {
  211. Y = Frame.Y + offset < 0
  212. ? 0
  213. : Frame.Y + offset + Frame.Height > superViewDimension
  214. ? Math.Max (superViewDimension - Frame.Height, 0)
  215. : Frame.Y + offset;
  216. }
  217. else
  218. {
  219. X = Frame.X + offset < 0
  220. ? 0
  221. : Frame.X + offset + Frame.Width > superViewDimension
  222. ? Math.Max (superViewDimension - Frame.Width, 0)
  223. : Frame.X + offset;
  224. }
  225. }
  226. else if (mouseEvent.Flags == MouseFlags.Button1Released)
  227. {
  228. _lastLocation = -1;
  229. if (Application.MouseGrabView == this)
  230. {
  231. Application.UngrabMouse ();
  232. }
  233. }
  234. return true;
  235. }
  236. return false;
  237. }
  238. /// <inheritdoc />
  239. public bool EnableForDesign ()
  240. {
  241. Orientation = Orientation.Vertical;
  242. Width = 1;
  243. Height = 10;
  244. ShowPercent = true;
  245. return true;
  246. }
  247. }