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
This commit is contained in:
Nick Gerleman
2024-07-27 11:39:27 -07:00
committed by Facebook GitHub Bot
parent 244e242897
commit 06e38b55da
@@ -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
}