Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -89,18 +91,28 @@ 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
// Recorded only once the event is actually dispatched, so a failed lookup
// above does not permanently swallow this inset value.
insets.copyInto(lastInsets)
hasLastInsets = true
hasHeldInsets = false
eventDispatcher.dispatchEvent(
SafeAreaInsetsChangeEvent(
surfaceId = UIManagerHelper.getSurfaceId(view),
Expand All @@ -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()
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<RCTModernEventEmitter>()
val payload = argumentCaptor<WritableMap>()
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)
Expand Down
Loading