Skip to content

Commit

Permalink
[MOB-1659] OutlineBorder 추가 (#41)
Browse files Browse the repository at this point in the history
* outlineBorder 추가

* outlineBorder 1px 보정 추가
  • Loading branch information
sodp5 authored Aug 1, 2024
1 parent 1bbec63 commit be6dd4b
Showing 1 changed file with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@ import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.drawOutline
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.graphics.drawscope.translate
import androidx.compose.ui.graphics.isSpecified
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
Expand All @@ -29,6 +37,36 @@ internal inline fun Modifier.thenIf(
this
}

internal fun Modifier.outlineBorder(
width: Dp,
color: Color,
shape: Shape = RectangleShape,
): Modifier = this.drawBehind {
/**
* 테두리와 컨텐츠 사이에 약간의 공간이 남는 현상을 방지하기 위해서
* 안쪽으로 1px 만큼 테두리를 표시합니다.
*/
val adjustmentValue = 1
val widthPx = width.toPx()

val outline = shape.createOutline(
Size(size.width + widthPx - adjustmentValue, size.height + widthPx - adjustmentValue),
layoutDirection,
this,
)

translate(
left = -(widthPx - adjustmentValue) / 2,
top = -(widthPx - adjustmentValue) / 2,
) {
drawOutline(
outline = outline,
color = color,
style = Stroke(widthPx + adjustmentValue),
)
}
}

fun Modifier.roundedBackground(
backgroundColor: Color,
borderRadius: Dp,
Expand Down

0 comments on commit be6dd4b

Please sign in to comment.