Skip to content

Commit 06b911f

Browse files
committed
Fix Dimensions window values when root window insets are unavailable
1 parent 946ca3c commit 06b911f

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

  • packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/deviceinfo

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/deviceinfo/DeviceInfoModule.kt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,22 @@ internal class DeviceInfoModule(reactContext: ReactApplicationContext) :
4242

4343
val activity = reactApplicationContext.currentActivity ?: return windowDisplayMetrics
4444
val bounds = WindowMetricsCalculator.getOrCreate().computeCurrentWindowMetrics(activity).bounds
45-
var width = bounds.width()
46-
var height = bounds.height()
4745

48-
// WindowMetrics bounds include system bars. When edge-to-edge is not enabled, we subtract them
49-
// so that window dimensions reflect the usable content area.
50-
if (!isEdgeToEdgeFeatureFlagOn) {
46+
if (isEdgeToEdgeFeatureFlagOn) {
47+
windowDisplayMetrics.widthPixels = bounds.width()
48+
windowDisplayMetrics.heightPixels = bounds.height()
49+
} else {
50+
// WindowMetrics bounds include system bars. When edge-to-edge is not enabled, we subtract
51+
// them so that window dimensions reflect the usable content area. If insets aren't yet
52+
// available (e.g. before the first layout pass), fall back to resources.displayMetrics,
53+
// which already excludes system bars in non-edge-to-edge mode.
5154
ViewCompat.getRootWindowInsets(activity.window.decorView)?.let {
5255
val insets = it.getInsets(WindowInsetsCompat.Type.systemBars())
53-
width -= (insets.left + insets.right)
54-
height -= (insets.top + insets.bottom)
56+
windowDisplayMetrics.widthPixels = bounds.width() - (insets.left + insets.right)
57+
windowDisplayMetrics.heightPixels = bounds.height() - (insets.top + insets.bottom)
5558
}
5659
}
5760

58-
windowDisplayMetrics.widthPixels = width
59-
windowDisplayMetrics.heightPixels = height
6061
return windowDisplayMetrics
6162
}
6263

0 commit comments

Comments
 (0)