From 06e38b55da4e958f365bb38e7d5b74a2c01bf6cc Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Sat, 27 Jul 2024 11:39:27 -0700 Subject: [PATCH] Explicitly handle NaN values in PixelUtil (#45690) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/45690 ViewManager layer often uses `YogaConstants.UNDEFINED` (`NaN`) as null-state value. Teaching PixelUtil how to handle `NaN` values makes glue code around easier. I think this technically isn't needed, since the resultant operations would become `NaN`, but it seems like poor form/hard to reason about to propagate NaN into arithmetic or library functions. Changelog: [Internal] Reviewed By: cortinico Differential Revision: D60265329 fbshipit-source-id: b2f4abaefb30ebd58c2644d072bb7e5bc4b3ee7b --- .../java/com/facebook/react/uimanager/PixelUtil.kt | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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 index 48f1f1e2f24..3d379268e8e 100644 --- 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 @@ -15,6 +15,10 @@ public object PixelUtil { /** Convert from DIP to PX */ @JvmStatic public fun toPixelFromDIP(value: Float): Float { + if (value.isNaN()) { + return Float.NaN + } + return TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, value, DisplayMetricsHolder.getWindowDisplayMetrics()) } @@ -29,6 +33,10 @@ public object PixelUtil { @JvmOverloads @JvmStatic public fun toPixelFromSP(value: Float, maxFontScale: Float = Float.NaN): Float { + if (value.isNaN()) { + return Float.NaN + } + val displayMetrics = DisplayMetricsHolder.getWindowDisplayMetrics() val scaledValue = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, value, displayMetrics) @@ -48,6 +56,10 @@ public object PixelUtil { /** Convert from PX to DP */ @JvmStatic public fun toDIPFromPixel(value: Float): Float { + if (value.isNaN()) { + return Float.NaN + } + return value / DisplayMetricsHolder.getWindowDisplayMetrics().density }