Scroll.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #nullable enable
  2. using System.ComponentModel;
  3. namespace Terminal.Gui;
  4. /// <summary>
  5. /// Provides a proportional control for scrolling through content. Used within a <see cref="ScrollBar"/>.
  6. /// </summary>
  7. public class Scroll : View
  8. {
  9. /// <inheritdoc/>
  10. public Scroll ()
  11. {
  12. _slider = new ();
  13. Add (_slider);
  14. WantContinuousButtonPressed = true;
  15. CanFocus = false;
  16. Orientation = Orientation.Vertical;
  17. Width = Dim.Auto (DimAutoStyle.Content, 1);
  18. Height = Dim.Auto (DimAutoStyle.Content, 1);
  19. }
  20. internal readonly ScrollSlider _slider;
  21. private Orientation _orientation;
  22. private int _position;
  23. private int _size;
  24. /// <inheritdoc/>
  25. public override void EndInit ()
  26. {
  27. base.EndInit ();
  28. AdjustScroll ();
  29. }
  30. /// <summary>
  31. /// Gets or sets if the Scroll is oriented vertically or horizontally.
  32. /// </summary>
  33. public Orientation Orientation
  34. {
  35. get => _orientation;
  36. set
  37. {
  38. _orientation = value;
  39. AdjustScroll ();
  40. }
  41. }
  42. /// <summary>
  43. /// Gets or sets the position of the start of the Scroll slider, relative to <see cref="Size"/>.
  44. /// </summary>
  45. public int Position
  46. {
  47. get => _position;
  48. set
  49. {
  50. if (value == _position || value < 0)
  51. {
  52. return;
  53. }
  54. if (SupView is { IsInitialized: false })
  55. {
  56. // Ensures a more exactly calculation
  57. SetRelativeLayout (SupView.Frame.Size);
  58. }
  59. int barSize = Orientation == Orientation.Vertical ? GetContentSize ().Height : GetContentSize ().Width;
  60. if (value + barSize > Size)
  61. {
  62. return;
  63. }
  64. CancelEventArgs<int> args = OnPositionChanging (_position, value);
  65. if (args.Cancel)
  66. {
  67. return;
  68. }
  69. _position = value;
  70. AdjustScroll ();
  71. OnPositionChanged (_position);
  72. }
  73. }
  74. /// <summary>Raised when the <see cref="Position"/> has changed.</summary>
  75. public event EventHandler<EventArgs<int>>? PositionChanged;
  76. /// <summary>
  77. /// Raised when the <see cref="Position"/> is changing. Set <see cref="CancelEventArgs.Cancel"/> to
  78. /// <see langword="true"/> to prevent the position from being changed.
  79. /// </summary>
  80. public event EventHandler<CancelEventArgs<int>>? PositionChanging;
  81. /// <summary>
  82. /// Gets or sets the size of the Scroll. This is the total size of the content that can be scrolled through.
  83. /// </summary>
  84. public int Size
  85. {
  86. get => _size;
  87. set
  88. {
  89. _size = value;
  90. OnSizeChanged (_size);
  91. AdjustScroll ();
  92. }
  93. }
  94. /// <summary>Raised when <see cref="Size"/> has changed.</summary>
  95. public event EventHandler<EventArgs<int>>? SizeChanged;
  96. /// <inheritdoc/>
  97. protected internal override bool OnMouseEvent (MouseEvent mouseEvent)
  98. {
  99. int location = Orientation == Orientation.Vertical ? mouseEvent.Position.Y : mouseEvent.Position.X;
  100. int barSize = Orientation == Orientation.Vertical ? GetContentSize ().Height : GetContentSize ().Width;
  101. (int topLeft, int bottomRight) sliderPos = _orientation == Orientation.Vertical
  102. ? new (_slider.Frame.Y, _slider.Frame.Bottom - 1)
  103. : new (_slider.Frame.X, _slider.Frame.Right - 1);
  104. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed) && location < sliderPos.topLeft)
  105. {
  106. Position = Math.Max (Position - barSize, 0);
  107. }
  108. else if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed) && location > sliderPos.bottomRight)
  109. {
  110. Position = Math.Min (Position + barSize, Size - barSize);
  111. }
  112. else if ((mouseEvent.Flags == MouseFlags.WheeledDown && Orientation == Orientation.Vertical)
  113. || (mouseEvent.Flags == MouseFlags.WheeledRight && Orientation == Orientation.Horizontal))
  114. {
  115. Position = Math.Min (Position + 1, Size - barSize);
  116. }
  117. else if ((mouseEvent.Flags == MouseFlags.WheeledUp && Orientation == Orientation.Vertical)
  118. || (mouseEvent.Flags == MouseFlags.WheeledLeft && Orientation == Orientation.Horizontal))
  119. {
  120. Position = Math.Max (Position - 1, 0);
  121. }
  122. else if (mouseEvent.Flags == MouseFlags.Button1Clicked)
  123. {
  124. if (_slider.Frame.Contains (mouseEvent.Position))
  125. {
  126. return _slider.OnMouseEvent (mouseEvent);
  127. }
  128. }
  129. return base.OnMouseEvent (mouseEvent);
  130. }
  131. // TODO: Move this into "ScrollSlider" and override it there. Scroll can then subscribe to _slider.LayoutComplete and call AdjustSlider.
  132. // QUESTION: I've been meaning to add a "View.FrameChanged" event (fired from LayoutComplete only if Frame has changed). Should we do that as part of this PR?
  133. // QUESTION: Note I *did* add "View.ViewportChanged" in a previous PR.
  134. /// <summary>Virtual method called when <see cref="Position"/> has changed. Raises <see cref="PositionChanged"/>.</summary>
  135. protected virtual void OnPositionChanged (int position) { PositionChanged?.Invoke (this, new (in position)); }
  136. /// <summary>
  137. /// Virtual method called when <see cref="Position"/> is changing. Raises <see cref="PositionChanging"/>, which is
  138. /// cancelable.
  139. /// </summary>
  140. protected virtual CancelEventArgs<int> OnPositionChanging (int currentPos, int newPos)
  141. {
  142. CancelEventArgs<int> args = new (ref currentPos, ref newPos);
  143. PositionChanging?.Invoke (this, args);
  144. return args;
  145. }
  146. /// <summary>Called when <see cref="Size"/> has changed. Raises <see cref="SizeChanged"/>.</summary>
  147. protected void OnSizeChanged (int size) { SizeChanged?.Invoke (this, new (in size)); }
  148. internal void AdjustScroll ()
  149. {
  150. if (SupView is { })
  151. {
  152. X = Orientation == Orientation.Vertical ? 0 : 1;
  153. Y = Orientation == Orientation.Vertical ? 1 : 0;
  154. Width = Orientation == Orientation.Vertical ? Dim.Fill () : Dim.Fill (1);
  155. Height = Orientation == Orientation.Vertical ? Dim.Fill (1) : Dim.Fill ();
  156. }
  157. _slider.AdjustSlider ();
  158. SetScrollText ();
  159. }
  160. /// <inheritdoc/>
  161. internal override void OnLayoutComplete (LayoutEventArgs args)
  162. {
  163. base.OnLayoutComplete (args);
  164. AdjustScroll ();
  165. }
  166. internal ScrollBar? SupView => SuperView as ScrollBar;
  167. private void SetScrollText ()
  168. {
  169. TextDirection = Orientation == Orientation.Vertical ? TextDirection.TopBottom_LeftRight : TextDirection.LeftRight_TopBottom;
  170. // QUESTION: Should these Glyphs be configurable via CM?
  171. Text = string.Concat (
  172. Enumerable.Repeat (
  173. Glyphs.Stipple.ToString (),
  174. GetContentSize ().Width * GetContentSize ().Height));
  175. }
  176. }