Skip to content

Commit 3e18765

Browse files
committed
library: Rollback some Card changes
1 parent f37d45d commit 3e18765

File tree

2 files changed

+62
-29
lines changed

2 files changed

+62
-29
lines changed

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package top.yukonga.miuix.kmp.basic
55

66
import androidx.compose.foundation.LocalIndication
7+
import androidx.compose.foundation.background
78
import androidx.compose.foundation.gestures.awaitEachGesture
89
import androidx.compose.foundation.gestures.awaitFirstDown
910
import androidx.compose.foundation.gestures.detectTapGestures
@@ -12,21 +13,28 @@ import androidx.compose.foundation.hoverable
1213
import androidx.compose.foundation.indication
1314
import androidx.compose.foundation.interaction.MutableInteractionSource
1415
import androidx.compose.foundation.interaction.PressInteraction
16+
import androidx.compose.foundation.layout.Box
1517
import androidx.compose.foundation.layout.Column
1618
import androidx.compose.foundation.layout.ColumnScope
1719
import androidx.compose.foundation.layout.PaddingValues
1820
import androidx.compose.foundation.layout.padding
21+
import androidx.compose.foundation.shape.RoundedCornerShape
1922
import androidx.compose.runtime.Composable
23+
import androidx.compose.runtime.CompositionLocalProvider
2024
import androidx.compose.runtime.Immutable
2125
import androidx.compose.runtime.getValue
2226
import androidx.compose.runtime.remember
2327
import androidx.compose.runtime.rememberUpdatedState
2428
import androidx.compose.ui.Modifier
29+
import androidx.compose.ui.draw.clip
2530
import androidx.compose.ui.graphics.Color
2631
import androidx.compose.ui.graphics.takeOrElse
2732
import androidx.compose.ui.input.pointer.pointerInput
33+
import androidx.compose.ui.semantics.isTraversalGroup
34+
import androidx.compose.ui.semantics.semantics
2835
import androidx.compose.ui.unit.Dp
2936
import androidx.compose.ui.unit.dp
37+
import top.yukonga.miuix.kmp.theme.LocalContentColor
3038
import top.yukonga.miuix.kmp.theme.MiuixTheme
3139
import top.yukonga.miuix.kmp.utils.PressFeedbackType
3240
import top.yukonga.miuix.kmp.utils.SmoothRoundedCornerShape
@@ -158,14 +166,23 @@ private fun BasicCard(
158166
content: @Composable () -> Unit,
159167
) {
160168
val shape = remember(cornerRadius) { SmoothRoundedCornerShape(cornerRadius) }
169+
val clipShape = remember(cornerRadius) { RoundedCornerShape(cornerRadius) }
161170

162-
Surface(
163-
modifier = modifier,
164-
color = colors.color,
165-
shape = shape,
166-
contentColor = colors.contentColor,
167-
content = content
168-
)
171+
CompositionLocalProvider(
172+
LocalContentColor provides colors.contentColor,
173+
) {
174+
Box(
175+
modifier = modifier
176+
.semantics(mergeDescendants = false) {
177+
isTraversalGroup = true
178+
}
179+
.background(color = colors.color, shape = shape)
180+
.clip(clipShape), // For touch feedback, there is a problem when using SmoothRoundedCornerShape.
181+
propagateMinConstraints = true,
182+
) {
183+
content()
184+
}
185+
}
169186
}
170187

171188
object CardDefaults {

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

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
package top.yukonga.miuix.kmp.utils
55

6+
import androidx.annotation.FloatRange
67
import androidx.compose.foundation.shape.CornerBasedShape
78
import androidx.compose.foundation.shape.CornerSize
89
import androidx.compose.ui.geometry.Size
@@ -11,7 +12,9 @@ import androidx.compose.ui.graphics.Path
1112
import androidx.compose.ui.unit.Dp
1213
import androidx.compose.ui.unit.LayoutDirection
1314
import androidx.graphics.shapes.CornerRounding
15+
import androidx.graphics.shapes.Cubic
1416
import androidx.graphics.shapes.RoundedPolygon
17+
import kotlin.jvm.JvmOverloads
1518

1619
/**
1720
* Creates a [Path] for a rectangle with smoothly rounded corners.
@@ -24,15 +27,14 @@ import androidx.graphics.shapes.RoundedPolygon
2427
* @param bottomRight The radius of the bottom-right corner.
2528
*/
2629
fun Path.Companion.smoothRoundedRectangle(
27-
smoothing: Float,
30+
@FloatRange(from = 0.0, 1.0) smoothing: Float,
2831
size: Size,
2932
topLeft: Float,
3033
topRight: Float,
3134
bottomLeft: Float,
3235
bottomRight: Float
3336
): Path {
3437
if (size.width <= 0f || size.height <= 0f) return Path()
35-
val clampedSmoothing = smoothing.coerceIn(0f, 1f)
3638

3739
return RoundedPolygon(
3840
vertices = floatArrayOf(
@@ -42,12 +44,12 @@ fun Path.Companion.smoothRoundedRectangle(
4244
0f, size.height
4345
),
4446
perVertexRounding = listOf(
45-
CornerRounding(radius = topLeft, smoothing = clampedSmoothing),
46-
CornerRounding(radius = topRight, smoothing = clampedSmoothing),
47-
CornerRounding(radius = bottomRight, smoothing = clampedSmoothing),
48-
CornerRounding(radius = bottomLeft, smoothing = clampedSmoothing),
47+
CornerRounding(radius = topLeft, smoothing = smoothing),
48+
CornerRounding(radius = topRight, smoothing = smoothing),
49+
CornerRounding(radius = bottomRight, smoothing = smoothing),
50+
CornerRounding(radius = bottomLeft, smoothing = smoothing),
4951
)
50-
).toComposePath()
52+
).toPath()
5153
}
5254

5355
/**
@@ -58,7 +60,7 @@ fun Path.Companion.smoothRoundedRectangle(
5860
*/
5961
fun SmoothRoundedCornerShape(
6062
corner: Dp,
61-
smoothing: Float = DefaultSmoothing
63+
@FloatRange(from = 0.0, 1.0) smoothing: Float = DefaultSmoothing
6264
): SmoothRoundedCornerShape = SmoothRoundedCornerShape(
6365
smoothing = smoothing,
6466
topStart = corner,
@@ -100,7 +102,7 @@ class SmoothRoundedCornerShape(
100102
* @param bottomStart The Dp value for the bottom-start corner.
101103
*/
102104
constructor(
103-
smoothing: Float = DefaultSmoothing,
105+
@FloatRange(from = 0.0, 1.0) smoothing: Float = DefaultSmoothing,
104106
topStart: Dp,
105107
topEnd: Dp,
106108
bottomEnd: Dp,
@@ -152,26 +154,40 @@ class SmoothRoundedCornerShape(
152154
}
153155

154156
/**
155-
* Converts a [RoundedPolygon] from the AndroidX graphics shapes library to a Jetpack Compose [Path].
157+
* Gets a [Path] representation for a [RoundedPolygon] shape. Note that there is some rounding
158+
* happening (to the nearest thousandth), to work around rendering artifacts introduced by some
159+
* points being just slightly off from each other (far less than a pixel). This also allows for a
160+
* more optimal path, as redundant curves (usually a single point) can be detected and not added to
161+
* the resulting path.
156162
*
157-
* @param path An optional existing [Path] to reuse. If provided, it will be rewound and populated.
158-
* Otherwise, a new [Path] will be created.
163+
* @param path an optional [Path] object which, if supplied, will avoid the function having to
164+
* create a new [Path] object
159165
*/
160-
fun RoundedPolygon.toComposePath(path: Path = Path()): Path {
161-
path.rewind()
166+
@JvmOverloads
167+
fun RoundedPolygon.toPath(path: Path = Path()): Path {
168+
pathFromCubics(path, cubics)
169+
return path
170+
}
162171

163-
if (cubics.isEmpty()) return path
164-
path.moveTo(cubics[0].anchor0X, cubics[0].anchor0Y)
165-
for (cubic in cubics) {
172+
private fun pathFromCubics(path: Path, cubics: List<Cubic>) {
173+
var first = true
174+
path.rewind()
175+
for (i in 0 until cubics.size) {
176+
val cubic = cubics[i]
177+
if (first) {
178+
path.moveTo(cubic.anchor0X, cubic.anchor0Y)
179+
first = false
180+
}
166181
path.cubicTo(
167-
cubic.control0X, cubic.control0Y,
168-
cubic.control1X, cubic.control1Y,
169-
cubic.anchor1X, cubic.anchor1Y
182+
cubic.control0X,
183+
cubic.control0Y,
184+
cubic.control1X,
185+
cubic.control1Y,
186+
cubic.anchor1X,
187+
cubic.anchor1Y
170188
)
171189
}
172-
173190
path.close()
174-
return path
175191
}
176192

177193
/**

0 commit comments

Comments
 (0)