Skip to content

Commit a865082

Browse files
committed
library: Add maxHeight parameter to ListPopup
1 parent d966a87 commit a865082

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

composeApp/src/commonMain/kotlin/component/TextComponent.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ fun TextComponent(
7777
val dropdownOptions2 = listOf(
7878
"Option 1", "Option 2", "Option 3", "Option 4",
7979
"Option 5", "Option 6", "Option 7", "Option 8",
80-
"Option 9", "Option 10", "Option 11", "Option 12 (this is a long long long option)"
80+
"Option 9", "Option 10", "Option 11", "Option 12 (this is a longest option)"
8181
)
8282
val spinnerOptions = listOf(
8383
SpinnerEntry(icon = { Icon(RoundedRectanglePainter(), "Icon", Modifier.padding(end = 12.dp), Color(0xFFFF5B29)) }, "Option 1", "Red"),
@@ -89,7 +89,7 @@ fun TextComponent(
8989
SpinnerEntry(icon = { Icon(RoundedRectanglePainter(), "Icon", Modifier.padding(end = 12.dp), Color(0xFFFF5B29)) }, "Option 1", "Red"),
9090
SpinnerEntry(icon = { Icon(RoundedRectanglePainter(), "Icon", Modifier.padding(end = 12.dp), Color(0xFF36D167)) }, "Option 2", "Green"),
9191
SpinnerEntry(icon = { Icon(RoundedRectanglePainter(), "Icon", Modifier.padding(end = 12.dp), Color(0xFF3482FF)) }, "Option 3", "Blue"),
92-
SpinnerEntry(icon = { Icon(RoundedRectanglePainter(), "Icon", Modifier.padding(end = 12.dp), Color(0xFFFFB21D)) }, "Option 4 (this is a long option)", "Yellow"),
92+
SpinnerEntry(icon = { Icon(RoundedRectanglePainter(), "Icon", Modifier.padding(end = 12.dp), Color(0xFFFFB21D)) }, "Option 4", "Yellow"),
9393
)
9494
SmallTitle(text = "Basic")
9595
Card(
@@ -343,7 +343,8 @@ fun TextComponent(
343343
items = dropdownOptions2,
344344
selectedIndex = dropdownOptionSelectedRight.value,
345345
onSelectedIndexChange = { newOption -> dropdownOptionSelectedRight.value = newOption },
346-
mode = DropDownMode.AlwaysOnRight
346+
mode = DropDownMode.AlwaysOnRight,
347+
maxHeight = 300.dp
347348
)
348349

349350
SuperDropdown(

miuix/src/commonMain/kotlin/top/yukonga/miuix/kmp/basic/ListPopup.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import androidx.compose.ui.layout.onGloballyPositioned
3535
import androidx.compose.ui.layout.positionInWindow
3636
import androidx.compose.ui.platform.LocalDensity
3737
import androidx.compose.ui.platform.LocalLayoutDirection
38+
import androidx.compose.ui.unit.Dp
3839
import androidx.compose.ui.unit.IntOffset
3940
import androidx.compose.ui.unit.IntRect
4041
import androidx.compose.ui.unit.IntSize
@@ -56,6 +57,7 @@ import kotlin.math.min
5657
* @param popupPositionProvider The [PopupPositionProvider] of the [ListPopup].
5758
* @param alignment The alignment of the [ListPopup].
5859
* @param onDismissRequest The callback when the [ListPopup] is dismissed.
60+
* @param maxHeight The maximum height of the [ListPopup]. If null, the height will be calculated automatically.
5961
* @param content The [Composable] content of the [ListPopup]. You should use the [ListPopupColumn] in general.
6062
*/
6163
@Composable
@@ -65,6 +67,7 @@ fun ListPopup(
6567
popupPositionProvider: PopupPositionProvider = ListPopupDefaults.DropdownPositionProvider,
6668
alignment: PopupPositionProvider.Align = PopupPositionProvider.Align.Right,
6769
onDismissRequest: (() -> Unit)? = null,
70+
maxHeight: Dp? = null,
6871
content: @Composable () -> Unit
6972
) {
7073
var offset by remember { mutableStateOf(IntOffset.Zero) }
@@ -155,7 +158,7 @@ fun ListPopup(
155158
constraints.copy(
156159
minWidth = 200.dp.roundToPx(),
157160
minHeight = 50.dp.roundToPx(),
158-
maxHeight = windowBounds.height - popupMargin.top - popupMargin.bottom
161+
maxHeight = if (maxHeight != null) maxHeight.roundToPx() else windowBounds.height - popupMargin.top - popupMargin.bottom
159162
)
160163
)
161164
popupContentSize = IntSize(placeable.width, placeable.height)

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import androidx.compose.ui.platform.LocalHapticFeedback
3131
import androidx.compose.ui.text.font.FontWeight
3232
import androidx.compose.ui.text.style.TextAlign
3333
import androidx.compose.ui.text.style.TextOverflow
34+
import androidx.compose.ui.unit.Dp
3435
import androidx.compose.ui.unit.dp
3536
import kotlinx.coroutines.launch
3637
import top.yukonga.miuix.kmp.basic.BasicComponent
@@ -59,6 +60,7 @@ import top.yukonga.miuix.kmp.utils.MiuixPopupUtil.Companion.dismissPopup
5960
* @param summaryColor The color of the summary.
6061
* @param mode The dropdown show mode of the [SuperDropdown].
6162
* @param insideMargin The margin inside the [SuperDropdown].
63+
* @param maxHeight The maximum height of the [ListPopup].
6264
* @param enabled Whether the [SuperDropdown] is enabled.
6365
* @param showValue Whether to show the selected value of the [SuperDropdown].
6466
* @param onSelectedIndexChange The callback when the selected index of the [SuperDropdown] is changed.
@@ -74,6 +76,7 @@ fun SuperDropdown(
7476
summaryColor: BasicComponentColors = BasicComponentDefaults.summaryColor(),
7577
mode: DropDownMode = DropDownMode.Normal,
7678
insideMargin: PaddingValues = BasicComponentDefaults.InsideMargin,
79+
maxHeight: Dp? = null,
7780
enabled: Boolean = true,
7881
showValue: Boolean = true,
7982
onSelectedIndexChange: ((Int) -> Unit)?,
@@ -135,7 +138,8 @@ fun SuperDropdown(
135138
PopupPositionProvider.Align.Left,
136139
onDismissRequest = {
137140
isDropdownExpanded.value = false
138-
}
141+
},
142+
maxHeight = maxHeight
139143
) {
140144
ListPopupColumn {
141145
items.forEachIndexed { index, string ->

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import androidx.compose.ui.platform.LocalHapticFeedback
4040
import androidx.compose.ui.text.font.FontWeight
4141
import androidx.compose.ui.text.style.TextAlign
4242
import androidx.compose.ui.text.style.TextOverflow
43+
import androidx.compose.ui.unit.Dp
4344
import androidx.compose.ui.unit.DpSize
4445
import androidx.compose.ui.unit.dp
4546
import kotlinx.coroutines.launch
@@ -72,6 +73,7 @@ import top.yukonga.miuix.kmp.utils.MiuixPopupUtil.Companion.dismissPopup
7273
* @param mode The mode of the [SuperSpinner].
7374
* @param leftAction The action to be shown at the left side of the [SuperSpinner].
7475
* @param insideMargin The [PaddingValues] to be applied inside the [SuperSpinner].
76+
* @param maxHeight The maximum height of the [ListPopup].
7577
* @param enabled Whether the [SuperSpinner] is enabled.
7678
* @param showValue Whether to show the value of the [SuperSpinner].
7779
* @param onSelectedIndexChange The callback to be invoked when the selected index of the [SuperSpinner] is changed.
@@ -88,6 +90,7 @@ fun SuperSpinner(
8890
mode: SpinnerMode = SpinnerMode.Normal,
8991
leftAction: @Composable (() -> Unit)? = null,
9092
insideMargin: PaddingValues = BasicComponentDefaults.InsideMargin,
93+
maxHeight: Dp? = null,
9194
enabled: Boolean = true,
9295
showValue: Boolean = true,
9396
onSelectedIndexChange: ((Int) -> Unit)?,
@@ -149,7 +152,8 @@ fun SuperSpinner(
149152
PopupPositionProvider.Align.Left,
150153
onDismissRequest = {
151154
isDropdownExpanded.value = false
152-
}
155+
},
156+
maxHeight = maxHeight
153157
) {
154158
ListPopupColumn {
155159
items.forEachIndexed { index, spinnerEntry ->
@@ -377,9 +381,11 @@ fun SuperSpinner(
377381
layout(0, 0) { }
378382
}
379383
val button = measurables[1].measure(constraints)
380-
val lazyList = measurables[0].measure(constraints.copy(
381-
maxHeight = constraints.maxHeight - button.height
382-
))
384+
val lazyList = measurables[0].measure(
385+
constraints.copy(
386+
maxHeight = constraints.maxHeight - button.height
387+
)
388+
)
383389
layout(constraints.maxWidth, lazyList.height + button.height) {
384390
lazyList.place(0, 0)
385391
button.place(0, lazyList.height)

0 commit comments

Comments
 (0)