ScrollBar.cs 11 KB

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