Skip to content

Commit 5c91ef8

Browse files
committed
VirtualizingStackPanel: support index of currently realizing item
1 parent f9cc88e commit 5c91ef8

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/Avalonia.Controls/VirtualizingStackPanel.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ public class VirtualizingStackPanel : VirtualizingPanel, IScrollSnapPointsInfo
7272
private Dictionary<object, Stack<Control>>? _recyclePool;
7373
private Control? _focusedElement;
7474
private int _focusedIndex = -1;
75+
private Control? _realizingElement;
76+
private int _realizingIndex = -1;
7577

7678
public VirtualizingStackPanel()
7779
{
@@ -336,6 +338,8 @@ protected override void OnItemsChanged(IReadOnlyList<object?> items, NotifyColle
336338
return _scrollToElement;
337339
if (_focusedIndex == index)
338340
return _focusedElement;
341+
if (index == _realizingIndex)
342+
return _realizingElement;
339343
if (GetRealizedElement(index) is { } realized)
340344
return realized;
341345
if (Items[index] is Control c && c.GetValue(RecycleKeyProperty) == s_itemIsItsOwnContainer)
@@ -349,6 +353,8 @@ protected internal override int IndexFromContainer(Control container)
349353
return _scrollToIndex;
350354
if (container == _focusedElement)
351355
return _focusedIndex;
356+
if (container == _realizingElement)
357+
return _realizingIndex;
352358
return _realizedElements?.GetIndex(container) ?? -1;
353359
}
354360

@@ -532,7 +538,9 @@ private void RealizeElements(
532538
// Start at the anchor element and move forwards, realizing elements.
533539
do
534540
{
541+
_realizingIndex = index;
535542
var e = GetOrCreateElement(items, index);
543+
_realizingElement = e;
536544
e.Measure(availableSize);
537545

538546
var sizeU = horizontal ? e.DesiredSize.Width : e.DesiredSize.Height;
@@ -543,6 +551,8 @@ private void RealizeElements(
543551

544552
u += sizeU;
545553
++index;
554+
_realizingIndex = -1;
555+
_realizingElement = null;
546556
} while (u < viewport.viewportUEnd && index < items.Count);
547557

548558
// Store the last index and end U position for the desired size calculation.

0 commit comments

Comments
 (0)