Skip to content

Commit 23fa25b

Browse files
committed
library: Only one dropdown/dialog is allowed to be displayed at a time.
1 parent 051fa15 commit 23fa25b

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ import androidx.compose.foundation.layout.padding
1616
import androidx.compose.foundation.layout.widthIn
1717
import androidx.compose.foundation.layout.windowInsetsPadding
1818
import androidx.compose.runtime.Composable
19+
import androidx.compose.runtime.LaunchedEffect
1920
import androidx.compose.runtime.MutableState
2021
import androidx.compose.runtime.derivedStateOf
2122
import androidx.compose.runtime.getValue
23+
import androidx.compose.runtime.mutableStateListOf
2224
import androidx.compose.runtime.remember
2325
import androidx.compose.runtime.rememberUpdatedState
2426
import androidx.compose.ui.Alignment
@@ -41,6 +43,11 @@ import top.yukonga.miuix.kmp.utils.getRoundedCorner
4143
import top.yukonga.miuix.kmp.utils.getWindowSize
4244
import top.yukonga.miuix.kmp.utils.squircleshape.SquircleShape
4345

46+
/**
47+
* Only one dialog is allowed to be displayed at a time.
48+
*/
49+
val dialogStates = mutableStateListOf<MutableState<Boolean>>()
50+
4451
/**
4552
* A dialog with a title, a summary, and a content.
4653
*
@@ -80,6 +87,18 @@ fun SuperDialog(
8087
val getWindowSize by rememberUpdatedState(getWindowSize())
8188
val contentAlignment by remember { derivedStateOf { if (getWindowSize.width > getWindowSize.height) Alignment.Center else Alignment.BottomCenter } }
8289

90+
if (!dialogStates.contains(show)) dialogStates.add(show)
91+
92+
LaunchedEffect(show.value) {
93+
if (show.value) {
94+
dialogStates.forEach { state ->
95+
if (state != show) {
96+
state.value = false
97+
}
98+
}
99+
}
100+
}
101+
83102
BackHandler(enabled = show.value) {
84103
dismissDialog()
85104
onDismissRequest()

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ import androidx.compose.foundation.layout.width
2727
import androidx.compose.foundation.layout.windowInsetsPadding
2828
import androidx.compose.foundation.lazy.LazyColumn
2929
import androidx.compose.runtime.Composable
30+
import androidx.compose.runtime.LaunchedEffect
3031
import androidx.compose.runtime.MutableState
3132
import androidx.compose.runtime.getValue
33+
import androidx.compose.runtime.mutableStateListOf
3234
import androidx.compose.runtime.mutableStateOf
3335
import androidx.compose.runtime.remember
3436
import androidx.compose.runtime.rememberCoroutineScope
@@ -77,6 +79,11 @@ import kotlin.math.roundToInt
7779
*/
7880
expect fun modifierPlatform(modifier: Modifier, isHovered: MutableState<Boolean>, isEnable: Boolean): Modifier
7981

82+
/**
83+
* Only one dropdown is allowed to be displayed at a time.
84+
*/
85+
val dropdownStates = mutableStateListOf<MutableState<Boolean>>()
86+
8087
/**
8188
* A dropdown with a title and a summary.
8289
*
@@ -150,6 +157,18 @@ fun SuperDropdown(
150157
with(density) { insideMargin.height.toPx() }.roundToInt()
151158
)
152159

160+
if (!dropdownStates.contains(isDropdownExpanded)) dropdownStates.add(isDropdownExpanded)
161+
162+
LaunchedEffect(isDropdownExpanded.value) {
163+
if (isDropdownExpanded.value) {
164+
dropdownStates.forEach { state ->
165+
if (state != isDropdownExpanded) {
166+
state.value = false
167+
}
168+
}
169+
}
170+
}
171+
153172
BasicComponent(
154173
modifier = modifierPlatform(modifier = Modifier, isHovered = isHovered, isEnable = enabled)
155174
.background(if (isHovered.value) MiuixTheme.colorScheme.onBackground.copy(0.08f) else Color.Transparent)

0 commit comments

Comments
 (0)