ScrollBar.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #nullable enable
  2. using System.ComponentModel;
  3. namespace Terminal.Gui;
  4. /// <summary>A proportional scroll bar that can be oriented either horizontally or vertically.</summary>
  5. /// <remarks>
  6. /// <para>
  7. /// <see cref="Position"/> indicates the current location between zero and <see cref="Size"/>.
  8. /// </para>
  9. /// <para>If the scrollbar is larger than three cells, arrow indicators are drawn.</para>
  10. /// </remarks>
  11. public class ScrollBar : View
  12. {
  13. /// <inheritdoc/>
  14. public ScrollBar ()
  15. {
  16. _scroll = new ();
  17. _decrease = new ();
  18. _increase = new () { NavigationDirection = NavigationDirection.Forward };
  19. Add (_scroll, _decrease, _increase);
  20. CanFocus = false;
  21. Orientation = Orientation.Vertical;
  22. Width = Dim.Auto (DimAutoStyle.Content, 1);
  23. Height = Dim.Auto (DimAutoStyle.Content, 1);
  24. _scroll.PositionChanging += Scroll_PositionChanging;
  25. _scroll.PositionChanged += Scroll_PositionChanged;
  26. _scroll.SizeChanged += _scroll_SizeChanged;
  27. }
  28. private readonly Scroll _scroll;
  29. private readonly ScrollButton _decrease;
  30. private readonly ScrollButton _increase;
  31. private bool _autoHideScrollBar = true;
  32. private bool _showScrollIndicator = true;
  33. /// <summary>If true the vertical/horizontal scroll bars won't be shown if it's not needed.</summary>
  34. public bool AutoHideScrollBar
  35. {
  36. get => _autoHideScrollBar;
  37. set
  38. {
  39. if (_autoHideScrollBar != value)
  40. {
  41. _autoHideScrollBar = value;
  42. AdjustAll ();
  43. }
  44. }
  45. }
  46. /// <summary>Defines if a scrollbar is vertical or horizontal.</summary>
  47. public Orientation Orientation
  48. {
  49. get => _scroll.Orientation;
  50. set
  51. {
  52. Resize (value);
  53. _scroll.Orientation = value;
  54. }
  55. }
  56. /// <summary>The position, relative to <see cref="Size"/>, to set the scrollbar at.</summary>
  57. /// <value>The position.</value>
  58. public int Position
  59. {
  60. get => _scroll.Position;
  61. set
  62. {
  63. _scroll.Position = value;
  64. AdjustAll ();
  65. }
  66. }
  67. /// <summary>Raised when the <see cref="Position"/> has changed.</summary>
  68. public event EventHandler<EventArgs<int>>? PositionChanged;
  69. /// <summary>
  70. /// Raised when the <see cref="Position"/> is changing. Set <see cref="CancelEventArgs.Cancel"/> to
  71. /// <see langword="true"/> to prevent the position from being changed.
  72. /// </summary>
  73. public event EventHandler<CancelEventArgs<int>>? PositionChanging;
  74. /// <summary>Gets or sets the visibility for the vertical or horizontal scroll indicator.</summary>
  75. /// <value><c>true</c> if show vertical or horizontal scroll indicator; otherwise, <c>false</c>.</value>
  76. public bool ShowScrollIndicator
  77. {
  78. get => Visible;
  79. set
  80. {
  81. if (value == _showScrollIndicator)
  82. {
  83. return;
  84. }
  85. _showScrollIndicator = value;
  86. if (IsInitialized)
  87. {
  88. SetNeedsLayout ();
  89. if (value)
  90. {
  91. Visible = true;
  92. }
  93. else
  94. {
  95. Visible = false;
  96. Position = 0;
  97. }
  98. AdjustAll ();
  99. }
  100. }
  101. }
  102. /// <summary>
  103. /// Gets or sets the size of the Scroll. This is the total size of the content that can be scrolled through.
  104. /// </summary>
  105. public int Size
  106. {
  107. get => _scroll.Size;
  108. set
  109. {
  110. _scroll.Size = value;
  111. AdjustAll ();
  112. }
  113. }
  114. /// <summary>Raised when <see cref="Size"/> has changed.</summary>
  115. public event EventHandler<EventArgs<int>>? SizeChanged;
  116. /// <inheritdoc/>
  117. internal override void OnLayoutComplete (LayoutEventArgs args)
  118. {
  119. base.OnLayoutComplete (args);
  120. AdjustAll ();
  121. }
  122. private void _scroll_SizeChanged (object? sender, EventArgs<int> e) { SizeChanged?.Invoke (this, e); }
  123. private void AdjustAll ()
  124. {
  125. CheckScrollBarVisibility ();
  126. _scroll.AdjustScroll ();
  127. _decrease.AdjustButton ();
  128. _increase.AdjustButton ();
  129. }
  130. private bool CheckScrollBarVisibility ()
  131. {
  132. if (!AutoHideScrollBar)
  133. {
  134. if (Visible != _showScrollIndicator)
  135. {
  136. Visible = _showScrollIndicator;
  137. SetNeedsDisplay ();
  138. }
  139. return _showScrollIndicator;
  140. }
  141. int barSize = Orientation == Orientation.Vertical ? Viewport.Height : Viewport.Width;
  142. if (barSize == 0 || barSize >= Size)
  143. {
  144. if (ShowScrollIndicator)
  145. {
  146. Visible = false;
  147. SetNeedsDisplay ();
  148. return false;
  149. }
  150. }
  151. else
  152. {
  153. if (!Visible)
  154. {
  155. Visible = true;
  156. SetNeedsDisplay ();
  157. }
  158. }
  159. return true;
  160. }
  161. private void Resize (Orientation orientation)
  162. {
  163. switch (orientation)
  164. {
  165. case Orientation.Horizontal:
  166. break;
  167. case Orientation.Vertical:
  168. break;
  169. default:
  170. throw new ArgumentOutOfRangeException (nameof (orientation), orientation, null);
  171. }
  172. }
  173. private void Scroll_PositionChanged (object? sender, EventArgs<int> e) { PositionChanged?.Invoke (this, e); }
  174. private void Scroll_PositionChanging (object? sender, CancelEventArgs<int> e) { PositionChanging?.Invoke (this, e); }
  175. }