11package top.yukonga.miuix.kmp.extra
22
3+ import androidx.compose.animation.animateColorAsState
4+ import androidx.compose.animation.core.spring
35import androidx.compose.foundation.Image
46import androidx.compose.foundation.background
57import androidx.compose.foundation.clickable
@@ -12,16 +14,19 @@ import androidx.compose.foundation.layout.asPaddingValues
1214import androidx.compose.foundation.layout.captionBar
1315import androidx.compose.foundation.layout.displayCutout
1416import androidx.compose.foundation.layout.fillMaxSize
17+ import androidx.compose.foundation.layout.heightIn
1518import androidx.compose.foundation.layout.navigationBars
1619import androidx.compose.foundation.layout.offset
1720import androidx.compose.foundation.layout.only
1821import androidx.compose.foundation.layout.padding
1922import androidx.compose.foundation.layout.size
2023import androidx.compose.foundation.layout.statusBars
2124import androidx.compose.foundation.layout.width
25+ import androidx.compose.foundation.layout.widthIn
2226import androidx.compose.foundation.layout.windowInsetsPadding
2327import androidx.compose.foundation.lazy.LazyColumn
2428import androidx.compose.runtime.Composable
29+ import androidx.compose.runtime.DisposableEffect
2530import androidx.compose.runtime.LaunchedEffect
2631import androidx.compose.runtime.MutableState
2732import 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 {
0 commit comments