Scroll.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. namespace Terminal.Gui;
  2. /// <summary>
  3. /// Represents the "inside part" of a scroll bar, minus the arrows.
  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. /// Determines the Orientation of the scroll.
  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. /// The position, relative to <see cref="Size"/>, to set the scrollbar at.
  49. /// </summary>
  50. public int Position
  51. {
  52. get => _position;
  53. set
  54. {
  55. int barSize = Orientation == Orientation.Vertical ? ContentSize.Height : ContentSize.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. int oldPos = _position;
  66. _position = value;
  67. OnPositionChanged (oldPos);
  68. if (!_wasSliderMouse)
  69. {
  70. AdjustSlider ();
  71. }
  72. }
  73. }
  74. /// <summary>This event is raised when the position on the scrollbar has changed.</summary>
  75. public event EventHandler<StateEventArgs<int>> PositionChanged;
  76. /// <summary>This event is raised when the position on the scrollbar is changing.</summary>
  77. public event EventHandler<StateEventArgs<int>> PositionChanging;
  78. private int _size;
  79. /// <summary>
  80. /// The size of content the scroll represents.
  81. /// </summary>
  82. public int Size
  83. {
  84. get => _size;
  85. set
  86. {
  87. int oldSize = _size;
  88. _size = value;
  89. OnSizeChanged (oldSize);
  90. AdjustSlider ();
  91. }
  92. }
  93. /// <summary>This event is raised when the size of the scroll has changed.</summary>
  94. public event EventHandler<StateEventArgs<int>> SizeChanged;
  95. /// <inheritdoc/>
  96. protected override void Dispose (bool disposing)
  97. {
  98. Added -= Scroll_Added;
  99. Initialized -= Scroll_Initialized;
  100. DrawContent -= Scroll_DrawContent;
  101. MouseEvent -= Scroll_MouseEvent;
  102. _slider.DrawContent -= Scroll_DrawContent;
  103. _slider.MouseEvent -= Slider_MouseEvent;
  104. base.Dispose (disposing);
  105. }
  106. /// <summary>Virtual method to invoke the <see cref="PositionChanged"/> event handler.</summary>
  107. protected virtual void OnPositionChanged (int oldPos) { PositionChanged?.Invoke (this, new (oldPos, Position)); }
  108. /// <summary>Virtual method to invoke the cancelable <see cref="PositionChanging"/> event handler.</summary>
  109. protected virtual StateEventArgs<int> OnPositionChanging (int oldPos, int newPos)
  110. {
  111. StateEventArgs<int> args = new (oldPos, newPos);
  112. PositionChanging?.Invoke (this, args);
  113. return args;
  114. }
  115. /// <summary>Virtual method to invoke the <see cref="SizeChanged"/> event handler.</summary>
  116. protected void OnSizeChanged (int oldSize) { SizeChanged?.Invoke (this, new (oldSize, Size)); }
  117. private int GetPositionFromSliderLocation (int location)
  118. {
  119. if (ContentSize.Height == 0 || ContentSize.Width == 0)
  120. {
  121. return 0;
  122. }
  123. int barSize = Orientation == Orientation.Vertical ? ContentSize.Height : ContentSize.Width;
  124. // Ensure the Position is valid if the slider is at end
  125. if ((Orientation == Orientation.Vertical && location + _slider.Frame.Height == barSize)
  126. || (Orientation == Orientation.Horizontal && location + _slider.Frame.Width == barSize))
  127. {
  128. return Size - barSize;
  129. }
  130. return Math.Min (location * Size / barSize, Size - barSize);
  131. }
  132. private (int Location, int Dimension) GetSliderLocationDimensionFromPosition ()
  133. {
  134. if (ContentSize.Height == 0 || ContentSize.Width == 0)
  135. {
  136. return new (0, 0);
  137. }
  138. int barSize = Orientation == Orientation.Vertical ? ContentSize.Height : ContentSize.Width;
  139. int location;
  140. int dimension;
  141. if (Size > 0)
  142. {
  143. dimension = Math.Min (Math.Max (barSize * barSize / Size, 1), barSize);
  144. // Ensure the Position is valid
  145. if (Position > 0 && Position + barSize > Size)
  146. {
  147. Position = Size - barSize;
  148. }
  149. location = Math.Min (Position * barSize / Size, barSize - dimension);
  150. if (Position == Size - barSize && location + dimension < barSize)
  151. {
  152. location = barSize - dimension;
  153. }
  154. }
  155. else
  156. {
  157. location = 0;
  158. dimension = barSize;
  159. }
  160. return new (location, dimension);
  161. }
  162. private void Parent_LayoutComplete (object sender, LayoutEventArgs e)
  163. {
  164. if (!_wasSliderMouse)
  165. {
  166. AdjustSlider ();
  167. }
  168. else
  169. {
  170. _wasSliderMouse = false;
  171. }
  172. }
  173. private void Parent_MouseEnter (object sender, MouseEventEventArgs e) { OnMouseEnter (e.MouseEvent); }
  174. private void Parent_MouseLeave (object sender, MouseEventEventArgs e) { OnMouseLeave (e.MouseEvent); }
  175. private void Scroll_Added (object sender, SuperViewChangedEventArgs e)
  176. {
  177. View parent = e.Parent is Adornment adornment ? adornment.Parent : e.Parent;
  178. parent.LayoutComplete += Parent_LayoutComplete;
  179. parent.MouseEnter += Parent_MouseEnter;
  180. parent.MouseLeave += Parent_MouseLeave;
  181. }
  182. private void Scroll_DrawContent (object sender, DrawEventArgs e) { SetColorSchemeWithSuperview (sender as View); }
  183. private void Scroll_Initialized (object sender, EventArgs e)
  184. {
  185. AdjustSlider ();
  186. }
  187. private void Scroll_MouseEvent (object sender, MouseEventEventArgs e)
  188. {
  189. MouseEvent me = e.MouseEvent;
  190. int location = Orientation == Orientation.Vertical ? me.Position.Y : me.Position.X;
  191. int barSize = Orientation == Orientation.Vertical ? ContentSize.Height : ContentSize.Width;
  192. (int topLeft, int bottomRight) sliderPos = _orientation == Orientation.Vertical
  193. ? new (_slider.Frame.Y, _slider.Frame.Bottom - 1)
  194. : new (_slider.Frame.X, _slider.Frame.Right - 1);
  195. if (me.Flags == MouseFlags.Button1Pressed && location < sliderPos.topLeft)
  196. {
  197. Position = Math.Max (Position - barSize, 0);
  198. }
  199. else if (me.Flags == MouseFlags.Button1Pressed && location > sliderPos.bottomRight)
  200. {
  201. Position = Math.Min (Position + barSize, Size - barSize);
  202. }
  203. else if ((me.Flags == MouseFlags.WheeledDown && Orientation == Orientation.Vertical)
  204. || (me.Flags == MouseFlags.WheeledRight && Orientation == Orientation.Horizontal))
  205. {
  206. Position = Math.Min (Position + 1, Size - barSize);
  207. }
  208. else if ((me.Flags == MouseFlags.WheeledUp && Orientation == Orientation.Vertical)
  209. || (me.Flags == MouseFlags.WheeledLeft && Orientation == Orientation.Horizontal))
  210. {
  211. Position = Math.Max (Position - 1, 0);
  212. }
  213. }
  214. private void Scroll_Removed (object sender, SuperViewChangedEventArgs e)
  215. {
  216. if (e.Parent is { })
  217. {
  218. View parent = e.Parent is Adornment adornment ? adornment.Parent : e.Parent;
  219. parent.LayoutComplete -= Parent_LayoutComplete;
  220. parent.MouseEnter -= Parent_MouseEnter;
  221. parent.MouseLeave -= Parent_MouseLeave;
  222. }
  223. }
  224. // TODO: Just override GetNormalColor instead of having this method
  225. private static void SetColorSchemeWithSuperview (View view)
  226. {
  227. if (view.SuperView is { })
  228. {
  229. View parent = view.SuperView is Adornment adornment ? adornment.Parent : view.SuperView;
  230. if (view.Id == "slider")
  231. {
  232. view.ColorScheme = new () { Normal = new (parent.ColorScheme.Normal.Foreground, parent.ColorScheme.Normal.Foreground) };
  233. }
  234. else
  235. {
  236. view.ColorScheme = parent.ColorScheme;
  237. }
  238. }
  239. }
  240. private void SetSliderText ()
  241. {
  242. TextDirection = Orientation == Orientation.Vertical ? TextDirection.TopBottom_LeftRight : TextDirection.LeftRight_TopBottom;
  243. Text = string.Concat (
  244. Enumerable.Repeat (
  245. Glyphs.Stipple.ToString (),
  246. ContentSize.Width * ContentSize.Height));
  247. _slider.TextDirection = Orientation == Orientation.Vertical ? TextDirection.TopBottom_LeftRight : TextDirection.LeftRight_TopBottom;
  248. _slider.Text = string.Concat (
  249. Enumerable.Repeat (
  250. Glyphs.ContinuousMeterSegment.ToString (),
  251. _slider.ContentSize.Width * _slider.ContentSize.Height));
  252. }
  253. private void AdjustSlider ()
  254. {
  255. if (!IsInitialized)
  256. {
  257. return;
  258. }
  259. (int Location, int Dimension) slider = GetSliderLocationDimensionFromPosition ();
  260. _slider.X = Orientation == Orientation.Vertical ? 0 : slider.Location;
  261. _slider.Y = Orientation == Orientation.Vertical ? slider.Location : 0;
  262. _slider.SetContentSize (
  263. new (
  264. Orientation == Orientation.Vertical ? ContentSize.Width : slider.Dimension,
  265. Orientation == Orientation.Vertical ? slider.Dimension : ContentSize.Height
  266. ));
  267. SetSliderText ();
  268. }
  269. private void Slider_MouseEvent (object sender, MouseEventEventArgs e)
  270. {
  271. MouseEvent me = e.MouseEvent;
  272. int location = Orientation == Orientation.Vertical ? me.Position.Y : me.Position.X;
  273. int barSize = Orientation == Orientation.Vertical ? ContentSize.Height : ContentSize.Width;
  274. int offset = _lastLocation > -1 ? location - _lastLocation : 0;
  275. if (me.Flags == MouseFlags.Button1Pressed)
  276. {
  277. if (Application.MouseGrabView != sender as View)
  278. {
  279. Application.GrabMouse (sender as View);
  280. _lastLocation = location;
  281. }
  282. }
  283. else if (me.Flags == (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))
  284. {
  285. if (Orientation == Orientation.Vertical)
  286. {
  287. if (_slider.Frame.Y + offset >= 0 && _slider.Frame.Y + offset + _slider.Frame.Height <= barSize)
  288. {
  289. _wasSliderMouse = true;
  290. _slider.Y = _slider.Frame.Y + offset;
  291. Position = GetPositionFromSliderLocation (_slider.Frame.Y);
  292. }
  293. }
  294. else
  295. {
  296. if (_slider.Frame.X + offset >= 0 && _slider.Frame.X + offset + _slider.Frame.Width <= barSize)
  297. {
  298. _wasSliderMouse = true;
  299. _slider.X = _slider.Frame.X + offset;
  300. Position = GetPositionFromSliderLocation (_slider.Frame.X);
  301. }
  302. }
  303. }
  304. else if (me.Flags == MouseFlags.Button1Released)
  305. {
  306. _lastLocation = -1;
  307. if (Application.MouseGrabView == sender as View)
  308. {
  309. Application.UngrabMouse ();
  310. }
  311. }
  312. else if ((me.Flags == MouseFlags.WheeledDown && Orientation == Orientation.Vertical)
  313. || (me.Flags == MouseFlags.WheeledRight && Orientation == Orientation.Horizontal))
  314. {
  315. Position = Math.Min (Position + 1, Size - barSize);
  316. }
  317. else if ((me.Flags == MouseFlags.WheeledUp && Orientation == Orientation.Vertical)
  318. || (me.Flags == MouseFlags.WheeledLeft && Orientation == Orientation.Horizontal))
  319. {
  320. Position = Math.Max (Position - 1, 0);
  321. }
  322. else
  323. {
  324. return;
  325. }
  326. e.Handled = true;
  327. }
  328. }