Scroll.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. return Math.Min (location * Size / barSize, Size - barSize);
  125. }
  126. private (int Location, int Dimension) GetSliderLocationDimensionFromPosition ()
  127. {
  128. if (ContentSize.Height == 0 || ContentSize.Width == 0)
  129. {
  130. return new (0, 0);
  131. }
  132. int barSize = Orientation == Orientation.Vertical ? ContentSize.Height : ContentSize.Width;
  133. int location;
  134. int dimension;
  135. if (Size > 0)
  136. {
  137. dimension = Math.Min (Math.Max (barSize * barSize / Size, 1), barSize);
  138. // Ensure the Position is valid
  139. if (Position > 0 && Position + barSize > Size)
  140. {
  141. Position = Size - barSize;
  142. }
  143. location = Math.Min (Position * barSize / Size, barSize - dimension);
  144. if (Position == Size - barSize && location + dimension < barSize)
  145. {
  146. location = barSize - dimension;
  147. }
  148. }
  149. else
  150. {
  151. location = 0;
  152. dimension = barSize;
  153. }
  154. return new (location, dimension);
  155. }
  156. private void Parent_LayoutComplete (object sender, LayoutEventArgs e)
  157. {
  158. if (!_wasSliderMouse)
  159. {
  160. AdjustSlider ();
  161. }
  162. else
  163. {
  164. _wasSliderMouse = false;
  165. }
  166. }
  167. private void Parent_MouseEnter (object sender, MouseEventEventArgs e) { OnMouseEnter (e.MouseEvent); }
  168. private void Parent_MouseLeave (object sender, MouseEventEventArgs e) { OnMouseLeave (e.MouseEvent); }
  169. private void Scroll_Added (object sender, SuperViewChangedEventArgs e)
  170. {
  171. View parent = e.Parent is Adornment adornment ? adornment.Parent : e.Parent;
  172. parent.LayoutComplete += Parent_LayoutComplete;
  173. parent.MouseEnter += Parent_MouseEnter;
  174. parent.MouseLeave += Parent_MouseLeave;
  175. }
  176. private void Scroll_DrawContent (object sender, DrawEventArgs e) { SetColorSchemeWithSuperview (sender as View); }
  177. private void Scroll_Initialized (object sender, EventArgs e)
  178. {
  179. AdjustSlider ();
  180. }
  181. private void Scroll_MouseEvent (object sender, MouseEventEventArgs e)
  182. {
  183. MouseEvent me = e.MouseEvent;
  184. int location = Orientation == Orientation.Vertical ? me.Position.Y : me.Position.X;
  185. int barSize = Orientation == Orientation.Vertical ? ContentSize.Height : ContentSize.Width;
  186. (int topLeft, int bottomRight) sliderPos = _orientation == Orientation.Vertical
  187. ? new (_slider.Frame.Y, _slider.Frame.Bottom - 1)
  188. : new (_slider.Frame.X, _slider.Frame.Right - 1);
  189. if (me.Flags == MouseFlags.Button1Pressed && location < sliderPos.topLeft)
  190. {
  191. Position = Math.Max (Position - barSize, 0);
  192. }
  193. else if (me.Flags == MouseFlags.Button1Pressed && location > sliderPos.bottomRight)
  194. {
  195. Position = Math.Min (Position + barSize, Size - barSize);
  196. }
  197. }
  198. private void Scroll_Removed (object sender, SuperViewChangedEventArgs e)
  199. {
  200. if (e.Parent is { })
  201. {
  202. View parent = e.Parent is Adornment adornment ? adornment.Parent : e.Parent;
  203. parent.LayoutComplete -= Parent_LayoutComplete;
  204. parent.MouseEnter -= Parent_MouseEnter;
  205. parent.MouseLeave -= Parent_MouseLeave;
  206. }
  207. }
  208. // TODO: Just override GetNormalColor instead of having this method
  209. private static void SetColorSchemeWithSuperview (View view)
  210. {
  211. if (view.SuperView is { })
  212. {
  213. View parent = view.SuperView is Adornment adornment ? adornment.Parent : view.SuperView;
  214. if (view.Id == "slider")
  215. {
  216. view.ColorScheme = new () { Normal = new (parent.ColorScheme.Normal.Foreground, parent.ColorScheme.Normal.Foreground) };
  217. }
  218. else
  219. {
  220. view.ColorScheme = parent.ColorScheme;
  221. }
  222. }
  223. }
  224. private void SetSliderText ()
  225. {
  226. TextDirection = Orientation == Orientation.Vertical ? TextDirection.TopBottom_LeftRight : TextDirection.LeftRight_TopBottom;
  227. Text = string.Concat (
  228. Enumerable.Repeat (
  229. Glyphs.Stipple.ToString (),
  230. ContentSize.Width * ContentSize.Height));
  231. _slider.TextDirection = Orientation == Orientation.Vertical ? TextDirection.TopBottom_LeftRight : TextDirection.LeftRight_TopBottom;
  232. _slider.Text = string.Concat (
  233. Enumerable.Repeat (
  234. Glyphs.ContinuousMeterSegment.ToString (),
  235. _slider.ContentSize.Width * _slider.ContentSize.Height));
  236. }
  237. private void AdjustSlider ()
  238. {
  239. if (!IsInitialized)
  240. {
  241. return;
  242. }
  243. (int Location, int Dimension) slider = GetSliderLocationDimensionFromPosition ();
  244. _slider.X = Orientation == Orientation.Vertical ? 0 : slider.Location;
  245. _slider.Y = Orientation == Orientation.Vertical ? slider.Location : 0;
  246. _slider.SetContentSize (
  247. new (
  248. Orientation == Orientation.Vertical ? ContentSize.Width : slider.Dimension,
  249. Orientation == Orientation.Vertical ? slider.Dimension : ContentSize.Height
  250. ));
  251. SetSliderText ();
  252. }
  253. private void Slider_MouseEvent (object sender, MouseEventEventArgs e)
  254. {
  255. MouseEvent me = e.MouseEvent;
  256. int location = Orientation == Orientation.Vertical ? me.Position.Y : me.Position.X;
  257. int barSize = Orientation == Orientation.Vertical ? ContentSize.Height : ContentSize.Width;
  258. int offset = _lastLocation > -1 ? location - _lastLocation : 0;
  259. if (me.Flags == MouseFlags.Button1Pressed)
  260. {
  261. if (Application.MouseGrabView != sender as View)
  262. {
  263. Application.GrabMouse (sender as View);
  264. _lastLocation = location;
  265. }
  266. }
  267. else if (me.Flags == (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))
  268. {
  269. if (Orientation == Orientation.Vertical)
  270. {
  271. if (_slider.Frame.Y + offset >= 0 && _slider.Frame.Y + offset + _slider.Frame.Height <= barSize)
  272. {
  273. _wasSliderMouse = true;
  274. _slider.Y = _slider.Frame.Y + offset;
  275. Position = GetPositionFromSliderLocation (_slider.Frame.Y);
  276. }
  277. }
  278. else
  279. {
  280. if (_slider.Frame.X + offset >= 0 && _slider.Frame.X + offset + _slider.Frame.Width <= barSize)
  281. {
  282. _wasSliderMouse = true;
  283. _slider.X = _slider.Frame.X + offset;
  284. Position = GetPositionFromSliderLocation (_slider.Frame.X);
  285. }
  286. }
  287. }
  288. else if (me.Flags == MouseFlags.Button1Released)
  289. {
  290. _lastLocation = -1;
  291. if (Application.MouseGrabView == sender as View)
  292. {
  293. Application.UngrabMouse ();
  294. }
  295. }
  296. else
  297. {
  298. return;
  299. }
  300. e.Handled = true;
  301. }
  302. }