diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactSurfaceView.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactSurfaceView.kt index 1ab36cda87d..9d62d7917a1 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactSurfaceView.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactSurfaceView.kt @@ -15,6 +15,8 @@ import android.graphics.Rect import android.view.KeyEvent import android.view.MotionEvent import android.view.View +import androidx.core.view.ViewCompat +import androidx.core.view.WindowInsetsCompat import com.facebook.common.logging.FLog import com.facebook.react.ReactRootView import com.facebook.react.bridge.ReactContext @@ -27,6 +29,7 @@ import com.facebook.react.uimanager.JSKeyDispatcher import com.facebook.react.uimanager.JSPointerDispatcher import com.facebook.react.uimanager.JSTouchDispatcher import com.facebook.react.uimanager.common.UIManagerType +import com.facebook.react.views.view.isEdgeToEdgeFeatureFlagOn import com.facebook.systrace.Systrace import java.util.Objects import kotlin.math.max @@ -47,16 +50,24 @@ public class ReactSurfaceView(context: Context?, internal val surface: ReactSurf private val viewportOffset: Point get() { - val locationOnScreen = IntArray(2) - getLocationOnScreen(locationOnScreen) + val locationInWindow = IntArray(2) + getLocationInWindow(locationInWindow) - // we need to subtract visibleWindowCoords - to subtract possible window insets, split - // screen or multi window - val visibleWindowFrame = Rect() - getWindowVisibleDisplayFrame(visibleWindowFrame) - locationOnScreen[0] -= visibleWindowFrame.left - locationOnScreen[1] -= visibleWindowFrame.top - return Point(locationOnScreen[0], locationOnScreen[1]) + if (!isEdgeToEdgeFeatureFlagOn) { + // When not in edge-to-edge mode, subtract the top system bar insets so the offset is + // relative to the content area (below the status bar / cutout). + ViewCompat.getRootWindowInsets(this)?.apply { + val insets = + getInsets( + WindowInsetsCompat.Type.statusBars() or WindowInsetsCompat.Type.displayCutout() + ) + + locationInWindow[0] -= insets.left + locationInWindow[1] -= insets.top + } + } + + return Point(locationInWindow[0], locationInWindow[1]) } init { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/RootViewUtil.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/RootViewUtil.kt index 2dc7c22624c..1894f9d2e1e 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/RootViewUtil.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/RootViewUtil.kt @@ -8,10 +8,12 @@ package com.facebook.react.uimanager import android.graphics.Point -import android.graphics.Rect import android.view.View import androidx.annotation.UiThread +import androidx.core.view.ViewCompat +import androidx.core.view.WindowInsetsCompat import com.facebook.infer.annotation.Assertions +import com.facebook.react.views.view.isEdgeToEdgeFeatureFlagOn public object RootViewUtil { /** Returns the root view of a given view in a react application. */ @@ -34,12 +36,20 @@ public object RootViewUtil { val locationInWindow = IntArray(2) v.getLocationInWindow(locationInWindow) - // we need to subtract visibleWindowCoords - to subtract possible window insets, split - // screen or multi window - val visibleWindowFrame = Rect() - v.getWindowVisibleDisplayFrame(visibleWindowFrame) - locationInWindow[0] -= visibleWindowFrame.left - locationInWindow[1] -= visibleWindowFrame.top + if (!isEdgeToEdgeFeatureFlagOn) { + // When not in edge-to-edge mode, subtract the top system bar insets so the offset is + // relative to the content area (below the status bar / cutout). + ViewCompat.getRootWindowInsets(v)?.apply { + val insets = + getInsets( + WindowInsetsCompat.Type.statusBars() or WindowInsetsCompat.Type.displayCutout() + ) + + locationInWindow[0] -= insets.left + locationInWindow[1] -= insets.top + } + } + return Point(locationInWindow[0], locationInWindow[1]) } }