From 2bdbe2e8d690884299889926a26cebbfd18cb720 Mon Sep 17 00:00:00 2001 From: Ruslan Shestopalyuk Date: Sun, 31 Mar 2024 15:51:52 -0700 Subject: [PATCH] Convert react/uimanager/*Util.java -> Kotlin (#43729) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/43729 # Changelog: [Internal] - As in the title, converts corresponding type declarations in `react/uimanager/*Util.kt` to Kotlin. Differential Revision: https://internalfb.com/D55574531 --- .../ReactAndroid/api/ReactAndroid.api | 33 +++++----- .../facebook/react/uimanager/FloatUtil.java | 23 ------- .../com/facebook/react/uimanager/FloatUtil.kt | 21 +++++++ .../facebook/react/uimanager/PixelUtil.java | 62 ------------------- .../com/facebook/react/uimanager/PixelUtil.kt | 56 +++++++++++++++++ .../react/uimanager/RootViewUtil.java | 49 --------------- .../facebook/react/uimanager/RootViewUtil.kt | 45 ++++++++++++++ .../uimanager/events/NativeGestureUtil.java | 5 +- 8 files changed, 143 insertions(+), 151 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/FloatUtil.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/FloatUtil.kt delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/PixelUtil.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/PixelUtil.kt delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/RootViewUtil.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/RootViewUtil.kt diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index 248f3343de4115..2ba2be2ff4c636 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -4025,9 +4025,9 @@ public abstract interface class com/facebook/react/uimanager/FabricViewStateMana public abstract fun getStateUpdate ()Lcom/facebook/react/bridge/WritableMap; } -public class com/facebook/react/uimanager/FloatUtil { - public fun ()V - public static fun floatsEqual (FF)Z +public final class com/facebook/react/uimanager/FloatUtil { + public static final field INSTANCE Lcom/facebook/react/uimanager/FloatUtil; + public static final fun floatsEqual (FF)Z } public abstract class com/facebook/react/uimanager/GuardedFrameCallback : android/view/Choreographer$FrameCallback { @@ -4213,15 +4213,16 @@ public class com/facebook/react/uimanager/OnLayoutEvent : com/facebook/react/uim public fun onDispose ()V } -public class com/facebook/react/uimanager/PixelUtil { - public fun ()V - public static fun getDisplayMetricDensity ()F - public static fun toDIPFromPixel (F)F - public static fun toPixelFromDIP (D)F - public static fun toPixelFromDIP (F)F - public static fun toPixelFromSP (D)F - public static fun toPixelFromSP (F)F - public static fun toPixelFromSP (FF)F +public final class com/facebook/react/uimanager/PixelUtil { + public static final field INSTANCE Lcom/facebook/react/uimanager/PixelUtil; + public static final fun getDisplayMetricDensity ()F + public static final fun toDIPFromPixel (F)F + public static final fun toPixelFromDIP (D)F + public static final fun toPixelFromDIP (F)F + public static final fun toPixelFromSP (D)F + public static final fun toPixelFromSP (F)F + public static final fun toPixelFromSP (FF)F + public static synthetic fun toPixelFromSP$default (FFILjava/lang/Object;)F } public final class com/facebook/react/uimanager/PointerEvents : java/lang/Enum { @@ -4776,10 +4777,10 @@ public class com/facebook/react/uimanager/RootViewManager$$PropsSetter : com/fac public synthetic fun setProperty (Lcom/facebook/react/uimanager/ViewManager;Landroid/view/View;Ljava/lang/String;Ljava/lang/Object;)V } -public class com/facebook/react/uimanager/RootViewUtil { - public fun ()V - public static fun getRootView (Landroid/view/View;)Lcom/facebook/react/uimanager/RootView; - public static fun getViewportOffset (Landroid/view/View;)Landroid/graphics/Point; +public final class com/facebook/react/uimanager/RootViewUtil { + public static final field INSTANCE Lcom/facebook/react/uimanager/RootViewUtil; + public static final fun getRootView (Landroid/view/View;)Lcom/facebook/react/uimanager/RootView; + public static final fun getViewportOffset (Landroid/view/View;)Landroid/graphics/Point; } public abstract class com/facebook/react/uimanager/SimpleViewManager : com/facebook/react/uimanager/BaseViewManager { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/FloatUtil.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/FloatUtil.java deleted file mode 100644 index 1d1a5d56a56bf6..00000000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/FloatUtil.java +++ /dev/null @@ -1,23 +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.uimanager; - -import com.facebook.infer.annotation.Nullsafe; - -@Nullsafe(Nullsafe.Mode.LOCAL) -public class FloatUtil { - - private static final float EPSILON = .00001f; - - public static boolean floatsEqual(float f1, float f2) { - if (Float.isNaN(f1) || Float.isNaN(f2)) { - return Float.isNaN(f1) && Float.isNaN(f2); - } - return Math.abs(f2 - f1) < EPSILON; - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/FloatUtil.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/FloatUtil.kt new file mode 100644 index 00000000000000..e5ad28d758a352 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/FloatUtil.kt @@ -0,0 +1,21 @@ +/* + * 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.uimanager + +import kotlin.math.abs + +public object FloatUtil { + private const val EPSILON = .00001f + + @JvmStatic + public fun floatsEqual(f1: Float, f2: Float): Boolean { + return if (java.lang.Float.isNaN(f1) || java.lang.Float.isNaN(f2)) { + java.lang.Float.isNaN(f1) && java.lang.Float.isNaN(f2) + } else abs(f2 - f1) < EPSILON + } +} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/PixelUtil.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/PixelUtil.java deleted file mode 100644 index 9af69391b86b04..00000000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/PixelUtil.java +++ /dev/null @@ -1,62 +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.uimanager; - -import android.util.DisplayMetrics; -import android.util.TypedValue; -import com.facebook.infer.annotation.Nullsafe; - -/** Android dp to pixel manipulation */ -@Nullsafe(Nullsafe.Mode.LOCAL) -public class PixelUtil { - - /** Convert from DIP to PX */ - public static float toPixelFromDIP(float value) { - return TypedValue.applyDimension( - TypedValue.COMPLEX_UNIT_DIP, value, DisplayMetricsHolder.getWindowDisplayMetrics()); - } - - /** Convert from DIP to PX */ - public static float toPixelFromDIP(double value) { - return toPixelFromDIP((float) value); - } - - /** Convert from SP to PX */ - public static float toPixelFromSP(float value) { - return toPixelFromSP(value, Float.NaN); - } - - /** Convert from SP to PX */ - public static float toPixelFromSP(float value, float maxFontScale) { - DisplayMetrics displayMetrics = DisplayMetricsHolder.getWindowDisplayMetrics(); - float scaledDensity = displayMetrics.scaledDensity; - float currentFontScale = scaledDensity / displayMetrics.density; - if (maxFontScale >= 1 && maxFontScale < currentFontScale) { - scaledDensity = displayMetrics.density * maxFontScale; - } - - return value * scaledDensity; - } - - /** Convert from SP to PX */ - public static float toPixelFromSP(double value) { - return toPixelFromSP((float) value); - } - - /** Convert from PX to DP */ - public static float toDIPFromPixel(float value) { - return value / DisplayMetricsHolder.getWindowDisplayMetrics().density; - } - - /** - * @return {@link float} that represents the density of the display metrics for device screen. - */ - public static float getDisplayMetricDensity() { - return DisplayMetricsHolder.getWindowDisplayMetrics().density; - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/PixelUtil.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/PixelUtil.kt new file mode 100644 index 00000000000000..99a67dafe6a94b --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/PixelUtil.kt @@ -0,0 +1,56 @@ +/* + * 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.uimanager + +import android.util.TypedValue + +/** Android dp to pixel manipulation */ +public object PixelUtil { + /** Convert from DIP to PX */ + @JvmStatic + public fun toPixelFromDIP(value: Float): Float { + return TypedValue.applyDimension( + TypedValue.COMPLEX_UNIT_DIP, value, DisplayMetricsHolder.getWindowDisplayMetrics()) + } + + /** Convert from DIP to PX */ + @JvmStatic + public fun toPixelFromDIP(value: Double): Float { + return toPixelFromDIP(value.toFloat()) + } + + /** Convert from SP to PX */ + @JvmOverloads + @JvmStatic + public fun toPixelFromSP(value: Float, maxFontScale: Float = Float.NaN): Float { + val displayMetrics = DisplayMetricsHolder.getWindowDisplayMetrics() + var scaledDensity = displayMetrics.scaledDensity + val currentFontScale = scaledDensity / displayMetrics.density + if (maxFontScale >= 1 && maxFontScale < currentFontScale) { + scaledDensity = displayMetrics.density * maxFontScale + } + return value * scaledDensity + } + + /** Convert from SP to PX */ + @JvmStatic + public fun toPixelFromSP(value: Double): Float { + return toPixelFromSP(value.toFloat()) + } + + /** Convert from PX to DP */ + @JvmStatic + public fun toDIPFromPixel(value: Float): Float { + return value / DisplayMetricsHolder.getWindowDisplayMetrics().density + } + + /** @return [float] that represents the density of the display metrics for device screen. */ + @JvmStatic + public fun getDisplayMetricDensity(): Float = + DisplayMetricsHolder.getWindowDisplayMetrics().density +} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/RootViewUtil.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/RootViewUtil.java deleted file mode 100644 index 466b87f1b0e16e..00000000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/RootViewUtil.java +++ /dev/null @@ -1,49 +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.uimanager; - -import android.graphics.Point; -import android.graphics.Rect; -import android.view.View; -import android.view.ViewParent; -import androidx.annotation.UiThread; -import com.facebook.infer.annotation.Assertions; - -public class RootViewUtil { - - /** Returns the root view of a given view in a react application. */ - public static RootView getRootView(View reactView) { - View current = reactView; - while (true) { - if (current instanceof RootView) { - return (RootView) current; - } - ViewParent next = current.getParent(); - if (next == null) { - return null; - } - Assertions.assertCondition(next instanceof View); - current = (View) next; - } - } - - @UiThread - public static Point getViewportOffset(View v) { - int[] locationInWindow = new int[2]; - v.getLocationInWindow(locationInWindow); - - // we need to subtract visibleWindowCoords - to subtract possible window insets, split - // screen or multi window - Rect visibleWindowFrame = new Rect(); - v.getWindowVisibleDisplayFrame(visibleWindowFrame); - locationInWindow[0] -= visibleWindowFrame.left; - locationInWindow[1] -= visibleWindowFrame.top; - - return new Point(locationInWindow[0], locationInWindow[1]); - } -} 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 new file mode 100644 index 00000000000000..2dc7c22624cd6d --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/RootViewUtil.kt @@ -0,0 +1,45 @@ +/* + * 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.uimanager + +import android.graphics.Point +import android.graphics.Rect +import android.view.View +import androidx.annotation.UiThread +import com.facebook.infer.annotation.Assertions + +public object RootViewUtil { + /** Returns the root view of a given view in a react application. */ + @JvmStatic + public fun getRootView(reactView: View): RootView? { + var current = reactView + while (true) { + if (current is RootView) { + return current + } + val next = current.parent ?: return null + Assertions.assertCondition(next is View) + current = next as View + } + } + + @UiThread + @JvmStatic + public fun getViewportOffset(v: View): Point { + 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 + return Point(locationInWindow[0], locationInWindow[1]) + } +} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/NativeGestureUtil.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/NativeGestureUtil.java index b0dd28a34b7538..940f15eec82735 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/NativeGestureUtil.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/NativeGestureUtil.java @@ -26,7 +26,10 @@ public class NativeGestureUtil { * @param event the MotionEvent that caused the gesture to be started */ public static void notifyNativeGestureStarted(View view, MotionEvent event) { - RootViewUtil.getRootView(view).onChildStartedNativeGesture(view, event); + RootView rootView = RootViewUtil.getRootView(view); + if (rootView != null) { + rootView.onChildStartedNativeGesture(view, event); + } } /**