@@ -4,7 +4,6 @@ import androidx.compose.foundation.Image
44import androidx.compose.foundation.background
55import androidx.compose.foundation.clickable
66import androidx.compose.foundation.gestures.detectTapGestures
7- import androidx.compose.foundation.interaction.MutableInteractionSource
87import androidx.compose.foundation.layout.Arrangement
98import androidx.compose.foundation.layout.Row
109import androidx.compose.foundation.layout.WindowInsets
@@ -64,6 +63,7 @@ import top.yukonga.miuix.kmp.icon.icons.Check
6463import top.yukonga.miuix.kmp.theme.MiuixTheme
6564import top.yukonga.miuix.kmp.utils.BackHandler
6665import top.yukonga.miuix.kmp.utils.MiuixPopupUtil.Companion.dismissPopup
66+ import top.yukonga.miuix.kmp.utils.MiuixPopupUtil.Companion.isPopupShowing
6767import top.yukonga.miuix.kmp.utils.MiuixPopupUtil.Companion.showPopup
6868import top.yukonga.miuix.kmp.utils.getWindowSize
6969import top.yukonga.miuix.kmp.utils.squircleshape.SquircleShape
@@ -103,12 +103,14 @@ fun SuperDropdown(
103103 enabled : Boolean = true
104104) {
105105 val isDropdownExpanded = remember { mutableStateOf(false ) }
106+
106107 if (! dropdownStates.contains(isDropdownExpanded)) dropdownStates.add(isDropdownExpanded)
107108 LaunchedEffect (isDropdownExpanded.value) {
108109 if (isDropdownExpanded.value) {
109- dropdownStates.forEach { state -> if (state = = isDropdownExpanded) state.value = false }
110+ dropdownStates.forEach { state -> if (state ! = isDropdownExpanded) state.value = false }
110111 }
111112 }
113+
112114 val hapticFeedback = LocalHapticFeedback .current
113115 val density = LocalDensity .current
114116 var alignLeft by rememberSaveable { mutableStateOf(true ) }
@@ -133,8 +135,15 @@ fun SuperDropdown(
133135 )
134136 val insideHeightPx by rememberUpdatedState(with (density) { insideMargin.height.toPx() }.roundToInt())
135137
138+ BackHandler (enabled = isPopupShowing()) {
139+ dismissPopup(isDropdownExpanded)
140+ }
141+
136142 BasicComponent (
137- onClick = { isDropdownExpanded.value = enabled },
143+ onClick = {
144+ isDropdownExpanded.value = enabled
145+ hapticFeedback.performHapticFeedback(HapticFeedbackType .LongPress )
146+ },
138147 modifier = modifier
139148 .pointerInput(Unit ) {
140149 awaitPointerEventScope {
@@ -178,15 +187,7 @@ fun SuperDropdown(
178187 },
179188 enabled = enabled
180189 )
181- BackHandler (
182- enabled = isDropdownExpanded.value
183- ) {
184- dismissPopup()
185- isDropdownExpanded.value = false
186- }
187-
188190 if (isDropdownExpanded.value) {
189- hapticFeedback.performHapticFeedback(HapticFeedbackType .LongPress )
190191 showPopup(
191192 content = {
192193 Box (
@@ -198,10 +199,11 @@ fun SuperDropdown(
198199 }
199200 .fillMaxSize()
200201 .pointerInput(Unit ) {
201- detectTapGestures(onTap = {
202- dismissPopup()
203- isDropdownExpanded.value = false
204- })
202+ detectTapGestures(
203+ onTap = {
204+ dismissPopup(isDropdownExpanded)
205+ }
206+ )
205207 }
206208 .offset(y = offsetPx.dp / density.density)
207209 ) {
@@ -228,22 +230,21 @@ fun SuperDropdown(
228230 .align(if (alignLeft && ! alwaysRight) AbsoluteAlignment .TopLeft else AbsoluteAlignment .TopRight )
229231 .graphicsLayer(
230232 shadowElevation = 18f ,
231- shape = SquircleShape (18 .dp),
232- clip = false
233+ shape = SquircleShape (18 .dp)
233234 )
234235 .clip(SquircleShape (18 .dp))
235236 .background(MiuixTheme .colorScheme.surface)
236237 ) {
237238 item {
238239 items.forEachIndexed { index, option ->
239240 DropdownImpl (
240- options = items,
241+ text = option,
242+ optionSize = items.size,
241243 isSelected = items[selectedIndex] == option,
242244 onSelectedIndexChange = {
243245 hapticFeedback.performHapticFeedback(HapticFeedbackType .LongPress )
244246 onSelectedIndexChange(it)
245- dismissPopup()
246- isDropdownExpanded.value = false
247+ dismissPopup(isDropdownExpanded)
247248 },
248249 textWidthDp = textWidthDp,
249250 index = index
@@ -260,23 +261,24 @@ fun SuperDropdown(
260261/* *
261262 * The implementation of the dropdown.
262263 *
263- * @param options The options of the dropdown.
264+ * @param text The text of the current option.
265+ * @param optionSize The size of the options.
264266 * @param isSelected Whether the option is selected.
265267 * @param index The index of the current option in the options.
266268 * @param onSelectedIndexChange The callback when the index is selected.
267269 * @param textWidthDp The maximum width of text in options.
268270 */
269271@Composable
270272fun DropdownImpl (
271- options : List <String >,
273+ text : String ,
274+ optionSize : Int ,
272275 isSelected : Boolean ,
273276 index : Int ,
274277 onSelectedIndexChange : (Int ) -> Unit ,
275278 textWidthDp : Dp ?
276279) {
277- val dropdownInteractionSource = remember { MutableInteractionSource () }
278280 val additionalTopPadding = if (index == 0 ) 24 .dp else 14 .dp
279- val additionalBottomPadding = if (index == options.size - 1 ) 24 .dp else 14 .dp
281+ val additionalBottomPadding = if (index == optionSize - 1 ) 24 .dp else 14 .dp
280282 val textColor = if (isSelected) {
281283 MiuixTheme .colorScheme.onTertiaryContainer
282284 } else {
@@ -305,7 +307,7 @@ fun DropdownImpl(
305307 ) {
306308 Text (
307309 modifier = Modifier .width(textWidthDp ? : 50 .dp),
308- text = options[index] ,
310+ text = text ,
309311 fontSize = 15 .sp,
310312 fontWeight = FontWeight .Medium ,
311313 color = textColor,
@@ -350,8 +352,7 @@ fun calculateOffsetPx(
350352 }
351353}
352354
353-
354355/* *
355356 * Only one dropdown is allowed to be displayed at a time.
356357 */
357- val dropdownStates = mutableStateListOf<MutableState <Boolean >>()
358+ val dropdownStates = mutableStateListOf<MutableState <Boolean >>()
0 commit comments