Scroll.cs 12 KB

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