Scroll.cs 11 KB

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