ScrollBar.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. using System;
  2. using Avalonia.Data;
  3. using Avalonia.Interactivity;
  4. using Avalonia.Input;
  5. using Avalonia.Layout;
  6. using Avalonia.Threading;
  7. using Avalonia.Controls.Metadata;
  8. using Avalonia.Automation.Peers;
  9. namespace Avalonia.Controls.Primitives
  10. {
  11. public class ScrollEventArgs : EventArgs
  12. {
  13. public ScrollEventArgs(ScrollEventType eventType, double newValue)
  14. {
  15. ScrollEventType = eventType;
  16. NewValue = newValue;
  17. }
  18. public double NewValue { get; private set; }
  19. public ScrollEventType ScrollEventType { get; private set; }
  20. }
  21. /// <summary>
  22. /// A scrollbar control.
  23. /// </summary>
  24. [TemplatePart("PART_LineDownButton", typeof(Button))]
  25. [TemplatePart("PART_LineUpButton", typeof(Button))]
  26. [TemplatePart("PART_PageDownButton", typeof(Button))]
  27. [TemplatePart("PART_PageUpButton", typeof(Button))]
  28. [PseudoClasses(":vertical", ":horizontal")]
  29. public class ScrollBar : RangeBase
  30. {
  31. /// <summary>
  32. /// Defines the <see cref="ViewportSize"/> property.
  33. /// </summary>
  34. public static readonly StyledProperty<double> ViewportSizeProperty =
  35. AvaloniaProperty.Register<ScrollBar, double>(nameof(ViewportSize), defaultValue: double.NaN);
  36. /// <summary>
  37. /// Defines the <see cref="Visibility"/> property.
  38. /// </summary>
  39. public static readonly StyledProperty<ScrollBarVisibility> VisibilityProperty =
  40. AvaloniaProperty.Register<ScrollBar, ScrollBarVisibility>(nameof(Visibility), ScrollBarVisibility.Visible);
  41. /// <summary>
  42. /// Defines the <see cref="Orientation"/> property.
  43. /// </summary>
  44. public static readonly StyledProperty<Orientation> OrientationProperty =
  45. AvaloniaProperty.Register<ScrollBar, Orientation>(nameof(Orientation), Orientation.Vertical);
  46. /// <summary>
  47. /// Defines the <see cref="IsExpanded"/> property.
  48. /// </summary>
  49. public static readonly DirectProperty<ScrollBar, bool> IsExpandedProperty =
  50. AvaloniaProperty.RegisterDirect<ScrollBar, bool>(
  51. nameof(IsExpanded),
  52. o => o.IsExpanded);
  53. /// <summary>
  54. /// Defines the <see cref="AllowAutoHide"/> property.
  55. /// </summary>
  56. public static readonly StyledProperty<bool> AllowAutoHideProperty =
  57. AvaloniaProperty.Register<ScrollBar, bool>(nameof(AllowAutoHide), true);
  58. /// <summary>
  59. /// Defines the <see cref="HideDelay"/> property.
  60. /// </summary>
  61. public static readonly StyledProperty<TimeSpan> HideDelayProperty =
  62. AvaloniaProperty.Register<ScrollBar, TimeSpan>(nameof(HideDelay), TimeSpan.FromSeconds(2));
  63. /// <summary>
  64. /// Defines the <see cref="ShowDelay"/> property.
  65. /// </summary>
  66. public static readonly StyledProperty<TimeSpan> ShowDelayProperty =
  67. AvaloniaProperty.Register<ScrollBar, TimeSpan>(nameof(ShowDelay), TimeSpan.FromSeconds(0.5));
  68. private Button? _lineUpButton;
  69. private Button? _lineDownButton;
  70. private Button? _pageUpButton;
  71. private Button? _pageDownButton;
  72. private DispatcherTimer? _timer;
  73. private bool _isExpanded;
  74. /// <summary>
  75. /// Initializes static members of the <see cref="ScrollBar"/> class.
  76. /// </summary>
  77. static ScrollBar()
  78. {
  79. Thumb.DragDeltaEvent.AddClassHandler<ScrollBar>((x, e) => x.OnThumbDragDelta(e), RoutingStrategies.Bubble);
  80. Thumb.DragCompletedEvent.AddClassHandler<ScrollBar>((x, e) => x.OnThumbDragComplete(e), RoutingStrategies.Bubble);
  81. }
  82. /// <summary>
  83. /// Initializes a new instance of the <see cref="ScrollBar"/> class.
  84. /// </summary>
  85. public ScrollBar()
  86. {
  87. UpdatePseudoClasses(Orientation);
  88. }
  89. /// <summary>
  90. /// Gets or sets the amount of the scrollable content that is currently visible.
  91. /// </summary>
  92. public double ViewportSize
  93. {
  94. get { return GetValue(ViewportSizeProperty); }
  95. set { SetValue(ViewportSizeProperty, value); }
  96. }
  97. /// <summary>
  98. /// Gets or sets a value that indicates whether the scrollbar should hide itself when it
  99. /// is not needed.
  100. /// </summary>
  101. public ScrollBarVisibility Visibility
  102. {
  103. get { return GetValue(VisibilityProperty); }
  104. set { SetValue(VisibilityProperty, value); }
  105. }
  106. /// <summary>
  107. /// Gets or sets the orientation of the scrollbar.
  108. /// </summary>
  109. public Orientation Orientation
  110. {
  111. get { return GetValue(OrientationProperty); }
  112. set { SetValue(OrientationProperty, value); }
  113. }
  114. /// <summary>
  115. /// Gets a value that indicates whether the scrollbar is expanded.
  116. /// </summary>
  117. public bool IsExpanded
  118. {
  119. get => _isExpanded;
  120. private set => SetAndRaise(IsExpandedProperty, ref _isExpanded, value);
  121. }
  122. /// <summary>
  123. /// Gets a value that indicates whether the scrollbar can hide itself when user is not interacting with it.
  124. /// </summary>
  125. public bool AllowAutoHide
  126. {
  127. get => GetValue(AllowAutoHideProperty);
  128. set => SetValue(AllowAutoHideProperty, value);
  129. }
  130. /// <summary>
  131. /// Gets a value that determines how long will be the hide delay after user stops interacting with the scrollbar.
  132. /// </summary>
  133. public TimeSpan HideDelay
  134. {
  135. get => GetValue(HideDelayProperty);
  136. set => SetValue(HideDelayProperty, value);
  137. }
  138. /// <summary>
  139. /// Gets a value that determines how long will be the show delay when user starts interacting with the scrollbar.
  140. /// </summary>
  141. public TimeSpan ShowDelay
  142. {
  143. get => GetValue(ShowDelayProperty);
  144. set => SetValue(ShowDelayProperty, value);
  145. }
  146. public event EventHandler<ScrollEventArgs>? Scroll;
  147. /// <summary>
  148. /// Calculates and updates whether the scrollbar should be visible.
  149. /// </summary>
  150. private void UpdateIsVisible()
  151. {
  152. var isVisible = Visibility switch
  153. {
  154. ScrollBarVisibility.Visible => true,
  155. ScrollBarVisibility.Disabled => false,
  156. ScrollBarVisibility.Hidden => false,
  157. ScrollBarVisibility.Auto => (double.IsNaN(ViewportSize) || Maximum > 0),
  158. _ => throw new InvalidOperationException("Invalid value for ScrollBar.Visibility.")
  159. };
  160. SetValue(IsVisibleProperty, isVisible);
  161. }
  162. protected override void OnKeyDown(KeyEventArgs e)
  163. {
  164. if (e.Key == Key.PageUp)
  165. {
  166. LargeDecrement();
  167. e.Handled = true;
  168. }
  169. else if (e.Key == Key.PageDown)
  170. {
  171. LargeIncrement();
  172. e.Handled = true;
  173. }
  174. }
  175. protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
  176. {
  177. base.OnPropertyChanged(change);
  178. if (change.Property == OrientationProperty)
  179. {
  180. UpdatePseudoClasses(change.GetNewValue<Orientation>());
  181. }
  182. else if (change.Property == AllowAutoHideProperty)
  183. {
  184. UpdateIsExpandedState();
  185. }
  186. else
  187. {
  188. if (change.Property == MinimumProperty ||
  189. change.Property == MaximumProperty ||
  190. change.Property == ViewportSizeProperty ||
  191. change.Property == VisibilityProperty)
  192. {
  193. UpdateIsVisible();
  194. }
  195. }
  196. }
  197. protected override void OnPointerEntered(PointerEventArgs e)
  198. {
  199. base.OnPointerEntered(e);
  200. if (AllowAutoHide)
  201. {
  202. ExpandAfterDelay();
  203. }
  204. }
  205. protected override void OnPointerExited(PointerEventArgs e)
  206. {
  207. base.OnPointerExited(e);
  208. if (AllowAutoHide)
  209. {
  210. CollapseAfterDelay();
  211. }
  212. }
  213. protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
  214. {
  215. if (_lineUpButton != null)
  216. {
  217. _lineUpButton.Click -= LineUpClick;
  218. }
  219. if (_lineDownButton != null)
  220. {
  221. _lineDownButton.Click -= LineDownClick;
  222. }
  223. if (_pageUpButton != null)
  224. {
  225. _pageUpButton.Click -= PageUpClick;
  226. }
  227. if (_pageDownButton != null)
  228. {
  229. _pageDownButton.Click -= PageDownClick;
  230. }
  231. _lineUpButton = e.NameScope.Find<Button>("PART_LineUpButton");
  232. _lineDownButton = e.NameScope.Find<Button>("PART_LineDownButton");
  233. _pageUpButton = e.NameScope.Find<Button>("PART_PageUpButton");
  234. _pageDownButton = e.NameScope.Find<Button>("PART_PageDownButton");
  235. if (_lineUpButton != null)
  236. {
  237. _lineUpButton.Click += LineUpClick;
  238. }
  239. if (_lineDownButton != null)
  240. {
  241. _lineDownButton.Click += LineDownClick;
  242. }
  243. if (_pageUpButton != null)
  244. {
  245. _pageUpButton.Click += PageUpClick;
  246. }
  247. if (_pageDownButton != null)
  248. {
  249. _pageDownButton.Click += PageDownClick;
  250. }
  251. }
  252. protected override AutomationPeer OnCreateAutomationPeer() => new ScrollBarAutomationPeer(this);
  253. private void InvokeAfterDelay(Action handler, TimeSpan delay)
  254. {
  255. if (_timer != null)
  256. {
  257. _timer.Stop();
  258. }
  259. else
  260. {
  261. _timer = new DispatcherTimer(DispatcherPriority.Normal);
  262. _timer.Tick += (sender, args) =>
  263. {
  264. var senderTimer = (DispatcherTimer)sender!;
  265. if (senderTimer.Tag is Action action)
  266. {
  267. action();
  268. }
  269. senderTimer.Stop();
  270. };
  271. }
  272. _timer.Tag = handler;
  273. _timer.Interval = delay;
  274. _timer.Start();
  275. }
  276. private void UpdateIsExpandedState()
  277. {
  278. if (!AllowAutoHide)
  279. {
  280. _timer?.Stop();
  281. IsExpanded = true;
  282. }
  283. else
  284. {
  285. IsExpanded = IsPointerOver;
  286. }
  287. }
  288. private void CollapseAfterDelay()
  289. {
  290. InvokeAfterDelay(Collapse, HideDelay);
  291. }
  292. private void ExpandAfterDelay()
  293. {
  294. InvokeAfterDelay(Expand, ShowDelay);
  295. }
  296. private void Collapse()
  297. {
  298. IsExpanded = false;
  299. }
  300. private void Expand()
  301. {
  302. IsExpanded = true;
  303. }
  304. private void LineUpClick(object? sender, RoutedEventArgs e)
  305. {
  306. SmallDecrement();
  307. }
  308. private void LineDownClick(object? sender, RoutedEventArgs e)
  309. {
  310. SmallIncrement();
  311. }
  312. private void PageUpClick(object? sender, RoutedEventArgs e)
  313. {
  314. LargeDecrement();
  315. }
  316. private void PageDownClick(object? sender, RoutedEventArgs e)
  317. {
  318. LargeIncrement();
  319. }
  320. private void SmallDecrement()
  321. {
  322. Value = Math.Max(Value - SmallChange, Minimum);
  323. OnScroll(ScrollEventType.SmallDecrement);
  324. }
  325. private void SmallIncrement()
  326. {
  327. Value = Math.Min(Value + SmallChange, Maximum);
  328. OnScroll(ScrollEventType.SmallIncrement);
  329. }
  330. private void LargeDecrement()
  331. {
  332. Value = Math.Max(Value - LargeChange, Minimum);
  333. OnScroll(ScrollEventType.LargeDecrement);
  334. }
  335. private void LargeIncrement()
  336. {
  337. Value = Math.Min(Value + LargeChange, Maximum);
  338. OnScroll(ScrollEventType.LargeIncrement);
  339. }
  340. private void OnThumbDragDelta(VectorEventArgs e)
  341. {
  342. OnScroll(ScrollEventType.ThumbTrack);
  343. }
  344. private void OnThumbDragComplete(VectorEventArgs e)
  345. {
  346. OnScroll(ScrollEventType.EndScroll);
  347. }
  348. protected void OnScroll(ScrollEventType scrollEventType)
  349. {
  350. Scroll?.Invoke(this, new ScrollEventArgs(scrollEventType, Value));
  351. }
  352. private void UpdatePseudoClasses(Orientation o)
  353. {
  354. PseudoClasses.Set(":vertical", o == Orientation.Vertical);
  355. PseudoClasses.Set(":horizontal", o == Orientation.Horizontal);
  356. }
  357. }
  358. }