Scroll.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. namespace Terminal.Gui;
  2. /// <summary>
  3. /// Provides a proportional control for scrolling through content. Used within a <see cref="ScrollBar"/>.
  4. /// </summary>
  5. public class Scroll : View
  6. {
  7. /// <inheritdoc/>
  8. public Scroll ()
  9. {
  10. WantContinuousButtonPressed = true;
  11. CanFocus = false;
  12. Orientation = Orientation.Vertical;
  13. Width = Dim.Auto (DimAutoStyle.Content, 1);
  14. Height = Dim.Auto (DimAutoStyle.Content, 1);
  15. _slider = new ()
  16. {
  17. Id = "slider",
  18. Width = Dim.Auto (DimAutoStyle.Content),
  19. Height = Dim.Auto (DimAutoStyle.Content)
  20. };
  21. Add (_slider);
  22. Added += Scroll_Added;
  23. Removed += Scroll_Removed;
  24. Initialized += Scroll_Initialized;
  25. DrawContent += Scroll_DrawContent;
  26. MouseEvent += Scroll_MouseEvent;
  27. _slider.DrawContent += Scroll_DrawContent;
  28. _slider.MouseEvent += Slider_MouseEvent;
  29. }
  30. private readonly View _slider;
  31. private int _lastLocation = -1;
  32. private bool _wasSliderMouse;
  33. private Orientation _orientation;
  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. AdjustSlider();
  44. }
  45. }
  46. private int _position;
  47. /// <summary>
  48. /// Gets or sets the position of the start of the Scroll slider, relative to <see cref="Size"/>.
  49. /// </summary>
  50. public int Position
  51. {
  52. get => _position;
  53. set
  54. {
  55. int barSize = Orientation == Orientation.Vertical ? GetContentSize ().Height : GetContentSize ().Width;
  56. if (value < 0 || (value > 0 && value + barSize > Size))
  57. {
  58. return;
  59. }
  60. StateEventArgs<int> args = OnPositionChanging (_position, value);
  61. if (args.Cancel)
  62. {
  63. return;
  64. }
  65. if (!_wasSliderMouse)
  66. {
  67. AdjustSlider ();
  68. }
  69. int oldPos = _position;
  70. _position = value;
  71. OnPositionChanged (oldPos);
  72. }
  73. }
  74. /// <summary>Raised when the <see cref="Position"/> has changed.</summary>
  75. public event EventHandler<StateEventArgs<int>> PositionChanged;
  76. /// <summary>Raised when the <see cref="Position"/> is changing. Set <see cref="StateEventArgs{T}.Cancel"/> to <see langword="true"/> to prevent the position from being changed.</summary>
  77. public event EventHandler<StateEventArgs<int>> PositionChanging;
  78. /// <summary>Virtual method called when <see cref="Position"/> has changed. Fires <see cref="PositionChanged"/>.</summary>
  79. protected virtual void OnPositionChanged (int oldPos) { PositionChanged?.Invoke (this, new (oldPos, Position)); }
  80. /// <summary>Virtual method called when <see cref="Position"/> is changing. Fires <see cref="PositionChanging"/>, which is cancelable.</summary>
  81. protected virtual StateEventArgs<int> OnPositionChanging (int oldPos, int newPos)
  82. {
  83. StateEventArgs<int> args = new (oldPos, newPos);
  84. PositionChanging?.Invoke (this, args);
  85. return args;
  86. }
  87. private int _size;
  88. /// <summary>
  89. /// Gets or sets the size of the Scroll. This is the total size of the content that can be scrolled through.
  90. /// </summary>
  91. public int Size
  92. {
  93. get => _size;
  94. set
  95. {
  96. int oldSize = _size;
  97. _size = value;
  98. OnSizeChanged (oldSize);
  99. AdjustSlider ();
  100. }
  101. }
  102. /// <summary>Raised when <see cref="Size"/> has changed.</summary>
  103. public event EventHandler<StateEventArgs<int>> SizeChanged;
  104. /// <summary>Virtual method called when <see cref="Size"/> has changed. Fires <see cref="SizeChanged"/>.</summary>
  105. protected void OnSizeChanged (int oldSize) { SizeChanged?.Invoke (this, new (oldSize, Size)); }
  106. private int GetPositionFromSliderLocation (int location)
  107. {
  108. if (GetContentSize ().Height == 0 || GetContentSize ().Width == 0)
  109. {
  110. return 0;
  111. }
  112. int scrollSize = Orientation == Orientation.Vertical ? GetContentSize ().Height : GetContentSize ().Width;
  113. // Ensure the Position is valid if the slider is at end
  114. // We use Frame here instead of ContentSize because even if the slider has a margin or border, Frame indicates the actual size
  115. if ((Orientation == Orientation.Vertical && location + _slider.Frame.Height == scrollSize)
  116. || (Orientation == Orientation.Horizontal && location + _slider.Frame.Width == scrollSize))
  117. {
  118. return Size - scrollSize;
  119. }
  120. return Math.Min (location * Size / scrollSize, Size - scrollSize);
  121. }
  122. // QUESTION: This method is only called from one place. Should it be inlined? Or, should it be made internal and unit tests be provided?
  123. private (int Location, int Dimension) GetSliderLocationDimensionFromPosition ()
  124. {
  125. if (GetContentSize ().Height == 0 || GetContentSize ().Width == 0)
  126. {
  127. return new (0, 0);
  128. }
  129. int scrollSize = Orientation == Orientation.Vertical ? GetContentSize ().Height : GetContentSize ().Width;
  130. int location;
  131. int dimension;
  132. if (Size > 0)
  133. {
  134. dimension = Math.Min (Math.Max (scrollSize * scrollSize / Size, 1), scrollSize);
  135. // Ensure the Position is valid
  136. if (Position > 0 && Position + scrollSize > Size)
  137. {
  138. Position = Size - scrollSize;
  139. }
  140. location = Math.Min (Position * scrollSize / Size, scrollSize - dimension);
  141. if (Position == Size - scrollSize && location + dimension < scrollSize)
  142. {
  143. location = scrollSize - dimension;
  144. }
  145. }
  146. else
  147. {
  148. location = 0;
  149. dimension = scrollSize;
  150. }
  151. return new (location, dimension);
  152. }
  153. // TODO: This is unnecessary. If Scroll.Width/Height is Dim.Auto, the Superview will get resized automatically.
  154. private void SuperView_LayoutComplete (object sender, LayoutEventArgs e)
  155. {
  156. if (!_wasSliderMouse)
  157. {
  158. AdjustSlider ();
  159. }
  160. else
  161. {
  162. _wasSliderMouse = false;
  163. }
  164. }
  165. private void SuperView_MouseEnter (object sender, MouseEventEventArgs e) { OnMouseEnter (e.MouseEvent); }
  166. private void SuperView_MouseLeave (object sender, MouseEventEventArgs e) { OnMouseLeave (e.MouseEvent); }
  167. private void Scroll_Added (object sender, SuperViewChangedEventArgs e)
  168. {
  169. View parent = e.SuperView is Adornment adornment ? adornment.Parent : e.SuperView;
  170. parent.LayoutComplete += SuperView_LayoutComplete;
  171. // QUESTION: I really don't like this. It feels like a hack that a subview needs to track its parent's mouse events.
  172. // QUESTION: Can we figure out a way to do this without tracking the parent's mouse events?
  173. parent.MouseEnter += SuperView_MouseEnter;
  174. parent.MouseLeave += SuperView_MouseLeave;
  175. }
  176. // TODO: Just override GetNormalColor instead of having this method (make Slider a View sub-class that overrides GetNormalColor)
  177. private void Scroll_DrawContent (object sender, DrawEventArgs e) { SetColorSchemeWithSuperview (sender as View); }
  178. private void Scroll_Initialized (object sender, EventArgs e)
  179. {
  180. AdjustSlider ();
  181. }
  182. // 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
  183. // that can be moved within it's Superview, constrained to move only horizontally or vertically depending on Orientation.
  184. // This will really simplify a lot of this.
  185. private void Scroll_MouseEvent (object sender, MouseEventEventArgs e)
  186. {
  187. MouseEvent me = e.MouseEvent;
  188. int location = Orientation == Orientation.Vertical ? me.Position.Y : me.Position.X;
  189. int barSize = Orientation == Orientation.Vertical ? GetContentSize ().Height : GetContentSize ().Width;
  190. (int topLeft, int bottomRight) sliderPos = _orientation == Orientation.Vertical
  191. ? new (_slider.Frame.Y, _slider.Frame.Bottom - 1)
  192. : new (_slider.Frame.X, _slider.Frame.Right - 1);
  193. if (me.Flags == MouseFlags.Button1Pressed && location < sliderPos.topLeft)
  194. {
  195. Position = Math.Max (Position - barSize, 0);
  196. }
  197. else if (me.Flags == MouseFlags.Button1Pressed && location > sliderPos.bottomRight)
  198. {
  199. Position = Math.Min (Position + barSize, Size - barSize);
  200. }
  201. else if ((me.Flags == MouseFlags.WheeledDown && Orientation == Orientation.Vertical)
  202. || (me.Flags == MouseFlags.WheeledRight && Orientation == Orientation.Horizontal))
  203. {
  204. Position = Math.Min (Position + 1, Size - barSize);
  205. }
  206. else if ((me.Flags == MouseFlags.WheeledUp && Orientation == Orientation.Vertical)
  207. || (me.Flags == MouseFlags.WheeledLeft && Orientation == Orientation.Horizontal))
  208. {
  209. Position = Math.Max (Position - 1, 0);
  210. }
  211. }
  212. private void Scroll_Removed (object sender, SuperViewChangedEventArgs e)
  213. {
  214. if (e.SuperView is { })
  215. {
  216. View parent = e.SuperView is Adornment adornment ? adornment.Parent : e.SuperView;
  217. parent.LayoutComplete -= SuperView_LayoutComplete;
  218. parent.MouseEnter -= SuperView_MouseEnter;
  219. parent.MouseLeave -= SuperView_MouseLeave;
  220. }
  221. }
  222. // TODO: Just override GetNormalColor instead of having this method
  223. private static void SetColorSchemeWithSuperview (View view)
  224. {
  225. if (view.SuperView is { })
  226. {
  227. View parent = view.SuperView is Adornment adornment ? adornment.Parent : view.SuperView;
  228. if (view.Id == "slider")
  229. {
  230. view.ColorScheme = new () { Normal = new (parent.ColorScheme.Normal.Foreground, parent.ColorScheme.Normal.Foreground) };
  231. }
  232. else
  233. {
  234. view.ColorScheme = parent.ColorScheme;
  235. }
  236. }
  237. }
  238. private void SetSliderText ()
  239. {
  240. TextDirection = Orientation == Orientation.Vertical ? TextDirection.TopBottom_LeftRight : TextDirection.LeftRight_TopBottom;
  241. // QUESTION: Should these Glyphs be configurable via CM?
  242. Text = string.Concat (
  243. Enumerable.Repeat (
  244. Glyphs.Stipple.ToString (),
  245. GetContentSize ().Width * GetContentSize ().Height));
  246. _slider.TextDirection = Orientation == Orientation.Vertical ? TextDirection.TopBottom_LeftRight : TextDirection.LeftRight_TopBottom;
  247. _slider.Text = string.Concat (
  248. Enumerable.Repeat (
  249. Glyphs.ContinuousMeterSegment.ToString (),
  250. _slider.GetContentSize ().Width * _slider.GetContentSize ().Height));
  251. }
  252. private void AdjustSlider ()
  253. {
  254. if (!IsInitialized)
  255. {
  256. return;
  257. }
  258. (int Location, int Dimension) slider = GetSliderLocationDimensionFromPosition ();
  259. _slider.X = Orientation == Orientation.Vertical ? 0 : slider.Location;
  260. _slider.Y = Orientation == Orientation.Vertical ? slider.Location : 0;
  261. _slider.SetContentSize (
  262. new (
  263. Orientation == Orientation.Vertical ? GetContentSize ().Width : slider.Dimension,
  264. Orientation == Orientation.Vertical ? slider.Dimension : GetContentSize ().Height
  265. ));
  266. SetSliderText ();
  267. }
  268. // TODO: Move this into "ScrollSlider" and override it there. Scroll can then subscribe to _slider.LayoutComplete and call AdjustSlider.
  269. // 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?
  270. // QUESTION: Note I *did* add "View.ViewportChanged" in a previous PR.
  271. private void Slider_MouseEvent (object sender, MouseEventEventArgs e)
  272. {
  273. MouseEvent me = e.MouseEvent;
  274. int location = Orientation == Orientation.Vertical ? me.Position.Y : me.Position.X;
  275. int barSize = Orientation == Orientation.Vertical ? GetContentSize ().Height : GetContentSize ().Width;
  276. int offset = _lastLocation > -1 ? location - _lastLocation : 0;
  277. if (me.Flags == MouseFlags.Button1Pressed)
  278. {
  279. if (Application.MouseGrabView != sender as View)
  280. {
  281. Application.GrabMouse (sender as View);
  282. _lastLocation = location;
  283. }
  284. }
  285. else if (me.Flags == (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))
  286. {
  287. if (Orientation == Orientation.Vertical)
  288. {
  289. if (_slider.Frame.Y + offset >= 0 && _slider.Frame.Y + offset + _slider.Frame.Height <= barSize)
  290. {
  291. _wasSliderMouse = true;
  292. _slider.Y = _slider.Frame.Y + offset;
  293. Position = GetPositionFromSliderLocation (_slider.Frame.Y);
  294. }
  295. }
  296. else
  297. {
  298. if (_slider.Frame.X + offset >= 0 && _slider.Frame.X + offset + _slider.Frame.Width <= barSize)
  299. {
  300. _wasSliderMouse = true;
  301. _slider.X = _slider.Frame.X + offset;
  302. Position = GetPositionFromSliderLocation (_slider.Frame.X);
  303. }
  304. }
  305. }
  306. else if (me.Flags == MouseFlags.Button1Released)
  307. {
  308. _lastLocation = -1;
  309. if (Application.MouseGrabView == sender as View)
  310. {
  311. Application.UngrabMouse ();
  312. }
  313. }
  314. else if ((me.Flags == MouseFlags.WheeledDown && Orientation == Orientation.Vertical)
  315. || (me.Flags == MouseFlags.WheeledRight && Orientation == Orientation.Horizontal))
  316. {
  317. Position = Math.Min (Position + 1, Size - barSize);
  318. }
  319. else if ((me.Flags == MouseFlags.WheeledUp && Orientation == Orientation.Vertical)
  320. || (me.Flags == MouseFlags.WheeledLeft && Orientation == Orientation.Horizontal))
  321. {
  322. Position = Math.Max (Position - 1, 0);
  323. }
  324. else
  325. {
  326. return;
  327. }
  328. e.Handled = true;
  329. }
  330. /// <inheritdoc/>
  331. protected override void Dispose (bool disposing)
  332. {
  333. Added -= Scroll_Added;
  334. Initialized -= Scroll_Initialized;
  335. DrawContent -= Scroll_DrawContent;
  336. MouseEvent -= Scroll_MouseEvent;
  337. _slider.DrawContent -= Scroll_DrawContent;
  338. _slider.MouseEvent -= Slider_MouseEvent;
  339. base.Dispose (disposing);
  340. }
  341. }