Last active
November 22, 2023 15:04
-
-
Save yongjhih/9e3d10395d244d058a1633946fe73710 to your computer and use it in GitHub Desktop.
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
fun Modifier.border( | |
top: Dp = 0.dp, | |
bottom: Dp = 0.dp, | |
start: Dp = 0.dp, | |
end: Dp = 0.dp, | |
color: Color, | |
) = border( | |
top = BorderStroke(top, color), | |
bottom = BorderStroke(bottom, color), | |
start = BorderStroke(start, color), | |
end = BorderStroke(end, color), | |
) | |
fun Modifier.border( | |
top: BorderStroke = BorderStroke(0.dp, Color.Transparent), | |
bottom: BorderStroke = BorderStroke(0.dp, Color.Transparent), | |
start: BorderStroke = BorderStroke(0.dp, Color.Transparent), | |
end: BorderStroke = BorderStroke(0.dp, Color.Transparent), | |
padding: PaddingValues = PaddingValues(0.dp), | |
) = composed { | |
val density = LocalDensity.current | |
val layoutDirection = LocalLayoutDirection.current | |
Modifier.drawBehind { | |
val sizeWidth = size.width | |
val sizeHeight = size.height | |
if (top.width > 0.dp) { | |
val strokeWidthPx = density.run { top.width.toPx() } | |
val startPaddingPx = density.run { padding.calculateStartPadding(layoutDirection).toPx() } | |
val endPaddingPx = density.run { padding.calculateEndPadding(layoutDirection).toPx() } | |
val y = strokeWidthPx / 2 // 0f + strokeWidthPx / 2 | |
drawLine( | |
brush = top.brush, | |
start = Offset(x = 0f + startPaddingPx, y = y), | |
end = Offset(x = sizeWidth - endPaddingPx, y = y), | |
strokeWidth = strokeWidthPx | |
) | |
} | |
if (bottom.width > 0.dp) { | |
val strokeWidthPx = density.run { bottom.width.toPx() } | |
val startPaddingPx = density.run { padding.calculateStartPadding(layoutDirection).toPx() } | |
val endPaddingPx = density.run { padding.calculateEndPadding(layoutDirection).toPx() } | |
val y = sizeHeight - strokeWidthPx / 2 | |
drawLine( | |
brush = bottom.brush, | |
start = Offset(x = 0f + startPaddingPx, y = y), | |
end = Offset(x = sizeWidth - endPaddingPx, y = y), | |
strokeWidth = strokeWidthPx | |
) | |
} | |
if (start.width > 0.dp) { | |
val strokeWidthPx = density.run { start.width.toPx() } | |
val topPaddingPx = density.run { padding.calculateTopPadding().toPx() } | |
val bottomPaddingPx = density.run { padding.calculateBottomPadding().toPx() } | |
val x = strokeWidthPx / 2 | |
drawLine( | |
brush = start.brush, | |
start = Offset(x = x, y = 0f + topPaddingPx), | |
end = Offset(x = x, y = sizeHeight - bottomPaddingPx), | |
strokeWidth = strokeWidthPx | |
) | |
} | |
if (end.width > 0.dp) { | |
val strokeWidthPx = density.run { end.width.toPx() } | |
val topPaddingPx = density.run { padding.calculateTopPadding().toPx() } | |
val bottomPaddingPx = density.run { padding.calculateBottomPadding().toPx() } | |
val x = sizeWidth - strokeWidthPx / 2 | |
drawLine( | |
brush = end.brush, | |
start = Offset(x = x, y = 0f + topPaddingPx), | |
end = Offset(x = x, y = sizeHeight - bottomPaddingPx), | |
strokeWidth = strokeWidthPx | |
) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment