Fix PixelUtil OSS Build failure (#43754)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/43754

Moves off of deprecated `DisplayMetrics.scaledDensity` API to builtin font scaling API, while still clamping to max multiplier using previous logic.

Changelog: [Internal]

Reviewed By: cortinico

Differential Revision: D55623674

fbshipit-source-id: 2668eea8dbd154cc046e2c515323ad66b289dc64
This commit is contained in:
Nick Gerleman
2024-04-02 01:52:39 -07:00
committed by Facebook GitHub Bot
parent 2477fbc600
commit ac74bd8c64
@@ -8,6 +8,7 @@
package com.facebook.react.uimanager
import android.util.TypedValue
import kotlin.math.min
/** Android dp to pixel manipulation */
public object PixelUtil {
@@ -29,12 +30,13 @@ public object PixelUtil {
@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
val scaledValue = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, value, displayMetrics)
if (maxFontScale >= 1) {
return min(scaledValue, value * displayMetrics.density * maxFontScale)
}
return value * scaledDensity
return scaledValue
}
/** Convert from SP to PX */