ScrollBar.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #nullable enable
  2. using System.ComponentModel;
  3. namespace Terminal.Gui;
  4. /// <summary>
  5. /// Provides a visual indicator that content can be scrolled. ScrollBars consist of two buttons, one each for scrolling
  6. /// forward or backwards, a <see cref="Scroll"/> that can be dragged
  7. /// to scroll continuously. ScrollBars can be oriented either horizontally or vertically and support the user dragging
  8. /// and clicking with the mouse to scroll.
  9. /// </summary>
  10. /// <remarks>
  11. /// <para>
  12. /// <see cref="SliderPosition"/> indicates the number of rows or columns the Scroll has moved from 0.
  13. /// </para>
  14. /// </remarks>
  15. public class ScrollBar : View, IOrientation, IDesignable
  16. {
  17. private readonly Scroll _scroll;
  18. private readonly Button _decreaseButton;
  19. private readonly Button _increaseButton;
  20. /// <inheritdoc/>
  21. public ScrollBar ()
  22. {
  23. CanFocus = false;
  24. _scroll = new ();
  25. _scroll.SliderPositionChanging += OnScrollOnSliderPositionChanging;
  26. _scroll.SliderPositionChanged += OnScrollOnSliderPositionChanged;
  27. _scroll.SizeChanged += OnScrollOnSizeChanged;
  28. _decreaseButton = new ()
  29. {
  30. CanFocus = false,
  31. NoDecorations = true,
  32. NoPadding = true,
  33. ShadowStyle = ShadowStyle.None,
  34. WantContinuousButtonPressed = true
  35. };
  36. _decreaseButton.Accepting += OnDecreaseButtonOnAccept;
  37. _increaseButton = new ()
  38. {
  39. CanFocus = false,
  40. NoDecorations = true,
  41. NoPadding = true,
  42. ShadowStyle = ShadowStyle.None,
  43. WantContinuousButtonPressed = true
  44. };
  45. _increaseButton.Accepting += OnIncreaseButtonOnAccept;
  46. Add (_decreaseButton, _scroll, _increaseButton);
  47. _orientationHelper = new (this); // Do not use object initializer!
  48. _orientationHelper.Orientation = Orientation.Vertical;
  49. _orientationHelper.OrientationChanging += (sender, e) => OrientationChanging?.Invoke (this, e);
  50. _orientationHelper.OrientationChanged += (sender, e) => OrientationChanged?.Invoke (this, e);
  51. // This sets the width/height etc...
  52. OnOrientationChanged (Orientation);
  53. return;
  54. void OnDecreaseButtonOnAccept (object? s, CommandEventArgs e)
  55. {
  56. _scroll.ContentPosition--;
  57. e.Cancel = true;
  58. }
  59. void OnIncreaseButtonOnAccept (object? s, CommandEventArgs e)
  60. {
  61. _scroll.ContentPosition++;
  62. e.Cancel = true;
  63. }
  64. }
  65. #region IOrientation members
  66. private readonly OrientationHelper _orientationHelper;
  67. /// <inheritdoc/>
  68. public Orientation Orientation
  69. {
  70. get => _orientationHelper.Orientation;
  71. set => _orientationHelper.Orientation = value;
  72. }
  73. /// <inheritdoc/>
  74. public event EventHandler<CancelEventArgs<Orientation>>? OrientationChanging;
  75. /// <inheritdoc/>
  76. public event EventHandler<EventArgs<Orientation>>? OrientationChanged;
  77. /// <inheritdoc/>
  78. public void OnOrientationChanged (Orientation newOrientation)
  79. {
  80. TextDirection = Orientation == Orientation.Vertical ? TextDirection.TopBottom_LeftRight : TextDirection.LeftRight_TopBottom;
  81. TextAlignment = Alignment.Center;
  82. VerticalTextAlignment = Alignment.Center;
  83. if (Orientation == Orientation.Vertical)
  84. {
  85. Width = 1;
  86. Height = Dim.Fill ();
  87. }
  88. else
  89. {
  90. Width = Dim.Fill ();
  91. Height = 1;
  92. }
  93. _scroll.Orientation = newOrientation;
  94. }
  95. #endregion
  96. private bool _autoHide = true;
  97. /// <summary>
  98. /// Gets or sets whether <see cref="View.Visible"/> will be set to <see langword="false"/> if the dimension of the
  99. /// scroll bar is greater than or equal to <see cref="Size"/>.
  100. /// </summary>
  101. public bool AutoHide
  102. {
  103. get => _autoHide;
  104. set
  105. {
  106. if (_autoHide != value)
  107. {
  108. _autoHide = value;
  109. if (!AutoHide)
  110. {
  111. Visible = true;
  112. }
  113. SetNeedsLayout ();
  114. }
  115. }
  116. }
  117. /// <inheritdoc/>
  118. protected override void OnFrameChanged (in Rectangle frame) { ShowHide (); }
  119. private void ShowHide ()
  120. {
  121. if (!AutoHide || !IsInitialized)
  122. {
  123. return;
  124. }
  125. if (Orientation == Orientation.Vertical)
  126. {
  127. Visible = Frame.Height - (_decreaseButton.Frame.Height + _increaseButton.Frame.Height) < Size;
  128. }
  129. else
  130. {
  131. Visible = Frame.Width - (_decreaseButton.Frame.Width + _increaseButton.Frame.Width) < Size;
  132. }
  133. }
  134. /// <summary>
  135. /// Gets or sets whether the Scroll will show the percentage the slider
  136. /// takes up within the <see cref="Size"/>.
  137. /// </summary>
  138. public bool ShowPercent
  139. {
  140. get => _scroll.ShowPercent;
  141. set => _scroll.ShowPercent = value;
  142. }
  143. /// <summary>Get or sets if the view-port is kept in all visible area of this <see cref="ScrollBar"/>.</summary>
  144. public bool KeepContentInAllViewport
  145. {
  146. //get => _scroll.KeepContentInAllViewport;
  147. //set => _scroll.KeepContentInAllViewport = value;
  148. get;
  149. set;
  150. }
  151. /// <summary>Gets or sets the position of the slider within the ScrollBar's Viewport.</summary>
  152. /// <value>The position.</value>
  153. public int SliderPosition
  154. {
  155. get => _scroll.SliderPosition;
  156. set => _scroll.SliderPosition = value;
  157. }
  158. private void OnScrollOnSliderPositionChanged (object? sender, EventArgs<int> e) { SliderPositionChanged?.Invoke (this, e); }
  159. private void OnScrollOnSliderPositionChanging (object? sender, CancelEventArgs<int> e) { SliderPositionChanging?.Invoke (this, e); }
  160. /// <summary>
  161. /// Raised when the <see cref="SliderPosition"/> is changing. Set <see cref="CancelEventArgs.Cancel"/> to
  162. /// <see langword="true"/> to prevent the position from being changed.
  163. /// </summary>
  164. public event EventHandler<CancelEventArgs<int>>? SliderPositionChanging;
  165. /// <summary>Raised when the <see cref="SliderPosition"/> has changed.</summary>
  166. public event EventHandler<EventArgs<int>>? SliderPositionChanged;
  167. /// <summary>
  168. /// Gets or sets the size of the Scroll. This is the total size of the content that can be scrolled through.
  169. /// </summary>
  170. public int Size
  171. {
  172. get => _scroll.Size;
  173. set => _scroll.Size = value;
  174. }
  175. /// <summary>
  176. /// Gets or sets the position of the ScrollSlider within the range of 0...<see cref="Size"/>.
  177. /// </summary>
  178. public int ContentPosition
  179. {
  180. get => _scroll.ContentPosition;
  181. set => _scroll.ContentPosition = value;
  182. }
  183. /// <summary>Raised when <see cref="Size"/> has changed.</summary>
  184. public event EventHandler<EventArgs<int>>? SizeChanged;
  185. private void OnScrollOnSizeChanged (object? sender, EventArgs<int> e)
  186. {
  187. ShowHide ();
  188. SizeChanged?.Invoke (this, e);
  189. }
  190. /// <inheritdoc/>
  191. protected override void OnSubviewLayout (LayoutEventArgs args) { PositionSubviews (); }
  192. private void PositionSubviews ()
  193. {
  194. if (Orientation == Orientation.Vertical)
  195. {
  196. _decreaseButton.Y = 0;
  197. _decreaseButton.X = 0;
  198. _decreaseButton.Width = Dim.Fill ();
  199. _decreaseButton.Height = 1;
  200. _decreaseButton.Title = Glyphs.UpArrow.ToString ();
  201. _increaseButton.Y = Pos.Bottom (_scroll);
  202. _increaseButton.X = 0;
  203. _increaseButton.Width = Dim.Fill ();
  204. _increaseButton.Height = 1;
  205. _increaseButton.Title = Glyphs.DownArrow.ToString ();
  206. _scroll.X = 0;
  207. _scroll.Y = Pos.Bottom (_decreaseButton);
  208. _scroll.Height = Dim.Fill (1);
  209. _scroll.Width = Dim.Fill ();
  210. }
  211. else
  212. {
  213. _decreaseButton.Y = 0;
  214. _decreaseButton.X = 0;
  215. _decreaseButton.Width = 1;
  216. _decreaseButton.Height = Dim.Fill ();
  217. _decreaseButton.Title = Glyphs.LeftArrow.ToString ();
  218. _increaseButton.Y = 0;
  219. _increaseButton.X = Pos.Right (_scroll);
  220. _increaseButton.Width = 1;
  221. _increaseButton.Height = Dim.Fill ();
  222. _increaseButton.Title = Glyphs.RightArrow.ToString ();
  223. _scroll.Y = 0;
  224. _scroll.X = Pos.Bottom (_decreaseButton);
  225. _scroll.Width = Dim.Fill (1);
  226. _scroll.Height = Dim.Fill ();
  227. }
  228. }
  229. /// <inheritdoc/>
  230. public bool EnableForDesign ()
  231. {
  232. OrientationChanged += (sender, args) =>
  233. {
  234. if (args.CurrentValue == Orientation.Vertical)
  235. {
  236. Width = 1;
  237. Height = Dim.Fill ();
  238. }
  239. else
  240. {
  241. Width = Dim.Fill ();
  242. Height = 1;
  243. }
  244. };
  245. Width = 1;
  246. Height = Dim.Fill ();
  247. Size = 200;
  248. SliderPosition = 10;
  249. //ShowPercent = true;
  250. return true;
  251. }
  252. }