From 8228ad5a66335a40687a9e0df3b6403f5d402b10 Mon Sep 17 00:00:00 2001 From: James Ide Date: Sat, 28 Nov 2026 02:34:40 -0800 Subject: [PATCH] Report Android safe-area insets after a last-pixel change settles `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 less than a pixel 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 --- .../internal/SafeAreaInsetsObserver.kt | 27 ++++++++++--- .../uimanager/SafeAreaInsetsObserverTest.kt | 38 +++++++++++++++++++ 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/internal/SafeAreaInsetsObserver.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/internal/SafeAreaInsetsObserver.kt index 51aa3b4995c1..7475197adb35 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/internal/SafeAreaInsetsObserver.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/internal/SafeAreaInsetsObserver.kt @@ -30,8 +30,10 @@ internal class SafeAreaInsetsObserver private constructor(private val view: View private val geometry = Geometry() private val insets = FloatArray(4) private val lastInsets = FloatArray(4) + private val heldInsets = FloatArray(4) private var hasLastInsets = false + private var hasHeldInsets = false private var isListening = false private fun start() { @@ -89,11 +91,20 @@ internal class SafeAreaInsetsObserver private constructor(private val view: View hasLastInsets = false return } - if ( - hasLastInsets && - insets.indices.all { abs(insets[it] - lastInsets[it]) <= INSET_CHANGE_THRESHOLD_PX } - ) { - return + if (hasLastInsets && within(insets, lastInsets, INSET_CHANGE_THRESHOLD_PX)) { + // A change within the threshold is not reported while the view moves, but the last frame of + // a transition often moves the view by its last pixel. Hold the change and draw another + // frame; insets that are the same on that frame have settled and are reported. + if (within(insets, lastInsets, INSET_ROUND_OFF_PX)) { + hasHeldInsets = false + return + } + if (!hasHeldInsets || !within(insets, heldInsets, INSET_ROUND_OFF_PX)) { + insets.copyInto(heldInsets) + hasHeldInsets = true + view.postInvalidateOnAnimation() + return + } } val eventDispatcher = UIManagerHelper.getEventDispatcher(UIManagerHelper.getReactContext(view)) ?: return @@ -101,6 +112,7 @@ internal class SafeAreaInsetsObserver private constructor(private val view: View // above does not permanently swallow this inset value. insets.copyInto(lastInsets) hasLastInsets = true + hasHeldInsets = false eventDispatcher.dispatchEvent( SafeAreaInsetsChangeEvent( surfaceId = UIManagerHelper.getSurfaceId(view), @@ -113,6 +125,9 @@ internal class SafeAreaInsetsObserver private constructor(private val view: View ) } + private fun within(a: FloatArray, b: FloatArray, tolerance: Float): Boolean = + a.indices.all { abs(a[it] - b[it]) <= tolerance } + private class Geometry { val visibleRect = Rect() val toRoot = Matrix() @@ -127,6 +142,8 @@ internal class SafeAreaInsetsObserver private constructor(private val view: View private const val LEFT = 3 // Match iOS notification precision without rounding the emitted measurement. private const val INSET_CHANGE_THRESHOLD_PX = 1f + // A transformed edge's float round-off, far below any change worth reporting. + private const val INSET_ROUND_OFF_PX = 0.01f // Bound accumulated transform round-off to one thousandth of a physical pixel. private const val MAX_AXIS_ALIGNMENT_ERROR_PX = 0.001f diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/SafeAreaInsetsObserverTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/SafeAreaInsetsObserverTest.kt index 332e0103f929..0115a761f283 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/SafeAreaInsetsObserverTest.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/SafeAreaInsetsObserverTest.kt @@ -531,6 +531,44 @@ class SafeAreaInsetsObserverTest { assertThat(insets.getDouble("bottom")).isEqualTo(50.625) } + @Test + @Suppress("DEPRECATION") + @SuppressLint("DeprecatedMethod") + fun lastPixelOfASlideIn_isReportedOnceTheViewHoldsStill() { + // A screen sliding in from the right: its last frame moves it one pixel, which is within the + // threshold of the frame before. + DisplayMetricsHolder.setScreenDisplayMetrics(DisplayMetrics().apply { density = 2f }) + val view = addView(root, 40, 100, 460, 900) + val observer = observe(view) + view.translationX = 3f + observer.onPreDraw() + view.translationX = 1f + observer.onPreDraw() + view.translationX = 0f + observer.onPreDraw() + assertThat(events).hasSize(2) + + observer.onPreDraw() + assertThat(events).hasSize(3) + val emitter = mock() + val payload = argumentCaptor() + events.last().dispatchModern(emitter) + verify(emitter) + .receiveEvent( + eq(1), + eq(2), + eq("topSafeAreaInsetsChange"), + any(), + any(), + payload.capture(), + any(), + ) + assertThat(checkNotNull(payload.firstValue.getMap("insets")).getDouble("right")).isEqualTo(0.0) + + observer.onPreDraw() + assertThat(events).hasSize(3) + } + @Test fun supportedUnsupportedSupported_reEmitsAfterRecovery() { val view = addView(root, 0, 0, 500, 1000)