Skip to content
Open
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 @@ -92,6 +92,7 @@
import com.facebook.react.uimanager.events.FabricEventDispatcher;
import com.facebook.react.uimanager.events.RCTEventEmitter;
import com.facebook.react.uimanager.events.SynchronousEventReceiver;
import com.facebook.react.views.modal.ModalContentInsetsKt;
import com.facebook.react.views.text.PreparedLayout;
import com.facebook.react.views.text.ReactTextViewManager;
import com.facebook.react.views.text.ReactTextViewManagerCallback;
Expand Down Expand Up @@ -767,15 +768,14 @@ public boolean getThemeData(int surfaceId, float[] defaultTextInputPadding) {
}

/**
* This method is used to get the encoded screen size without vertical insets for a given surface.
* It's used by the Modal component to determine the size of the screen without vertical insets.
* Returns initial Modal content bounds, excluding only system bars that the Dialog fits around.
* The method is private as it's accessed via JNI from C++.
*
* @param surfaceId The surface ID of the surface for which the Modal is going to render.
* @return The encoded screen size as a long (both width and height) are represented without
* vertical insets.
* @return The encoded content width and height in dp for the initial Modal layout.
*/
private long getEncodedScreenSizeWithoutVerticalInsets(int surfaceId) {
private long getEncodedScreenSizeWithoutVerticalInsets(
int surfaceId, boolean statusBarTranslucent, boolean navigationBarTranslucent) {
ThemedReactContext context =
mMountingManager
.getSurfaceManagerEnforced(surfaceId, "getEncodedScreenSizeWithoutVerticalInsets")
Expand All @@ -785,7 +785,10 @@ private long getEncodedScreenSizeWithoutVerticalInsets(int surfaceId) {
return 0;
} else {
return DisplayMetricsHolder.getEncodedScreenSizeWithoutVerticalInsets(
context.getCurrentActivity());
context.getCurrentActivity(),
ModalContentInsetsKt.modalContentFitsTopInset(statusBarTranslucent),
ModalContentInsetsKt.modalContentFitsBottomInset(
statusBarTranslucent, navigationBarTranslucent));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,27 +88,34 @@ public object DisplayMetricsHolder {
}

/**
* Returns the encoded screen size without vertical insets.
* Returns the encoded screen size, subtracting the requested vertical insets.
*
* This is needed to render components that needs to be correctly positioned on the screen on
* their first frame. Modal is one of such components.
*
* @param activity the [Activity] to get the insets from.
* @param subtractTopInset whether the content fits below the top system inset.
* @param subtractBottomInset whether the content fits above the bottom system inset.
* @return the encoded screen size as a [Long] value, where the first 32 bits represent the width
* and the last 32 bits represent the height in dp (density-independent pixels).
*/
// This annotation can be removed once FabricUIManager is migrated to Kotlin
@JvmName("getEncodedScreenSizeWithoutVerticalInsets")
@JvmStatic
internal fun getEncodedScreenSizeWithoutVerticalInsets(activity: Activity?): Long {
internal fun getEncodedScreenSizeWithoutVerticalInsets(
activity: Activity?,
subtractTopInset: Boolean,
subtractBottomInset: Boolean,
): Long {
val windowInsets = activity?.window?.decorView?.let(ViewCompat::getRootWindowInsets) ?: return 0
val insets =
windowInsets.getInsets(
WindowInsetsCompat.Type.statusBars() or
WindowInsetsCompat.Type.navigationBars() or
WindowInsetsCompat.Type.displayCutout(),
)
val verticalInsets = insets.top + insets.bottom
val verticalInsets =
(if (subtractTopInset) insets.top else 0) + (if (subtractBottomInset) insets.bottom else 0)
return encodeFloatsToLong(
(checkNotNull(screenDisplayMetrics).widthPixels).toFloat().pxToDp(),
(checkNotNull(screenDisplayMetrics).heightPixels - verticalInsets).toFloat().pxToDp(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.views.modal

import com.facebook.react.views.view.isEdgeToEdgeFeatureFlagOn

// These must stay in sync with the window configuration applied by ReactModalHostView in
// contentView and updateProperties, including the app-wide edge-to-edge override.

/**
* Whether the content of a Modal is laid out below the top system bar inset. With an opaque status
* bar, the content FrameLayout fits the system windows.
*/
internal fun modalContentFitsTopInset(statusBarTranslucent: Boolean): Boolean =
!(statusBarTranslucent || isEdgeToEdgeFeatureFlagOn)

/**
* Whether the content of a Modal is laid out above the bottom system bar inset. Only a Modal with
* both system bars translucent extends under the bottom inset: with an opaque status bar, the
* content FrameLayout fits the system windows even if the Dialog window is edge-to-edge.
*/
internal fun modalContentFitsBottomInset(
statusBarTranslucent: Boolean,
navigationBarTranslucent: Boolean,
): Boolean =
!((statusBarTranslucent || isEdgeToEdgeFeatureFlagOn) &&
(navigationBarTranslucent || isEdgeToEdgeFeatureFlagOn))
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import android.view.View
import android.view.Window
import android.view.WindowInsets
import androidx.annotation.RequiresApi
import androidx.core.graphics.Insets
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsDefaults
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests
Expand All @@ -31,6 +34,7 @@ import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.Mockito.mockStatic
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
import org.robolectric.RobolectricTestRunner
Expand Down Expand Up @@ -112,13 +116,38 @@ class DisplayMetricsHolderTest {
.thenReturn(android.graphics.Insets.of(10, 20, 10, 20))

// Should return a non-zero encoded value
val encoded = DisplayMetricsHolder.getEncodedScreenSizeWithoutVerticalInsets(activity)
val encoded =
DisplayMetricsHolder.getEncodedScreenSizeWithoutVerticalInsets(activity, true, true)
assertThat(encoded).isNotZero()
}

@Test
fun getEncodedScreenSizeWithoutVerticalInsets_subtractsTopAndBottomInsets() =
assertEncodedScreenSize(
subtractTopInset = true,
subtractBottomInset = true,
expectedHeightPx = 950f,
)

@Test
fun getEncodedScreenSizeWithoutVerticalInsets_subtractsOnlyBottomInset() =
assertEncodedScreenSize(
subtractTopInset = false,
subtractBottomInset = true,
expectedHeightPx = 970f,
)

@Test
fun getEncodedScreenSizeWithoutVerticalInsets_doesNotSubtractInsets() =
assertEncodedScreenSize(
subtractTopInset = false,
subtractBottomInset = false,
expectedHeightPx = 1000f,
)

@Test
fun getEncodedScreenSizeWithoutVerticalInsets_returnsZeroIfActivityIsNull() {
val encoded = DisplayMetricsHolder.getEncodedScreenSizeWithoutVerticalInsets(null)
val encoded = DisplayMetricsHolder.getEncodedScreenSizeWithoutVerticalInsets(null, true, true)
assertThat(encoded).isZero()
}

Expand Down Expand Up @@ -205,6 +234,49 @@ class DisplayMetricsHolderTest {
assertThat(DisplayMetricsHolder.getScreenDisplayMetrics()).isNotNull()
}

private fun assertEncodedScreenSize(
subtractTopInset: Boolean,
subtractBottomInset: Boolean,
expectedHeightPx: Float,
) {
DisplayMetricsHolder.initDisplayMetrics(context)
val activity: Activity = mock()
val window: Window = mock()
val decorView: View = mock()
whenever(activity.window).thenReturn(window)
whenever(window.decorView).thenReturn(decorView)
val rootInsets: WindowInsetsCompat = mock()
whenever(
rootInsets.getInsets(
WindowInsetsCompat.Type.statusBars() or
WindowInsetsCompat.Type.navigationBars() or
WindowInsetsCompat.Type.displayCutout()
)
)
.thenReturn(Insets.of(0, 20, 0, 30))
DisplayMetricsHolder.setScreenDisplayMetrics(
DisplayMetrics().apply {
setTo(displayMetrics)
widthPixels = 600
heightPixels = 1000
}
)
mockStatic(ViewCompat::class.java).use { viewCompat ->
viewCompat
.`when`<WindowInsetsCompat?> { ViewCompat.getRootWindowInsets(decorView) }
.thenReturn(rootInsets)
val encoded =
DisplayMetricsHolder.getEncodedScreenSizeWithoutVerticalInsets(
activity,
subtractTopInset,
subtractBottomInset,
)
assertThat(Float.fromBits((encoded shr 32).toInt())).isEqualTo(PixelUtil.toDIPFromPixel(600f))
assertThat(Float.fromBits(encoded.toInt()))
.isEqualTo(PixelUtil.toDIPFromPixel(expectedHeightPx))
}
}

private fun overrideWindowDensityFlag(enabled: Boolean) {
ReactNativeFeatureFlags.override(
object : ReactNativeFeatureFlagsDefaults() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.views.modal

import com.facebook.react.views.view.isEdgeToEdgeFeatureFlagOn
import org.assertj.core.api.Assertions.assertThat
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
class ModalContentInsetsTest {
private var previousEdgeToEdge = false

@Before
fun setUp() {
previousEdgeToEdge = isEdgeToEdgeFeatureFlagOn
}

@After
fun tearDown() {
isEdgeToEdgeFeatureFlagOn = previousEdgeToEdge
}

@Test
fun edgeToEdge_withNeitherProp_extendsUnderBothInsets() =
assertFits(true, false, false, top = false, bottom = false)

@Test
fun edgeToEdge_withStatusProp_extendsUnderBothInsets() =
assertFits(true, true, false, top = false, bottom = false)

@Test
fun edgeToEdge_withNavigationProp_extendsUnderBothInsets() =
assertFits(true, false, true, top = false, bottom = false)

@Test
fun edgeToEdge_withBothProps_extendsUnderBothInsets() =
assertFits(true, true, true, top = false, bottom = false)

@Test
fun noEdgeToEdge_withNeitherProp_fitsBothInsets() =
assertFits(false, false, false, top = true, bottom = true)

@Test
fun noEdgeToEdge_withStatusProp_fitsOnlyBottomInset() =
assertFits(false, true, false, top = false, bottom = true)

// This unsupported JS combination still reaches native. The content FrameLayout fits both bars.
@Test
fun noEdgeToEdge_withNavigationProp_fitsBothInsets() =
assertFits(false, false, true, top = true, bottom = true)

@Test
fun noEdgeToEdge_withBothProps_extendsUnderBothInsets() =
assertFits(false, true, true, top = false, bottom = false)

private fun assertFits(
edgeToEdge: Boolean,
statusBarTranslucent: Boolean,
navigationBarTranslucent: Boolean,
top: Boolean,
bottom: Boolean,
) {
isEdgeToEdgeFeatureFlagOn = edgeToEdge
assertThat(modalContentFitsTopInset(statusBarTranslucent)).isEqualTo(top)
assertThat(modalContentFitsBottomInset(statusBarTranslucent, navigationBarTranslucent))
.isEqualTo(bottom)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ namespace facebook::react {
State::Shared ModalHostViewComponentDescriptor::createInitialState(
const Props::Shared& props,
const ShadowNodeFamily::Shared& family) const {
// For Android, we need to get the size of the screen without the vertical
// insets to correctly position the modal on the first rendering.
// For this reason we provide the `createInitialState` implementation
// that will query FabricUIManager for the size of the screen without
// vertical insets.
// Match the Dialog's system bar configuration before its first measurement.
const auto& modalProps = static_cast<const ModalHostViewProps&>(*props);

int surfaceId = family->getSurfaceId();

Expand All @@ -26,10 +23,14 @@ State::Shared ModalHostViewComponentDescriptor::createInitialState(

static auto getEncodedScreenSizeWithoutVerticalInsets =
jni::findClassStatic(UIManagerJavaDescriptor)
->getMethod<jlong(jint)>("getEncodedScreenSizeWithoutVerticalInsets");

auto result =
getEncodedScreenSizeWithoutVerticalInsets(fabricUIManager, surfaceId);
->getMethod<jlong(jint, jboolean, jboolean)>(
"getEncodedScreenSizeWithoutVerticalInsets");

auto result = getEncodedScreenSizeWithoutVerticalInsets(
fabricUIManager,
surfaceId,
modalProps.statusBarTranslucent,
modalProps.navigationBarTranslucent);

// Inspired from yogaMeasureToSize from conversions.h
int32_t wBits = 0xFFFFFFFF & (result >> 32);
Expand Down
Loading