Scroll.cs 12 KB

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