ScrollBar.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. /// </remarks>
  12. public class ScrollBar : View, IOrientation, IDesignable
  13. {
  14. private readonly Scroll _scroll;
  15. private readonly Button _decreaseButton;
  16. private readonly Button _increaseButton;
  17. /// <inheritdoc/>
  18. public ScrollBar ()
  19. {
  20. CanFocus = false;
  21. _scroll = new ();
  22. _scroll.SliderPositionChanged += OnScrollOnSliderPositionChanged;
  23. _scroll.ContentPositionChanging += OnScrollOnContentPositionChanging;
  24. _scroll.ContentPositionChanged += OnScrollOnContentPositionChanged;
  25. _scroll.SizeChanged += OnScrollOnSizeChanged;
  26. _decreaseButton = new ()
  27. {
  28. CanFocus = false,
  29. NoDecorations = true,
  30. NoPadding = true,
  31. ShadowStyle = ShadowStyle.None,
  32. WantContinuousButtonPressed = true
  33. };
  34. _decreaseButton.Accepting += OnDecreaseButtonOnAccept;
  35. _increaseButton = new ()
  36. {
  37. CanFocus = false,
  38. NoDecorations = true,
  39. NoPadding = true,
  40. ShadowStyle = ShadowStyle.None,
  41. WantContinuousButtonPressed = true
  42. };
  43. _increaseButton.Accepting += OnIncreaseButtonOnAccept;
  44. Add (_decreaseButton, _scroll, _increaseButton);
  45. _orientationHelper = new (this); // Do not use object initializer!
  46. _orientationHelper.Orientation = Orientation.Vertical;
  47. _orientationHelper.OrientationChanging += (sender, e) => OrientationChanging?.Invoke (this, e);
  48. _orientationHelper.OrientationChanged += (sender, e) => OrientationChanged?.Invoke (this, e);
  49. // This sets the width/height etc...
  50. OnOrientationChanged (Orientation);
  51. return;
  52. void OnDecreaseButtonOnAccept (object? s, CommandEventArgs e)
  53. {
  54. ContentPosition -= Increment;
  55. e.Cancel = true;
  56. }
  57. void OnIncreaseButtonOnAccept (object? s, CommandEventArgs e)
  58. {
  59. ContentPosition += Increment;
  60. e.Cancel = true;
  61. }
  62. }
  63. #region IOrientation members
  64. private readonly OrientationHelper _orientationHelper;
  65. /// <inheritdoc/>
  66. public Orientation Orientation
  67. {
  68. get => _orientationHelper.Orientation;
  69. set => _orientationHelper.Orientation = value;
  70. }
  71. /// <inheritdoc/>
  72. public event EventHandler<CancelEventArgs<Orientation>>? OrientationChanging;
  73. /// <inheritdoc/>
  74. public event EventHandler<EventArgs<Orientation>>? OrientationChanged;
  75. /// <inheritdoc/>
  76. public void OnOrientationChanged (Orientation newOrientation)
  77. {
  78. TextDirection = Orientation == Orientation.Vertical ? TextDirection.TopBottom_LeftRight : TextDirection.LeftRight_TopBottom;
  79. TextAlignment = Alignment.Center;
  80. VerticalTextAlignment = Alignment.Center;
  81. if (Orientation == Orientation.Vertical)
  82. {
  83. Width = 1;
  84. Height = Dim.Fill ();
  85. }
  86. else
  87. {
  88. Width = Dim.Fill ();
  89. Height = 1;
  90. }
  91. // Force a layout to ensure _scroll
  92. Layout ();
  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 < Size;
  128. }
  129. else
  130. {
  131. Visible = 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. /// <returns>The position.</returns>
  153. public int GetSliderPosition () => _scroll.GetSliderPosition ();
  154. private void OnScrollOnSliderPositionChanged (object? sender, EventArgs<int> e) { SliderPositionChanged?.Invoke (this, e); }
  155. /// <summary>Raised when the position of the slider has changed.</summary>
  156. public event EventHandler<EventArgs<int>>? SliderPositionChanged;
  157. /// <summary>
  158. /// Gets or sets the size of the Scroll. This is the total size of the content that can be scrolled through.
  159. /// </summary>
  160. public int Size
  161. {
  162. get => _scroll.Size;
  163. set => _scroll.Size = value;
  164. }
  165. /// <summary>
  166. /// Gets or sets the size of the viewport into the content being scrolled, bounded by <see cref="Size"/>.
  167. /// </summary>
  168. /// <remarks>
  169. /// If not explicitly set, will be the appropriate dimension of the Scroll's Frame.
  170. /// </remarks>
  171. public int ViewportDimension
  172. {
  173. get => _scroll.ViewportDimension;
  174. set => _scroll.ViewportDimension = value;
  175. }
  176. /// <summary>
  177. /// Gets or sets the position of the ScrollSlider within the range of 0...<see cref="Size"/>.
  178. /// </summary>
  179. public int ContentPosition
  180. {
  181. get => _scroll.ContentPosition;
  182. set => _scroll.ContentPosition = value;
  183. }
  184. private void OnScrollOnContentPositionChanging (object? sender, CancelEventArgs<int> e) { ContentPositionChanging?.Invoke (this, e); }
  185. private void OnScrollOnContentPositionChanged (object? sender, EventArgs<int> e) { ContentPositionChanged?.Invoke (this, e); }
  186. /// <summary>
  187. /// Raised when the <see cref="ContentPosition"/> is changing. Set <see cref="CancelEventArgs.Cancel"/> to
  188. /// <see langword="true"/> to prevent the position from being changed.
  189. /// </summary>
  190. public event EventHandler<CancelEventArgs<int>>? ContentPositionChanging;
  191. /// <summary>Raised when the <see cref="ContentPosition"/> has changed.</summary>
  192. public event EventHandler<EventArgs<int>>? ContentPositionChanged;
  193. /// <summary>Raised when <see cref="Size"/> has changed.</summary>
  194. public event EventHandler<EventArgs<int>>? SizeChanged;
  195. private void OnScrollOnSizeChanged (object? sender, EventArgs<int> e)
  196. {
  197. ShowHide ();
  198. SizeChanged?.Invoke (this, e);
  199. }
  200. /// <summary>
  201. /// Gets or sets the amount each click of the increment/decrement buttons and each
  202. /// mouse wheel event will incremenet/decrement the <see cref="ContentPosition"/>.
  203. /// </summary>
  204. /// <remarks>
  205. /// The default is 1.
  206. /// </remarks>
  207. public int Increment { get => _scroll.Increment; set => _scroll.Increment = value; }
  208. /// <inheritdoc/>
  209. protected override void OnSubviewLayout (LayoutEventArgs args) { PositionSubviews (); }
  210. private void PositionSubviews ()
  211. {
  212. if (Orientation == Orientation.Vertical)
  213. {
  214. _decreaseButton.Y = 0;
  215. _decreaseButton.X = 0;
  216. _decreaseButton.Width = Dim.Fill ();
  217. _decreaseButton.Height = 1;
  218. _decreaseButton.Title = Glyphs.UpArrow.ToString ();
  219. _increaseButton.Y = Pos.Bottom (_scroll);
  220. _increaseButton.X = 0;
  221. _increaseButton.Width = Dim.Fill ();
  222. _increaseButton.Height = 1;
  223. _increaseButton.Title = Glyphs.DownArrow.ToString ();
  224. _scroll.X = 0;
  225. _scroll.Y = Pos.Bottom (_decreaseButton);
  226. _scroll.Height = Dim.Fill (1);
  227. _scroll.Width = Dim.Fill ();
  228. }
  229. else
  230. {
  231. _decreaseButton.Y = 0;
  232. _decreaseButton.X = 0;
  233. _decreaseButton.Width = 1;
  234. _decreaseButton.Height = Dim.Fill ();
  235. _decreaseButton.Title = Glyphs.LeftArrow.ToString ();
  236. _increaseButton.Y = 0;
  237. _increaseButton.X = Pos.Right (_scroll);
  238. _increaseButton.Width = 1;
  239. _increaseButton.Height = Dim.Fill ();
  240. _increaseButton.Title = Glyphs.RightArrow.ToString ();
  241. _scroll.Y = 0;
  242. _scroll.X = Pos.Right (_decreaseButton);
  243. _scroll.Width = Dim.Fill (1);
  244. _scroll.Height = Dim.Fill ();
  245. }
  246. }
  247. /// <inheritdoc/>
  248. public bool EnableForDesign ()
  249. {
  250. OrientationChanged += (sender, args) =>
  251. {
  252. if (args.CurrentValue == Orientation.Vertical)
  253. {
  254. Width = 1;
  255. Height = Dim.Fill ();
  256. }
  257. else
  258. {
  259. Width = Dim.Fill ();
  260. Height = 1;
  261. }
  262. };
  263. Width = 1;
  264. Height = Dim.Fill ();
  265. Size = 200;
  266. //ShowPercent = true;
  267. return true;
  268. }
  269. }