Scroll.cs 12 KB

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