Skip to content

Commit 6b646f1

Browse files
authored
library: Optimize SuperDropdown again(#24)
* Modify the position of the popup * Add: SuperDropdown will be "selected" when the popup is expanded. * update: [SuperDropdown] Change background instead of focus state when "selected". * add: [SuperDropdown] Close popup automatically. fix: [SuperDropdown] Limit popup size and margins. update: [SuperDropdown] Popup content padding, rounded corners, font size * update: [SuperDropdown] Format Code. * update: [SuperDropdown] Format Code x2. * fix: [SuperDropdown] Shadow color
1 parent d2f1a94 commit 6b646f1

File tree

2 files changed

+41
-17
lines changed

2 files changed

+41
-17
lines changed

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

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,21 @@ import androidx.compose.foundation.layout.asPaddingValues
1414
import androidx.compose.foundation.layout.captionBar
1515
import androidx.compose.foundation.layout.displayCutout
1616
import androidx.compose.foundation.layout.fillMaxSize
17+
import androidx.compose.foundation.layout.heightIn
1718
import androidx.compose.foundation.layout.navigationBars
1819
import androidx.compose.foundation.layout.offset
1920
import androidx.compose.foundation.layout.only
2021
import androidx.compose.foundation.layout.padding
2122
import androidx.compose.foundation.layout.size
2223
import androidx.compose.foundation.layout.statusBars
2324
import androidx.compose.foundation.layout.width
25+
import androidx.compose.foundation.layout.widthIn
2426
import androidx.compose.foundation.layout.windowInsetsPadding
2527
import androidx.compose.foundation.lazy.LazyColumn
2628
import androidx.compose.runtime.Composable
29+
import androidx.compose.runtime.DisposableEffect
2730
import androidx.compose.runtime.LaunchedEffect
2831
import androidx.compose.runtime.MutableState
29-
import androidx.compose.runtime.State
3032
import androidx.compose.runtime.getValue
3133
import androidx.compose.runtime.mutableStateListOf
3234
import androidx.compose.runtime.mutableStateOf
@@ -110,14 +112,20 @@ fun SuperDropdown(
110112
val isDropdownExpanded = remember { mutableStateOf(false) }
111113
val hapticFeedback = LocalHapticFeedback.current
112114
val actionColor = if (enabled) MiuixTheme.colorScheme.onSurfaceVariantActions else MiuixTheme.colorScheme.disabledOnSecondaryVariant
113-
val targetColor = if (isDropdownExpanded.value) MiuixTheme.colorScheme.onBackground.copy(alpha = 0.15f) else Color.Transparent
114-
val touchTint by animateColorAsState(targetValue = targetColor, animationSpec = spring(stiffness = 2000f))
115+
val targetColor = if (isDropdownExpanded.value) MiuixTheme.colorScheme.selectedTint else Color.Transparent
116+
val selectedTint by animateColorAsState(targetValue = targetColor, animationSpec = spring(stiffness = 2000f))
115117
var alignLeft by rememberSaveable { mutableStateOf(true) }
116118
var dropdownOffsetXPx by remember { mutableStateOf(0) }
117119
var dropdownOffsetYPx by remember { mutableStateOf(0) }
118120
var componentHeightPx by remember { mutableStateOf(0) }
119121
var componentWidthPx by remember { mutableStateOf(0) }
120122

123+
DisposableEffect(Unit) {
124+
onDispose {
125+
dismissPopup(isDropdownExpanded)
126+
}
127+
}
128+
121129
BasicComponent(
122130
modifier = modifier
123131
.pointerInput(Unit) {
@@ -140,7 +148,7 @@ fun SuperDropdown(
140148
componentWidthPx = coordinates.size.width
141149
}
142150
}
143-
.background(touchTint),
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,14 +254,15 @@ fun SuperDropdown(
243254
captionBarPx
244255
)
245256
}
257+
.heightIn(50.dp, dropdownMaxHeight)
246258
.align(AbsoluteAlignment.TopLeft)
247259
.graphicsLayer(
248260
shadowElevation = 40f,
249-
shape = SmoothRoundedCornerShape(18.dp),
250-
ambientShadowColor = MiuixTheme.colorScheme.onBackground.copy(alpha = 0.2f),
251-
spotShadowColor = MiuixTheme.colorScheme.onBackground.copy(alpha = 0.6f)
261+
shape = SmoothRoundedCornerShape(16.dp),
262+
ambientShadowColor = Color.Black.copy(alpha = 0.2f),
263+
spotShadowColor = Color.Black.copy(alpha = 0.6f)
252264
)
253-
.clip(SmoothRoundedCornerShape(18.dp))
265+
.clip(SmoothRoundedCornerShape(16.dp))
254266
.background(MiuixTheme.colorScheme.surface)
255267
) {
256268
item {
@@ -295,8 +307,15 @@ fun DropdownImpl(
295307
onSelectedIndexChange: (Int) -> Unit,
296308
textWidthDp: Dp?
297309
) {
298-
val additionalTopPadding = if (index == 0) 24.dp else 14.dp
299-
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+
}
300319
val textColor = if (isSelected) {
301320
MiuixTheme.colorScheme.onTertiaryContainer
302321
} else {
@@ -320,18 +339,19 @@ fun DropdownImpl(
320339
onSelectedIndexChange(index)
321340
}
322341
.background(backgroundColor)
323-
.padding(horizontal = 24.dp)
342+
.widthIn(200.dp, 288.dp)
343+
.padding(horizontal = 20.dp)
324344
.padding(top = additionalTopPadding, bottom = additionalBottomPadding)
325345
) {
326346
Text(
327347
modifier = Modifier.width(textWidthDp ?: 50.dp),
328348
text = text,
329-
fontSize = 15.sp,
349+
fontSize = 16.sp,
330350
fontWeight = FontWeight.Medium,
331351
color = textColor,
332352
)
333353
Image(
334-
modifier = Modifier.padding(start = 50.dp).size(20.dp),
354+
modifier = Modifier.padding(start = 12.dp).size(20.dp),
335355
imageVector = MiuixIcons.Check,
336356
colorFilter = BlendModeColorFilter(selectColor, BlendMode.SrcIn),
337357
contentDescription = null,
@@ -370,7 +390,7 @@ fun calculateOffsetYPx(
370390
dropdownOffsetPx - dropdownHeightPx + insideHeightPx / 2
371391
} else if (windowHeightPx - statusBarPx - captionBarPx - navigationBarPx <= dropdownHeightPx) {
372392
// Special handling when the height of the popup is maxsize (== windowHeightPx)
373-
0
393+
statusBarPx
374394
} else if (windowHeightPx - dropdownOffsetPx < dropdownHeightPx / 2 + captionBarPx + navigationBarPx + insideHeightPx + componentHeightPx / 2) {
375395
windowHeightPx - dropdownHeightPx - insideHeightPx - captionBarPx - navigationBarPx
376396
} 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)