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..d91dde81d73c 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 pendingInsets = FloatArray(4) private var hasLastInsets = false + private var hasPendingInsetsToApply = false private var isListening = false private fun start() { @@ -89,11 +91,22 @@ 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. The + // comparison is with the held frame, not with `lastInsets`: the last report cannot tell a + // view that has stopped from one still moving within the threshold. + if (within(insets, lastInsets, INSET_ROUND_OFF_PX)) { + hasPendingInsetsToApply = false + return + } + if (!hasPendingInsetsToApply || !within(insets, pendingInsets, INSET_ROUND_OFF_PX)) { + insets.copyInto(pendingInsets) + hasPendingInsetsToApply = true + view.postInvalidateOnAnimation() + return + } } val eventDispatcher = UIManagerHelper.getEventDispatcher(UIManagerHelper.getReactContext(view)) ?: return @@ -101,6 +114,7 @@ internal class SafeAreaInsetsObserver private constructor(private val view: View // above does not permanently swallow this inset value. insets.copyInto(lastInsets) hasLastInsets = true + hasPendingInsetsToApply = false eventDispatcher.dispatchEvent( SafeAreaInsetsChangeEvent( surfaceId = UIManagerHelper.getSurfaceId(view), @@ -113,6 +127,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 +144,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..ca632ea69b8c 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,57 @@ 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 }) + var frameRequests = 0 + val view = + object : View(root.context) { + override fun postInvalidateOnAnimation() { + frameRequests++ + } + } + root.addView(view) + view.layout(40, 100, 460, 900) + val observer = observe(view) + view.translationX = 3f + observer.onPreDraw() + view.translationX = 1f + observer.onPreDraw() + assertThat(frameRequests).isEqualTo(0) + view.translationX = 0f + observer.onPreDraw() + assertThat(events).hasSize(2) + // Nothing else draws once the view has stopped, so the observer has to request the frame on + // which it reports the held change. + assertThat(frameRequests).isEqualTo(1) + + 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) + assertThat(frameRequests).isEqualTo(1) + } + @Test fun supportedUnsupportedSupported_reEmitsAfterRecovery() { val view = addView(root, 0, 0, 500, 1000)