Skip to content

ScrollViewer on Skia jumps back to its pre-clamp offset when content grows (or viewport shrinks) again #26404

Open

Description

@MartinZikmund

Current behavior 🐛

Severity: Medium - any app whose scrolled content shrinks and grows again (collapsing sections, reloading or filtering content, window resizes) sees the ScrollViewer jump back to an old position the user has already left: wrong scroll state, no crash.

On Skia, after a programmatic ChangeView, the ScrollViewer remembers the requested offset. When the content (or the viewport) changes so the offset has to be clamped, the clamp is applied, but the old requested offset stays armed. As soon as the extent allows it again, the ScrollViewer scrolls back to that old offset.

Step WinUI Uno (Skia)
ChangeView(null, 250, null, true) (content 600, viewport 300) V=250 V=250
content Height = 400 (clamp) V=100 V=100
content Height = 1000 V=100 V=250 (jumps back)
ChangeView(null, 700, ...) (content 1000, viewport 300) V=700 V=700
ScrollViewer Height = 500 (clamp) V=500 V=500
ScrollViewer Height = 200 V=500 V=700 (jumps back)

The remembered offset is only dropped by user input (wheel/touch), so the jump-back can happen long after the ChangeView call.

Expected behavior 🎯

Like WinUI: once the offset has been clamped because the content or viewport changed, the clamped value is the new offset. Growing the content (or shrinking the viewport) again must not scroll back to the pre-clamp offset.

How to reproduce it (as minimally and precisely as possible) 🔬

  1. Create a ScrollViewer (300x300) hosting a Border (600x600) and wait for it to load.
  2. Call sv.ChangeView(null, 250, null, true); VerticalOffset is 250.
  3. Set content.Height = 400; VerticalOffset is clamped to 100.
  4. Set content.Height = 1000; VerticalOffset goes back to 250 (WinUI: stays 100).

Runtime test (fails on Skia Desktop with expected 100, actual 250 and expected 500, actual 700):

[TestMethod]
[RunsOnUIThread]
public async Task When_ContentShrinksThenGrows_Then_ClampedOffsetIsKept()
{
	var content = new Border { Width = 600, Height = 600 };
	var sv = new ScrollViewer { Width = 300, Height = 300, Content = content };
	WindowHelper.WindowContent = sv;
	await WindowHelper.WaitForLoaded(sv);
	await WindowHelper.WaitForIdle();

	sv.ChangeView(null, 250, null, true);
	await WindowHelper.WaitForIdle();
	Assert.AreEqual(250, sv.VerticalOffset, 0.5);

	content.Height = 400;
	await WindowHelper.WaitForIdle();
	Assert.AreEqual(100, sv.VerticalOffset, 0.5);

	content.Height = 1000;
	await WindowHelper.WaitForIdle();
	Assert.AreEqual(100, sv.VerticalOffset, 0.5); // Uno: 250
}

[TestMethod]
[RunsOnUIThread]
public async Task When_ViewportGrowsThenShrinks_Then_ClampedOffsetIsKept()
{
	var content = new Border { Width = 600, Height = 1000 };
	var sv = new ScrollViewer { Width = 300, Height = 300, Content = content };
	WindowHelper.WindowContent = sv;
	await WindowHelper.WaitForLoaded(sv);
	await WindowHelper.WaitForIdle();

	sv.ChangeView(null, 700, null, true);
	await WindowHelper.WaitForIdle();

	sv.Height = 500;
	await WindowHelper.WaitForIdle();
	Assert.AreEqual(500, sv.VerticalOffset, 0.5);

	sv.Height = 200;
	await WindowHelper.WaitForIdle();
	Assert.AreEqual(500, sv.VerticalOffset, 0.5); // Uno: 700
}

Workaround 🛠️

After changing the content size, re-apply the current offset so the remembered one is overwritten, e.g. sv.ChangeView(null, sv.VerticalOffset, null, true) right after the clamp has happened (after the next layout pass).

Renderer 🎨

  • Skia
  • Native

Affected platforms 📱💻🖥️

Desktop (Windows) - runtime-verified. The code path is the shared managed ScrollViewer, so all Skia targets (Desktop macOS/X11, WebAssembly, Android, iOS) are expected to behave the same; those were not run.

Uno.Sdk version (and other relevant versions) 📦

Source build of master at 46ea872, net11.0-desktop, .NET SDK 11.0.100. The behaviour was introduced by #23269 (commit 385b614, first shipped in 6.7.x).

IDE version 🧑‍💻

n/a (runtime test)

Anything else we need to know? 💬

What was verified how

  • Uno: runtime-verified on Skia Desktop (Win32) with the two runtime tests above, on master 46ea872. Both fail with the values in the table.
  • WinUI: the WinUI column comes from the same scenario run on native WinUI 3 (WinAppSDK 2.5.1), and is backed by the WinUI sources below.

Root cause (Uno)

#23269 (fixing #23041 / #23042) added an "offset intent" to ScrollViewer (_verticalOffsetIntent / _horizontalOffsetIntent), modelled on WinUI's m_Offset (requested) vs m_ComputedOffset (coerced) in ScrollContentPresenter. Every layout pass, RecomputeOffsetsFromIntent sets VerticalOffset = clamp(intent, 0, ScrollableHeight), and the intent itself is never updated by the clamp:

Before #23269, UpdateDimensionProperties called TrimOverscroll directly, which applied the clamp as the new offset with nothing remembered, matching WinUI. So this is a regression.

WinUI behaviour (source)

In WinUI, m_Offset and m_ComputedOffset only stay apart while a manipulation is in progress. ScrollContentPresenter::CoerceOffsets writes the clamped value to m_ComputedOffset (ScrollContentPresenter_Partial.cpp#L3170-L3207), and then ScrollViewer::InvalidateScrollInfoImpl, on an extent change that is not expected from a manipulation and outside a manipulation, calls SynchronizeScrollOffsets (ScrollViewer_Partial.cpp#L4803-L4823). That writes the computed offset back into m_Offset (ScrollViewer_Partial.cpp#L14411-L14433), so the pre-clamp request is forgotten.

Suggested fix (without regressing #23041 / #23042)

The intent exists so a request like ChangeView(ScrollableHeight) on a freshly loaded, high-variance ItemsRepeater still ends up at the bottom while the realization cascade briefly shrinks and regrows the extent. That case should keep working: the tests in Given_ItemsRepeater_FastScroll (When_BottomClickedFromInitialState_Then_LastItemFlushAtViewportBottom, When_WheelScrollDownThroughVarianceList_Then_OffsetMonotonicallyAdvances) must stay green.

The intent should be a short-lived "request in flight", not a permanent memory. Some options:

  • Once the request has been seated (VerticalOffset reached the intent and layout settled), drop the intent. From then on, a clamp caused by a content or viewport change becomes the new offset, which matches WinUI's SynchronizeScrollOffsets.
  • Or, when the clamp happens because the SV's own viewport changed, or the content's size changed outside a realization pass, sync the intent to the clamped value (_verticalOffsetIntent = VerticalOffset) instead of keeping the old one.
  • Keep the long-lived intent only where it is needed, i.e. while virtualizing content with anchor candidates is still realizing after the request.

Both scenarios in the tests above should then pass, with the ItemsRepeater tests unchanged.

Related

#26381 covers a different symptom in the same area: ListView keeping its old offset after ItemsSource is replaced. This issue is only about the plain ScrollViewer offset coming back after a clamp.

Activity

  1. added
    kind/bug
    triage/untriaged
    kind/regression
    area/skia ✏️
    project/layout 🧱
    area/scrollviewer ⏬
    difficulty/medium 🤔
    platform/win32 🪟
    on Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/scrollviewer ⏬area/skia ✏️difficulty/medium 🤔kind/bugkind/regressionplatform/win32 🪟project/layout 🧱triage/untriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions