Scroll.cs 7.4 KB

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