ScrollBar.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 _autoHide = true;
  32. private bool _showScrollIndicator = true;
  33. /// <summary>
  34. /// Gets or sets whether <see cref="View.Visible"/> will be set to <see langword="false"/> if the dimension of the
  35. /// scroll bar is greater than or equal to <see cref="Size"/>.
  36. /// </summary>
  37. public bool AutoHide
  38. {
  39. get => _autoHide;
  40. set
  41. {
  42. if (_autoHide != value)
  43. {
  44. _autoHide = value;
  45. AdjustAll ();
  46. }
  47. }
  48. }
  49. /// <summary>Get or sets if the view-port is kept in all visible area of this <see cref="ScrollBar"/></summary>
  50. public bool KeepContentInAllViewport
  51. {
  52. get => _scroll.KeepContentInAllViewport;
  53. set => _scroll.KeepContentInAllViewport = value;
  54. }
  55. /// <summary>Gets or sets if a scrollbar is vertical or horizontal.</summary>
  56. public Orientation Orientation
  57. {
  58. get => _scroll.Orientation;
  59. set
  60. {
  61. Resize (value);
  62. _scroll.Orientation = value;
  63. }
  64. }
  65. /// <summary>Gets or sets the position, relative to <see cref="Size"/>, to set the scrollbar at.</summary>
  66. /// <value>The position.</value>
  67. public int Position
  68. {
  69. get => _scroll.Position;
  70. set
  71. {
  72. _scroll.Position = value;
  73. AdjustAll ();
  74. }
  75. }
  76. /// <summary>Raised when the <see cref="Position"/> has changed.</summary>
  77. public event EventHandler<EventArgs<int>>? PositionChanged;
  78. /// <summary>
  79. /// Raised when the <see cref="Position"/> is changing. Set <see cref="CancelEventArgs.Cancel"/> to
  80. /// <see langword="true"/> to prevent the position from being changed.
  81. /// </summary>
  82. public event EventHandler<CancelEventArgs<int>>? PositionChanging;
  83. /// <summary>Gets or sets the visibility for the vertical or horizontal scroll indicator.</summary>
  84. /// <value><c>true</c> if show vertical or horizontal scroll indicator; otherwise, <c>false</c>.</value>
  85. public bool ShowScrollIndicator
  86. {
  87. get => Visible;
  88. set
  89. {
  90. if (value == _showScrollIndicator)
  91. {
  92. return;
  93. }
  94. _showScrollIndicator = value;
  95. if (IsInitialized)
  96. {
  97. SetNeedsLayout ();
  98. if (value)
  99. {
  100. Visible = true;
  101. }
  102. else
  103. {
  104. Visible = false;
  105. Position = 0;
  106. }
  107. AdjustAll ();
  108. }
  109. }
  110. }
  111. /// <summary>
  112. /// Gets or sets the size of the Scroll. This is the total size of the content that can be scrolled through.
  113. /// </summary>
  114. public int Size
  115. {
  116. get => _scroll.Size;
  117. set
  118. {
  119. _scroll.Size = value;
  120. AdjustAll ();
  121. }
  122. }
  123. /// <summary>Raised when <see cref="Size"/> has changed.</summary>
  124. public event EventHandler<EventArgs<int>>? SizeChanged;
  125. /// <inheritdoc/>
  126. internal override void OnLayoutComplete (LayoutEventArgs args)
  127. {
  128. base.OnLayoutComplete (args);
  129. AdjustAll ();
  130. }
  131. private void _scroll_SizeChanged (object? sender, EventArgs<int> e) { SizeChanged?.Invoke (this, e); }
  132. private void AdjustAll ()
  133. {
  134. CheckVisibility ();
  135. _scroll.AdjustScroll ();
  136. _decrease.AdjustButton ();
  137. _increase.AdjustButton ();
  138. }
  139. private bool CheckVisibility ()
  140. {
  141. if (!AutoHide)
  142. {
  143. if (Visible != _showScrollIndicator)
  144. {
  145. Visible = _showScrollIndicator;
  146. SetNeedsDisplay ();
  147. }
  148. return _showScrollIndicator;
  149. }
  150. int barSize = Orientation == Orientation.Vertical ? Viewport.Height : Viewport.Width;
  151. if (barSize == 0 || barSize >= Size)
  152. {
  153. if (Visible)
  154. {
  155. Visible = false;
  156. SetNeedsDisplay ();
  157. return false;
  158. }
  159. }
  160. else
  161. {
  162. if (!Visible)
  163. {
  164. Visible = true;
  165. SetNeedsDisplay ();
  166. }
  167. }
  168. return true;
  169. }
  170. private void Resize (Orientation orientation)
  171. {
  172. switch (orientation)
  173. {
  174. case Orientation.Horizontal:
  175. break;
  176. case Orientation.Vertical:
  177. break;
  178. default:
  179. throw new ArgumentOutOfRangeException (nameof (orientation), orientation, null);
  180. }
  181. }
  182. private void Scroll_PositionChanged (object? sender, EventArgs<int> e) { PositionChanged?.Invoke (this, e); }
  183. private void Scroll_PositionChanging (object? sender, CancelEventArgs<int> e) { PositionChanging?.Invoke (this, e); }
  184. }