Add support for assymetrical bonder radii when using % (#45985)

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

As title.

We can now create ellipses when using percentages. The algorithm for this is still flawed and to get it to be a 1:1 to web it will probably require a re-write of some of the logic but this should get us closer for now.

Some examples:

1. Border thinning on large single corner radii (100%)
 {F1798145800}
2. Thinning gets worse when having irregular border colors (100%)
 {F1798148002}

Changelog: [Internal]

Reviewed By: NickGerleman

Differential Revision: D61025927

fbshipit-source-id: 218d44af014bc8351c329ff1bca82658aebac38c
This commit is contained in:
Jorge Cabiedes Acosta authored and Facebook GitHub Bot committed 2024-08-15 12:35:15 -07:00
1 parent abc322c708
commit d69dbd6f15
11 files changed
+257 -131

No files matched your search

@@ -4365,7 +4365,7 @@ public final class com/facebook/react/uimanager/LengthPercentage {
public fun equals (Ljava/lang/Object;)Z
public final fun getType ()Lcom/facebook/react/uimanager/LengthPercentageType;
public fun hashCode ()I
public final fun resolve (FF)F
public final fun resolve (FF)Lcom/facebook/react/uimanager/style/CornerRadii;
public static final fun setFromDynamic (Lcom/facebook/react/bridge/Dynamic;)Lcom/facebook/react/uimanager/LengthPercentage;
public fun toString ()Ljava/lang/String;
}
@@ -6119,19 +6119,19 @@ public final class com/facebook/react/uimanager/style/BoxShadow$Companion {
public final class com/facebook/react/uimanager/style/ComputedBorderRadius {
public fun <init> ()V
public fun <init> (FFFF)V
public final fun component1 ()F
public final fun component2 ()F
public final fun component3 ()F
public final fun component4 ()F
public final fun copy (FFFF)Lcom/facebook/react/uimanager/style/ComputedBorderRadius;
public static synthetic fun copy$default (Lcom/facebook/react/uimanager/style/ComputedBorderRadius;FFFFILjava/lang/Object;)Lcom/facebook/react/uimanager/style/ComputedBorderRadius;
public fun <init> (Lcom/facebook/react/uimanager/style/CornerRadii;Lcom/facebook/react/uimanager/style/CornerRadii;Lcom/facebook/react/uimanager/style/CornerRadii;Lcom/facebook/react/uimanager/style/CornerRadii;)V
public final fun component1 ()Lcom/facebook/react/uimanager/style/CornerRadii;
public final fun component2 ()Lcom/facebook/react/uimanager/style/CornerRadii;
public final fun component3 ()Lcom/facebook/react/uimanager/style/CornerRadii;
public final fun component4 ()Lcom/facebook/react/uimanager/style/CornerRadii;
public final fun copy (Lcom/facebook/react/uimanager/style/CornerRadii;Lcom/facebook/react/uimanager/style/CornerRadii;Lcom/facebook/react/uimanager/style/CornerRadii;Lcom/facebook/react/uimanager/style/CornerRadii;)Lcom/facebook/react/uimanager/style/ComputedBorderRadius;
public static synthetic fun copy$default (Lcom/facebook/react/uimanager/style/ComputedBorderRadius;Lcom/facebook/react/uimanager/style/CornerRadii;Lcom/facebook/react/uimanager/style/CornerRadii;Lcom/facebook/react/uimanager/style/CornerRadii;Lcom/facebook/react/uimanager/style/CornerRadii;ILjava/lang/Object;)Lcom/facebook/react/uimanager/style/ComputedBorderRadius;
public fun equals (Ljava/lang/Object;)Z
public final fun get (Lcom/facebook/react/uimanager/style/ComputedBorderRadiusProp;)F
public final fun getBottomLeft ()F
public final fun getBottomRight ()F
public final fun getTopLeft ()F
public final fun getTopRight ()F
public final fun get (Lcom/facebook/react/uimanager/style/ComputedBorderRadiusProp;)Lcom/facebook/react/uimanager/style/CornerRadii;
public final fun getBottomLeft ()Lcom/facebook/react/uimanager/style/CornerRadii;
public final fun getBottomRight ()Lcom/facebook/react/uimanager/style/CornerRadii;
public final fun getTopLeft ()Lcom/facebook/react/uimanager/style/CornerRadii;
public final fun getTopRight ()Lcom/facebook/react/uimanager/style/CornerRadii;
public final fun hasRoundedBorders ()Z
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
@@ -6147,6 +6147,22 @@ public final class com/facebook/react/uimanager/style/ComputedBorderRadiusProp :
public static fun values ()[Lcom/facebook/react/uimanager/style/ComputedBorderRadiusProp;
}
public final class com/facebook/react/uimanager/style/CornerRadii {
public fun <init> ()V
public fun <init> (FF)V
public synthetic fun <init> (FFILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun component1 ()F
public final fun component2 ()F
public final fun copy (FF)Lcom/facebook/react/uimanager/style/CornerRadii;
public static synthetic fun copy$default (Lcom/facebook/react/uimanager/style/CornerRadii;FFILjava/lang/Object;)Lcom/facebook/react/uimanager/style/CornerRadii;
public fun equals (Ljava/lang/Object;)Z
public final fun getHorizontal ()F
public final fun getVertical ()F
public fun hashCode ()I
public final fun toPixelFromDIP ()Lcom/facebook/react/uimanager/style/CornerRadii;
public fun toString ()Ljava/lang/String;
}
public final class com/facebook/react/uimanager/style/Gradient {
public fun <init> (Lcom/facebook/react/bridge/ReadableMap;)V
public final fun getShader (Landroid/graphics/Rect;)Landroid/graphics/Shader;
@@ -11,6 +11,7 @@ import com.facebook.common.logging.FLog
import com.facebook.react.bridge.Dynamic
import com.facebook.react.bridge.ReadableType
import com.facebook.react.common.ReactConstants
import com.facebook.react.uimanager.style.CornerRadii
import java.lang.NumberFormatException
public enum class LengthPercentageType {
@@ -61,12 +62,12 @@ public data class LengthPercentage(
}
}
public fun resolve(width: Float, height: Float): Float {
public fun resolve(width: Float, height: Float): CornerRadii {
if (type == LengthPercentageType.PERCENT) {
return (value / 100) * Math.min(width, height)
return CornerRadii((value / 100) * width, (value / 100) * height)
}
return value
return CornerRadii(value, value)
}
public constructor() : this(0f, LengthPercentageType.POINT)
@@ -42,6 +42,7 @@ import com.facebook.react.uimanager.style.BorderRadiusProp;
import com.facebook.react.uimanager.style.BorderRadiusStyle;
import com.facebook.react.uimanager.style.BorderStyle;
import com.facebook.react.uimanager.style.ComputedBorderRadius;
import com.facebook.react.uimanager.style.CornerRadii;
import com.facebook.react.uimanager.style.Gradient;
import java.util.Locale;
import java.util.Objects;
@@ -666,21 +667,27 @@ public class CSSBackgroundDrawable extends Drawable {
mContext,
PixelUtil.toDIPFromPixel(mOuterClipTempRectForBorderRadius.width()),
PixelUtil.toDIPFromPixel(mOuterClipTempRectForBorderRadius.height()));
float topLeftRadius = PixelUtil.toPixelFromDIP(mComputedBorderRadius.getTopLeft());
float topRightRadius = PixelUtil.toPixelFromDIP(mComputedBorderRadius.getTopRight());
float bottomLeftRadius = PixelUtil.toPixelFromDIP(mComputedBorderRadius.getBottomLeft());
float bottomRightRadius = PixelUtil.toPixelFromDIP(mComputedBorderRadius.getBottomRight());
CornerRadii topLeftRadius = mComputedBorderRadius.getTopLeft().toPixelFromDIP();
CornerRadii topRightRadius = mComputedBorderRadius.getTopRight().toPixelFromDIP();
CornerRadii bottomLeftRadius = mComputedBorderRadius.getBottomLeft().toPixelFromDIP();
CornerRadii bottomRightRadius = mComputedBorderRadius.getBottomRight().toPixelFromDIP();
final float innerTopLeftRadiusX = getInnerBorderRadius(topLeftRadius, borderWidth.left);
final float innerTopLeftRadiusY = getInnerBorderRadius(topLeftRadius, borderWidth.top);
final float innerTopRightRadiusX = getInnerBorderRadius(topRightRadius, borderWidth.right);
final float innerTopRightRadiusY = getInnerBorderRadius(topRightRadius, borderWidth.top);
final float innerTopLeftRadiusX =
getInnerBorderRadius(topLeftRadius.getHorizontal(), borderWidth.left);
final float innerTopLeftRadiusY =
getInnerBorderRadius(topLeftRadius.getVertical(), borderWidth.top);
final float innerTopRightRadiusX =
getInnerBorderRadius(topRightRadius.getHorizontal(), borderWidth.right);
final float innerTopRightRadiusY =
getInnerBorderRadius(topRightRadius.getVertical(), borderWidth.top);
final float innerBottomRightRadiusX =
getInnerBorderRadius(bottomRightRadius, borderWidth.right);
getInnerBorderRadius(bottomRightRadius.getHorizontal(), borderWidth.right);
final float innerBottomRightRadiusY =
getInnerBorderRadius(bottomRightRadius, borderWidth.bottom);
final float innerBottomLeftRadiusX = getInnerBorderRadius(bottomLeftRadius, borderWidth.left);
final float innerBottomLeftRadiusY = getInnerBorderRadius(bottomLeftRadius, borderWidth.bottom);
getInnerBorderRadius(bottomRightRadius.getVertical(), borderWidth.bottom);
final float innerBottomLeftRadiusX =
getInnerBorderRadius(bottomLeftRadius.getHorizontal(), borderWidth.left);
final float innerBottomLeftRadiusY =
getInnerBorderRadius(bottomLeftRadius.getVertical(), borderWidth.bottom);
mInnerClipPathForBorderRadius.addRoundRect(
mInnerClipTempRectForBorderRadius,
@@ -720,14 +727,14 @@ public class CSSBackgroundDrawable extends Drawable {
mOuterClipPathForBorderRadius.addRoundRect(
mOuterClipTempRectForBorderRadius,
new float[] {
topLeftRadius,
topLeftRadius,
topRightRadius,
topRightRadius,
bottomRightRadius,
bottomRightRadius,
bottomLeftRadius,
bottomLeftRadius
topLeftRadius.getHorizontal(),
topLeftRadius.getVertical(),
topRightRadius.getHorizontal(),
topRightRadius.getVertical(),
bottomRightRadius.getHorizontal(),
bottomRightRadius.getVertical(),
bottomLeftRadius.getHorizontal(),
bottomLeftRadius.getVertical()
},
Path.Direction.CW);
@@ -740,14 +747,14 @@ public class CSSBackgroundDrawable extends Drawable {
mPathForBorderRadiusOutline.addRoundRect(
mTempRectForBorderRadiusOutline,
new float[] {
topLeftRadius + extraRadiusForOutline,
topLeftRadius + extraRadiusForOutline,
topRightRadius + extraRadiusForOutline,
topRightRadius + extraRadiusForOutline,
bottomRightRadius + extraRadiusForOutline,
bottomRightRadius + extraRadiusForOutline,
bottomLeftRadius + extraRadiusForOutline,
bottomLeftRadius + extraRadiusForOutline
topLeftRadius.getHorizontal() + extraRadiusForOutline,
topLeftRadius.getVertical() + extraRadiusForOutline,
topRightRadius.getHorizontal() + extraRadiusForOutline,
topRightRadius.getVertical() + extraRadiusForOutline,
bottomRightRadius.getHorizontal() + extraRadiusForOutline,
bottomRightRadius.getVertical() + extraRadiusForOutline,
bottomLeftRadius.getHorizontal() + extraRadiusForOutline,
bottomLeftRadius.getVertical() + extraRadiusForOutline
},
Path.Direction.CW);
@@ -755,29 +762,41 @@ public class CSSBackgroundDrawable extends Drawable {
mTempRectForCenterDrawPath,
new float[] {
Math.max(
topLeftRadius - borderWidth.left * 0.5f,
(borderWidth.left > 0.0f) ? (topLeftRadius / borderWidth.left) : 0.0f),
topLeftRadius.getHorizontal() - borderWidth.left * 0.5f,
(borderWidth.left > 0.0f)
? (topLeftRadius.getHorizontal() / borderWidth.left)
: 0.0f),
Math.max(
topLeftRadius - borderWidth.top * 0.5f,
(borderWidth.top > 0.0f) ? (topLeftRadius / borderWidth.top) : 0.0f),
topLeftRadius.getVertical() - borderWidth.top * 0.5f,
(borderWidth.top > 0.0f) ? (topLeftRadius.getVertical() / borderWidth.top) : 0.0f),
Math.max(
topRightRadius - borderWidth.right * 0.5f,
(borderWidth.right > 0.0f) ? (topRightRadius / borderWidth.right) : 0.0f),
topRightRadius.getHorizontal() - borderWidth.right * 0.5f,
(borderWidth.right > 0.0f)
? (topRightRadius.getHorizontal() / borderWidth.right)
: 0.0f),
Math.max(
topRightRadius - borderWidth.top * 0.5f,
(borderWidth.top > 0.0f) ? (topRightRadius / borderWidth.top) : 0.0f),
topRightRadius.getVertical() - borderWidth.top * 0.5f,
(borderWidth.top > 0.0f) ? (topRightRadius.getVertical() / borderWidth.top) : 0.0f),
Math.max(
bottomRightRadius - borderWidth.right * 0.5f,
(borderWidth.right > 0.0f) ? (bottomRightRadius / borderWidth.right) : 0.0f),
bottomRightRadius.getHorizontal() - borderWidth.right * 0.5f,
(borderWidth.right > 0.0f)
? (bottomRightRadius.getHorizontal() / borderWidth.right)
: 0.0f),
Math.max(
bottomRightRadius - borderWidth.bottom * 0.5f,
(borderWidth.bottom > 0.0f) ? (bottomRightRadius / borderWidth.bottom) : 0.0f),
bottomRightRadius.getVertical() - borderWidth.bottom * 0.5f,
(borderWidth.bottom > 0.0f)
? (bottomRightRadius.getVertical() / borderWidth.bottom)
: 0.0f),
Math.max(
bottomLeftRadius - borderWidth.left * 0.5f,
(borderWidth.left > 0.0f) ? (bottomLeftRadius / borderWidth.left) : 0.0f),
bottomLeftRadius.getHorizontal() - borderWidth.left * 0.5f,
(borderWidth.left > 0.0f)
? (bottomLeftRadius.getHorizontal() / borderWidth.left)
: 0.0f),
Math.max(
bottomLeftRadius - borderWidth.bottom * 0.5f,
(borderWidth.bottom > 0.0f) ? (bottomLeftRadius / borderWidth.bottom) : 0.0f)
bottomLeftRadius.getVertical() - borderWidth.bottom * 0.5f,
(borderWidth.bottom > 0.0f)
? (bottomLeftRadius.getVertical() / borderWidth.bottom)
: 0.0f)
},
Path.Direction.CW);
@@ -141,16 +141,19 @@ internal class InsetBoxShadowDrawable(
val computedBorderRadii = background.computedBorderRadius
val borderWidth = background.getDirectionAwareBorderInsets()
val topLeftRadius = PixelUtil.toPixelFromDIP(computedBorderRadii.topLeft)
val topRightRadius = PixelUtil.toPixelFromDIP(computedBorderRadii.topRight)
val bottomLeftRadius = PixelUtil.toPixelFromDIP(computedBorderRadii.bottomLeft)
val bottomRightRadius = PixelUtil.toPixelFromDIP(computedBorderRadii.bottomRight)
val topLeftRadius = computedBorderRadii.topLeft.toPixelFromDIP()
val topRightRadius = computedBorderRadii.topRight.toPixelFromDIP()
val bottomLeftRadius = computedBorderRadii.bottomLeft.toPixelFromDIP()
val bottomRightRadius = computedBorderRadii.bottomRight.toPixelFromDIP()
val innerTopLeftRadius = background.getInnerBorderRadius(topLeftRadius, borderWidth.left)
val innerTopRightRadius = background.getInnerBorderRadius(topRightRadius, borderWidth.right)
val innerTopLeftRadius =
background.getInnerBorderRadius(topLeftRadius.horizontal, borderWidth.left)
val innerTopRightRadius =
background.getInnerBorderRadius(topRightRadius.horizontal, borderWidth.right)
val innerBottomRightRadius =
background.getInnerBorderRadius(bottomRightRadius, borderWidth.right)
val innerBottomLeftRadius = background.getInnerBorderRadius(bottomLeftRadius, borderWidth.left)
background.getInnerBorderRadius(bottomRightRadius.horizontal, borderWidth.right)
val innerBottomLeftRadius =
background.getInnerBorderRadius(bottomLeftRadius.horizontal, borderWidth.left)
val spreadWithDirection = -spread.toFloat()
return BorderRadiusStyle(
@@ -23,6 +23,7 @@ import com.facebook.react.uimanager.FilterHelper
import com.facebook.react.uimanager.PixelUtil
import com.facebook.react.uimanager.style.BorderRadiusStyle
import com.facebook.react.uimanager.style.ComputedBorderRadius
import com.facebook.react.uimanager.style.CornerRadii
import kotlin.math.roundToInt
private const val TAG = "OutsetBoxShadowDrawable"
@@ -96,19 +97,44 @@ internal class OutsetBoxShadowDrawable(
val computedBorderRadii =
borderRadius?.resolve(layoutDirection, context, resolutionWidth, resolutionHeight)?.let {
ComputedBorderRadius(
topLeft = PixelUtil.toPixelFromDIP(it.topLeft),
topRight = PixelUtil.toPixelFromDIP(it.topRight),
bottomLeft = PixelUtil.toPixelFromDIP(it.bottomLeft),
bottomRight = PixelUtil.toPixelFromDIP(it.bottomRight))
topLeft =
CornerRadii(
PixelUtil.toPixelFromDIP(it.topLeft.horizontal),
PixelUtil.toPixelFromDIP(it.topLeft.vertical)),
topRight =
CornerRadii(
PixelUtil.toPixelFromDIP(it.topRight.horizontal),
PixelUtil.toPixelFromDIP(it.topRight.vertical)),
bottomLeft =
CornerRadii(
PixelUtil.toPixelFromDIP(it.bottomLeft.horizontal),
PixelUtil.toPixelFromDIP(it.bottomRight.vertical)),
bottomRight =
CornerRadii(
PixelUtil.toPixelFromDIP(it.bottomRight.horizontal),
PixelUtil.toPixelFromDIP(it.bottomRight.vertical)),
)
}
val shadowBorderRadii =
computedBorderRadii?.let { radii ->
ComputedBorderRadius(
topLeft = adjustRadiusForSpread(radii.topLeft, spreadExtent.toFloat()),
topRight = adjustRadiusForSpread(radii.topRight, spreadExtent.toFloat()),
bottomRight = adjustRadiusForSpread(radii.bottomRight, spreadExtent.toFloat()),
bottomLeft = adjustRadiusForSpread(radii.bottomLeft, spreadExtent.toFloat()),
topLeft =
CornerRadii(
adjustRadiusForSpread(radii.topLeft.horizontal, spreadExtent.toFloat()),
adjustRadiusForSpread(radii.topLeft.vertical, spreadExtent.toFloat())),
topRight =
CornerRadii(
adjustRadiusForSpread(radii.topRight.horizontal, spreadExtent.toFloat()),
adjustRadiusForSpread(radii.topRight.vertical, spreadExtent.toFloat())),
bottomRight =
CornerRadii(
adjustRadiusForSpread(radii.bottomRight.horizontal, spreadExtent.toFloat()),
adjustRadiusForSpread(radii.bottomRight.vertical, spreadExtent.toFloat())),
bottomLeft =
CornerRadii(
adjustRadiusForSpread(radii.bottomLeft.horizontal, spreadExtent.toFloat()),
adjustRadiusForSpread(radii.bottomLeft.vertical, spreadExtent.toFloat())),
)
}
@@ -117,7 +143,6 @@ internal class OutsetBoxShadowDrawable(
lastBorderRadius != shadowBorderRadii) {
lastBounds = shadowShapeBounds
lastBorderRadius = shadowBorderRadii
// We remove the portion of the shadow which overlaps the background border box, to avoid
// showing the shadow shape e.g. behind a transparent background. There may be a subpixel gap
// between the border box path, and the edge of border rendering, so we slightly inflate the
@@ -130,28 +155,28 @@ internal class OutsetBoxShadowDrawable(
shadowClipOutPath.addRoundRect(
subpixelInsetBounds,
floatArrayOf(
computedBorderRadii.topLeft,
computedBorderRadii.topLeft,
computedBorderRadii.topRight,
computedBorderRadii.topRight,
computedBorderRadii.bottomRight,
computedBorderRadii.bottomRight,
computedBorderRadii.bottomLeft,
computedBorderRadii.bottomLeft),
computedBorderRadii.topLeft.horizontal,
computedBorderRadii.topLeft.vertical,
computedBorderRadii.topRight.horizontal,
computedBorderRadii.topRight.vertical,
computedBorderRadii.bottomRight.horizontal,
computedBorderRadii.bottomRight.vertical,
computedBorderRadii.bottomLeft.horizontal,
computedBorderRadii.bottomLeft.vertical),
Path.Direction.CW)
shadowOuterPath.rewind()
shadowOuterPath.addRoundRect(
RectF(shadowShapeBounds),
floatArrayOf(
shadowBorderRadii.topLeft,
shadowBorderRadii.topLeft,
shadowBorderRadii.topRight,
shadowBorderRadii.topRight,
shadowBorderRadii.bottomRight,
shadowBorderRadii.bottomRight,
shadowBorderRadii.bottomLeft,
shadowBorderRadii.bottomLeft),
shadowBorderRadii.topLeft.horizontal,
shadowBorderRadii.topLeft.vertical,
shadowBorderRadii.topRight.horizontal,
shadowBorderRadii.topRight.vertical,
shadowBorderRadii.bottomRight.horizontal,
shadowBorderRadii.bottomRight.vertical,
shadowBorderRadii.bottomLeft.horizontal,
shadowBorderRadii.bottomLeft.vertical),
Path.Direction.CW)
}
@@ -107,37 +107,52 @@ public data class BorderRadiusStyle(
width: Float,
height: Float,
): ComputedBorderRadius {
val zeroRadii: CornerRadii = CornerRadii(0f, 0f)
return when (layoutDirection) {
LayoutDirection.LTR ->
ComputedBorderRadius(
topLeft =
(startStart ?: topStart ?: topLeft ?: uniform)?.resolve(width, height) ?: 0f,
topRight = (endStart ?: topEnd ?: topRight ?: uniform)?.resolve(width, height) ?: 0f,
(startStart ?: topStart ?: topLeft ?: uniform)?.resolve(width, height)
?: zeroRadii,
topRight =
(endStart ?: topEnd ?: topRight ?: uniform)?.resolve(width, height) ?: zeroRadii,
bottomLeft =
(startEnd ?: bottomStart ?: bottomLeft ?: uniform)?.resolve(width, height) ?: 0f,
(startEnd ?: bottomStart ?: bottomLeft ?: uniform)?.resolve(width, height)
?: zeroRadii,
bottomRight =
(endEnd ?: bottomEnd ?: bottomRight ?: uniform)?.resolve(width, height) ?: 0f,
(endEnd ?: bottomEnd ?: bottomRight ?: uniform)?.resolve(width, height)
?: zeroRadii,
)
LayoutDirection.RTL ->
if (I18nUtil.instance.doLeftAndRightSwapInRTL(context)) {
ComputedBorderRadius(
topLeft = (endStart ?: topEnd ?: topRight ?: uniform)?.resolve(width, height) ?: 0f,
topLeft =
(endStart ?: topEnd ?: topRight ?: uniform)?.resolve(width, height)
?: zeroRadii,
topRight =
(startStart ?: topStart ?: topLeft ?: uniform)?.resolve(width, height) ?: 0f,
(startStart ?: topStart ?: topLeft ?: uniform)?.resolve(width, height)
?: zeroRadii,
bottomLeft =
(endEnd ?: bottomStart ?: bottomRight ?: uniform)?.resolve(width, height) ?: 0f,
(endEnd ?: bottomStart ?: bottomRight ?: uniform)?.resolve(width, height)
?: zeroRadii,
bottomRight =
(startEnd ?: bottomEnd ?: bottomLeft ?: uniform)?.resolve(width, height) ?: 0f,
(startEnd ?: bottomEnd ?: bottomLeft ?: uniform)?.resolve(width, height)
?: zeroRadii,
)
} else {
ComputedBorderRadius(
topLeft = (endStart ?: topEnd ?: topLeft ?: uniform)?.resolve(width, height) ?: 0f,
topLeft =
(endStart ?: topEnd ?: topLeft ?: uniform)?.resolve(width, height) ?: zeroRadii,
topRight =
(startStart ?: topStart ?: topRight ?: uniform)?.resolve(width, height) ?: 0f,
(startStart ?: topStart ?: topRight ?: uniform)?.resolve(width, height)
?: zeroRadii,
bottomLeft =
(endEnd ?: bottomStart ?: bottomLeft ?: uniform)?.resolve(width, height) ?: 0f,
(endEnd ?: bottomStart ?: bottomLeft ?: uniform)?.resolve(width, height)
?: zeroRadii,
bottomRight =
(startEnd ?: bottomEnd ?: bottomRight ?: uniform)?.resolve(width, height) ?: 0f,
(startEnd ?: bottomEnd ?: bottomRight ?: uniform)?.resolve(width, height)
?: zeroRadii,
)
}
else -> throw IllegalArgumentException("Expected?.resolved layout direction")
@@ -17,16 +17,22 @@ public enum class ComputedBorderRadiusProp {
/** Phsysical edge lengths (in DIPs) for a border-radius. */
public data class ComputedBorderRadius(
val topLeft: Float,
val topRight: Float,
val bottomLeft: Float,
val bottomRight: Float,
val topLeft: CornerRadii,
val topRight: CornerRadii,
val bottomLeft: CornerRadii,
val bottomRight: CornerRadii,
) {
public fun hasRoundedBorders(): Boolean {
return topLeft > 0f || topRight > 0f || bottomLeft > 0f || bottomRight > 0f
return topLeft.horizontal > 0f ||
topLeft.vertical > 0f ||
topRight.horizontal > 0f ||
topRight.vertical > 0f ||
bottomLeft.horizontal > 0f ||
bottomLeft.vertical > 0f ||
bottomRight.horizontal > 0f
}
public fun get(property: ComputedBorderRadiusProp): Float {
public fun get(property: ComputedBorderRadiusProp): CornerRadii {
return when (property) {
ComputedBorderRadiusProp.COMPUTED_BORDER_TOP_LEFT_RADIUS -> topLeft
ComputedBorderRadiusProp.COMPUTED_BORDER_TOP_RIGHT_RADIUS -> topRight
@@ -35,5 +41,6 @@ public data class ComputedBorderRadius(
}
}
public constructor() : this(0f, 0f, 0f, 0f)
public constructor() :
this(CornerRadii(0f, 0f), CornerRadii(0f, 0f), CornerRadii(0f, 0f), CornerRadii(0f, 0f))
}
@@ -0,0 +1,19 @@
/*
* 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.style
import com.facebook.react.uimanager.PixelUtil
public data class CornerRadii(
val horizontal: Float = 0f,
val vertical: Float = 0f,
) {
public fun toPixelFromDIP(): CornerRadii {
return CornerRadii(PixelUtil.toPixelFromDIP(horizontal), PixelUtil.toPixelFromDIP(vertical))
}
}
@@ -60,6 +60,7 @@ import com.facebook.react.uimanager.drawable.CSSBackgroundDrawable;
import com.facebook.react.uimanager.style.BorderRadiusProp;
import com.facebook.react.uimanager.style.BorderStyle;
import com.facebook.react.uimanager.style.ComputedBorderRadius;
import com.facebook.react.uimanager.style.CornerRadii;
import com.facebook.react.uimanager.style.Gradient;
import com.facebook.react.uimanager.style.LogicalEdge;
import com.facebook.react.uimanager.style.Overflow;
@@ -994,23 +995,23 @@ public class ReactViewGroup extends ViewGroup
mPath = new Path();
}
float topLeftRadius = PixelUtil.toPixelFromDIP(borderRadius.getTopLeft());
float topRightRadius = PixelUtil.toPixelFromDIP(borderRadius.getTopRight());
float bottomLeftRadius = PixelUtil.toPixelFromDIP(borderRadius.getBottomLeft());
float bottomRightRadius = PixelUtil.toPixelFromDIP(borderRadius.getBottomRight());
CornerRadii topLeftRadius = borderRadius.getTopLeft().toPixelFromDIP();
CornerRadii topRightRadius = borderRadius.getTopRight().toPixelFromDIP();
CornerRadii bottomLeftRadius = borderRadius.getBottomLeft().toPixelFromDIP();
CornerRadii bottomRightRadius = borderRadius.getBottomRight().toPixelFromDIP();
mPath.rewind();
mPath.addRoundRect(
new RectF(left, top, right, bottom),
new float[] {
Math.max(topLeftRadius - borderWidth.left, 0),
Math.max(topLeftRadius - borderWidth.top, 0),
Math.max(topRightRadius - borderWidth.right, 0),
Math.max(topRightRadius - borderWidth.top, 0),
Math.max(bottomRightRadius - borderWidth.right, 0),
Math.max(bottomRightRadius - borderWidth.bottom, 0),
Math.max(bottomLeftRadius - borderWidth.left, 0),
Math.max(bottomLeftRadius - borderWidth.bottom, 0),
Math.max(topLeftRadius.getHorizontal() - borderWidth.left, 0),
Math.max(topLeftRadius.getVertical() - borderWidth.top, 0),
Math.max(topRightRadius.getHorizontal() - borderWidth.right, 0),
Math.max(topRightRadius.getVertical() - borderWidth.top, 0),
Math.max(bottomRightRadius.getHorizontal() - borderWidth.right, 0),
Math.max(bottomRightRadius.getVertical() - borderWidth.bottom, 0),
Math.max(bottomLeftRadius.getHorizontal() - borderWidth.left, 0),
Math.max(bottomLeftRadius.getVertical() - borderWidth.bottom, 0),
},
Path.Direction.CW);
canvas.clipPath(mPath);
@@ -11,6 +11,7 @@ import android.content.Context
import com.facebook.react.uimanager.style.BorderRadiusProp
import com.facebook.react.uimanager.style.BorderRadiusStyle
import com.facebook.react.uimanager.style.ComputedBorderRadiusProp
import com.facebook.react.uimanager.style.CornerRadii
import org.assertj.core.api.Assertions.*
import org.junit.Test
import org.junit.runner.RunWith
@@ -60,7 +61,7 @@ class BorderRadiusStyleTest {
for (prop in order.value) {
borderRadiusStyle.set(prop, LengthPercentage(count, LengthPercentageType.POINT))
val resolved = borderRadiusStyle.resolve(0, context = ctx, width = 100f, height = 100f)
assertThat(resolved.get(order.key)).isEqualTo(count)
assertThat(resolved.get(order.key)).isEqualTo(CornerRadii(count, count))
count -= 1f
}
}
@@ -104,7 +105,7 @@ class BorderRadiusStyleTest {
for (prop in order.value) {
borderRadiusStyle.set(prop, LengthPercentage(count, LengthPercentageType.POINT))
val resolved = borderRadiusStyle.resolve(1, context = ctx, width = 100f, height = 100f)
assertThat(resolved.get(order.key)).isEqualTo(count)
assertThat(resolved.get(order.key)).isEqualTo(CornerRadii(count, count))
count -= 1f
}
}
@@ -148,7 +149,7 @@ class BorderRadiusStyleTest {
for (prop in order.value) {
borderRadiusStyle.set(prop, LengthPercentage(count, LengthPercentageType.POINT))
val resolved = borderRadiusStyle.resolve(1, context = ctx, width = 100f, height = 100f)
assertThat(resolved.get(order.key)).isEqualTo(count)
assertThat(resolved.get(order.key)).isEqualTo(CornerRadii(count, count))
count -= 1f
}
}
@@ -165,10 +166,10 @@ class BorderRadiusStyleTest {
)
val resolved = borderRadiusStyle.resolve(0, context = ctx, width = 1000f, height = 1000f)
assertThat(resolved.topLeft).isEqualTo(0f)
assertThat(resolved.topRight).isEqualTo(100f)
assertThat(resolved.bottomLeft).isEqualTo(200f)
assertThat(resolved.bottomRight).isEqualTo(300f)
assertThat(resolved.topLeft).isEqualTo(CornerRadii(0f, 0f))
assertThat(resolved.topRight).isEqualTo(CornerRadii(100f, 100f))
assertThat(resolved.bottomLeft).isEqualTo(CornerRadii(200f, 200f))
assertThat(resolved.bottomRight).isEqualTo(CornerRadii(300f, 300f))
}
/*
@@ -733,6 +733,25 @@ export default ({
borderBottomRightRadius: '40%',
}}
/>
<View
style={{
width: 100,
height: 50,
borderWidth: 6,
borderRadius: '100%',
}}
/>
<View
style={{
width: 100,
height: 50,
borderWidth: 6,
borderTopLeftRadius: '10%',
borderTopRightRadius: '20%',
borderBottomRightRadius: '50%',
borderBottomLeftRadius: '100%',
}}
/>
</View>
);
},