Skip to content

Commit bd16851

Browse files
committed
library: Fix a crash issue
* Padding must be non-negative
1 parent ba59c18 commit bd16851

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

miuix/src/commonMain/kotlin/top/yukonga/miuix/kmp/extra/SuperDropdown.kt

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import androidx.compose.foundation.lazy.LazyColumn
2424
import androidx.compose.runtime.Composable
2525
import androidx.compose.runtime.LaunchedEffect
2626
import androidx.compose.runtime.MutableState
27+
import androidx.compose.runtime.derivedStateOf
2728
import androidx.compose.runtime.getValue
2829
import androidx.compose.runtime.mutableStateListOf
2930
import androidx.compose.runtime.mutableStateOf
@@ -53,6 +54,7 @@ import androidx.compose.ui.unit.Dp
5354
import androidx.compose.ui.unit.DpSize
5455
import androidx.compose.ui.unit.LayoutDirection
5556
import androidx.compose.ui.unit.dp
57+
import androidx.compose.ui.unit.max
5658
import androidx.compose.ui.unit.sp
5759
import top.yukonga.miuix.kmp.basic.BasicComponent
5860
import top.yukonga.miuix.kmp.basic.Box
@@ -73,6 +75,7 @@ import kotlin.math.roundToInt
7375
* A dropdown with a title and a summary.
7476
*
7577
* @param modifier The modifier to be applied to the [SuperDropdown].
78+
* @param popupModifier The modifier to be applied to the popup of the [SuperDropdown].
7679
* @param title The title of the [SuperDropdown].
7780
* @param titleColor The color of the title.
7881
* @param summary The summary of the [SuperDropdown].
@@ -89,6 +92,7 @@ import kotlin.math.roundToInt
8992
@Composable
9093
fun SuperDropdown(
9194
modifier: Modifier = Modifier,
95+
popupModifier: Modifier = Modifier,
9296
title: String,
9397
titleColor: Color = MiuixTheme.colorScheme.onSurface,
9498
summary: String? = null,
@@ -135,6 +139,17 @@ fun SuperDropdown(
135139
with(density) { WindowInsets.captionBar.asPaddingValues().calculateBottomPadding().toPx() }.roundToInt()
136140
)
137141
val insideHeightPx by rememberUpdatedState(with(density) { insideMargin.height.toPx() }.roundToInt())
142+
val displayCutoutSize =
143+
WindowInsets.displayCutout.asPaddingValues(density).calculateLeftPadding(LayoutDirection.Ltr) +
144+
WindowInsets.displayCutout.asPaddingValues(density).calculateRightPadding(LayoutDirection.Ltr)
145+
val popupPadding by rememberUpdatedState {
146+
derivedStateOf {
147+
max(
148+
horizontalPadding + (windowWeightPx.dp - componentWidthPx.dp) / 2 / density.density -
149+
if (defaultWindowInsetsPadding) displayCutoutSize / 2 else 0.dp, 0.dp
150+
)
151+
}
152+
}
138153

139154
BackHandler(enabled = isPopupShowing()) {
140155
dismissPopup(isDropdownExpanded)
@@ -193,10 +208,10 @@ fun SuperDropdown(
193208
content = {
194209
Box(
195210
modifier = if (defaultWindowInsetsPadding) {
196-
modifier
211+
popupModifier
197212
.windowInsetsPadding(WindowInsets.displayCutout.only(WindowInsetsSides.Horizontal))
198213
} else {
199-
modifier
214+
popupModifier
200215
}
201216
.fillMaxSize()
202217
.pointerInput(Unit) {
@@ -211,9 +226,7 @@ fun SuperDropdown(
211226
LazyColumn(
212227
modifier = Modifier
213228
.padding(
214-
horizontal = horizontalPadding + (windowWeightPx.dp - componentWidthPx.dp) / 2 / density.density - if (defaultWindowInsetsPadding)
215-
(WindowInsets.displayCutout.asPaddingValues(density).calculateLeftPadding(LayoutDirection.Ltr) +
216-
WindowInsets.displayCutout.asPaddingValues(density).calculateRightPadding(LayoutDirection.Ltr)) / 2 else 0.dp
229+
horizontal = popupPadding.invoke().value
217230
)
218231
.onGloballyPositioned { layoutCoordinates ->
219232
dropdownHeightPx = layoutCoordinates.size.height

0 commit comments

Comments
 (0)