Skip to content

fix: StackLayout.GetExtent clamps extent origin to 0, breaking ItemsRepeater's negative-offset correction #23042

Closed

Description

@MartinZikmund

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:

  1. ViewportManagerWithPlatformFeatures::SetLayoutExtent compares the new extent to m_layoutExtent and accumulates m_expectedViewportShift += m_layoutExtent.X/Y - extent.X/Y. A negative new origin produces a positive expected shift.
  2. 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.
  3. If the scroller cannot service the shift (already pinned at 0, or — as today in Uno — the anchor provider is not implemented), OnLayoutUpdated accumulates the unserviced amount into m_unshiftableShift, which is then added back in GetLayoutVisibleWindow (see ViewportManagerWithPlatformFeatures.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_expectedViewportShift for the variable-size-correction case.
  • Never triggers the m_unshiftableShift absorption path.
  • Produces ExtentHeight / ScrollableHeight values 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 returns null from CurrentAnchor), OnLayoutUpdated accumulates into m_unshiftableShift and GetLayoutVisibleWindow applies it on the next measure pass.
  • Watch for races: OnLayoutUpdated currently registers only when |expectedViewportShift| > 1 (see ViewportManagerWithPlatformFeatures.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 from StackLayout.cs.
  • Existing When_ItemSignificantlyTaller_Then_VirtualizeProperly continues 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 SamplesApp page with mixed tall/short items matches WinUI: no top blank, no overlapping items, first item flush at 0 when scrolled to the top.

Related

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

  1. added 5 commits that reference this issue on Apr 23, 2026
    3955be1
    0f6f5ca
    14c466b
    f5cfeba
    85ed46e
  2. added 5 commits that reference this issue on May 7, 2026
    f71a588
    8939f8b
    1d4c3d7
    797ca0d
    cf780d3
  3. added a commit that references this issue on May 14, 2026
    3b7f02f
  4. added 3 commits that reference this issue on May 28, 2026
    52bcf55
    ab92f75
    c419e89
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions