Scroll.cs 8.0 KB

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