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)