Scroll.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 ()
  11. {
  12. WantContinuousButtonPressed = true;
  13. CanFocus = false;
  14. Orientation = Orientation.Vertical;
  15. Width = Dim.Auto (DimAutoStyle.Content, 1);
  16. Height = Dim.Auto (DimAutoStyle.Content, 1);
  17. _slider = new (this);
  18. Add (_slider);
  19. Added += Scroll_Added!;
  20. Removed += Scroll_Removed!;
  21. Initialized += Scroll_Initialized!;
  22. MouseEvent += Scroll_MouseEvent!;
  23. }
  24. private readonly ScrollSlider _slider;
  25. private Orientation _orientation;
  26. private int _position;
  27. private int _size;
  28. /// <summary>
  29. /// Gets or sets if the Scroll is oriented vertically or horizontally.
  30. /// </summary>
  31. public Orientation Orientation
  32. {
  33. get => _orientation;
  34. set
  35. {
  36. _orientation = value;
  37. AdjustSlider ();
  38. }
  39. }
  40. /// <summary>
  41. /// Gets or sets the position of the start of the Scroll slider, relative to <see cref="Size"/>.
  42. /// </summary>
  43. public int Position
  44. {
  45. get => _position;
  46. set
  47. {
  48. if (value == _position || value < 0)
  49. {
  50. return;
  51. }
  52. int barSize = Orientation == Orientation.Vertical ? GetContentSize ().Height : GetContentSize ().Width;
  53. if (value + barSize > Size)
  54. {
  55. return;
  56. }
  57. CancelEventArgs<int> args = OnPositionChanging (_position, value);
  58. if (args.Cancel)
  59. {
  60. return;
  61. }
  62. _position = value;
  63. if (!_slider._wasSliderMouse)
  64. {
  65. AdjustSlider ();
  66. }
  67. OnPositionChanged (_position);
  68. }
  69. }
  70. /// <summary>Raised when the <see cref="Position"/> has changed.</summary>
  71. public event EventHandler<EventArgs<int>>? PositionChanged;
  72. /// <summary>
  73. /// Raised when the <see cref="Position"/> is changing. Set <see cref="CancelEventArgs.Cancel"/> to
  74. /// <see langword="true"/> to prevent the position from being changed.
  75. /// </summary>
  76. public event EventHandler<CancelEventArgs<int>>? PositionChanging;
  77. /// <summary>
  78. /// Gets or sets the size of the Scroll. This is the total size of the content that can be scrolled through.
  79. /// </summary>
  80. public int Size
  81. {
  82. get => _size;
  83. set
  84. {
  85. _size = value;
  86. OnSizeChanged (_size);
  87. AdjustSlider ();
  88. }
  89. }
  90. /// <summary>Raised when <see cref="Size"/> has changed.</summary>
  91. public event EventHandler<EventArgs<int>>? SizeChanged;
  92. // TODO: Move this into "ScrollSlider" and override it there. Scroll can then subscribe to _slider.LayoutComplete and call AdjustSlider.
  93. // 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?
  94. // QUESTION: Note I *did* add "View.ViewportChanged" in a previous PR.
  95. /// <inheritdoc/>
  96. protected override void Dispose (bool disposing)
  97. {
  98. Added -= Scroll_Added!;
  99. Initialized -= Scroll_Initialized!;
  100. MouseEvent -= Scroll_MouseEvent!;
  101. base.Dispose (disposing);
  102. }
  103. /// <summary>Virtual method called when <see cref="Position"/> has changed. Raises <see cref="PositionChanged"/>.</summary>
  104. protected virtual void OnPositionChanged (int position) { PositionChanged?.Invoke (this, new (in position)); }
  105. /// <summary>
  106. /// Virtual method called when <see cref="Position"/> is changing. Raises <see cref="PositionChanging"/>, which is
  107. /// cancelable.
  108. /// </summary>
  109. protected virtual CancelEventArgs<int> OnPositionChanging (int currentPos, int newPos)
  110. {
  111. CancelEventArgs<int> args = new (ref currentPos, ref newPos);
  112. PositionChanging?.Invoke (this, args);
  113. return args;
  114. }
  115. /// <summary>Virtual method called when <see cref="Size"/> has changed. Raises <see cref="SizeChanged"/>.</summary>
  116. protected void OnSizeChanged (int size) { SizeChanged?.Invoke (this, new (in size)); }
  117. private void AdjustSlider ()
  118. {
  119. if (!IsInitialized)
  120. {
  121. return;
  122. }
  123. (int Location, int Dimension) slider = GetSliderLocationDimensionFromPosition ();
  124. _slider.X = Orientation == Orientation.Vertical ? 0 : slider.Location;
  125. _slider.Y = Orientation == Orientation.Vertical ? slider.Location : 0;
  126. _slider.SetContentSize (
  127. new (
  128. Orientation == Orientation.Vertical ? GetContentSize ().Width : slider.Dimension,
  129. Orientation == Orientation.Vertical ? slider.Dimension : GetContentSize ().Height
  130. ));
  131. SetSliderText ();
  132. }
  133. // QUESTION: This method is only called from one place. Should it be inlined? Or, should it be made internal and unit tests be provided?
  134. private (int Location, int Dimension) GetSliderLocationDimensionFromPosition ()
  135. {
  136. if (GetContentSize ().Height == 0 || GetContentSize ().Width == 0)
  137. {
  138. return new (0, 0);
  139. }
  140. int scrollSize = Orientation == Orientation.Vertical ? GetContentSize ().Height : GetContentSize ().Width;
  141. int location;
  142. int dimension;
  143. if (Size > 0)
  144. {
  145. dimension = Math.Min (Math.Max (scrollSize * scrollSize / Size, 1), scrollSize);
  146. // Ensure the Position is valid
  147. if (Position > 0 && Position + scrollSize > Size)
  148. {
  149. Position = Size - scrollSize;
  150. }
  151. location = Math.Min ((Position * scrollSize + Position) / Size, scrollSize - dimension);
  152. if (Position == Size - scrollSize && location + dimension < scrollSize)
  153. {
  154. location = scrollSize - dimension;
  155. }
  156. }
  157. else
  158. {
  159. location = 0;
  160. dimension = scrollSize;
  161. }
  162. return new (location, dimension);
  163. }
  164. private void Scroll_Added (object sender, SuperViewChangedEventArgs e)
  165. {
  166. View parent = (e.Parent is Adornment adornment ? adornment.Parent : e.Parent)!;
  167. parent.LayoutComplete += SuperView_LayoutComplete!;
  168. }
  169. private void Scroll_Initialized (object sender, EventArgs e) { AdjustSlider (); }
  170. // TODO: I think you should create a new `internal` view named "ScrollSlider" with an `Orientation` property. It should inherit from View and override GetNormalColor and the mouse events
  171. // that can be moved within it's Superview, constrained to move only horizontally or vertically depending on Orientation.
  172. // This will really simplify a lot of this.
  173. private void Scroll_MouseEvent (object sender, MouseEventEventArgs e)
  174. {
  175. MouseEvent me = e.MouseEvent;
  176. int location = Orientation == Orientation.Vertical ? me.Position.Y : me.Position.X;
  177. int barSize = Orientation == Orientation.Vertical ? GetContentSize ().Height : GetContentSize ().Width;
  178. (int topLeft, int bottomRight) sliderPos = _orientation == Orientation.Vertical
  179. ? new (_slider.Frame.Y, _slider.Frame.Bottom - 1)
  180. : new (_slider.Frame.X, _slider.Frame.Right - 1);
  181. if (me.Flags == MouseFlags.Button1Pressed && location < sliderPos.topLeft)
  182. {
  183. Position = Math.Max (Position - barSize, 0);
  184. }
  185. else if (me.Flags == MouseFlags.Button1Pressed && location > sliderPos.bottomRight)
  186. {
  187. Position = Math.Min (Position + barSize, Size - barSize);
  188. }
  189. else if ((me.Flags == MouseFlags.WheeledDown && Orientation == Orientation.Vertical)
  190. || (me.Flags == MouseFlags.WheeledRight && Orientation == Orientation.Horizontal))
  191. {
  192. Position = Math.Min (Position + 1, Size - barSize);
  193. }
  194. else if ((me.Flags == MouseFlags.WheeledUp && Orientation == Orientation.Vertical)
  195. || (me.Flags == MouseFlags.WheeledLeft && Orientation == Orientation.Horizontal))
  196. {
  197. Position = Math.Max (Position - 1, 0);
  198. }
  199. else if (me.Flags == MouseFlags.Button1Clicked)
  200. {
  201. if (_slider.Frame.Contains (me.Position))
  202. {
  203. _slider.OnMouseEvent (e.MouseEvent);
  204. }
  205. }
  206. }
  207. private void Scroll_Removed (object sender, SuperViewChangedEventArgs e)
  208. {
  209. if (e.Parent is { })
  210. {
  211. View parent = (e.Parent is Adornment adornment ? adornment.Parent : e.Parent)!;
  212. parent.LayoutComplete -= SuperView_LayoutComplete!;
  213. }
  214. }
  215. private void SetSliderText ()
  216. {
  217. TextDirection = Orientation == Orientation.Vertical ? TextDirection.TopBottom_LeftRight : TextDirection.LeftRight_TopBottom;
  218. // QUESTION: Should these Glyphs be configurable via CM?
  219. Text = string.Concat (
  220. Enumerable.Repeat (
  221. Glyphs.Stipple.ToString (),
  222. GetContentSize ().Width * GetContentSize ().Height));
  223. _slider.TextDirection = Orientation == Orientation.Vertical ? TextDirection.TopBottom_LeftRight : TextDirection.LeftRight_TopBottom;
  224. _slider.Text = string.Concat (
  225. Enumerable.Repeat (
  226. Glyphs.ContinuousMeterSegment.ToString (),
  227. _slider.GetContentSize ().Width * _slider.GetContentSize ().Height));
  228. }
  229. // TODO: This is unnecessary. If Scroll.Width/Height is Dim.Auto, the Superview will get resized automatically.
  230. private void SuperView_LayoutComplete (object sender, LayoutEventArgs e)
  231. {
  232. if (!_slider._wasSliderMouse)
  233. {
  234. AdjustSlider ();
  235. }
  236. else
  237. {
  238. _slider._wasSliderMouse = false;
  239. }
  240. }
  241. }