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 += Scroll_DrawContent;
  28. MouseEvent += Scroll_MouseEvent;
  29. _slider.DrawContent += Scroll_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 -= Scroll_DrawContent;
  103. MouseEvent -= Scroll_MouseEvent;
  104. _slider.DrawContent -= Scroll_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_DrawContent (object sender, DrawEventArgs e) { SetColorSchemeWithSuperview (sender as View); }
  179. private void Scroll_Initialized (object sender, EventArgs e) { SetWidthHeight (); }
  180. private void Scroll_MouseEvent (object sender, MouseEventEventArgs e)
  181. {
  182. MouseEvent me = e.MouseEvent;
  183. int location = Orientation == Orientation.Vertical ? me.Position.Y : me.Position.X;
  184. int barSize = Orientation == Orientation.Vertical ? Frame.Height : Frame.Width;
  185. (int topLeft, int bottomRight) sliderPos = _orientation == Orientation.Vertical
  186. ? new (_slider.Frame.Y, _slider.Frame.Bottom - 1)
  187. : new (_slider.Frame.X, _slider.Frame.Right - 1);
  188. if (me.Flags == MouseFlags.Button1Pressed && location < sliderPos.topLeft)
  189. {
  190. Position = Math.Max (Position - barSize, 0);
  191. }
  192. else if (me.Flags == MouseFlags.Button1Pressed && location > sliderPos.bottomRight)
  193. {
  194. Position = Math.Min (Position + barSize, Size - barSize);
  195. }
  196. }
  197. private void Scroll_Removed (object sender, SuperViewChangedEventArgs e)
  198. {
  199. if (e.Parent is { })
  200. {
  201. View parent = e.Parent is Adornment adornment ? adornment.Parent : e.Parent;
  202. parent.LayoutComplete -= Parent_LayoutComplete;
  203. parent.MouseEnter -= Parent_MouseEnter;
  204. parent.MouseLeave -= Parent_MouseLeave;
  205. }
  206. }
  207. private static void SetColorSchemeWithSuperview (View view)
  208. {
  209. if (view.SuperView is { })
  210. {
  211. View parent = view.SuperView is Adornment adornment ? adornment.Parent : view.SuperView;
  212. if (view.Id == "slider")
  213. {
  214. view.ColorScheme = new () { Normal = new (parent.ColorScheme.Normal.Foreground, parent.ColorScheme.Normal.Foreground) };
  215. }
  216. else
  217. {
  218. view.ColorScheme = parent.ColorScheme;
  219. }
  220. }
  221. }
  222. private void SetSliderText ()
  223. {
  224. TextDirection = Orientation == Orientation.Vertical ? TextDirection.TopBottom_LeftRight : TextDirection.LeftRight_TopBottom;
  225. Text = string.Concat (
  226. Enumerable.Repeat (
  227. Glyphs.Stipple.ToString (),
  228. Frame.Width * Frame.Height));
  229. _slider.TextDirection = Orientation == Orientation.Vertical ? TextDirection.TopBottom_LeftRight : TextDirection.LeftRight_TopBottom;
  230. _slider.Text = string.Concat (
  231. Enumerable.Repeat (
  232. Glyphs.ContinuousMeterSegment.ToString (),
  233. _slider.Frame.Width * _slider.Frame.Height));
  234. }
  235. private void SetWidthHeight ()
  236. {
  237. if (!IsInitialized)
  238. {
  239. return;
  240. }
  241. (int Location, int Dimension) slider = GetSliderLocationDimensionFromPosition ();
  242. _slider.X = Orientation == Orientation.Vertical ? 0 : slider.Location;
  243. _slider.Y = Orientation == Orientation.Vertical ? slider.Location : 0;
  244. _slider.Width = Orientation == Orientation.Vertical ? Dim.Fill () : slider.Dimension;
  245. _slider.Height = Orientation == Orientation.Vertical ? slider.Dimension : Dim.Fill ();
  246. SetSliderText ();
  247. }
  248. private void Slider_MouseEvent (object sender, MouseEventEventArgs e)
  249. {
  250. MouseEvent me = e.MouseEvent;
  251. int location = Orientation == Orientation.Vertical ? me.Position.Y : me.Position.X;
  252. int barSize = Orientation == Orientation.Vertical ? Frame.Height : Frame.Width;
  253. int offset = _lastLocation > -1 ? location - _lastLocation : 0;
  254. if (me.Flags == MouseFlags.Button1Pressed)
  255. {
  256. if (Application.MouseGrabView != sender as View)
  257. {
  258. Application.GrabMouse (sender as View);
  259. _lastLocation = location;
  260. }
  261. }
  262. else if (me.Flags == (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))
  263. {
  264. if (Orientation == Orientation.Vertical)
  265. {
  266. if (_slider.Frame.Y + offset >= 0 && _slider.Frame.Y + offset + _slider.Frame.Height <= barSize)
  267. {
  268. _wasSliderMouse = true;
  269. _slider.Y = _slider.Frame.Y + offset;
  270. Position = GetPositionFromSliderLocation (_slider.Frame.Y);
  271. }
  272. }
  273. else
  274. {
  275. if (_slider.Frame.X + offset >= 0 && _slider.Frame.X + offset + _slider.Frame.Width <= barSize)
  276. {
  277. _wasSliderMouse = true;
  278. _slider.X = _slider.Frame.X + offset;
  279. Position = GetPositionFromSliderLocation (_slider.Frame.X);
  280. }
  281. }
  282. }
  283. else if (me.Flags == MouseFlags.Button1Released)
  284. {
  285. _lastLocation = -1;
  286. if (Application.MouseGrabView == sender as View)
  287. {
  288. Application.UngrabMouse ();
  289. }
  290. }
  291. else
  292. {
  293. return;
  294. }
  295. e.Handled = true;
  296. }
  297. }