diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/SurfaceHandlerBinding.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/SurfaceHandlerBinding.java deleted file mode 100644 index ca03205c016d61..00000000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/SurfaceHandlerBinding.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * 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.fabric; - -import static com.facebook.react.fabric.mounting.LayoutMetricsConversions.getMaxSize; -import static com.facebook.react.fabric.mounting.LayoutMetricsConversions.getMinSize; - -import com.facebook.infer.annotation.Nullsafe; -import com.facebook.jni.HybridClassBase; -import com.facebook.react.bridge.NativeMap; -import com.facebook.react.interfaces.fabric.SurfaceHandler; - -@Nullsafe(Nullsafe.Mode.LOCAL) -public class SurfaceHandlerBinding extends HybridClassBase implements SurfaceHandler { - static { - FabricSoLoader.staticInit(); - } - - private static final int NO_SURFACE_ID = 0; - - /* Keep in sync with SurfaceHandler.cpp */ - public static final int DISPLAY_MODE_VISIBLE = 0; - public static final int DISPLAY_MODE_SUSPENDED = 1; - public static final int DISPLAY_MODE_HIDDEN = 2; - - private native void initHybrid(int surfaceId, String moduleName); - - public SurfaceHandlerBinding(String moduleName) { - initHybrid(NO_SURFACE_ID, moduleName); - } - - @Override - public native int getSurfaceId(); - - @Override - public native String getModuleName(); - - @Override - public native boolean isRunning(); - - @Override - public void setLayoutConstraints( - int widthMeasureSpec, - int heightMeasureSpec, - int offsetX, - int offsetY, - boolean doLeftAndRightSwapInRTL, - boolean isRTL, - float pixelDensity) { - setLayoutConstraintsNative( - getMinSize(widthMeasureSpec) / pixelDensity, - getMaxSize(widthMeasureSpec) / pixelDensity, - getMinSize(heightMeasureSpec) / pixelDensity, - getMaxSize(heightMeasureSpec) / pixelDensity, - offsetX / pixelDensity, - offsetY / pixelDensity, - doLeftAndRightSwapInRTL, - isRTL, - pixelDensity); - } - - private native void setLayoutConstraintsNative( - float minWidth, - float maxWidth, - float minHeight, - float maxHeight, - float offsetX, - float offsetY, - boolean doLeftAndRightSwapInRTL, - boolean isRTL, - float pixelDensity); - - @Override - public native void setProps(NativeMap props); - - @Override - public void setMountable(boolean mountable) { - setDisplayMode(mountable ? DISPLAY_MODE_VISIBLE : DISPLAY_MODE_SUSPENDED); - } - - private native void setDisplayMode(int mode); -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/SurfaceHandlerBinding.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/SurfaceHandlerBinding.kt new file mode 100644 index 00000000000000..6b579493465a27 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/SurfaceHandlerBinding.kt @@ -0,0 +1,91 @@ +/* + * 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.fabric + +import com.facebook.jni.HybridClassBase +import com.facebook.react.bridge.NativeMap +import com.facebook.react.fabric.mounting.LayoutMetricsConversions +import com.facebook.react.interfaces.fabric.SurfaceHandler + +public open class SurfaceHandlerBinding(moduleName: String) : HybridClassBase(), SurfaceHandler { + + init { + initHybrid(NO_SURFACE_ID, moduleName) + } + + private external fun initHybrid(surfaceId: Int, moduleName: String) + + override val surfaceId: Int + get() = _getSurfaceId() + + override val isRunning: Boolean + get() = _isRunning() + + override val moduleName: String + get() = _getModuleName() + + private external fun _getSurfaceId(): Int + + private external fun _getModuleName(): String + + private external fun _isRunning(): Boolean + + override fun setLayoutConstraints( + widthMeasureSpec: Int, + heightMeasureSpec: Int, + offsetX: Int, + offsetY: Int, + doLeftAndRightSwapInRTL: Boolean, + isRTL: Boolean, + pixelDensity: Float + ) { + setLayoutConstraintsNative( + LayoutMetricsConversions.getMinSize(widthMeasureSpec) / pixelDensity, + LayoutMetricsConversions.getMaxSize(widthMeasureSpec) / pixelDensity, + LayoutMetricsConversions.getMinSize(heightMeasureSpec) / pixelDensity, + LayoutMetricsConversions.getMaxSize(heightMeasureSpec) / pixelDensity, + offsetX / pixelDensity, + offsetY / pixelDensity, + doLeftAndRightSwapInRTL, + isRTL, + pixelDensity) + } + + private external fun setLayoutConstraintsNative( + minWidth: Float, + maxWidth: Float, + minHeight: Float, + maxHeight: Float, + offsetX: Float, + offsetY: Float, + doLeftAndRightSwapInRTL: Boolean, + isRTL: Boolean, + pixelDensity: Float + ) + + external override fun setProps(props: NativeMap) + + override fun setMountable(mountable: Boolean) { + setDisplayMode(if (mountable) DISPLAY_MODE_VISIBLE else DISPLAY_MODE_SUSPENDED) + } + + private external fun setDisplayMode(mode: Int) + + private companion object { + private const val NO_SURFACE_ID = 0 + + // Keep in sync with SurfaceHandler.cpp + const val DISPLAY_MODE_VISIBLE = 0 + const val DISPLAY_MODE_SUSPENDED = 1 + const val DISPLAY_MODE_HIDDEN = 2 + + init { + FabricSoLoader.staticInit() + } + } +} diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/fabric/SurfaceHandlerBinding.cpp b/packages/react-native/ReactAndroid/src/main/jni/react/fabric/SurfaceHandlerBinding.cpp index 37e5624fced319..50b68d631f6216 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/react/fabric/SurfaceHandlerBinding.cpp +++ b/packages/react-native/ReactAndroid/src/main/jni/react/fabric/SurfaceHandlerBinding.cpp @@ -73,9 +73,9 @@ const SurfaceHandler& SurfaceHandlerBinding::getSurfaceHandler() { void SurfaceHandlerBinding::registerNatives() { registerHybrid({ makeNativeMethod("initHybrid", SurfaceHandlerBinding::initHybrid), - makeNativeMethod("getSurfaceId", SurfaceHandlerBinding::getSurfaceId), - makeNativeMethod("isRunning", SurfaceHandlerBinding::isRunning), - makeNativeMethod("getModuleName", SurfaceHandlerBinding::getModuleName), + makeNativeMethod("_getSurfaceId", SurfaceHandlerBinding::getSurfaceId), + makeNativeMethod("_isRunning", SurfaceHandlerBinding::isRunning), + makeNativeMethod("_getModuleName", SurfaceHandlerBinding::getModuleName), makeNativeMethod( "setLayoutConstraintsNative", SurfaceHandlerBinding::setLayoutConstraints),