Scroll.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 ()
  18. {
  19. Id = "slider",
  20. Width = Dim.Auto (DimAutoStyle.Content),
  21. Height = Dim.Auto (DimAutoStyle.Content),
  22. WantMousePositionReports = true
  23. };
  24. Add (_slider);
  25. Added += Scroll_Added;
  26. Removed += Scroll_Removed;
  27. Initialized += Scroll_Initialized;
  28. MouseEvent += Scroll_MouseEvent;
  29. _slider.MouseEvent += Slider_MouseEvent;
  30. _slider.MouseEnter += Slider_MouseEnter;
  31. _slider.MouseLeave += Slider_MouseLeave;
  32. }
  33. private readonly View _slider;
  34. private int _lastLocation = -1;
  35. private bool _wasSliderMouse;
  36. private Orientation _orientation;
  37. /// <summary>
  38. /// Gets or sets if the Scroll is oriented vertically or horizontally.
  39. /// </summary>
  40. public Orientation Orientation
  41. {
  42. get => _orientation;
  43. set
  44. {
  45. _orientation = value;
  46. AdjustSlider ();
  47. }
  48. }
  49. private int _position;
  50. /// <summary>
  51. /// Gets or sets the position of the start of the Scroll slider, relative to <see cref="Size"/>.
  52. /// </summary>
  53. public int Position
  54. {
  55. get => _position;
  56. set
  57. {
  58. if (value == _position || value < 0)
  59. {
  60. return;
  61. }
  62. int barSize = Orientation == Orientation.Vertical ? GetContentSize ().Height : GetContentSize ().Width;
  63. if (value + barSize > Size)
  64. {
  65. return;
  66. }
  67. CancelEventArgs<int> args = OnPositionChanging (_position, value);
  68. if (args.Cancel)
  69. {
  70. return;
  71. }
  72. _position = value;
  73. if (!_wasSliderMouse)
  74. {
  75. AdjustSlider ();
  76. }
  77. OnPositionChanged (_position);
  78. }
  79. }
  80. /// <summary>Raised when the <see cref="Position"/> has changed.</summary>
  81. public event EventHandler<EventArgs<int>> PositionChanged;
  82. /// <summary>Raised when the <see cref="Position"/> is changing. Set <see cref="CancelEventArgs.Cancel"/> to <see langword="true"/> to prevent the position from being changed.</summary>
  83. public event EventHandler<CancelEventArgs<int>> PositionChanging;
  84. /// <summary>Virtual method called when <see cref="Position"/> has changed. Raises <see cref="PositionChanged"/>.</summary>
  85. protected virtual void OnPositionChanged (int position) { PositionChanged?.Invoke (this, new (ref position)); }
  86. /// <summary>Virtual method called when <see cref="Position"/> is changing. Raises <see cref="PositionChanging"/>, which is cancelable.</summary>
  87. protected virtual CancelEventArgs<int> OnPositionChanging (int currentPos, int newPos)
  88. {
  89. CancelEventArgs<int> args = new (ref currentPos, ref newPos);
  90. PositionChanging?.Invoke (this, args);
  91. return args;
  92. }
  93. private int _size;
  94. /// <summary>
  95. /// Gets or sets the size of the Scroll. This is the total size of the content that can be scrolled through.
  96. /// </summary>
  97. public int Size
  98. {
  99. get => _size;
  100. set
  101. {
  102. _size = value;
  103. OnSizeChanged (_size);
  104. AdjustSlider ();
  105. }
  106. }
  107. /// <summary>Raised when <see cref="Size"/> has changed.</summary>
  108. public event EventHandler<EventArgs<int>> SizeChanged;
  109. /// <summary>Virtual method called when <see cref="Size"/> has changed. Raises <see cref="SizeChanged"/>.</summary>
  110. protected void OnSizeChanged (int size) { SizeChanged?.Invoke (this, new (ref 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 + location) / 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 + Position) / 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.Parent is Adornment adornment ? adornment.Parent : e.Parent;
  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.Parent is { })
  232. {
  233. View parent = e.Parent is Adornment adornment ? adornment.Parent : e.Parent;
  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. }