From 70644f708b1c4ccd9ed61ee45f309fde2571b6c5 Mon Sep 17 00:00:00 2001 From: Julien Lebosquain Date: Fri, 3 Nov 2023 16:51:24 +0100 Subject: [PATCH] Remove viewport estimation from VirtualizingStackPanel (#13169) * Added failing VirtualizingStackPanel viewport test * Remove viewport estimation from VirtualizingStackPanel #Conflicts: # tests/Avalonia.Controls.UnitTests/VirtualizingStackPanelTests.cs --- .../VirtualizingStackPanel.cs | 34 ++----------------- 1 file changed, 3 insertions(+), 31 deletions(-) diff --git a/src/Avalonia.Controls/VirtualizingStackPanel.cs b/src/Avalonia.Controls/VirtualizingStackPanel.cs index a255b38bc8..d234bcbd0d 100644 --- a/src/Avalonia.Controls/VirtualizingStackPanel.cs +++ b/src/Avalonia.Controls/VirtualizingStackPanel.cs @@ -55,7 +55,6 @@ namespace Avalonia.Controls private static readonly AttachedProperty RecycleKeyProperty = AvaloniaProperty.RegisterAttached("RecycleKey"); - private static readonly Rect s_invalidViewport = new(double.PositiveInfinity, double.PositiveInfinity, 0, 0); private static readonly object s_itemIsItsOwnContainer = new object(); private readonly Action _recycleElement; private readonly Action _recycleElementOnItemRemoved; @@ -68,7 +67,7 @@ namespace Avalonia.Controls private RealizedStackElements? _measureElements; private RealizedStackElements? _realizedElements; private IScrollAnchorProvider? _scrollAnchorProvider; - private Rect _viewport = s_invalidViewport; + private Rect _viewport; private Dictionary>? _recyclePool; private Control? _focusedElement; private int _focusedIndex = -1; @@ -450,18 +449,17 @@ namespace Avalonia.Controls // If the control has not yet been laid out then the effective viewport won't have been set. // Try to work it out from an ancestor control. - var viewport = _viewport != s_invalidViewport ? _viewport : EstimateViewport(); + var viewport = _viewport; // Get the viewport in the orientation direction. var viewportStart = Orientation == Orientation.Horizontal ? viewport.X : viewport.Y; var viewportEnd = Orientation == Orientation.Horizontal ? viewport.Right : viewport.Bottom; // Get or estimate the anchor element from which to start realization. - var itemCount = items?.Count ?? 0; var (anchorIndex, anchorU) = _realizedElements.GetOrEstimateAnchorElementForViewport( viewportStart, viewportEnd, - itemCount, + items.Count, ref _lastEstimatedElementSizeU); // Check if the anchor element is not within the currently realized elements. @@ -503,32 +501,6 @@ namespace Avalonia.Controls return _lastEstimatedElementSizeU; } - private Rect EstimateViewport() - { - var c = this.GetVisualParent(); - var viewport = new Rect(); - - if (c is null) - { - return viewport; - } - - while (c is not null) - { - if ((c.Bounds.Width != 0 || c.Bounds.Height != 0) && - c.TransformToVisual(this) is Matrix transform) - { - viewport = new Rect(0, 0, c.Bounds.Width, c.Bounds.Height) - .TransformToAABB(transform); - break; - } - - c = c?.GetVisualParent(); - } - - return viewport.Intersect(new Rect(0, 0, double.PositiveInfinity, double.PositiveInfinity)); - } - private void RealizeElements( IReadOnlyList items, Size availableSize,