Skip to content

Commit

Permalink
Convert react/uimanager/*Util.java -> Kotlin (facebook#43729)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#43729

# Changelog:
[Internal] -

As in the title, converts corresponding type declarations in `react/uimanager/*Util.kt` to Kotlin.

Differential Revision: https://internalfb.com/D55574531
  • Loading branch information
rshest authored and facebook-github-bot committed Mar 31, 2024
1 parent b4e3ae5 commit 2bdbe2e
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 151 deletions.
33 changes: 17 additions & 16 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -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 <init> ()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 {
Expand Down Expand Up @@ -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 <init> ()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 {
Expand Down Expand Up @@ -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 <init> ()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 {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/**
Expand Down

0 comments on commit 2bdbe2e

Please sign in to comment.