ItemVirtualizer.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 System.Collections;
  5. using System.Collections.Specialized;
  6. using System.Reactive.Linq;
  7. using Avalonia.Controls.Primitives;
  8. using Avalonia.Controls.Utils;
  9. using Avalonia.Input;
  10. namespace Avalonia.Controls.Presenters
  11. {
  12. /// <summary>
  13. /// Base class for classes which handle virtualization for an <see cref="ItemsPresenter"/>.
  14. /// </summary>
  15. internal abstract class ItemVirtualizer : IVirtualizingController, IDisposable
  16. {
  17. private double _crossAxisOffset;
  18. private IDisposable _subscriptions;
  19. /// <summary>
  20. /// Initializes a new instance of the <see cref="ItemVirtualizer"/> class.
  21. /// </summary>
  22. /// <param name="owner"></param>
  23. public ItemVirtualizer(ItemsPresenter owner)
  24. {
  25. Owner = owner;
  26. Items = owner.Items;
  27. ItemCount = owner.Items.Count();
  28. var panel = VirtualizingPanel;
  29. if (panel != null)
  30. {
  31. _subscriptions = panel.GetObservable(Panel.BoundsProperty)
  32. .Skip(1)
  33. .Subscribe(_ => InvalidateScroll());
  34. }
  35. }
  36. /// <summary>
  37. /// Gets the <see cref="ItemsPresenter"/> which owns the virtualizer.
  38. /// </summary>
  39. public ItemsPresenter Owner { get; }
  40. /// <summary>
  41. /// Gets the <see cref="IVirtualizingPanel"/> which will host the items.
  42. /// </summary>
  43. public IVirtualizingPanel VirtualizingPanel => Owner.Panel as IVirtualizingPanel;
  44. /// <summary>
  45. /// Gets the items to display.
  46. /// </summary>
  47. public IEnumerable Items { get; private set; }
  48. /// <summary>
  49. /// Gets the number of items in <see cref="Items"/>.
  50. /// </summary>
  51. public int ItemCount { get; private set; }
  52. /// <summary>
  53. /// Gets or sets the index of the first item displayed in the panel.
  54. /// </summary>
  55. public int FirstIndex { get; protected set; }
  56. /// <summary>
  57. /// Gets or sets the index of the first item beyond those displayed in the panel.
  58. /// </summary>
  59. public int NextIndex { get; protected set; }
  60. /// <summary>
  61. /// Gets a value indicating whether the items should be scroll horizontally or vertically.
  62. /// </summary>
  63. public bool Vertical => VirtualizingPanel?.ScrollDirection == Orientation.Vertical;
  64. /// <summary>
  65. /// Gets a value indicating whether logical scrolling is enabled.
  66. /// </summary>
  67. public abstract bool IsLogicalScrollEnabled { get; }
  68. /// <summary>
  69. /// Gets the value of the scroll extent.
  70. /// </summary>
  71. public abstract double ExtentValue { get; }
  72. /// <summary>
  73. /// Gets or sets the value of the current scroll offset.
  74. /// </summary>
  75. public abstract double OffsetValue { get; set; }
  76. /// <summary>
  77. /// Gets the value of the scrollable viewport.
  78. /// </summary>
  79. public abstract double ViewportValue { get; }
  80. /// <summary>
  81. /// Gets the <see cref="ExtentValue"/> as a <see cref="Size"/>.
  82. /// </summary>
  83. public Size Extent
  84. {
  85. get
  86. {
  87. return Vertical ?
  88. new Size(Owner.Panel.DesiredSize.Width, ExtentValue) :
  89. new Size(ExtentValue, Owner.Panel.DesiredSize.Height);
  90. }
  91. }
  92. /// <summary>
  93. /// Gets the <see cref="ViewportValue"/> as a <see cref="Size"/>.
  94. /// </summary>
  95. public Size Viewport
  96. {
  97. get
  98. {
  99. return Vertical ?
  100. new Size(Owner.Panel.Bounds.Width, ViewportValue) :
  101. new Size(ViewportValue, Owner.Panel.Bounds.Height);
  102. }
  103. }
  104. /// <summary>
  105. /// Gets or sets the <see cref="OffsetValue"/> as a <see cref="Vector"/>.
  106. /// </summary>
  107. public Vector Offset
  108. {
  109. get
  110. {
  111. return Vertical ? new Vector(_crossAxisOffset, OffsetValue) : new Vector(OffsetValue, _crossAxisOffset);
  112. }
  113. set
  114. {
  115. var oldCrossAxisOffset = _crossAxisOffset;
  116. if (Vertical)
  117. {
  118. OffsetValue = value.Y;
  119. _crossAxisOffset = value.X;
  120. }
  121. else
  122. {
  123. OffsetValue = value.X;
  124. _crossAxisOffset = value.Y;
  125. }
  126. if (_crossAxisOffset != oldCrossAxisOffset)
  127. {
  128. Owner.InvalidateArrange();
  129. }
  130. }
  131. }
  132. /// <summary>
  133. /// Creates an <see cref="ItemVirtualizer"/> based on an item presenter's
  134. /// <see cref="ItemVirtualizationMode"/>.
  135. /// </summary>
  136. /// <param name="owner">The items presenter.</param>
  137. /// <returns>An <see cref="ItemVirtualizer"/>.</returns>
  138. public static ItemVirtualizer Create(ItemsPresenter owner)
  139. {
  140. if (owner.Panel == null)
  141. {
  142. return null;
  143. }
  144. var virtualizingPanel = owner.Panel as IVirtualizingPanel;
  145. var scrollable = (ILogicalScrollable)owner;
  146. ItemVirtualizer result = null;
  147. if (virtualizingPanel != null && scrollable.InvalidateScroll != null)
  148. {
  149. switch (owner.VirtualizationMode)
  150. {
  151. case ItemVirtualizationMode.Simple:
  152. result = new ItemVirtualizerSimple(owner);
  153. break;
  154. }
  155. }
  156. if (result == null)
  157. {
  158. result = new ItemVirtualizerNone(owner);
  159. }
  160. if (virtualizingPanel != null)
  161. {
  162. virtualizingPanel.Controller = result;
  163. }
  164. return result;
  165. }
  166. /// <summary>
  167. /// Carries out a measure for the related <see cref="ItemsPresenter"/>.
  168. /// </summary>
  169. /// <param name="availableSize">The size available to the control.</param>
  170. /// <returns>The desired size for the control.</returns>
  171. public virtual Size MeasureOverride(Size availableSize)
  172. {
  173. Owner.Panel.Measure(availableSize);
  174. return Owner.Panel.DesiredSize;
  175. }
  176. /// <summary>
  177. /// Carries out an arrange for the related <see cref="ItemsPresenter"/>.
  178. /// </summary>
  179. /// <param name="finalSize">The size available to the control.</param>
  180. /// <returns>The actual size used.</returns>
  181. public virtual Size ArrangeOverride(Size finalSize)
  182. {
  183. if (VirtualizingPanel != null)
  184. {
  185. VirtualizingPanel.CrossAxisOffset = _crossAxisOffset;
  186. Owner.Panel.Arrange(new Rect(finalSize));
  187. }
  188. else
  189. {
  190. var origin = Vertical ? new Point(-_crossAxisOffset, 0) : new Point(0, _crossAxisOffset);
  191. Owner.Panel.Arrange(new Rect(origin, finalSize));
  192. }
  193. return finalSize;
  194. }
  195. /// <inheritdoc/>
  196. public virtual void UpdateControls()
  197. {
  198. }
  199. /// <summary>
  200. /// Gets the next control in the specified direction.
  201. /// </summary>
  202. /// <param name="direction">The movement direction.</param>
  203. /// <param name="from">The control from which movement begins.</param>
  204. /// <returns>The control.</returns>
  205. public virtual IControl GetControlInDirection(NavigationDirection direction, IControl from)
  206. {
  207. return null;
  208. }
  209. /// <summary>
  210. /// Called when the items for the presenter change, either because
  211. /// <see cref="ItemsPresenterBase.Items"/> has been set, the items collection has been
  212. /// modified, or the panel has been created.
  213. /// </summary>
  214. /// <param name="items">The items.</param>
  215. /// <param name="e">A description of the change.</param>
  216. public virtual void ItemsChanged(IEnumerable items, NotifyCollectionChangedEventArgs e)
  217. {
  218. Items = items;
  219. ItemCount = items.Count();
  220. }
  221. /// <summary>
  222. /// Scrolls the specified item into view.
  223. /// </summary>
  224. /// <param name="item">The item.</param>
  225. public virtual void ScrollIntoView(object item)
  226. {
  227. }
  228. /// <inheritdoc/>
  229. public virtual void Dispose()
  230. {
  231. _subscriptions?.Dispose();
  232. _subscriptions = null;
  233. if (VirtualizingPanel != null)
  234. {
  235. VirtualizingPanel.Controller = null;
  236. VirtualizingPanel.Children.Clear();
  237. }
  238. Owner.ItemContainerGenerator.Clear();
  239. }
  240. /// <summary>
  241. /// Invalidates the current scroll.
  242. /// </summary>
  243. protected void InvalidateScroll() => ((ILogicalScrollable)Owner).InvalidateScroll();
  244. }
  245. }