ScrollViewer.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using Avalonia.Controls.Presenters;
  5. using Avalonia.Controls.Primitives;
  6. using Avalonia.Input;
  7. namespace Avalonia.Controls
  8. {
  9. /// <summary>
  10. /// A control scrolls its content if the content is bigger than the space available.
  11. /// </summary>
  12. public class ScrollViewer : ContentControl, IScrollable, IScrollAnchorProvider
  13. {
  14. /// <summary>
  15. /// Defines the <see cref="CanHorizontallyScroll"/> property.
  16. /// </summary>
  17. /// <remarks>
  18. /// There is no public C# accessor for this property as it is intended to be bound to by a
  19. /// <see cref="ScrollContentPresenter"/> in the control's template.
  20. /// </remarks>
  21. public static readonly DirectProperty<ScrollViewer, bool> CanHorizontallyScrollProperty =
  22. AvaloniaProperty.RegisterDirect<ScrollViewer, bool>(
  23. nameof(CanHorizontallyScroll),
  24. o => o.CanHorizontallyScroll);
  25. /// <summary>
  26. /// Defines the <see cref="CanVerticallyScroll"/> property.
  27. /// </summary>
  28. /// <remarks>
  29. /// There is no public C# accessor for this property as it is intended to be bound to by a
  30. /// <see cref="ScrollContentPresenter"/> in the control's template.
  31. /// </remarks>
  32. public static readonly DirectProperty<ScrollViewer, bool> CanVerticallyScrollProperty =
  33. AvaloniaProperty.RegisterDirect<ScrollViewer, bool>(
  34. nameof(CanVerticallyScroll),
  35. o => o.CanVerticallyScroll);
  36. /// <summary>
  37. /// Defines the <see cref="Extent"/> property.
  38. /// </summary>
  39. public static readonly DirectProperty<ScrollViewer, Size> ExtentProperty =
  40. AvaloniaProperty.RegisterDirect<ScrollViewer, Size>(nameof(Extent),
  41. o => o.Extent,
  42. (o, v) => o.Extent = v);
  43. /// <summary>
  44. /// Defines the <see cref="Offset"/> property.
  45. /// </summary>
  46. public static readonly DirectProperty<ScrollViewer, Vector> OffsetProperty =
  47. AvaloniaProperty.RegisterDirect<ScrollViewer, Vector>(
  48. nameof(Offset),
  49. o => o.Offset,
  50. (o, v) => o.Offset = v);
  51. /// <summary>
  52. /// Defines the <see cref="Viewport"/> property.
  53. /// </summary>
  54. public static readonly DirectProperty<ScrollViewer, Size> ViewportProperty =
  55. AvaloniaProperty.RegisterDirect<ScrollViewer, Size>(nameof(Viewport),
  56. o => o.Viewport,
  57. (o, v) => o.Viewport = v);
  58. /// <summary>
  59. /// Defines the HorizontalScrollBarMaximum property.
  60. /// </summary>
  61. /// <remarks>
  62. /// There is no public C# accessor for this property as it is intended to be bound to by a
  63. /// <see cref="ScrollContentPresenter"/> in the control's template.
  64. /// </remarks>
  65. public static readonly DirectProperty<ScrollViewer, double> HorizontalScrollBarMaximumProperty =
  66. AvaloniaProperty.RegisterDirect<ScrollViewer, double>(
  67. nameof(HorizontalScrollBarMaximum),
  68. o => o.HorizontalScrollBarMaximum);
  69. /// <summary>
  70. /// Defines the HorizontalScrollBarValue property.
  71. /// </summary>
  72. /// <remarks>
  73. /// There is no public C# accessor for this property as it is intended to be bound to by a
  74. /// <see cref="ScrollContentPresenter"/> in the control's template.
  75. /// </remarks>
  76. public static readonly DirectProperty<ScrollViewer, double> HorizontalScrollBarValueProperty =
  77. AvaloniaProperty.RegisterDirect<ScrollViewer, double>(
  78. nameof(HorizontalScrollBarValue),
  79. o => o.HorizontalScrollBarValue,
  80. (o, v) => o.HorizontalScrollBarValue = v);
  81. /// <summary>
  82. /// Defines the HorizontalScrollBarViewportSize property.
  83. /// </summary>
  84. /// <remarks>
  85. /// There is no public C# accessor for this property as it is intended to be bound to by a
  86. /// <see cref="ScrollContentPresenter"/> in the control's template.
  87. /// </remarks>
  88. public static readonly DirectProperty<ScrollViewer, double> HorizontalScrollBarViewportSizeProperty =
  89. AvaloniaProperty.RegisterDirect<ScrollViewer, double>(
  90. nameof(HorizontalScrollBarViewportSize),
  91. o => o.HorizontalScrollBarViewportSize);
  92. /// <summary>
  93. /// Defines the <see cref="HorizontalScrollBarVisibility"/> property.
  94. /// </summary>
  95. public static readonly AttachedProperty<ScrollBarVisibility> HorizontalScrollBarVisibilityProperty =
  96. AvaloniaProperty.RegisterAttached<ScrollViewer, Control, ScrollBarVisibility>(
  97. nameof(HorizontalScrollBarVisibility),
  98. ScrollBarVisibility.Hidden);
  99. /// <summary>
  100. /// Defines the VerticalScrollBarMaximum property.
  101. /// </summary>
  102. /// <remarks>
  103. /// There is no public C# accessor for this property as it is intended to be bound to by a
  104. /// <see cref="ScrollContentPresenter"/> in the control's template.
  105. /// </remarks>
  106. public static readonly DirectProperty<ScrollViewer, double> VerticalScrollBarMaximumProperty =
  107. AvaloniaProperty.RegisterDirect<ScrollViewer, double>(
  108. nameof(VerticalScrollBarMaximum),
  109. o => o.VerticalScrollBarMaximum);
  110. /// <summary>
  111. /// Defines the VerticalScrollBarValue property.
  112. /// </summary>
  113. /// <remarks>
  114. /// There is no public C# accessor for this property as it is intended to be bound to by a
  115. /// <see cref="ScrollContentPresenter"/> in the control's template.
  116. /// </remarks>
  117. public static readonly DirectProperty<ScrollViewer, double> VerticalScrollBarValueProperty =
  118. AvaloniaProperty.RegisterDirect<ScrollViewer, double>(
  119. nameof(VerticalScrollBarValue),
  120. o => o.VerticalScrollBarValue,
  121. (o, v) => o.VerticalScrollBarValue = v);
  122. /// <summary>
  123. /// Defines the VerticalScrollBarViewportSize property.
  124. /// </summary>
  125. /// <remarks>
  126. /// There is no public C# accessor for this property as it is intended to be bound to by a
  127. /// <see cref="ScrollContentPresenter"/> in the control's template.
  128. /// </remarks>
  129. public static readonly DirectProperty<ScrollViewer, double> VerticalScrollBarViewportSizeProperty =
  130. AvaloniaProperty.RegisterDirect<ScrollViewer, double>(
  131. nameof(VerticalScrollBarViewportSize),
  132. o => o.VerticalScrollBarViewportSize);
  133. /// <summary>
  134. /// Defines the <see cref="VerticalScrollBarVisibility"/> property.
  135. /// </summary>
  136. public static readonly AttachedProperty<ScrollBarVisibility> VerticalScrollBarVisibilityProperty =
  137. AvaloniaProperty.RegisterAttached<ScrollViewer, Control, ScrollBarVisibility>(
  138. nameof(VerticalScrollBarVisibility),
  139. ScrollBarVisibility.Auto);
  140. private Size _extent;
  141. private Vector _offset;
  142. private Size _viewport;
  143. /// <summary>
  144. /// Initializes static members of the <see cref="ScrollViewer"/> class.
  145. /// </summary>
  146. static ScrollViewer()
  147. {
  148. HorizontalScrollBarVisibilityProperty.Changed.AddClassHandler<ScrollViewer>((x, e) => x.ScrollBarVisibilityChanged(e));
  149. VerticalScrollBarVisibilityProperty.Changed.AddClassHandler<ScrollViewer>((x, e) => x.ScrollBarVisibilityChanged(e));
  150. }
  151. /// <summary>
  152. /// Initializes a new instance of the <see cref="ScrollViewer"/> class.
  153. /// </summary>
  154. public ScrollViewer()
  155. {
  156. }
  157. /// <summary>
  158. /// Gets the extent of the scrollable content.
  159. /// </summary>
  160. public Size Extent
  161. {
  162. get
  163. {
  164. return _extent;
  165. }
  166. private set
  167. {
  168. if (SetAndRaise(ExtentProperty, ref _extent, value))
  169. {
  170. CalculatedPropertiesChanged();
  171. }
  172. }
  173. }
  174. /// <summary>
  175. /// Gets or sets the current scroll offset.
  176. /// </summary>
  177. public Vector Offset
  178. {
  179. get
  180. {
  181. return _offset;
  182. }
  183. set
  184. {
  185. value = ValidateOffset(this, value);
  186. if (SetAndRaise(OffsetProperty, ref _offset, value))
  187. {
  188. CalculatedPropertiesChanged();
  189. }
  190. }
  191. }
  192. /// <summary>
  193. /// Gets the size of the viewport on the scrollable content.
  194. /// </summary>
  195. public Size Viewport
  196. {
  197. get
  198. {
  199. return _viewport;
  200. }
  201. private set
  202. {
  203. if (SetAndRaise(ViewportProperty, ref _viewport, value))
  204. {
  205. CalculatedPropertiesChanged();
  206. }
  207. }
  208. }
  209. /// <summary>
  210. /// Gets or sets the horizontal scrollbar visibility.
  211. /// </summary>
  212. public ScrollBarVisibility HorizontalScrollBarVisibility
  213. {
  214. get { return GetValue(HorizontalScrollBarVisibilityProperty); }
  215. set { SetValue(HorizontalScrollBarVisibilityProperty, value); }
  216. }
  217. /// <summary>
  218. /// Gets or sets the vertical scrollbar visibility.
  219. /// </summary>
  220. public ScrollBarVisibility VerticalScrollBarVisibility
  221. {
  222. get { return GetValue(VerticalScrollBarVisibilityProperty); }
  223. set { SetValue(VerticalScrollBarVisibilityProperty, value); }
  224. }
  225. /// <summary>
  226. /// Scrolls to the top-left corner of the content.
  227. /// </summary>
  228. public void ScrollToHome()
  229. {
  230. Offset = new Vector(double.NegativeInfinity, double.NegativeInfinity);
  231. }
  232. /// <summary>
  233. /// Scrolls to the bottom-left corner of the content.
  234. /// </summary>
  235. public void ScrollToEnd()
  236. {
  237. Offset = new Vector(double.NegativeInfinity, double.PositiveInfinity);
  238. }
  239. /// <summary>
  240. /// Gets a value indicating whether the viewer can scroll horizontally.
  241. /// </summary>
  242. protected bool CanHorizontallyScroll
  243. {
  244. get { return HorizontalScrollBarVisibility != ScrollBarVisibility.Disabled; }
  245. }
  246. /// <summary>
  247. /// Gets a value indicating whether the viewer can scroll vertically.
  248. /// </summary>
  249. protected bool CanVerticallyScroll
  250. {
  251. get { return VerticalScrollBarVisibility != ScrollBarVisibility.Disabled; }
  252. }
  253. /// <summary>
  254. /// Gets the maximum horizontal scrollbar value.
  255. /// </summary>
  256. protected double HorizontalScrollBarMaximum
  257. {
  258. get { return Max(_extent.Width - _viewport.Width, 0); }
  259. }
  260. /// <summary>
  261. /// Gets or sets the horizontal scrollbar value.
  262. /// </summary>
  263. protected double HorizontalScrollBarValue
  264. {
  265. get { return _offset.X; }
  266. set
  267. {
  268. if (_offset.X != value)
  269. {
  270. var old = Offset.X;
  271. Offset = Offset.WithX(value);
  272. RaisePropertyChanged(HorizontalScrollBarValueProperty, old, value);
  273. }
  274. }
  275. }
  276. /// <summary>
  277. /// Gets the size of the horizontal scrollbar viewport.
  278. /// </summary>
  279. protected double HorizontalScrollBarViewportSize
  280. {
  281. get { return _viewport.Width; }
  282. }
  283. /// <summary>
  284. /// Gets the maximum vertical scrollbar value.
  285. /// </summary>
  286. protected double VerticalScrollBarMaximum
  287. {
  288. get { return Max(_extent.Height - _viewport.Height, 0); }
  289. }
  290. /// <summary>
  291. /// Gets or sets the vertical scrollbar value.
  292. /// </summary>
  293. protected double VerticalScrollBarValue
  294. {
  295. get { return _offset.Y; }
  296. set
  297. {
  298. if (_offset.Y != value)
  299. {
  300. var old = Offset.Y;
  301. Offset = Offset.WithY(value);
  302. RaisePropertyChanged(VerticalScrollBarValueProperty, old, value);
  303. }
  304. }
  305. }
  306. /// <summary>
  307. /// Gets the size of the vertical scrollbar viewport.
  308. /// </summary>
  309. protected double VerticalScrollBarViewportSize
  310. {
  311. get { return _viewport.Height; }
  312. }
  313. /// <inheritdoc/>
  314. IControl IScrollAnchorProvider.CurrentAnchor => null; // TODO: Implement
  315. /// <summary>
  316. /// Gets the value of the HorizontalScrollBarVisibility attached property.
  317. /// </summary>
  318. /// <param name="control">The control to read the value from.</param>
  319. /// <returns>The value of the property.</returns>
  320. public static ScrollBarVisibility GetHorizontalScrollBarVisibility(Control control)
  321. {
  322. return control.GetValue(HorizontalScrollBarVisibilityProperty);
  323. }
  324. /// <summary>
  325. /// Gets the value of the HorizontalScrollBarVisibility attached property.
  326. /// </summary>
  327. /// <param name="control">The control to set the value on.</param>
  328. /// <param name="value">The value of the property.</param>
  329. public static void SetHorizontalScrollBarVisibility(Control control, ScrollBarVisibility value)
  330. {
  331. control.SetValue(HorizontalScrollBarVisibilityProperty, value);
  332. }
  333. /// <summary>
  334. /// Gets the value of the VerticalScrollBarVisibility attached property.
  335. /// </summary>
  336. /// <param name="control">The control to read the value from.</param>
  337. /// <returns>The value of the property.</returns>
  338. public static ScrollBarVisibility GetVerticalScrollBarVisibility(Control control)
  339. {
  340. return control.GetValue(VerticalScrollBarVisibilityProperty);
  341. }
  342. /// <summary>
  343. /// Gets the value of the VerticalScrollBarVisibility attached property.
  344. /// </summary>
  345. /// <param name="control">The control to set the value on.</param>
  346. /// <param name="value">The value of the property.</param>
  347. public static void SetVerticalScrollBarVisibility(Control control, ScrollBarVisibility value)
  348. {
  349. control.SetValue(VerticalScrollBarVisibilityProperty, value);
  350. }
  351. void IScrollAnchorProvider.RegisterAnchorCandidate(IControl element)
  352. {
  353. // TODO: Implement
  354. }
  355. void IScrollAnchorProvider.UnregisterAnchorCandidate(IControl element)
  356. {
  357. // TODO: Implement
  358. }
  359. internal static Vector CoerceOffset(Size extent, Size viewport, Vector offset)
  360. {
  361. var maxX = Math.Max(extent.Width - viewport.Width, 0);
  362. var maxY = Math.Max(extent.Height - viewport.Height, 0);
  363. return new Vector(Clamp(offset.X, 0, maxX), Clamp(offset.Y, 0, maxY));
  364. }
  365. private static double Clamp(double value, double min, double max)
  366. {
  367. return (value < min) ? min : (value > max) ? max : value;
  368. }
  369. private static double Max(double x, double y)
  370. {
  371. var result = Math.Max(x, y);
  372. return double.IsNaN(result) ? 0 : result;
  373. }
  374. private static Vector ValidateOffset(AvaloniaObject o, Vector value)
  375. {
  376. ScrollViewer scrollViewer = o as ScrollViewer;
  377. if (scrollViewer != null)
  378. {
  379. var extent = scrollViewer.Extent;
  380. var viewport = scrollViewer.Viewport;
  381. return CoerceOffset(extent, viewport, value);
  382. }
  383. else
  384. {
  385. return value;
  386. }
  387. }
  388. private void ScrollBarVisibilityChanged(AvaloniaPropertyChangedEventArgs e)
  389. {
  390. var wasEnabled = !ScrollBarVisibility.Disabled.Equals(e.OldValue);
  391. var isEnabled = !ScrollBarVisibility.Disabled.Equals(e.NewValue);
  392. if (wasEnabled != isEnabled)
  393. {
  394. if (e.Property == HorizontalScrollBarVisibilityProperty)
  395. {
  396. RaisePropertyChanged(
  397. CanHorizontallyScrollProperty,
  398. wasEnabled,
  399. isEnabled);
  400. }
  401. else if (e.Property == VerticalScrollBarVisibilityProperty)
  402. {
  403. RaisePropertyChanged(
  404. CanVerticallyScrollProperty,
  405. wasEnabled,
  406. isEnabled);
  407. }
  408. }
  409. }
  410. private void CalculatedPropertiesChanged()
  411. {
  412. // Pass old values of 0 here because we don't have the old values at this point,
  413. // and it shouldn't matter as only the template uses these properies.
  414. RaisePropertyChanged(HorizontalScrollBarMaximumProperty, 0, HorizontalScrollBarMaximum);
  415. RaisePropertyChanged(HorizontalScrollBarValueProperty, 0, HorizontalScrollBarValue);
  416. RaisePropertyChanged(HorizontalScrollBarViewportSizeProperty, 0, HorizontalScrollBarViewportSize);
  417. RaisePropertyChanged(VerticalScrollBarMaximumProperty, 0, VerticalScrollBarMaximum);
  418. RaisePropertyChanged(VerticalScrollBarValueProperty, 0, VerticalScrollBarValue);
  419. RaisePropertyChanged(VerticalScrollBarViewportSizeProperty, 0, VerticalScrollBarViewportSize);
  420. }
  421. protected override void OnKeyDown(KeyEventArgs e)
  422. {
  423. if (e.Key == Key.PageUp)
  424. {
  425. VerticalScrollBarValue = Math.Max(_offset.Y - _viewport.Height, 0);
  426. e.Handled = true;
  427. }
  428. else if (e.Key == Key.PageDown)
  429. {
  430. VerticalScrollBarValue = Math.Min(_offset.Y + _viewport.Height, VerticalScrollBarMaximum);
  431. e.Handled = true;
  432. }
  433. }
  434. }
  435. }