Scroll.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #nullable enable
  2. using System.ComponentModel;
  3. namespace Terminal.Gui;
  4. /// <summary>
  5. /// Indicates the size of scrollable content and provides a visible element, referred to as the "ScrollSlider" that
  6. /// that is sized to
  7. /// show the proportion of the scrollable content to the size of the <see cref="View.Viewport"/>. The ScrollSlider
  8. /// can be dragged with the mouse. A Scroll can be oriented either vertically or horizontally and is used within a
  9. /// <see cref="ScrollBar"/>.
  10. /// </summary>
  11. /// <remarks>
  12. /// <para>
  13. /// By default, this view cannot be focused and does not support keyboard.
  14. /// </para>
  15. /// </remarks>
  16. public class Scroll : View
  17. {
  18. /// <inheritdoc/>
  19. public Scroll ()
  20. {
  21. _slider = new ();
  22. Add (_slider);
  23. WantContinuousButtonPressed = true;
  24. CanFocus = false;
  25. Orientation = Orientation.Vertical;
  26. Width = Dim.Auto (DimAutoStyle.Content, 1);
  27. Height = Dim.Auto (DimAutoStyle.Content, 1);
  28. }
  29. internal readonly ScrollSlider _slider;
  30. private Orientation _orientation;
  31. private int _position;
  32. private int _size;
  33. private bool _keepContentInAllViewport;
  34. /// <inheritdoc/>
  35. public override void EndInit ()
  36. {
  37. base.EndInit ();
  38. AdjustScroll ();
  39. }
  40. /// <summary>Get or sets if the view-port is kept in all visible area of this <see cref="Scroll"/></summary>
  41. public bool KeepContentInAllViewport
  42. {
  43. get => _keepContentInAllViewport;
  44. set
  45. {
  46. if (_keepContentInAllViewport != value)
  47. {
  48. _keepContentInAllViewport = value;
  49. var pos = 0;
  50. if (value
  51. && Orientation == Orientation.Horizontal
  52. && _position + (SuperViewAsScrollBar is { } ? SuperViewAsScrollBar.GetContentSize ().Width : GetContentSize ().Width) > Size)
  53. {
  54. pos = Size - (SuperViewAsScrollBar is { } ? SuperViewAsScrollBar.GetContentSize ().Width : GetContentSize ().Width);
  55. }
  56. if (value
  57. && Orientation == Orientation.Vertical
  58. && _position + (SuperViewAsScrollBar is { } ? SuperViewAsScrollBar.GetContentSize ().Height : GetContentSize ().Height) > Size)
  59. {
  60. pos = _size - (SuperViewAsScrollBar is { } ? SuperViewAsScrollBar.GetContentSize ().Height : GetContentSize ().Height);
  61. }
  62. if (pos != 0)
  63. {
  64. Position = pos;
  65. }
  66. SetNeedsDisplay ();
  67. AdjustScroll ();
  68. }
  69. }
  70. }
  71. /// <summary>
  72. /// Gets or sets if the Scroll is oriented vertically or horizontally.
  73. /// </summary>
  74. public Orientation Orientation
  75. {
  76. get => _orientation;
  77. set
  78. {
  79. _orientation = value;
  80. AdjustScroll ();
  81. }
  82. }
  83. /// <summary>
  84. /// Gets or sets the position of the start of the Scroll slider, relative to <see cref="Size"/>.
  85. /// </summary>
  86. public int Position
  87. {
  88. get => _position;
  89. set
  90. {
  91. if (value == _position || value < 0)
  92. {
  93. return;
  94. }
  95. if (SuperViewAsScrollBar is { IsInitialized: false })
  96. {
  97. // Ensures a more exactly calculation
  98. SetRelativeLayout (SuperViewAsScrollBar.Frame.Size);
  99. }
  100. int pos = SetPosition (value);
  101. if (pos == _position)
  102. {
  103. return;
  104. }
  105. CancelEventArgs<int> args = OnPositionChanging (_position, pos);
  106. if (args.Cancel)
  107. {
  108. return;
  109. }
  110. _position = pos;
  111. AdjustScroll ();
  112. OnPositionChanged (_position);
  113. }
  114. }
  115. /// <summary>Raised when the <see cref="Position"/> has changed.</summary>
  116. public event EventHandler<EventArgs<int>>? PositionChanged;
  117. /// <summary>
  118. /// Raised when the <see cref="Position"/> is changing. Set <see cref="CancelEventArgs.Cancel"/> to
  119. /// <see langword="true"/> to prevent the position from being changed.
  120. /// </summary>
  121. public event EventHandler<CancelEventArgs<int>>? PositionChanging;
  122. /// <summary>
  123. /// Gets or sets the size of the Scroll. This is the total size of the content that can be scrolled through.
  124. /// </summary>
  125. public int Size
  126. {
  127. get => _size;
  128. set
  129. {
  130. if (value == _size || value < 0)
  131. {
  132. return;
  133. }
  134. _size = value;
  135. OnSizeChanged (_size);
  136. AdjustScroll ();
  137. }
  138. }
  139. /// <summary>Raised when <see cref="Size"/> has changed.</summary>
  140. public event EventHandler<EventArgs<int>>? SizeChanged;
  141. /// <inheritdoc/>
  142. protected internal override bool OnMouseEvent (MouseEvent mouseEvent)
  143. {
  144. int location = Orientation == Orientation.Vertical ? mouseEvent.Position.Y : mouseEvent.Position.X;
  145. int barSize = BarSize;
  146. (int start, int end) sliderPos = _orientation == Orientation.Vertical
  147. ? new (_slider.Frame.Y, _slider.Frame.Bottom - 1)
  148. : new (_slider.Frame.X, _slider.Frame.Right - 1);
  149. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed) && location < sliderPos.start)
  150. {
  151. int distance = sliderPos.start - location;
  152. int scrollAmount = (int)((double)distance / barSize * (Size - barSize));
  153. Position = Math.Max (Position - scrollAmount, 0);
  154. }
  155. else if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed) && location > sliderPos.end)
  156. {
  157. Position = Math.Min (Position + barSize, Size - barSize + (KeepContentInAllViewport ? 0 : barSize));
  158. }
  159. else if ((mouseEvent.Flags == MouseFlags.WheeledDown && Orientation == Orientation.Vertical)
  160. || (mouseEvent.Flags == MouseFlags.WheeledRight && Orientation == Orientation.Horizontal))
  161. {
  162. Position = Math.Min (Position + 1, Size - barSize + (KeepContentInAllViewport ? 0 : barSize));
  163. }
  164. else if ((mouseEvent.Flags == MouseFlags.WheeledUp && Orientation == Orientation.Vertical)
  165. || (mouseEvent.Flags == MouseFlags.WheeledLeft && Orientation == Orientation.Horizontal))
  166. {
  167. Position = Math.Max (Position - 1, 0);
  168. }
  169. else if (mouseEvent.Flags == MouseFlags.Button1Clicked)
  170. {
  171. if (_slider.Frame.Contains (mouseEvent.Position))
  172. {
  173. return _slider.OnMouseEvent (mouseEvent);
  174. }
  175. }
  176. return base.OnMouseEvent (mouseEvent);
  177. }
  178. /// <summary>Virtual method called when <see cref="Position"/> has changed. Raises <see cref="PositionChanged"/>.</summary>
  179. protected virtual void OnPositionChanged (int position) { PositionChanged?.Invoke (this, new (in position)); }
  180. /// <summary>
  181. /// Virtual method called when <see cref="Position"/> is changing. Raises <see cref="PositionChanging"/>, which is
  182. /// cancelable.
  183. /// </summary>
  184. protected virtual CancelEventArgs<int> OnPositionChanging (int currentPos, int newPos)
  185. {
  186. CancelEventArgs<int> args = new (ref currentPos, ref newPos);
  187. PositionChanging?.Invoke (this, args);
  188. return args;
  189. }
  190. /// <summary>Called when <see cref="Size"/> has changed. Raises <see cref="SizeChanged"/>.</summary>
  191. protected void OnSizeChanged (int size) { SizeChanged?.Invoke (this, new (in size)); }
  192. internal void AdjustScroll ()
  193. {
  194. if (SuperViewAsScrollBar is { })
  195. {
  196. X = Orientation == Orientation.Vertical ? 0 : 1;
  197. Y = Orientation == Orientation.Vertical ? 1 : 0;
  198. Width = Orientation == Orientation.Vertical ? Dim.Fill () : Dim.Fill (1);
  199. Height = Orientation == Orientation.Vertical ? Dim.Fill (1) : Dim.Fill ();
  200. }
  201. _slider.AdjustSlider ();
  202. SetScrollText ();
  203. }
  204. /// <inheritdoc/>
  205. internal override void OnLayoutComplete (LayoutEventArgs args)
  206. {
  207. base.OnLayoutComplete (args);
  208. AdjustScroll ();
  209. }
  210. internal ScrollBar? SuperViewAsScrollBar => SuperView as ScrollBar;
  211. private int BarSize => Orientation == Orientation.Vertical ? Viewport.Height : Viewport.Width;
  212. private int SetPosition (int position)
  213. {
  214. int barSize = BarSize;
  215. if (position + barSize > Size + (KeepContentInAllViewport ? 0 : barSize) - (SuperViewAsScrollBar is { } ? 2 : 0))
  216. {
  217. return KeepContentInAllViewport ? Math.Max (Size - barSize - (SuperViewAsScrollBar is { } ? 2 : 0), 0) : Math.Max (Size - 1, 0);
  218. }
  219. return position;
  220. }
  221. private void SetScrollText ()
  222. {
  223. TextDirection = Orientation == Orientation.Vertical ? TextDirection.TopBottom_LeftRight : TextDirection.LeftRight_TopBottom;
  224. // QUESTION: Should these Glyphs be configurable via CM?
  225. Text = string.Concat (
  226. Enumerable.Repeat (
  227. Glyphs.Stipple.ToString (),
  228. GetContentSize ().Width * GetContentSize ().Height));
  229. }
  230. }