Repository navigation
[iOS] ScrollView: Complete element scroll requests inside collapsed branches #37409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
a8e8d6d
d79d09c
9f439ed
7306000
7f625b3
7e4f1bf
a3323ee
567c5a1
74e9c16
f02311a
6ec1c5a
c56e736
fa03bc0
386214e
efdf0d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,19 +71,29 @@ void DispatchPendingScrollToRequest() | |
| return; | ||
| } | ||
|
|
||
| if (pending.Mode == ScrollToMode.Element) | ||
| if (pending.Mode == ScrollToMode.Element && !IsElementTargetGeometryReady()) | ||
| { | ||
| if (!IsElementTargetGeometryReady()) | ||
| if (!WillArrange()) | ||
| { | ||
| // The request has to wait; OnSizeAllocated and ContentSizeChanged retry it. | ||
| return; | ||
| // A collapsed branch is skipped by layout, so no callback is coming to | ||
| // resolve the target. Release the caller now, but keep the request | ||
| // parked: if the branch is later shown, the first arrange replays it | ||
| // against real geometry so the scroll still lands on the element | ||
| // (a newer request supersedes it as usual). | ||
| SendScrollFinished(); | ||
| } | ||
|
|
||
| // Those callbacks run while the pass that produced the sizes is still arranging | ||
| // children, so resolve on the next tick, once positions are final. Posting on | ||
| // every retry is deliberate: SendPendingScrollToRequest is a no-op once the | ||
| // request has been sent or superseded, so a dropped callback cannot wedge the | ||
| // request the way an "already queued" flag would. | ||
| // The request has to wait; OnSizeAllocated and ContentSizeChanged retry it. | ||
| return; | ||
| } | ||
|
|
||
| if (pending.Mode == ScrollToMode.Element) | ||
| { | ||
| // The geometry callbacks run while the pass that produced the sizes is still | ||
| // arranging children, so resolve on the next tick, once positions are final. | ||
| // Posting on every retry is deliberate: SendPendingScrollToRequest is a no-op | ||
| // once the request has been sent or superseded, so a dropped callback cannot | ||
| // wedge the request the way an "already queued" flag would. | ||
| Dispatcher.Dispatch(SendPendingScrollToRequest); | ||
| return; | ||
| } | ||
|
|
@@ -101,6 +111,30 @@ void DispatchPendingScrollToRequest() | |
| bool IsElementTargetGeometryReady() => | ||
| Width >= 0 && Height >= 0 && Content is not ({ Width: < 0 } or { Height: < 0 }); | ||
|
|
||
| // A parked element request is retried only from OnSizeAllocated/ContentSizeChanged, | ||
| // which fire when this view gets arranged — and a view inside a collapsed | ||
| // (IsVisible=false) branch is skipped by layout entirely, so a request parked there | ||
| // has no callback coming. In that state the awaiting caller is released immediately | ||
| // while the request stays parked: showing the branch arranges it and replays the | ||
| // scroll against real geometry (see DispatchPendingScrollToRequest). | ||
| // The walk checks IsVisible on every VisualElement ancestor, skipping over non-visual | ||
| // links in the chain (e.g. Shell's ShellContent/ShellSection) rather than stopping at | ||
| // them. Non-visual containers' own visibility semantics (a hidden tab, say) are | ||
| // deliberately not consulted: parking keeps the scroll correct if that container is | ||
| // ever shown, and a handler detach still completes the task. | ||
| bool WillArrange() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
[moderate] Logic and Correctness —
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adopted in a3323ee, taking your "complete the parked request when the branch becomes collapsed" shape — and reversing my earlier rebuttal, honestly: park-and-complete changed the cost. The objection was that closing this ordering meant subscription machinery to replay the request on visibility transitions; now the request stays parked regardless and all a collapse has to do is release the caller, which is a one-line re-check. Both transitions with a real hook are covered: the ScrollView's own collapse ( One boundary stated in the code rather than papered over: an ancestor collapsing after the park sends no signal down the tree — layout managers simply skip the collapsed child and nothing reaches descendants (I checked; MAUI has no downward visibility propagation) — so that request is released on re-show or handler detach. Closing it would need per-ancestor
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
[moderate] Logic and Correctness —
No test covers either case — the five new unit tests all express "no arrange coming" as
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both cases resolved in fd0c039, by removing the proxy rather than widening it. (1) Reparent-to-null is real — I verified |
||
| { | ||
| for (Element element = this; element is not null; element = element.RealParent) | ||
| { | ||
| if (element is VisualElement { IsVisible: false }) | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| void SendPendingScrollToRequest() | ||
| { | ||
| if (Handler is null || _pendingScrollToRequested is not { } pending) | ||
|
|
@@ -535,6 +569,15 @@ void OnScrollToRequested(ScrollToRequestedEventArgs e) | |
| // subscribers were already notified above, so the replay must not re-raise. | ||
| _pendingScrollToRequested = e; | ||
| _replayPendingScrollToRequestedEvent = false; | ||
|
|
||
| if (!WillArrange()) | ||
| { | ||
| // A collapsed branch gets no layout callbacks, so parking alone would | ||
| // hang the caller. Complete the task now and leave the request parked: | ||
| // showing the branch arranges it and replays the scroll against real | ||
| // geometry. | ||
| SendScrollFinished(); | ||
| } | ||
| } | ||
| else | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -392,7 +392,7 @@ bool ValidateSafeArea() | |
| // it can push ContentSize over the Bounds, causing AdjustedContentInset to become non-zero and SafeAreaInsets on the child to reset to zero. | ||
| // This can result in a loop of invalidations as the layout toggles between these states. | ||
| // To prevent this, we ignore safe area calculations on child views when they are inside a scroll view. | ||
| if (SystemAdjustedContentInset == UIEdgeInsets.Zero || ContentInsetAdjustmentBehavior == UIScrollViewContentInsetAdjustmentBehavior.Never) | ||
| if (!UIKitCompensatesForSafeArea) | ||
| _safeArea = GetInset().ToSafeAreaInsets(); | ||
| else | ||
| _safeArea = SystemAdjustedContentInset.ToSafeAreaInsets(); | ||
|
|
@@ -481,6 +481,19 @@ internal CGSize ScrollableContentSize | |
| /// </summary> | ||
| CGRect? _arrangedContentRect; | ||
|
|
||
| /// <summary> | ||
| /// Whether UIKit is compensating for the safe area through | ||
| /// <see cref="UIScrollView.AdjustedContentInset"/>. When it is not | ||
| /// (<see cref="UIScrollViewContentInsetAdjustmentBehavior.Never"/>, or a zero system | ||
| /// inset), <see cref="CrossPlatformArrange"/> bakes the safe area into the content's | ||
| /// coordinate space instead. Shared by the arrange branch, the <c>_safeArea</c> source | ||
| /// selection in <see cref="ValidateSafeArea"/>, and | ||
| /// <see cref="SafeAreaBakedIntoContent"/> so the sites can never desynchronize. | ||
| /// </summary> | ||
| bool UIKitCompensatesForSafeArea => | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
[minor] Complexity Reduction — the new property's summary states it is "shared by the arrange branch and
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adopted in 9f439ed — the
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
[minor] Safe Area and Window Insets — The extracted predicate is behavior-preserving (
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adopted in 7e4f1bf — you're right that sharing a live expression only removes textual divergence, and the |
||
| SystemAdjustedContentInset != UIEdgeInsets.Zero | ||
| && ContentInsetAdjustmentBehavior != UIScrollViewContentInsetAdjustmentBehavior.Never; | ||
|
|
||
| /// <summary> | ||
| /// The safe area <see cref="CrossPlatformArrange"/> baked into the content's coordinate | ||
| /// space: when it applies the safe area while UIKit is not compensating through | ||
|
|
@@ -489,16 +502,15 @@ internal CGSize ScrollableContentSize | |
| /// padding still obscures the viewport without ever appearing in the adjusted inset. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Mirrors the arrange-side branch exactly: bounds are inset only while | ||
| /// <c>_appliesSafeAreaAdjustments</c>, and the inset origin is kept only when UIKit | ||
| /// contributes nothing (<see cref="UIScrollViewContentInsetAdjustmentBehavior.Never"/>, | ||
| /// or a zero system inset). In the remaining case (<see cref="UIScrollViewContentInsetAdjustmentBehavior.Always"/>) | ||
| /// the content is re-based at the origin and UIKit's inset owns the compensation, so the | ||
| /// arranged rect <see cref="ScrollableContentSize"/> measures naturally excludes it (issue #36801). | ||
| /// Shares <see cref="UIKitCompensatesForSafeArea"/> with the arrange branch, so the | ||
| /// baked state always describes what the last arrange actually did: when UIKit is not | ||
| /// compensating, the content is arranged inside safe-area-inset bounds and the padding | ||
| /// lives in its coordinate space; when UIKit compensates, the content is re-based at | ||
| /// the origin and the adjusted inset owns the compensation, which the arranged rect | ||
| /// <see cref="ScrollableContentSize"/> measures naturally excludes (issue #36801). | ||
| /// </remarks> | ||
| internal SafeAreaPadding SafeAreaBakedIntoContent => | ||
| _appliesSafeAreaAdjustments && | ||
| (SystemAdjustedContentInset == UIEdgeInsets.Zero || ContentInsetAdjustmentBehavior == UIScrollViewContentInsetAdjustmentBehavior.Never) | ||
| _appliesSafeAreaAdjustments && !UIKitCompensatesForSafeArea | ||
| ? _safeArea | ||
| : SafeAreaPadding.Empty; | ||
|
|
||
|
|
@@ -538,7 +550,7 @@ Size CrossPlatformArrange(CGRect bounds) | |
| CGPoint contentOrigin; | ||
| double width; | ||
| double height; | ||
| if (SystemAdjustedContentInset == UIEdgeInsets.Zero || ContentInsetAdjustmentBehavior == UIScrollViewContentInsetAdjustmentBehavior.Never) | ||
| if (!UIKitCompensatesForSafeArea) | ||
| { | ||
| contentSize = CrossPlatformLayout?.CrossPlatformArrange(bounds.ToRectangle()) ?? Size.Zero; | ||
| contentOrigin = bounds.Location; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[moderate] Cross-Platform Behavioral Consistency — Releasing the caller while leaving the request parked means
await ScrollToAsync(element, …)can report completion and then perform the scroll an arbitrary amount of time later, when the branch is next shown. Concrete scenario: a collapsed detail panel whoseScrollToAsync(item, Center)is awaited duringOnAppearing; the user expands the panel minutes later and the ScrollView jumps to a target chosen for stale content. Only a subsequentScrollToAsyncclears the park (_pendingScrollToRequested = null, line 621) — the caller that was already released has no way to cancel it, andScrollX/ScrollYhave no public setter to override it. Worth confirming this deferred replay is the intended contract versus dropping the parked request at release time (which would makeTaskcompletion mean "this request is over" on every path).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adopted in fd0c039 — and this finding is the one that resolved the whole series, so thank you for it. You're right that releasing the task while leaving the request parked meant a completed
awaitcould scroll arbitrarily later with no way to cancel it; that is a ghost scroll, and it's the exact hazard I'd argued against on the detach thread in #37060. The park-and-complete design was internally contradictory and every prior round was one end of that contradiction being pulled.Replaced with a single invariant, now stated in the code and the PR description: a parked element request lives exactly as long as its task. It leaves the park in exactly two ways and no other — the arrange arrives and it's replayed against real geometry (task completes with the scroll), or the view's lifecycle ends (handler goes away / removed from the tree) and it's dropped (task completes without a scroll). Nothing releases the task while a request is parked, so completion means "this request is over" on every path; nothing but a lifecycle end drops it, so a merely hidden view still scrolls to the element once shown. The visibility-proxy machinery (
WillArrange,OnIsVisibleChanged, the collapse releases) is gone entirely — every proxy for "no arrange is coming" leaks, and each leak was a hang.