Repository navigation
feat(scrollviewer): Implement CurrentAnchor and scroll anchoring - #23053
Conversation
There was a problem hiding this comment.
Pull request overview
Implements classic Microsoft.UI.Xaml.Controls.ScrollViewer scroll anchoring on Uno by porting WinUI’s anchor-selection and offset-correction logic, enabling IScrollAnchorProvider scenarios (e.g., chat/log views and ItemsRepeater viewport stabilization).
Changes:
- Add
CurrentAnchor, anchor candidate registration APIs, andAnchorRequestedevent, plus arrange-time anchoring/offset correction logic. - Integrate anchoring with layout by forcing an arrange pass when
ScrollContentPresenteris re-measured while anchoring is active. - Add runtime tests (
Given_ScrollViewer_Anchoring) and a SamplesApp UI test page demonstrating anchoring behavior.
Reviewed changes
Copilot reviewed 8 out of 11 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Uno.UI/UI/Xaml/Controls/ScrollViewer/ScrollViewer.cs | Routes ArrangeOverride through anchoring orchestration and removes old CurrentAnchor stub. |
| src/Uno.UI/UI/Xaml/Controls/ScrollViewer/ScrollViewer.Anchoring.cs | New anchoring implementation: candidate management, anchor selection, pre/post-arrange distance diff, and offset correction. |
| src/Uno.UI/UI/Xaml/Controls/ScrollViewer/AnchorRequestedEventArgs.cs | Adds managed AnchorRequestedEventArgs with mutable AnchorCandidates. |
| src/Uno.UI/UI/Xaml/Controls/ScrollContentPresenter/ScrollContentPresenter.cs | Calls back into ScrollViewer after measure to invalidate arrange when anchoring is active. |
| src/Uno.UI/Generated/3.0.0.0/Microsoft.UI.Xaml.Controls/ScrollViewer.cs | Removes generated stubs for Register/Unregister and AnchorRequested event (now implemented in partial). |
| src/Uno.UI/Generated/3.0.0.0/Microsoft.UI.Xaml.Controls/IScrollAnchorProvider.cs | Removes platform-guarded NotImplemented gating so the interface is always available. |
| src/Uno.UI/Generated/3.0.0.0/Microsoft.UI.Xaml.Controls/AnchorRequestedEventArgs.cs | Removes generated stub type (now provided by real implementation). |
| src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/ScrollViewerTests/Given_ScrollViewer_Anchoring.cs | Adds runtime tests covering selection, events, duplicate semantics, and position preservation. |
| src/SamplesApp/UITests.Shared/Windows_UI_Xaml_Controls/ScrollViewerTests/ScrollViewer_Anchoring.xaml.cs | Adds a sample page to manually exercise anchoring behavior. |
| src/SamplesApp/UITests.Shared/Windows_UI_Xaml_Controls/ScrollViewerTests/ScrollViewer_Anchoring.xaml | XAML UI for the anchoring demo (ratio picker, insert/remove above/below, top/bottom buttons). |
| src/SamplesApp/UITests.Shared/UITests.Shared.projitems | Includes the new sample XAML + code-behind in the shared project items. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-23053/wasm-skia-net9/index.html |
|
|
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-23053/wasm-skia-net9/index.html |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 8 out of 11 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-23053/wasm-skia-net9/index.html |
|
@copilot resolve the merge conflicts in this pull request |
Head branch was pushed to by a user without write access
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-23053/wasm-skia-net9/index.html |
|
|
Cover CurrentAnchor selection across ratios, register/unregister (including WinUI's allow-duplicate semantics), AnchorRequested event and handler overrides, position preservation on content grow/shrink, far-edge anchoring, collapsed and non-descendant candidate exclusion, and the IScrollAnchorProvider cast path. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Port the scroll-anchoring flow from microsoft-ui-xaml2 ScrollViewer_Partial.cpp: CurrentAnchor getter, Register/Unregister/ClearAnchorCandidates, AnchorRequested event with AnchorRequestedEventArgs, IsAnchoring, ComputeAnchorPoint / ComputeViewportAnchorPoint / ComputeElementAnchorPoint, ComputeViewportToElementAnchorPointsDistance, EnsureAnchorElementSelection, ProcessAnchorCandidate, GetDescendantBounds, IsElementValidAnchor, PerformPositionAdjustment, and the ArrangeOverride orchestration. ScrollContentPresenter.MeasureOverride now calls back to ScrollViewer.OnScrollContentPresenterMeasured (mirror of ScrollViewer_Partial.cpp:9440) which invalidates arrange when anchoring is active, so ArrangeOverride re-fires on grandchild extent changes. Fixes #4548, fixes #23043. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Interactive sample that exercises VerticalAnchorRatio and CurrentAnchor: insert/remove items above and below the viewport anchor, switch between NaN / 0.0 / 0.5 / 1.0 ratios, and watch the live CurrentAnchor / offset readout. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
9494f35 to
58f8032
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 8 out of 10 changed files in this pull request and generated 4 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 9 out of 12 changed files in this pull request and generated 5 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Code reviewFound 4 issues (focus: logic accuracy vs WinUI C++
uno/src/Uno.UI/Generated/3.0.0.0/Microsoft.UI.Xaml.Controls/ScrollViewer.cs Lines 137 to 144 in 2344233
uno/src/Uno.UI/UI/Xaml/Controls/ScrollViewer/ScrollViewer.Anchoring.cs Lines 250 to 295 in 2344233
uno/src/Uno.UI/UI/Xaml/Controls/ScrollViewer/ScrollViewer.Anchoring.cs Lines 113 to 167 in 2344233
uno/src/Uno.UI/UI/Xaml/Controls/ScrollViewer/ScrollViewer.Anchoring.cs Lines 468 to 478 in 2344233 uno/src/Uno.UI/UI/Xaml/Controls/ScrollViewer/ScrollViewer.cs Lines 695 to 708 in 2344233 🤖 Generated with Claude Code - If this code review was useful, please react with 👍. Otherwise, react with 👎. |
|
|
🤖 Addressed PR comments up to this pointCode changes:
Already resolved before this pass (by commit All 17 |
WinUI never calls ClearAnchorCandidates internally either; matching that keeps the Uno port faithful and resolves IDE0051.
|
|
Summary
Implements
ScrollViewer.CurrentAnchor,RegisterAnchorCandidate/UnregisterAnchorCandidate, theAnchorRequestedevent, and honorsHorizontalAnchorRatio/VerticalAnchorRatio— previously stubbed with[NotImplemented]. With this change, aScrollViewerthat has an anchor ratio set will preserve the visual position of its chosen anchor element when content above it grows or shrinks (the classic "chat window" / "log view" use case).Ported line-for-line from
microsoft-ui-xaml2/src/dxaml/xcp/dxaml/lib/ScrollViewer_Partial.cppusing/winui-port:CurrentAnchorgetter (get_CurrentAnchorImpl)RegisterAnchorCandidateImpl/UnregisterAnchorCandidateImpl(preserves WinUI's allow-duplicate release-build semantics)RaiseAnchorRequested+AnchorRequestedEventArgs(withAnchorand mutableAnchorCandidates)IsAnchoring,ComputeAnchorPoint/ComputeViewportAnchorPoint/ComputeElementAnchorPointComputeViewportToElementAnchorPointsDistanceEnsureAnchorElementSelection,ProcessAnchorCandidateGetDescendantBounds,IsElementValidAnchorPerformPositionAdjustmentArrangeOverrideorchestration (pre-distance → baseArrange → post-distance → offset correction, plus far-edge anchoring against the cached extent/viewport)To match WinUI's behavior on Uno,
ScrollContentPresenter.MeasureOverridenow callsScrollViewer.OnScrollContentPresenterMeasured(mirror ofScrollViewer_Partial.cpp:9440), which invalidates arrange when anchoring is active. Without this hook, Uno's layout short-circuit would skipArrangeCorewhen the SV's ownfinalRectis unchanged, and the anchoring flow would not fire on grandchild extent changes.Scope
ScrollContentPresenterpipeline (Skia + WASM).CurrentAnchor/Register/Unregister/AnchorRequestedsurface is available on all platforms.CanContentRenderOutsideBounds(still stubbed — separate feature), zoom-factor coupling during anchoring (managed SV'sZoomFactoris always 1), integration with native scroll containers on Android/iOS.Test plan
Given_ScrollViewer_Anchoring) covering:CurrentAnchornull cases (no ratios, no candidates)CurrentAnchorupdates on scrollAnchorRequestedfiring, handler anchor override, handler candidate-list mutationVerticalAnchorRatio == 1.0) and near-edge no-op (== 0.0)IScrollAnchorProvidercast/winui-runtime-tests(validates tests encode correct reference behavior)./runtime-tests.Given_ScrollViewerregression suite: no new failures (four pre-existing flaky tests unchanged, confirmed viagit stashbaseline).SamplesApp.Skia.Genericvia the newScrollViewer_Anchoringsample (insert above / remove above / ratio picker, liveCurrentAnchorand offset readout).Closes #4548
Closes #23043
🤖 Generated with Claude Code