Skip to content

Commit 491a590

Browse files
committed
add: [SuperDropdown] Close popup automatically.
fix: [SuperDropdown] Limit popup size and margins. update: [SuperDropdown] Popup content padding, rounded corners, font size
1 parent 7b0090c commit 491a590

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

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

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package top.yukonga.miuix.kmp.extra
22

3+
import androidx.compose.animation.animateColorAsState
4+
import androidx.compose.animation.core.spring
35
import androidx.compose.foundation.Image
46
import androidx.compose.foundation.background
57
import androidx.compose.foundation.clickable
@@ -12,16 +14,19 @@ import androidx.compose.foundation.layout.asPaddingValues
1214
import androidx.compose.foundation.layout.captionBar
1315
import androidx.compose.foundation.layout.displayCutout
1416
import androidx.compose.foundation.layout.fillMaxSize
17+
import androidx.compose.foundation.layout.heightIn
1518
import androidx.compose.foundation.layout.navigationBars
1619
import androidx.compose.foundation.layout.offset
1720
import androidx.compose.foundation.layout.only
1821
import androidx.compose.foundation.layout.padding
1922
import androidx.compose.foundation.layout.size
2023
import androidx.compose.foundation.layout.statusBars
2124
import androidx.compose.foundation.layout.width
25+
import androidx.compose.foundation.layout.widthIn
2226
import androidx.compose.foundation.layout.windowInsetsPadding
2327
import androidx.compose.foundation.lazy.LazyColumn
2428
import androidx.compose.runtime.Composable
29+
import androidx.compose.runtime.DisposableEffect
2530
import androidx.compose.runtime.LaunchedEffect
2631
import androidx.compose.runtime.MutableState
2732
import androidx.compose.runtime.getValue
@@ -107,15 +112,18 @@ fun SuperDropdown(
107112
val isDropdownExpanded = remember { mutableStateOf(false) }
108113
val hapticFeedback = LocalHapticFeedback.current
109114
val actionColor = if (enabled) MiuixTheme.colorScheme.onSurfaceVariantActions else MiuixTheme.colorScheme.disabledOnSecondaryVariant
115+
val targetColor = if (isDropdownExpanded.value) MiuixTheme.colorScheme.selectedTint else Color.Transparent
116+
val selectedTint by animateColorAsState(targetValue = targetColor, animationSpec = spring(stiffness = 2000f))
110117
var alignLeft by rememberSaveable { mutableStateOf(true) }
111118
var dropdownOffsetXPx by remember { mutableStateOf(0) }
112119
var dropdownOffsetYPx by remember { mutableStateOf(0) }
113120
var componentHeightPx by remember { mutableStateOf(0) }
114121
var componentWidthPx by remember { mutableStateOf(0) }
115-
val touchTint = remember { mutableStateOf(Color.Transparent) }
116122

117-
isDropdownExpanded.value.let {
118-
touchTint.value = if (it) MiuixTheme.colorScheme.touchTint else Color.Transparent
123+
DisposableEffect(Unit) {
124+
onDispose {
125+
dismissPopup(isDropdownExpanded)
126+
}
119127
}
120128

121129
BasicComponent(
@@ -140,7 +148,7 @@ fun SuperDropdown(
140148
componentWidthPx = coordinates.size.width
141149
}
142150
}
143-
.background(touchTint.value),
151+
.background(selectedTint),
144152
insideMargin = insideMargin,
145153
title = title,
146154
titleColor = titleColor,
@@ -196,6 +204,9 @@ fun SuperDropdown(
196204
val captionBarPx by rememberUpdatedState(
197205
with(density) { WindowInsets.captionBar.asPaddingValues().calculateBottomPadding().toPx() }.roundToInt()
198206
)
207+
val dropdownMaxHeight by rememberUpdatedState(with(density) {
208+
(windowHeightPx - statusBarPx - navigationBarPx - captionBarPx ).toDp()
209+
})
199210
val insideWidthPx by rememberUpdatedState(with(density){ insideMargin.width.toPx() }.roundToInt())
200211
val insideHeightPx by rememberUpdatedState(with(density) { insideMargin.height.toPx() }.roundToInt())
201212
val displayCutoutLeftSize = rememberUpdatedState(with(density) {
@@ -243,12 +254,15 @@ fun SuperDropdown(
243254
captionBarPx
244255
)
245256
}
257+
.heightIn(50.dp, dropdownMaxHeight)
246258
.align(AbsoluteAlignment.TopLeft)
247259
.graphicsLayer(
248-
shadowElevation = 18f,
249-
shape = SmoothRoundedCornerShape(18.dp)
260+
shadowElevation = 40f,
261+
shape = SmoothRoundedCornerShape(16.dp),
262+
ambientShadowColor = MiuixTheme.colorScheme.onBackground.copy(alpha = 0.2f),
263+
spotShadowColor = MiuixTheme.colorScheme.onBackground.copy(alpha = 0.6f)
250264
)
251-
.clip(SmoothRoundedCornerShape(18.dp))
265+
.clip(SmoothRoundedCornerShape(16.dp))
252266
.background(MiuixTheme.colorScheme.surface)
253267
) {
254268
item {
@@ -293,8 +307,15 @@ fun DropdownImpl(
293307
onSelectedIndexChange: (Int) -> Unit,
294308
textWidthDp: Dp?
295309
) {
296-
val additionalTopPadding = if (index == 0) 24.dp else 14.dp
297-
val additionalBottomPadding = if (index == optionSize - 1) 24.dp else 14.dp
310+
val additionalTopPadding: Dp
311+
val additionalBottomPadding: Dp
312+
if (optionSize == 1) {
313+
additionalTopPadding = 16.dp
314+
additionalBottomPadding = 16.dp
315+
} else {
316+
additionalTopPadding = if (index == 0) 22.5f.dp else 14.5f.dp
317+
additionalBottomPadding = if (index == optionSize - 1) 22.5f.dp else 14.5f.dp
318+
}
298319
val textColor = if (isSelected) {
299320
MiuixTheme.colorScheme.onTertiaryContainer
300321
} else {
@@ -318,18 +339,19 @@ fun DropdownImpl(
318339
onSelectedIndexChange(index)
319340
}
320341
.background(backgroundColor)
321-
.padding(horizontal = 24.dp)
342+
.widthIn(200.dp, 288.dp)
343+
.padding(horizontal = 20.dp)
322344
.padding(top = additionalTopPadding, bottom = additionalBottomPadding)
323345
) {
324346
Text(
325347
modifier = Modifier.width(textWidthDp ?: 50.dp),
326348
text = text,
327-
fontSize = 15.sp,
349+
fontSize = 16.sp,
328350
fontWeight = FontWeight.Medium,
329351
color = textColor,
330352
)
331353
Image(
332-
modifier = Modifier.padding(start = 50.dp).size(20.dp),
354+
modifier = Modifier.padding(start = 12.dp).size(20.dp),
333355
imageVector = MiuixIcons.Check,
334356
colorFilter = BlendModeColorFilter(selectColor, BlendMode.SrcIn),
335357
contentDescription = null,
@@ -368,7 +390,7 @@ fun calculateOffsetYPx(
368390
dropdownOffsetPx - dropdownHeightPx + insideHeightPx / 2
369391
} else if (windowHeightPx - statusBarPx - captionBarPx - navigationBarPx <= dropdownHeightPx) {
370392
// Special handling when the height of the popup is maxsize (== windowHeightPx)
371-
0
393+
statusBarPx
372394
} else if (windowHeightPx - dropdownOffsetPx < dropdownHeightPx / 2 + captionBarPx + navigationBarPx + insideHeightPx + componentHeightPx / 2) {
373395
windowHeightPx - dropdownHeightPx - insideHeightPx - captionBarPx - navigationBarPx
374396
} else {

miuix/src/commonMain/kotlin/top/yukonga/miuix/kmp/theme/MiuixColor.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ class MiuixColor(
103103
onSurfaceContainerHigh: Color,
104104
surfaceContainerHighest: Color,
105105
onSurfaceContainerHighest: Color,
106-
windowDimming: Color
106+
windowDimming: Color,
107+
selectedTint: Color,
107108
) {
108109
val primary by mutableStateOf(primary, structuralEqualityPolicy())
109110
val onPrimary by mutableStateOf(onPrimary, structuralEqualityPolicy())
@@ -151,6 +152,7 @@ class MiuixColor(
151152
val surfaceContainerHighest by mutableStateOf(surfaceContainerHighest, structuralEqualityPolicy())
152153
val onSurfaceContainerHighest by mutableStateOf(onSurfaceContainerHighest, structuralEqualityPolicy())
153154
val windowDimming by mutableStateOf(windowDimming, structuralEqualityPolicy())
155+
val selectedTint by mutableStateOf(selectedTint, structuralEqualityPolicy())
154156
}
155157

156158
fun lightColorScheme() = MiuixColor(
@@ -199,7 +201,8 @@ fun lightColorScheme() = MiuixColor(
199201
onSurfaceContainerHigh = Color(0xFFA5A5A5),
200202
surfaceContainerHighest = Color(0xFFE8E8E8),
201203
onSurfaceContainerHighest = Color.Black,
202-
windowDimming = Color.Black.copy(alpha = 0.3f)
204+
windowDimming = Color.Black.copy(alpha = 0.3f),
205+
selectedTint = Color(0x14000000)
203206
)
204207

205208
fun darkColorScheme() = MiuixColor(
@@ -248,5 +251,6 @@ fun darkColorScheme() = MiuixColor(
248251
onSurfaceContainerHigh = Color(0xFF6C6C6C),
249252
surfaceContainerHighest = Color(0xFF2D2D2D),
250253
onSurfaceContainerHighest = Color(0xFFE9E9E9),
251-
windowDimming = Color.Black.copy(alpha = 0.6f)
254+
windowDimming = Color.Black.copy(alpha = 0.3f),
255+
selectedTint = Color(0xCC393939)
252256
)

0 commit comments

Comments
 (0)