ScrollBar.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #nullable enable
  2. using System.ComponentModel;
  3. namespace Terminal.Gui;
  4. /// <summary>ScrollBars are views that display a 1-character scrollbar, either horizontal or vertical</summary>
  5. /// <remarks>
  6. /// <para>
  7. /// The scrollbar is drawn to be a representation of the Size, assuming that the scroll position is set at
  8. /// Position.
  9. /// </para>
  10. /// <para>If the region to display the scrollbar is larger than three characters, arrow indicators are drawn.</para>
  11. /// </remarks>
  12. public class ScrollBar : View
  13. {
  14. /// <inheritdoc/>
  15. public ScrollBar ()
  16. {
  17. _scroll = new ();
  18. _decrease = new ();
  19. _increase = new () { VariationMode = VariationMode.Increase };
  20. Add (_scroll, _decrease, _increase);
  21. CanFocus = false;
  22. Orientation = Orientation.Vertical;
  23. Width = Dim.Auto (DimAutoStyle.Content, 1);
  24. Height = Dim.Auto (DimAutoStyle.Content, 1);
  25. _scroll.PositionChanging += Scroll_PositionChanging;
  26. _scroll.PositionChanged += Scroll_PositionChanged;
  27. _scroll.SizeChanged += _scroll_SizeChanged;
  28. }
  29. private readonly Scroll _scroll;
  30. private readonly ScrollButton _decrease;
  31. private readonly ScrollButton _increase;
  32. /// <summary>Defines if a scrollbar is vertical or horizontal.</summary>
  33. public Orientation Orientation
  34. {
  35. get => _scroll.Orientation;
  36. set
  37. {
  38. Resize (value);
  39. _scroll.Orientation = value;
  40. }
  41. }
  42. /// <summary>The position, relative to <see cref="Size"/>, to set the scrollbar at.</summary>
  43. /// <value>The position.</value>
  44. public int Position
  45. {
  46. get => _scroll.Position;
  47. set
  48. {
  49. _scroll.Position = value;
  50. AdjustAll ();
  51. }
  52. }
  53. /// <summary>Raised when the <see cref="Position"/> has changed.</summary>
  54. public event EventHandler<EventArgs<int>>? PositionChanged;
  55. /// <summary>
  56. /// Raised when the <see cref="Position"/> is changing. Set <see cref="CancelEventArgs.Cancel"/> to
  57. /// <see langword="true"/> to prevent the position from being changed.
  58. /// </summary>
  59. public event EventHandler<CancelEventArgs<int>>? PositionChanging;
  60. /// <summary>
  61. /// Gets or sets the size of the Scroll. This is the total size of the content that can be scrolled through.
  62. /// </summary>
  63. public int Size
  64. {
  65. get => _scroll.Size;
  66. set
  67. {
  68. _scroll.Size = value;
  69. AdjustAll ();
  70. }
  71. }
  72. /// <summary>Raised when <see cref="Size"/> has changed.</summary>
  73. public event EventHandler<EventArgs<int>>? SizeChanged;
  74. /// <inheritdoc/>
  75. internal override void OnLayoutComplete (LayoutEventArgs args)
  76. {
  77. base.OnLayoutComplete (args);
  78. AdjustAll ();
  79. }
  80. private void _scroll_SizeChanged (object? sender, EventArgs<int> e) { SizeChanged?.Invoke (this, e); }
  81. private void AdjustAll ()
  82. {
  83. _scroll.AdjustScroll ();
  84. _decrease.AdjustButton ();
  85. _increase.AdjustButton ();
  86. }
  87. private void Resize (Orientation orientation)
  88. {
  89. switch (orientation)
  90. {
  91. case Orientation.Horizontal:
  92. break;
  93. case Orientation.Vertical:
  94. break;
  95. default:
  96. throw new ArgumentOutOfRangeException (nameof (orientation), orientation, null);
  97. }
  98. }
  99. private void Scroll_PositionChanged (object? sender, EventArgs<int> e) { PositionChanged?.Invoke (this, e); }
  100. private void Scroll_PositionChanging (object? sender, CancelEventArgs<int> e) { PositionChanging?.Invoke (this, e); }
  101. }