-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #751 from DataDog/marcosaia/RUM-6028/sr-rn-75-support
[RUM-6028, 0.75 Support] DrawableUtils based on RN version
- Loading branch information
Showing
9 changed files
with
180 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
...75/kotlin/com.datadog.reactnative.sessionreplay/utils/ReactViewBackgroundDrawableUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import android.graphics.drawable.Drawable | ||
import android.graphics.drawable.InsetDrawable | ||
import android.graphics.drawable.LayerDrawable | ||
import com.datadog.android.sessionreplay.model.MobileSegment | ||
import com.datadog.reactnative.sessionreplay.extensions.convertToDensityNormalized | ||
import com.datadog.reactnative.sessionreplay.utils.DrawableUtils | ||
import com.datadog.reactnative.sessionreplay.utils.formatAsRgba | ||
import com.facebook.react.common.annotations.UnstableReactNativeAPI | ||
import com.facebook.react.uimanager.LengthPercentage | ||
import com.facebook.react.uimanager.Spacing | ||
import com.facebook.react.uimanager.drawable.CSSBackgroundDrawable | ||
|
||
internal class ReactViewBackgroundDrawableUtils : DrawableUtils() { | ||
@OptIn(UnstableReactNativeAPI::class) | ||
override fun resolveShapeAndBorder( | ||
drawable: Drawable, | ||
opacity: Float, | ||
pixelDensity: Float | ||
): Pair<MobileSegment.ShapeStyle?, MobileSegment.ShapeBorder?> { | ||
if (drawable !is CSSBackgroundDrawable) { | ||
return null to null | ||
} | ||
|
||
val borderProps = resolveBorder(drawable, pixelDensity) | ||
val backgroundColor = getBackgroundColor(drawable) | ||
val colorHexString = if (backgroundColor != null) { | ||
formatAsRgba(backgroundColor) | ||
} else { | ||
return null to borderProps | ||
} | ||
|
||
return MobileSegment.ShapeStyle( | ||
colorHexString, | ||
opacity, | ||
getBorderRadius(drawable) | ||
) to borderProps | ||
} | ||
|
||
@OptIn(UnstableReactNativeAPI::class) | ||
override fun getReactBackgroundFromDrawable(drawable: Drawable?): Drawable? { | ||
if (drawable is CSSBackgroundDrawable) { | ||
return drawable | ||
} | ||
|
||
if (drawable is InsetDrawable) { | ||
return getReactBackgroundFromDrawable(drawable.drawable) | ||
} | ||
|
||
if (drawable is LayerDrawable) { | ||
for (layerNumber in 0 until drawable.numberOfLayers) { | ||
val layer = drawable.getDrawable(layerNumber) | ||
if (layer is CSSBackgroundDrawable) { | ||
return layer | ||
} | ||
} | ||
} | ||
|
||
return null | ||
} | ||
|
||
@OptIn(UnstableReactNativeAPI::class) | ||
private fun getBorderRadius(drawable: CSSBackgroundDrawable): Float { | ||
val width = drawable.intrinsicWidth.toFloat() | ||
val height = drawable.intrinsicHeight.toFloat() | ||
return drawable.borderRadius.uniform?.getRadius(width, height) ?: 0f | ||
} | ||
|
||
@OptIn(UnstableReactNativeAPI::class) | ||
private fun getBackgroundColor( | ||
backgroundDrawable: CSSBackgroundDrawable | ||
): Int? { | ||
return reflectionUtils.getDeclaredField( | ||
backgroundDrawable, | ||
COLOR_FIELD_NAME | ||
) as Int? | ||
} | ||
|
||
@OptIn(UnstableReactNativeAPI::class) | ||
private fun resolveBorder( | ||
backgroundDrawable: CSSBackgroundDrawable, | ||
pixelDensity: Float | ||
): MobileSegment.ShapeBorder { | ||
val borderWidth = | ||
backgroundDrawable.fullBorderWidth.toLong().convertToDensityNormalized(pixelDensity) | ||
val borderColor = formatAsRgba(backgroundDrawable.getBorderColor(Spacing.ALL)) | ||
|
||
return MobileSegment.ShapeBorder( | ||
color = borderColor, | ||
width = borderWidth | ||
) | ||
} | ||
|
||
private fun LengthPercentage?.getRadius(width: Float, height: Float) = this | ||
?.resolve(width, height) | ||
?.let { (it.horizontal + it.vertical) / 2f } | ||
?: 0f | ||
|
||
private companion object { | ||
private const val COLOR_FIELD_NAME = "mColor" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.