diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/BorderDrawable.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/BorderDrawable.kt index b962b2f5c28..5c184c739a7 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/BorderDrawable.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/BorderDrawable.kt @@ -87,11 +87,12 @@ internal class BorderDrawable( private var pathForBorder: Path? = null private val borderPaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG) + private var pathEffectStyle: BorderStyle? = null + private var pathEffectWidth: Float = 0f private var needUpdatePath: Boolean = true private var pathForSingleBorder: Path? = null - private var pathForOutline: Path? = null private var centerDrawPath: Path? = null private var outerClipPathForBorderRadius: Path? = null @@ -174,7 +175,6 @@ internal class BorderDrawable( } override fun draw(canvas: Canvas) { - updatePathEffect() computedBorderColors = borderColors?.resolve(layoutDirection, context) ?: computedBorderColors if (borderRadius?.hasRoundedBorders() == true) { drawRoundedBorders(canvas) @@ -266,42 +266,42 @@ internal class BorderDrawable( val bottom = bounds.bottom borderPaint.color = multiplyColorAlpha(fastBorderColor, borderAlpha) borderPaint.style = Paint.Style.STROKE - pathForSingleBorder = Path() + val path = pathForSingleBorder ?: Path().also { pathForSingleBorder = it } if (borderLeft > 0) { - pathForSingleBorder?.reset() + path.reset() val width = borderWidth.left.roundToInt() - updatePathEffect(width) + updatePathEffect(width.toFloat()) borderPaint.strokeWidth = width.toFloat() - pathForSingleBorder?.moveTo((left + width / 2).toFloat(), top.toFloat()) - pathForSingleBorder?.lineTo((left + width / 2).toFloat(), bottom.toFloat()) - pathForSingleBorder?.let { canvas.drawPath(it, borderPaint) } + path.moveTo((left + width / 2).toFloat(), top.toFloat()) + path.lineTo((left + width / 2).toFloat(), bottom.toFloat()) + canvas.drawPath(path, borderPaint) } if (borderTop > 0) { - pathForSingleBorder?.reset() + path.reset() val width = borderWidth.top.roundToInt() - updatePathEffect(width) + updatePathEffect(width.toFloat()) borderPaint.strokeWidth = width.toFloat() - pathForSingleBorder?.moveTo(left.toFloat(), (top + width / 2).toFloat()) - pathForSingleBorder?.lineTo(right.toFloat(), (top + width / 2).toFloat()) - pathForSingleBorder?.let { canvas.drawPath(it, borderPaint) } + path.moveTo(left.toFloat(), (top + width / 2).toFloat()) + path.lineTo(right.toFloat(), (top + width / 2).toFloat()) + canvas.drawPath(path, borderPaint) } if (borderRight > 0) { - pathForSingleBorder?.reset() + path.reset() val width = borderWidth.right.roundToInt() - updatePathEffect(width) + updatePathEffect(width.toFloat()) borderPaint.strokeWidth = width.toFloat() - pathForSingleBorder?.moveTo((right - width / 2).toFloat(), top.toFloat()) - pathForSingleBorder?.lineTo((right - width / 2).toFloat(), bottom.toFloat()) - pathForSingleBorder?.let { canvas.drawPath(it, borderPaint) } + path.moveTo((right - width / 2).toFloat(), top.toFloat()) + path.lineTo((right - width / 2).toFloat(), bottom.toFloat()) + canvas.drawPath(path, borderPaint) } if (borderBottom > 0) { - pathForSingleBorder?.reset() + path.reset() val width = borderWidth.bottom.roundToInt() - updatePathEffect(width) + updatePathEffect(width.toFloat()) borderPaint.strokeWidth = width.toFloat() - pathForSingleBorder?.moveTo(left.toFloat(), (bottom - width / 2).toFloat()) - pathForSingleBorder?.lineTo(right.toFloat(), (bottom - width / 2).toFloat()) - pathForSingleBorder?.let { canvas.drawPath(it, borderPaint) } + path.moveTo(left.toFloat(), (bottom - width / 2).toFloat()) + path.lineTo(right.toFloat(), (bottom - width / 2).toFloat()) + canvas.drawPath(path, borderPaint) } } } else { @@ -312,6 +312,7 @@ internal class BorderDrawable( */ borderPaint.isAntiAlias = false borderPaint.style = Paint.Style.FILL + updatePathEffect(getFullBorderWidth()) val width = bounds.width() val height = bounds.height() if (borderLeft > 0) { @@ -366,6 +367,7 @@ internal class BorderDrawable( } private fun drawRoundedBorders(canvas: Canvas) { + updatePathEffect(getFullBorderWidth()) updatePath() canvas.save() @@ -570,17 +572,14 @@ internal class BorderDrawable( return if (!borderWidth.isNaN()) borderWidth else 0f } - private fun updatePathEffect() { - /** Used for rounded border and rounded background. */ - val pathEffectForBorderStyle = - this.borderStyle?.let { style -> getPathEffect(style, getFullBorderWidth()) } - borderPaint.setPathEffect(pathEffectForBorderStyle) - } - - private fun updatePathEffect(borderWidth: Int) { - val pathEffectForBorderStyle = - this.borderStyle?.let { style -> getPathEffect(style, borderWidth.toFloat()) } - borderPaint.setPathEffect(pathEffectForBorderStyle) + private fun updatePathEffect(borderWidth: Float) { + val style = borderStyle + if (style == pathEffectStyle && borderWidth == pathEffectWidth) { + return + } + pathEffectStyle = style + pathEffectWidth = borderWidth + borderPaint.setPathEffect(style?.let { getPathEffect(it, borderWidth) }) } private fun getPathEffect(style: BorderStyle, borderWidth: Float): PathEffect? { @@ -676,7 +675,6 @@ internal class BorderDrawable( // Path innerClipPathForBorderRadius = innerClipPathForBorderRadius ?: Path() outerClipPathForBorderRadius = outerClipPathForBorderRadius ?: Path() - pathForOutline = Path() // RectF innerClipTempRectForBorderRadius = innerClipTempRectForBorderRadius ?: RectF() @@ -781,27 +779,6 @@ internal class BorderDrawable( ) } - var extraRadiusForOutline = 0f - - if (this.borderWidth != null) { - extraRadiusForOutline = this.borderWidth[Spacing.ALL] / 2f - } - - pathForOutline?.addRoundRect( - RectF(bounds), - floatArrayOf( - topLeftRadius.horizontal + extraRadiusForOutline, - topLeftRadius.vertical + extraRadiusForOutline, - topRightRadius.horizontal + extraRadiusForOutline, - topRightRadius.vertical + extraRadiusForOutline, - bottomRightRadius.horizontal + extraRadiusForOutline, - bottomRightRadius.vertical + extraRadiusForOutline, - bottomLeftRadius.horizontal + extraRadiusForOutline, - bottomLeftRadius.vertical + extraRadiusForOutline, - ), - Path.Direction.CW, - ) - if (computedBorderRadius?.isUniform() != true) { centerDrawPath = centerDrawPath ?: Path() centerDrawPath?.reset() diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/drawable/BorderDrawableTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/drawable/BorderDrawableTest.kt index f7ae7d1e563..35f8be51202 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/drawable/BorderDrawableTest.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/drawable/BorderDrawableTest.kt @@ -23,7 +23,6 @@ import org.junit.After import org.junit.Before import org.junit.Test import org.junit.runner.RunWith -import org.mockito.kotlin.any import org.mockito.kotlin.argumentCaptor import org.mockito.kotlin.atLeastOnce import org.mockito.kotlin.mock @@ -45,7 +44,7 @@ class BorderDrawableTest { } @Test - fun removingBorderStyleClearsDashedPathEffect() { + fun dashedBorderChangesItsPathEffectOnlyWithItsStyleOrWidth() { val borderInsets = BorderInsets().apply { setBorderWidth(LogicalEdge.ALL, 8f) } val drawable = BorderDrawable( @@ -59,7 +58,18 @@ class BorderDrawableTest { drawable.setBorderWidth(Spacing.ALL, 8f) drawable.setBorderColor(LogicalEdge.ALL, Color.RED) - assertThat(drawBorderPaint(drawable).pathEffect).isInstanceOf(DashPathEffect::class.java) + val (firstPath, firstPaint) = drawBorder(drawable) + val dashedPathEffect = firstPaint.pathEffect + assertThat(dashedPathEffect).isInstanceOf(DashPathEffect::class.java) + val (secondPath, secondPaint) = drawBorder(drawable) + assertThat(secondPath).isSameAs(firstPath) + assertThat(secondPaint.pathEffect).isSameAs(dashedPathEffect) + + borderInsets.setBorderWidth(LogicalEdge.ALL, 12f) + + assertThat(drawBorderPaint(drawable).pathEffect) + .isInstanceOf(DashPathEffect::class.java) + .isNotSameAs(dashedPathEffect) drawable.borderStyle = null @@ -94,11 +104,14 @@ class BorderDrawableTest { } } - private fun drawBorderPaint(drawable: BorderDrawable): Paint { + private fun drawBorderPaint(drawable: BorderDrawable): Paint = drawBorder(drawable).second + + private fun drawBorder(drawable: BorderDrawable): Pair { val canvas = mock() drawable.draw(canvas) + val path = argumentCaptor() val paint = argumentCaptor() - verify(canvas, atLeastOnce()).drawPath(any(), paint.capture()) - return paint.lastValue + verify(canvas, atLeastOnce()).drawPath(path.capture(), paint.capture()) + return path.lastValue to paint.lastValue } }