Scroll.cs 7.7 KB

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