ScrollBar.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 (this);
  18. _decrease = new (this);
  19. _increase = new (this, 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. public int Size
  61. {
  62. get => _scroll.Size;
  63. set
  64. {
  65. _scroll.Size = value;
  66. AdjustAll ();
  67. }
  68. }
  69. /// <summary>Raised when <see cref="Size"/> has changed.</summary>
  70. public event EventHandler<EventArgs<int>>? SizeChanged;
  71. /// <inheritdoc/>
  72. internal override void OnLayoutComplete (LayoutEventArgs args)
  73. {
  74. base.OnLayoutComplete (args);
  75. AdjustAll ();
  76. }
  77. private void _scroll_SizeChanged (object? sender, EventArgs<int> e) { SizeChanged?.Invoke (this, e); }
  78. private void AdjustAll ()
  79. {
  80. _scroll.AdjustScroll ();
  81. _decrease.AdjustButton ();
  82. _increase.AdjustButton ();
  83. }
  84. private void Resize (Orientation orientation)
  85. {
  86. switch (orientation)
  87. {
  88. case Orientation.Horizontal:
  89. break;
  90. case Orientation.Vertical:
  91. break;
  92. default:
  93. throw new ArgumentOutOfRangeException (nameof (orientation), orientation, null);
  94. }
  95. }
  96. private void Scroll_PositionChanged (object? sender, EventArgs<int> e) { PositionChanged?.Invoke (this, e); }
  97. private void Scroll_PositionChanging (object? sender, CancelEventArgs<int> e) { PositionChanging?.Invoke (this, e); }
  98. }