ScrollBar.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. /// <summary>Defines if a scrollbar is vertical or horizontal.</summary>
  32. public Orientation Orientation
  33. {
  34. get => _scroll.Orientation;
  35. set
  36. {
  37. Resize (value);
  38. _scroll.Orientation = value;
  39. }
  40. }
  41. /// <summary>The position, relative to <see cref="Size"/>, to set the scrollbar at.</summary>
  42. /// <value>The position.</value>
  43. public int Position
  44. {
  45. get => _scroll.Position;
  46. set
  47. {
  48. _scroll.Position = value;
  49. AdjustAll ();
  50. }
  51. }
  52. /// <summary>Raised when the <see cref="Position"/> has changed.</summary>
  53. public event EventHandler<EventArgs<int>>? PositionChanged;
  54. /// <summary>
  55. /// Raised when the <see cref="Position"/> is changing. Set <see cref="CancelEventArgs.Cancel"/> to
  56. /// <see langword="true"/> to prevent the position from being changed.
  57. /// </summary>
  58. public event EventHandler<CancelEventArgs<int>>? PositionChanging;
  59. /// <summary>
  60. /// Gets or sets the size of the Scroll. This is the total size of the content that can be scrolled through.
  61. /// </summary>
  62. public int Size
  63. {
  64. get => _scroll.Size;
  65. set
  66. {
  67. _scroll.Size = value;
  68. AdjustAll ();
  69. }
  70. }
  71. /// <summary>Raised when <see cref="Size"/> has changed.</summary>
  72. public event EventHandler<EventArgs<int>>? SizeChanged;
  73. /// <inheritdoc/>
  74. internal override void OnLayoutComplete (LayoutEventArgs args)
  75. {
  76. base.OnLayoutComplete (args);
  77. AdjustAll ();
  78. }
  79. private void _scroll_SizeChanged (object? sender, EventArgs<int> e) { SizeChanged?.Invoke (this, e); }
  80. private void AdjustAll ()
  81. {
  82. _scroll.AdjustScroll ();
  83. _decrease.AdjustButton ();
  84. _increase.AdjustButton ();
  85. }
  86. private void Resize (Orientation orientation)
  87. {
  88. switch (orientation)
  89. {
  90. case Orientation.Horizontal:
  91. break;
  92. case Orientation.Vertical:
  93. break;
  94. default:
  95. throw new ArgumentOutOfRangeException (nameof (orientation), orientation, null);
  96. }
  97. }
  98. private void Scroll_PositionChanged (object? sender, EventArgs<int> e) { PositionChanged?.Invoke (this, e); }
  99. private void Scroll_PositionChanging (object? sender, CancelEventArgs<int> e) { PositionChanging?.Invoke (this, e); }
  100. }