Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -366,6 +367,7 @@ internal class BorderDrawable(
}

private fun drawRoundedBorders(canvas: Canvas) {
updatePathEffect(getFullBorderWidth())
updatePath()
canvas.save()

Expand Down Expand Up @@ -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? {
Expand Down Expand Up @@ -676,7 +675,6 @@ internal class BorderDrawable(
// Path
innerClipPathForBorderRadius = innerClipPathForBorderRadius ?: Path()
outerClipPathForBorderRadius = outerClipPathForBorderRadius ?: Path()
pathForOutline = Path()

// RectF
innerClipTempRectForBorderRadius = innerClipTempRectForBorderRadius ?: RectF()
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -45,7 +44,7 @@ class BorderDrawableTest {
}

@Test
fun removingBorderStyleClearsDashedPathEffect() {
fun dashedBorderChangesItsPathEffectOnlyWithItsStyleOrWidth() {
val borderInsets = BorderInsets().apply { setBorderWidth(LogicalEdge.ALL, 8f) }
val drawable =
BorderDrawable(
Expand All @@ -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

Expand Down Expand Up @@ -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<Path, Paint> {
val canvas = mock<Canvas>()
drawable.draw(canvas)
val path = argumentCaptor<Path>()
val paint = argumentCaptor<Paint>()
verify(canvas, atLeastOnce()).drawPath(any<Path>(), paint.capture())
return paint.lastValue
verify(canvas, atLeastOnce()).drawPath(path.capture(), paint.capture())
return path.lastValue to paint.lastValue
}
}
Loading