Repository navigation
Description
Summary
StackLayout.GetExtent in Uno contains a workaround that clamps the extent's major-axis start to >= 0. This is not present in WinUI and it breaks the estimation-correction mechanism that ItemsRepeater relies on to stay accurate when realized item sizes diverge from the running average. Removing this clamp (and ensuring the supporting ViewportManagerWithPlatformFeatures / anchor-provider pipeline absorbs the resulting negative origin correctly) is required to fix fast-scroll rendering glitches and "cannot scroll to top" problems with variable-height content.
Current behavior (the divergence)
Uno — src/Uno.UI/UI/Xaml/Controls/Repeater/StackLayout.cs:182-190:
if (firstRealized != null)
{
MUX_ASSERT(lastRealized != null);
var firstRealizedMajor = (float)(MajorStart(firstRealizedLayoutBounds) - firstRealizedItemIndex * averageElementSize);
// Uno workaround [BEGIN]: Make sure to not move items above the viewport. This can be the case if an items is significantly higher than previous items (will increase the average items size)
firstRealizedMajor = Math.Max(0.0f, firstRealizedMajor);
// Uno workaround [END]
SetMajorStart(ref extent, firstRealizedMajor);
var remainingItems = itemsCount - lastRealizedItemIndex - 1;
SetMajorSize(ref extent, MajorEnd(lastRealizedLayoutBounds) - MajorStart(extent) + (float)(remainingItems * averageElementSize));
}WinUI — src/controls/dev/Repeater/StackLayout.cpp:249-256:
if (firstRealized)
{
MUX_ASSERT(lastRealized);
MajorStart(extent) = static_cast<float>(MajorStart(firstRealizedLayoutBounds) - firstRealizedItemIndex * averageElementSize);
const auto remainingItems = itemsCount - lastRealizedItemIndex - 1;
MajorSize(extent) = MajorEnd(lastRealizedLayoutBounds) - MajorStart(extent) + static_cast<float>(remainingItems * averageElementSize);
}The clamp was introduced in commit 08f9b15fcf (March 19, 2024):
fix(itemsRepeater): Fix an issue where IR might re-position first items if items below are significantly higher
It replaces a symptom (first items visibly re-positioning in certain scenarios) with a structural break of the anchor-shift correction pipeline.
Why the clamp is wrong
WinUI's design: when realized item sizes diverge from the running average, GetExtent returns an extent whose major-axis start is negative. That negative value is the signal the pipeline uses to correct:
ViewportManagerWithPlatformFeatures::SetLayoutExtentcompares the new extent tom_layoutExtentand accumulatesm_expectedViewportShift += m_layoutExtent.X/Y - extent.X/Y. A negative new origin produces a positive expected shift.- The scroller (via
IScrollAnchorProvider.CurrentAnchor) is asked to compensate so the anchor element stays visually fixed. This is how WinUI keeps visible items stable while realized content grows above/below. - If the scroller cannot service the shift (already pinned at 0, or — as today in Uno — the anchor provider is not implemented),
OnLayoutUpdatedaccumulates the unserviced amount intom_unshiftableShift, which is then added back inGetLayoutVisibleWindow(seeViewportManagerWithPlatformFeatures.cs:163-194) so the realization rect is offset and items are drawn at the correct visual positions.
By clamping to 0, Uno:
- Never produces a negative extent origin.
- Never accumulates
m_expectedViewportShiftfor the variable-size-correction case. - Never triggers the
m_unshiftableShiftabsorption path. - Produces
ExtentHeight/ScrollableHeightvalues that are inconsistent with real child positions, which causes: items overlapping on fast scroll; first items not flush at offset 0 after scroll-to-top; content appearing cropped at the top of variable-height lists.
Steps to reproduce
See the runtime tests proposed in #23041 (test: Add runtime tests for ItemsRepeater fast-scroll...) — specifically Test A (When_FastScrollWithHighVarianceItems_Then_NoOverlap) and Test B (When_ScrollToTop_AfterVariableScrolling_Then_FirstItemFlushAtZero). The tests fail today on Skia Desktop and WebAssembly.
Minimal XAML repro (any variable-height content will do):
<ScrollViewer x:Name="Host" Height="500">
<ItemsRepeater x:Name="Repeater" ItemsSource="{Binding Items}">
<ItemsRepeater.Layout>
<StackLayout Orientation="Vertical" />
</ItemsRepeater.Layout>
<ItemsRepeater.ItemTemplate>
<DataTemplate>
<!-- Height bound: mix of 40 and 1200 every Nth item -->
<Border Height="{Binding Height}" Background="{Binding Color}" />
</DataTemplate>
</ItemsRepeater.ItemTemplate>
</ItemsRepeater>
</ScrollViewer>Then call Host.ChangeView(null, 5000, null); followed by Host.ChangeView(null, 0, null); and observe either item overlap or a blank band at the top.
Proposed fix
Step 1 — Remove the clamp.
- var firstRealizedMajor = (float)(MajorStart(firstRealizedLayoutBounds) - firstRealizedItemIndex * averageElementSize);
- // Uno workaround [BEGIN]: Make sure to not move items above the viewport. ...
- firstRealizedMajor = Math.Max(0.0f, firstRealizedMajor);
- // Uno workaround [END]
- SetMajorStart(ref extent, firstRealizedMajor);
+ SetMajorStart(ref extent, (float)(MajorStart(firstRealizedLayoutBounds) - firstRealizedItemIndex * averageElementSize));Step 2 — Ensure the fallback absorption path is green.
- Confirm via targeted test that, when the parent scroller is a classic
ScrollViewer(which today returnsnullfromCurrentAnchor),OnLayoutUpdatedaccumulates intom_unshiftableShiftandGetLayoutVisibleWindowapplies it on the next measure pass. - Watch for races:
OnLayoutUpdatedcurrently registers only when|expectedViewportShift| > 1(seeViewportManagerWithPlatformFeatures.cs:227). Verify that very small per-measure deltas can accumulate across measure passes without missing the registration.
Step 3 — Re-green the original regression.
The issue that commit 08f9b15fcf claimed to fix (When_ItemSignificantlyTaller_Then_VirtualizeProperly, Given_ItemsRepeater.cs:413) must still pass with the clamp removed. If it does not, the fix is in the anchor-provider / unshiftable-shift pipeline, not in the layout. See #23043 (feat: Implement IScrollAnchorProvider on classic ScrollViewer) — the clamp was arguably a symptomatic fix because the anchor provider is missing, so Step 2 plus that companion feature are the principled resolution.
Acceptance
Math.Max(0.0f, firstRealizedMajor)(and the paired "Uno workaround [BEGIN/END]" comment block) removed fromStackLayout.cs.- Existing
When_ItemSignificantlyTaller_Then_VirtualizeProperlycontinues to pass on Skia Desktop. - New fast-scroll / scroll-to-top runtime tests from test: Add runtime tests for ItemsRepeater fast-scroll and scroll-to-top with high-variance item heights #23041 pass on Skia Desktop + WebAssembly.
- Visual comparison on
SamplesApppage with mixed tall/short items matches WinUI: no top blank, no overlapping items, first item flush at0when scrolled to the top.
Related
- Depends on / validated by: test: Add runtime tests for ItemsRepeater fast-scroll and scroll-to-top with high-variance item heights #23041 (runtime tests).
- Works together with: feat: Implement IScrollAnchorProvider on classic ScrollViewer (CurrentAnchor, RegisterAnchorCandidate, anchor-tracking across layout) #23043 (
feat: Implement IScrollAnchorProvider on classic ScrollViewer). - Follow-up: chore: Resync ItemsRepeater / StackLayout / FlowLayoutAlgorithm / ViewportManagerWithPlatformFeatures from WinUI #23044 (
chore: Resync ItemsRepeater from WinUI).
References
- Uno:
src/Uno.UI/UI/Xaml/Controls/Repeater/StackLayout.cs:125-211. - Uno:
src/Uno.UI/UI/Xaml/Controls/Repeater/ViewportManagerWithPlatformFeatures.cs:160-260, 354-382. - WinUI:
src/controls/dev/Repeater/StackLayout.cpp:132-285. - WinUI:
src/controls/dev/Repeater/ViewportManagerWithPlatformFeatures.cpp:226-607.
Activity
fix(itemsrepeater): Remove StackLayout.GetExtent origin clamp (fixes #…
fix(itemsrepeater): Remove StackLayout.GetExtent origin clamp (fixes #…
fix(itemsrepeater): Remove StackLayout.GetExtent origin clamp (fixes #…
fix(itemsrepeater): Remove StackLayout.GetExtent origin clamp (fixes #…
fix(itemsrepeater): Remove StackLayout.GetExtent origin clamp (fixes #…
fix(itemsrepeater): Remove StackLayout.GetExtent origin clamp (fixes #…
fix(scrollviewer): Preserve offset intent across virtualization exten…
fix(itemsrepeater): Remove StackLayout.GetExtent origin clamp (fixes #…
fix(scrollviewer): Restore WinUI parity for ItemsRepeater scroll beha…
fix(itemsrepeater): Stabilize StackLayout origin to suppress wheel-sc…
fix(scrollviewer): Preserve offset intent across virtualization exten…
fix(itemsrepeater): Remove StackLayout.GetExtent origin clamp (fixes u…
fix(itemsrepeater): Remove StackLayout.GetExtent origin clamp (fixes #…
fix(scrollviewer): Preserve offset intent across virtualization exten…
fix(itemsrepeater): Stabilize StackLayout origin to suppress wheel-sc…