Skip to content

Report Android safe-area insets after a last-pixel change settles - #60373

Merged
meta-codesync[bot] merged 1 commit into
react:mainfrom
expo:@ide/android-safe-area-last-pixel
Dec 1, 2026
Merged

meta-codesync[bot] merged 1 commit into
react:mainfrom
expo:@ide/android-safe-area-last-pixel

Conversation

@ide

@ide ide commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

SafeAreaInsetsObserver reports a view's safe-area insets only when an edge moves more than one physical pixel from the last report, to match iOS's precision while the view moves. A change within that threshold is dropped for good. The last frame of a transition often moves a view by its last pixel, so a screen that slides in from the side can keep the one-pixel inset it had on the frame before it stopped. Which frames the observer sees varies, so the same screen can end with different insets each time it opens.

This change holds a change within the threshold and requests another frame (postInvalidateOnAnimation):

  • If the insets are the same on the next frame, they have settled and are reported.
  • If they moved again, they are held again. A view that moves a pixel or less per frame sends nothing until it stops or moves past the threshold.
  • Insets within 0.01 px of the last report count as unchanged, so a settled view sends nothing more.

The threshold while the view moves is unchanged.

Changelog:

[ANDROID] [FIXED] - Report a view's safe-area insets when a change of one pixel or less settles

Test Plan:

./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest --tests com.facebook.react.uimanager.SafeAreaInsetsObserverTest -Preact.internal.useHermesStable=true --console=plain

The new lastPixelOfASlideIn_isReportedOnceTheViewHoldsStill moves a view in by 3, 1 and 0 px of translationX, and expects the last pixel to be reported as right: 0 on the frame after the view stops, and no report on the frame after that. It also expects one postInvalidateOnAnimation call, made on the frame the view stops: the test calls onPreDraw itself, and on a device nothing else would draw that next frame. The test fails on main, and fails with the postInvalidateOnAnimation call removed (0 frame requests instead of 1).

The observer keeps the held frame's insets as well as the last reported ones. Without them it cannot tell a view that has stopped from one still moving within the threshold, and subpixelChanges_emitOnlyBeyondOnePhysicalPixelWithoutRoundingPayload fails.

ktfmt and git diff --check are clean. Not run: a device or emulator.

@github-actions github-actions Bot added the Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. label Nov 28, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 28, 2026
@github-actions

github-actions Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Visual regression

No visual differences at ee655bcf2ab7.

ios: no capture

@ide
ide force-pushed the @ide/android-safe-area-last-pixel branch from 8228ad5 to 609daf0 Compare November 29, 2026 12:02
@ide
ide marked this pull request as ready for review November 29, 2026 13:49
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Nov 29, 2026

@chrfalch chrfalch left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic looks right to me, and the new test fails on main. (Commenting rather than approving since it's still a draft.)

One request on the test: lastPixelOfASlideIn_isReportedOnceTheViewHoldsStill calls observer.onPreDraw() by hand for the extra frame. On a device nothing would call it once the animation has ended. The view.postInvalidateOnAnimation() call is what makes that extra frame happen, so it is the core of the fix. Because the test drives every frame itself, it still passes if that call is removed. Could the test also assert that a frame was requested after the held sub-pixel change? For example, spy on the view and verify postInvalidateOnAnimation(), or check the invalidation through Robolectric's looper or shadow view. Then the request can't be dropped by accident later.

private val geometry = Geometry()
private val insets = FloatArray(4)
private val lastInsets = FloatArray(4)
private val heldInsets = FloatArray(4)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do this without the extra insets copy?

if hasHeldInsets is true (maybe rename to hasPendingInsetsToApply?) don't insets and lastInsets already contain the info you need?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to hasPendingInsetsToApply, and the array to pendingInsets. The copy is still needed: lastInsets is the last value reported and insets is this frame's, but "settled" means this frame equals the previous one, which neither holds. Without the copy, a pending change is reported on the next frame even if the view is still moving, so a view moving less than a pixel per frame, as at the end of an ease-out, reports every other frame. subpixelChanges_emitOnlyBeyondOnePhysicalPixelWithoutRoundingPayload fails without it, and a comment now says why. If you'd rather have the simpler version and the extra events, I'll switch.

`SafeAreaInsetsObserver` reports a view's safe-area insets only when an
edge moves more than one physical pixel from the last report, to match
iOS's precision while the view moves. A change within that threshold
was dropped for good. The last frame of a transition often moves a view
by its last pixel, so a screen that slid in from the side kept
reporting the one-pixel inset it had on the frame before it stopped.
Which frames the observer saw decided whether this happened, so the
same screen could end with different insets each time it opened.

Hold a change within the threshold and request another frame. If the
insets are the same on that frame, they have settled and are reported.
If they moved again, they are held again, so a view that moves a pixel
or less per frame still sends nothing until it stops or moves past the
threshold. Insets within 0.01 px of the last report count as
unchanged, so a settled view sends nothing more.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ide
ide force-pushed the @ide/android-safe-area-last-pixel branch from 609daf0 to ee655bc Compare December 1, 2026 01:24
@ide

ide commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

@chrfalch The test now uses a view that counts postInvalidateOnAnimation() calls. It expects none while the view moves, one on the frame where the last pixel is held, and no more after the report. With the call removed from the observer, the test fails (0 requests instead of 1).

@chrfalch chrfalch left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update, LGTM.

@meta-codesync

meta-codesync Bot commented Dec 1, 2026

Copy link
Copy Markdown

@javache has imported this pull request. If you are a Meta employee, you can view this in D135308620.

@zeyap zeyap left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review automatically exported from Phabricator review in Meta.

@meta-codesync
meta-codesync Bot merged commit c3c3b7b into react:main Dec 1, 2026
64 checks passed
@react-native-bot

Copy link
Copy Markdown
Collaborator

This pull request was successfully merged by @ide in c3c3b7b

When will my fix make it into a release? | How to file a pick request?

@react-native-bot react-native-bot added the Merged This PR has been merged. label Dec 1, 2026
@ide
ide deleted the @ide/android-safe-area-last-pixel branch December 1, 2026 17:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants