Scroll.cs 15 KB

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