Scroll.cs 8.8 KB

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