Skip to content

Instantly share code, notes, and snippets.

@dkaera
Created April 29, 2023 14:57
Show Gist options
  • Save dkaera/80adcad0d92495db6274ac92d499fc3a to your computer and use it in GitHub Desktop.
Save dkaera/80adcad0d92495db6274ac92d499fc3a to your computer and use it in GitHub Desktop.
fun Modifier.ticketBorder(
cornerTopStart: Dp? = null,
cornerTopEnd: Dp? = null,
cornerBottomEnd: Dp? = null,
cornerBottomStart: Dp? = null,
strokeWidth: Dp = 1.dp,
color: Color,
) = composed(
factory = {
with(LocalDensity.current) {
val cornerTopStartPx = cornerTopStart?.toPx()
val cornerTopEndPx = cornerTopEnd?.toPx()
val cornerBottomEndPx = cornerBottomEnd?.toPx()
val cornerBottomStartPx = cornerBottomStart?.toPx()
val strokeWidthPx = strokeWidth.toPx()
val halfStrokeWidthPx = strokeWidthPx / 2
val pathEffect = PathEffect.dashPathEffect(floatArrayOf(10f, 10f))
Modifier.drawBehind {
val width = size.width
val height = size.height
// top start
cornerTopStartPx?.let {
drawArc(
color = color,
startAngle = 0f,
sweepAngle = 90f,
size = Size(height = it, width = it),
topLeft = Offset(x = -it / 2, y = -it / 2),
useCenter = false,
style = Stroke(width = strokeWidthPx, pathEffect = pathEffect)
)
}
// top line
drawLine(
color = color,
strokeWidth = strokeWidthPx,
pathEffect = pathEffect,
start = Offset(
x = (cornerTopStartPx ?: 0f) / 2f,
y = halfStrokeWidthPx
),
end = Offset(
x = width - (cornerTopEndPx ?: 0f) / 2f,
y = halfStrokeWidthPx
)
)
// top end
cornerTopEndPx?.let {
drawArc(
color = color,
startAngle = 90f,
sweepAngle = 90f,
size = Size(height = it, width = it),
useCenter = false,
topLeft = Offset(x = width - it + it / 2, y = -it / 2),
style = Stroke(width = strokeWidthPx, pathEffect = pathEffect)
)
}
// end line
drawLine(
color = color,
strokeWidth = strokeWidthPx,
pathEffect = pathEffect,
start = Offset(
x = width - halfStrokeWidthPx,
y = (cornerTopEndPx ?: 0f) / 2
),
end = Offset(
x = width - halfStrokeWidthPx,
y = height - (cornerBottomEndPx ?: 0f) / 2
)
)
// bottom end
cornerBottomEndPx?.let {
drawArc(
color = color,
startAngle = 180f,
sweepAngle = 90f,
size = Size(height = it, width = it),
useCenter = false,
topLeft = Offset(x = width - it + it / 2, y = height - it / 2),
style = Stroke(width = strokeWidthPx, pathEffect = pathEffect)
)
}
// bottom line
drawLine(
color = color,
strokeWidth = strokeWidthPx,
pathEffect = pathEffect,
start = Offset(
x = (cornerBottomStartPx ?: 0f) / 2f,
y = height - halfStrokeWidthPx
),
end = Offset(
x = width - (cornerBottomEndPx ?: 0f) / 2f,
y = height - halfStrokeWidthPx
)
)
// bottom start
cornerBottomStartPx?.let {
drawArc(
color = color,
startAngle = 270f,
sweepAngle = 90f,
size = Size(height = it, width = it),
useCenter = false,
topLeft = Offset(x = -it / 2, y = height - it / 2),
style = Stroke(width = strokeWidthPx, pathEffect = pathEffect)
)
}
// start line
drawLine(
color = color,
strokeWidth = strokeWidthPx,
pathEffect = pathEffect,
start = Offset(
x = halfStrokeWidthPx,
y = (cornerTopStartPx ?: 0f) / 2
),
end = Offset(
x = halfStrokeWidthPx,
y = height - (cornerBottomStartPx ?: 0f) / 2
)
)
}
}
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment