Scroll.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 bool _wasSliderLayoutComplete = true;
  33. private readonly ScrollSlider _slider;
  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. if (!_slider._wasSliderMouse)
  84. {
  85. AdjustScroll ();
  86. }
  87. OnPositionChanged (_position);
  88. }
  89. }
  90. /// <summary>Raised when the <see cref="Position"/> has changed.</summary>
  91. public event EventHandler<EventArgs<int>>? PositionChanged;
  92. /// <summary>
  93. /// Raised when the <see cref="Position"/> is changing. Set <see cref="CancelEventArgs.Cancel"/> to
  94. /// <see langword="true"/> to prevent the position from being changed.
  95. /// </summary>
  96. public event EventHandler<CancelEventArgs<int>>? PositionChanging;
  97. /// <summary>
  98. /// Gets or sets the size of the Scroll. This is the total size of the content that can be scrolled through.
  99. /// </summary>
  100. public int Size
  101. {
  102. get => _size;
  103. set
  104. {
  105. _size = value;
  106. OnSizeChanged (_size);
  107. AdjustScroll ();
  108. }
  109. }
  110. /// <summary>Raised when <see cref="Size"/> has changed.</summary>
  111. public event EventHandler<EventArgs<int>>? SizeChanged;
  112. /// <inheritdoc/>
  113. protected internal override bool OnMouseEvent (MouseEvent mouseEvent)
  114. {
  115. if (!_wasSliderLayoutComplete)
  116. {
  117. // Do not process if slider layout wasn't yet completed
  118. return base.OnMouseEvent (mouseEvent);
  119. }
  120. int location = Orientation == Orientation.Vertical ? mouseEvent.Position.Y : mouseEvent.Position.X;
  121. int barSize = Orientation == Orientation.Vertical ? GetContentSize ().Height : GetContentSize ().Width;
  122. (int topLeft, int bottomRight) sliderPos = _orientation == Orientation.Vertical
  123. ? new (_slider.Frame.Y, _slider.Frame.Bottom - 1)
  124. : new (_slider.Frame.X, _slider.Frame.Right - 1);
  125. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed) && location < sliderPos.topLeft)
  126. {
  127. Position = Math.Max (Position - barSize, 0);
  128. }
  129. else if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed) && location > sliderPos.bottomRight)
  130. {
  131. Position = Math.Min (Position + barSize, Size - barSize);
  132. }
  133. else if ((mouseEvent.Flags == MouseFlags.WheeledDown && Orientation == Orientation.Vertical)
  134. || (mouseEvent.Flags == MouseFlags.WheeledRight && Orientation == Orientation.Horizontal))
  135. {
  136. Position = Math.Min (Position + 1, Size - barSize);
  137. }
  138. else if ((mouseEvent.Flags == MouseFlags.WheeledUp && Orientation == Orientation.Vertical)
  139. || (mouseEvent.Flags == MouseFlags.WheeledLeft && Orientation == Orientation.Horizontal))
  140. {
  141. Position = Math.Max (Position - 1, 0);
  142. }
  143. else if (mouseEvent.Flags == MouseFlags.Button1Clicked)
  144. {
  145. if (_slider.Frame.Contains (mouseEvent.Position))
  146. {
  147. return _slider.OnMouseEvent (mouseEvent);
  148. }
  149. }
  150. // Flag as false until slider layout is completed
  151. _wasSliderLayoutComplete = false;
  152. return base.OnMouseEvent (mouseEvent);
  153. }
  154. /// <inheritdoc/>
  155. protected internal override bool OnMouseLeave (MouseEvent mouseEvent)
  156. {
  157. // If scroll isn't handling mouse then reset the flag
  158. _wasSliderLayoutComplete = true;
  159. return base.OnMouseLeave (mouseEvent);
  160. }
  161. // TODO: Move this into "ScrollSlider" and override it there. Scroll can then subscribe to _slider.LayoutComplete and call AdjustSlider.
  162. // 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?
  163. // QUESTION: Note I *did* add "View.ViewportChanged" in a previous PR.
  164. /// <summary>Virtual method called when <see cref="Position"/> has changed. Raises <see cref="PositionChanged"/>.</summary>
  165. protected virtual void OnPositionChanged (int position) { PositionChanged?.Invoke (this, new (in position)); }
  166. /// <summary>
  167. /// Virtual method called when <see cref="Position"/> is changing. Raises <see cref="PositionChanging"/>, which is
  168. /// cancelable.
  169. /// </summary>
  170. protected virtual CancelEventArgs<int> OnPositionChanging (int currentPos, int newPos)
  171. {
  172. CancelEventArgs<int> args = new (ref currentPos, ref newPos);
  173. PositionChanging?.Invoke (this, args);
  174. return args;
  175. }
  176. /// <summary>Virtual method called when <see cref="Size"/> has changed. Raises <see cref="SizeChanged"/>.</summary>
  177. protected void OnSizeChanged (int size) { SizeChanged?.Invoke (this, new (in size)); }
  178. internal void AdjustScroll ()
  179. {
  180. if (_host is { })
  181. {
  182. X = Orientation == Orientation.Vertical ? 0 : 1;
  183. Y = Orientation == Orientation.Vertical ? 1 : 0;
  184. Width = Orientation == Orientation.Vertical ? Dim.Fill () : Dim.Fill (1);
  185. Height = Orientation == Orientation.Vertical ? Dim.Fill (1) : Dim.Fill ();
  186. }
  187. _slider.AdjustSlider ();
  188. SetScrollText ();
  189. }
  190. /// <inheritdoc/>
  191. internal override void OnLayoutComplete (LayoutEventArgs args)
  192. {
  193. base.OnLayoutComplete (args);
  194. AdjustScroll ();
  195. }
  196. private void SetScrollText ()
  197. {
  198. TextDirection = Orientation == Orientation.Vertical ? TextDirection.TopBottom_LeftRight : TextDirection.LeftRight_TopBottom;
  199. // QUESTION: Should these Glyphs be configurable via CM?
  200. Text = string.Concat (
  201. Enumerable.Repeat (
  202. Glyphs.Stipple.ToString (),
  203. GetContentSize ().Width * GetContentSize ().Height));
  204. }
  205. }