Skip to content

Commit 2538aac

Browse files
committed
library: Fixed crash caused by SmoothRoundedCornerShape
1 parent 5387883 commit 2538aac

File tree

14 files changed

+60
-38
lines changed

14 files changed

+60
-38
lines changed

miuix/src/androidMain/kotlin/top/yukonga/miuix/kmp/utils/SmoothRoundedCornerShape.android.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ actual fun Path.Companion.smoothRoundedRectangle(
1414
topRight: Float,
1515
bottomLeft: Float,
1616
bottomRight: Float
17-
): Path =
18-
RoundedPolygon(
17+
): Path {
18+
if (size.width <= 0f || size.height <= 0f) return Path()
19+
20+
return RoundedPolygon(
1921
vertices = floatArrayOf(
2022
0f, 0f,
2123
size.width, 0f,
@@ -28,4 +30,5 @@ actual fun Path.Companion.smoothRoundedRectangle(
2830
CornerRounding(radius = bottomRight, smoothing = smoothing),
2931
CornerRounding(radius = bottomLeft, smoothing = smoothing),
3032
)
31-
).toPath().asComposePath()
33+
).toPath().asComposePath()
34+
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import androidx.compose.foundation.layout.padding
99
import androidx.compose.runtime.Composable
1010
import androidx.compose.runtime.Immutable
1111
import androidx.compose.runtime.Stable
12+
import androidx.compose.runtime.derivedStateOf
13+
import androidx.compose.runtime.remember
1214
import androidx.compose.ui.Alignment
1315
import androidx.compose.ui.Modifier
1416
import androidx.compose.ui.graphics.Color
@@ -45,13 +47,14 @@ fun Button(
4547
insideMargin: PaddingValues = ButtonDefaults.InsideMargin,
4648
content: @Composable RowScope.() -> Unit
4749
) {
50+
val shape = remember { derivedStateOf { SmoothRoundedCornerShape(cornerRadius) } }
4851
Surface(
4952
onClick = {
5053
onClick()
5154
},
5255
enabled = enabled,
5356
modifier = modifier.semantics { role = Role.Button },
54-
shape = SmoothRoundedCornerShape(cornerRadius),
57+
shape = shape.value,
5558
color = colors.color(enabled)
5659
) {
5760
Row(

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import androidx.compose.foundation.layout.PaddingValues
88
import androidx.compose.foundation.layout.padding
99
import androidx.compose.foundation.shape.RoundedCornerShape
1010
import androidx.compose.runtime.Composable
11+
import androidx.compose.runtime.derivedStateOf
12+
import androidx.compose.runtime.remember
1113
import androidx.compose.ui.Modifier
1214
import androidx.compose.ui.draw.clip
1315
import androidx.compose.ui.graphics.Color
@@ -39,9 +41,10 @@ fun Card(
3941
color: Color = CardDefaults.DefaultColor(),
4042
content: @Composable ColumnScope.() -> Unit
4143
) {
44+
val shape = remember { derivedStateOf { SmoothRoundedCornerShape(cornerRadius) } }
4245
Box(
4346
modifier = modifier
44-
.background(color = color, shape = SmoothRoundedCornerShape(cornerRadius))
47+
.background(color = color, shape = shape.value)
4548
.clip(RoundedCornerShape(cornerRadius)) // For touch feedback, because there is a problem when using Smooth RoundedCornerShape.
4649
.semantics(mergeDescendants = false) {
4750
isTraversalGroup = true

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import androidx.compose.foundation.clickable
55
import androidx.compose.foundation.layout.Box
66
import androidx.compose.foundation.layout.defaultMinSize
77
import androidx.compose.runtime.Composable
8+
import androidx.compose.runtime.derivedStateOf
9+
import androidx.compose.runtime.remember
810
import androidx.compose.ui.Alignment
911
import androidx.compose.ui.Modifier
1012
import androidx.compose.ui.draw.clip
@@ -40,10 +42,11 @@ fun IconButton(
4042
minWidth: Dp = IconButtonDefaults.MinWidth,
4143
content: @Composable () -> Unit
4244
) {
45+
val shape = remember { derivedStateOf { SmoothRoundedCornerShape(cornerRadius) } }
4346
Box(
4447
modifier = modifier
4548
.defaultMinSize(minWidth = minWidth, minHeight = minHeight)
46-
.clip(SmoothRoundedCornerShape(cornerRadius))
49+
.clip(shape.value)
4750
.background(color = backgroundColor)
4851
.clickable(
4952
onClick = onClick,

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ fun ListPopup(
161161
constraints.copy(
162162
minWidth = 200.dp.roundToPx(),
163163
minHeight = 50.dp.roundToPx(),
164-
maxHeight = if (maxHeight != null) maxHeight.roundToPx() else windowBounds.height - popupMargin.top - popupMargin.bottom
164+
maxHeight = maxHeight?.roundToPx() ?: (windowBounds.height - popupMargin.top - popupMargin.bottom)
165165
)
166166
)
167167
popupContentSize = IntSize(placeable.width, placeable.height)
@@ -245,15 +245,11 @@ fun ListPopupColumn(
245245
modifier = Modifier.verticalScroll(rememberScrollState())
246246
) { constraints ->
247247
var listHeight = 0
248-
val tempConstraints = constraints.copy(
249-
minWidth = 200.dp.roundToPx(), maxWidth = 288.dp.roundToPx()
250-
)
248+
val tempConstraints = constraints.copy(minWidth = 200.dp.roundToPx(), maxWidth = 288.dp.roundToPx(), minHeight = 0)
251249
val listWidth = subcompose("miuixPopupListFake", content).map {
252250
it.measure(tempConstraints)
253251
}.maxOf { it.width }.coerceIn(200.dp.roundToPx(), 288.dp.roundToPx())
254-
val childConstraints = constraints.copy(
255-
minWidth = listWidth, maxWidth = listWidth
256-
)
252+
val childConstraints = constraints.copy(minWidth = listWidth, maxWidth = listWidth, minHeight = 0)
257253
val placeables = subcompose("miuixPopupListReal", content).map {
258254
val placeable = it.measure(childConstraints)
259255
listHeight += placeable.height

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import androidx.compose.foundation.text.KeyboardActions
1616
import androidx.compose.foundation.text.KeyboardOptions
1717
import androidx.compose.runtime.Composable
1818
import androidx.compose.runtime.LaunchedEffect
19+
import androidx.compose.runtime.derivedStateOf
1920
import androidx.compose.runtime.remember
2021
import androidx.compose.ui.Alignment
2122
import androidx.compose.ui.Modifier
@@ -161,11 +162,12 @@ fun InputField(
161162
interactionSource = interactionSource,
162163
decorationBox =
163164
@Composable { innerTextField ->
165+
val shape = remember { derivedStateOf { SmoothRoundedCornerShape(50.dp) } }
164166
Box(
165167
modifier = Modifier
166168
.background(
167169
color = MiuixTheme.colorScheme.surfaceContainerHigh,
168-
shape = SmoothRoundedCornerShape(50.dp)
170+
shape = shape.value
169171
)
170172
) {
171173
Row(

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import androidx.compose.foundation.layout.height
1414
import androidx.compose.runtime.Composable
1515
import androidx.compose.runtime.Immutable
1616
import androidx.compose.runtime.Stable
17+
import androidx.compose.runtime.derivedStateOf
1718
import androidx.compose.runtime.getValue
1819
import androidx.compose.runtime.mutableStateOf
1920
import androidx.compose.runtime.remember
@@ -142,10 +143,10 @@ fun SliderBackground(
142143
targetValue = if (isDragging) 0.044f else 0f,
143144
animationSpec = tween(150)
144145
).value
145-
146+
val shape = remember { derivedStateOf { SmoothRoundedCornerShape(height) } }
146147
Canvas(
147148
modifier = modifier
148-
.clip(SmoothRoundedCornerShape(height))
149+
.clip(shape.value)
149150
.background(backgroundColor)
150151
.drawBehind { drawRect(Color.Black.copy(alpha = backgroundAlpha)) }
151152
) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import androidx.compose.foundation.lazy.LazyRow
1111
import androidx.compose.foundation.lazy.itemsIndexed
1212
import androidx.compose.foundation.lazy.rememberLazyListState
1313
import androidx.compose.runtime.Composable
14+
import androidx.compose.runtime.derivedStateOf
1415
import androidx.compose.runtime.remember
1516
import androidx.compose.ui.Alignment
1617
import androidx.compose.ui.Modifier
@@ -61,7 +62,7 @@ fun TabRow(
6162
((windowSize.width.toDp() - (tabs.size - 1) * 9.dp) / tabs.size).coerceAtLeast(62.dp)
6263
}
6364

64-
val shape = remember { SmoothRoundedCornerShape(cornerRadius) }
65+
val shape = remember { derivedStateOf { SmoothRoundedCornerShape(cornerRadius) } }
6566

6667
LazyRow(
6768
state = listState,
@@ -72,7 +73,7 @@ fun TabRow(
7273
) {
7374
itemsIndexed(tabs) { index, tabText ->
7475
Surface(
75-
shape = shape,
76+
shape = shape.value,
7677
onClick = {
7778
onSelect?.invoke(index)
7879
},

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import androidx.compose.foundation.text.BasicTextField
1616
import androidx.compose.foundation.text.KeyboardActions
1717
import androidx.compose.foundation.text.KeyboardOptions
1818
import androidx.compose.runtime.Composable
19+
import androidx.compose.runtime.derivedStateOf
1920
import androidx.compose.runtime.getValue
2021
import androidx.compose.runtime.remember
2122
import androidx.compose.ui.Alignment
@@ -121,12 +122,13 @@ fun TextField(
121122
interactionSource = interactionSource,
122123
cursorBrush = SolidColor(MiuixTheme.colorScheme.primary),
123124
decorationBox = { innerTextField ->
125+
val shape = remember { derivedStateOf { SmoothRoundedCornerShape(cornerRadius) } }
124126
Box(
125127
modifier = Modifier
126128
.fillMaxWidth()
127129
.background(
128130
color = backgroundColor,
129-
shape = SmoothRoundedCornerShape(cornerRadius)
131+
shape = shape.value
130132
)
131133
.then(border)
132134
) {

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -593,12 +593,12 @@ private fun TopAppBarLayout(
593593
val navigationIconPlaceable =
594594
measurables
595595
.fastFirst { it.layoutId == "navigationIcon" }
596-
.measure(constraints.copy(minWidth = 0))
596+
.measure(constraints.copy(minWidth = 0, minHeight = 0))
597597

598598
val actionIconsPlaceable =
599599
measurables
600600
.fastFirst { it.layoutId == "actionIcons" }
601-
.measure(constraints.copy(minWidth = 0))
601+
.measure(constraints.copy(minWidth = 0, minHeight = 0))
602602

603603
val maxTitleWidth =
604604
if (constraints.maxWidth == Constraints.Infinity) {
@@ -611,12 +611,12 @@ private fun TopAppBarLayout(
611611
val titlePlaceable =
612612
measurables
613613
.fastFirst { it.layoutId == "title" }
614-
.measure(constraints.copy(minWidth = 0, maxWidth = maxTitleWidth))
614+
.measure(constraints.copy(minWidth = 0, maxWidth = maxTitleWidth, minHeight = 0))
615615

616616
val largeTitlePlaceable =
617617
measurables
618618
.fastFirst { it.layoutId == "largeTitle" }
619-
.measure(constraints.copy(minWidth = 0))
619+
.measure(constraints.copy(minWidth = 0, minHeight = 0))
620620

621621
// Subtract the scrolledOffset from the maxHeight. The scrolledOffset is expected to be
622622
// equal or smaller than zero.
@@ -720,12 +720,12 @@ private fun SmallTopAppBarLayout(
720720
val navigationIconPlaceable =
721721
measurables
722722
.fastFirst { it.layoutId == "navigationIcon" }
723-
.measure(constraints.copy(minWidth = 0))
723+
.measure(constraints.copy(minWidth = 0, minHeight = 0))
724724

725725
val actionIconsPlaceable =
726726
measurables
727727
.fastFirst { it.layoutId == "actionIcons" }
728-
.measure(constraints.copy(minWidth = 0))
728+
.measure(constraints.copy(minWidth = 0, minHeight = 0))
729729

730730
val maxTitleWidth =
731731
if (constraints.maxWidth == Constraints.Infinity) {
@@ -738,7 +738,7 @@ private fun SmallTopAppBarLayout(
738738
val titlePlaceable =
739739
measurables
740740
.fastFirst { it.layoutId == "title" }
741-
.measure(constraints.copy(minWidth = 0, maxWidth = maxTitleWidth))
741+
.measure(constraints.copy(minWidth = 0, maxWidth = maxTitleWidth, minHeight = 0))
742742

743743

744744
val layoutHeight =

0 commit comments

Comments
 (0)