Skip to content

Commit bc69e5f

Browse files
committed
library: Opt SmoothRoundedCornerShape
1 parent 10b3957 commit bc69e5f

File tree

2 files changed

+20
-26
lines changed

2 files changed

+20
-26
lines changed

miuix/src/commonMain/kotlin/top/yukonga/miuix/kmp/basic/Card.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import androidx.compose.runtime.remember
1313
import androidx.compose.ui.Modifier
1414
import androidx.compose.ui.draw.clip
1515
import androidx.compose.ui.graphics.Color
16-
import androidx.compose.ui.input.pointer.pointerInput
1716
import androidx.compose.ui.semantics.isTraversalGroup
1817
import androidx.compose.ui.semantics.semantics
1918
import androidx.compose.ui.unit.Dp
@@ -44,12 +43,11 @@ fun Card(
4443
val shape = remember { derivedStateOf { SmoothRoundedCornerShape(cornerRadius) } }
4544
Box(
4645
modifier = modifier
47-
.background(color = color, shape = shape.value)
48-
.clip(RoundedCornerShape(cornerRadius)) // For touch feedback, because there is a problem when using SmoothRoundedCornerShape.
4946
.semantics(mergeDescendants = false) {
5047
isTraversalGroup = true
5148
}
52-
.pointerInput(Unit) {},
49+
.background(color = color, shape = shape.value)
50+
.clip(RoundedCornerShape(cornerRadius)), // For touch feedback, there is a problem when using SmoothRoundedCornerShape.
5351
propagateMinConstraints = true
5452
) {
5553
Column(

miuix/src/commonMain/kotlin/top/yukonga/miuix/kmp/utils/SmoothRoundedCornerShape.kt

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import androidx.compose.ui.graphics.Path
88
import androidx.compose.ui.unit.Dp
99
import androidx.compose.ui.unit.LayoutDirection
1010
import androidx.graphics.shapes.CornerRounding
11-
import androidx.graphics.shapes.Cubic
1211
import androidx.graphics.shapes.RoundedPolygon
1312

1413
fun Path.Companion.smoothRoundedRectangle(
@@ -20,6 +19,7 @@ fun Path.Companion.smoothRoundedRectangle(
2019
bottomRight: Float
2120
): Path {
2221
if (size.width <= 0f || size.height <= 0f) return Path()
22+
val clampedSmoothing = smoothing.coerceIn(0f, 1f)
2323

2424
return RoundedPolygon(
2525
vertices = floatArrayOf(
@@ -29,10 +29,10 @@ fun Path.Companion.smoothRoundedRectangle(
2929
0f, size.height
3030
),
3131
perVertexRounding = listOf(
32-
CornerRounding(radius = topLeft, smoothing = smoothing),
33-
CornerRounding(radius = topRight, smoothing = smoothing),
34-
CornerRounding(radius = bottomRight, smoothing = smoothing),
35-
CornerRounding(radius = bottomLeft, smoothing = smoothing),
32+
CornerRounding(radius = topLeft, smoothing = clampedSmoothing),
33+
CornerRounding(radius = topRight, smoothing = clampedSmoothing),
34+
CornerRounding(radius = bottomRight, smoothing = clampedSmoothing),
35+
CornerRounding(radius = bottomLeft, smoothing = clampedSmoothing),
3636
)
3737
).toComposePath()
3838
}
@@ -105,36 +105,32 @@ class SmoothRoundedCornerShape(
105105
size = size,
106106
topLeft = topLeft,
107107
topRight = topRight,
108-
bottomRight = bottomRight,
109108
bottomLeft = bottomLeft,
109+
bottomRight = bottomRight
110110
)
111111
return Outline.Generic(path)
112112
}
113113
}
114114

115115
fun RoundedPolygon.toComposePath(path: Path = Path()): Path {
116-
pathFromCubicList(path, cubics)
117-
return path
118-
}
119-
120-
private fun pathFromCubicList(
121-
path: Path,
122-
cubicList: List<Cubic>
123-
) {
124-
var first = true
125116
path.rewind()
126-
for (element in cubicList) {
127-
if (first) {
128-
path.moveTo(element.anchor0X, element.anchor0Y)
129-
first = false
130-
}
117+
118+
if (cubics.isEmpty()) return path
119+
path.moveTo(cubics[0].anchor0X, cubics[0].anchor0Y)
120+
for (cubic in cubics) {
131121
path.cubicTo(
132-
element.control0X, element.control0Y, element.control1X, element.control1Y,
133-
element.anchor1X, element.anchor1Y
122+
cubic.control0X, cubic.control0Y,
123+
cubic.control1X, cubic.control1Y,
124+
cubic.anchor1X, cubic.anchor1Y
134125
)
135126
}
127+
136128
path.close()
129+
return path
137130
}
138131

132+
/**
133+
* Default smoothing value for [SmoothRoundedCornerShape].
134+
*/
139135
const val DefaultSmoothing = 0.6f
140136

0 commit comments

Comments
 (0)