Skip to content

Commit 275966e

Browse files
committed
library: Fix dropdown popup padding when in landscape layout
1 parent 07262a4 commit 275966e

File tree

4 files changed

+21
-16
lines changed

4 files changed

+21
-16
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,7 @@ fun dialog2(showDialog: MutableState<Boolean>) {
360360
title = "Dropdown",
361361
items = dropdownOptions,
362362
selectedIndex = dropdownSelectedOption.value,
363-
onSelectedIndexChange = { newOption -> dropdownSelectedOption.value = newOption },
364-
popupHorizontalPadding = 12.dp + 24.dp
363+
onSelectedIndexChange = { newOption -> dropdownSelectedOption.value = newOption }
365364
)
366365
}
367366
Spacer(Modifier.height(12.dp))

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import androidx.compose.ui.Modifier
2828
import androidx.compose.ui.graphics.Color
2929
import androidx.compose.ui.graphics.graphicsLayer
3030
import androidx.compose.ui.input.pointer.pointerInput
31+
import androidx.compose.ui.platform.LocalDensity
3132
import androidx.compose.ui.text.font.FontWeight
3233
import androidx.compose.ui.text.style.TextAlign
3334
import androidx.compose.ui.unit.Dp
@@ -74,11 +75,12 @@ fun SuperDialog(
7475
defaultWindowInsetsPadding: Boolean = true,
7576
content: @Composable () -> Unit
7677
) {
78+
val density = LocalDensity.current
7779
val paddingModifier = remember(outsideMargin) { Modifier.padding(horizontal = outsideMargin.width).padding(bottom = outsideMargin.height) }
7880
val roundedCorner by rememberUpdatedState(getRoundedCorner())
7981
val bottomCornerRadius by remember { derivedStateOf { if (roundedCorner != 0.dp) roundedCorner - outsideMargin.width else 32.dp } }
8082
val getWindowSize by rememberUpdatedState(getWindowSize())
81-
val contentAlignment by remember { derivedStateOf { if (getWindowSize.width > getWindowSize.height) Alignment.Center else Alignment.BottomCenter } }
83+
val contentAlignment by remember { derivedStateOf { if (getWindowSize.width > getWindowSize.height || getWindowSize.width.dp / density.density > 440.dp) Alignment.Center else Alignment.BottomCenter } }
8284

8385
if (!dialogStates.contains(show)) dialogStates.add(show)
8486
LaunchedEffect(show.value) {
@@ -104,10 +106,12 @@ fun SuperDialog(
104106
}
105107
.fillMaxSize()
106108
.pointerInput(Unit) {
107-
detectTapGestures(onTap = {
108-
dismissDialog()
109-
onDismissRequest()
110-
})
109+
detectTapGestures(
110+
onTap = {
111+
dismissDialog()
112+
onDismissRequest()
113+
}
114+
)
111115
}
112116
.then(paddingModifier)
113117
) {

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ fun SuperDropdown(
9595
items: List<String>,
9696
alwaysRight: Boolean = false,
9797
insideMargin: DpSize = DpSize(16.dp, 16.dp),
98-
popupHorizontalPadding: Dp = 12.dp,
9998
defaultWindowInsetsPadding: Boolean = true,
10099
selectedIndex: Int,
101100
onSelectedIndexChange: (Int) -> Unit,
@@ -114,11 +113,13 @@ fun SuperDropdown(
114113
val textMeasurer = rememberTextMeasurer()
115114
val textStyle = remember { TextStyle(fontWeight = FontWeight.Medium, fontSize = 17.sp) }
116115
val textWidthDp = remember(items) { items.maxOfOrNull { with(density) { textMeasurer.measure(text = it, style = textStyle).size.width.toDp() } } }
117-
var dropdownHeightPx by remember { mutableStateOf(0) }
116+
val windowWeightPx by rememberUpdatedState(getWindowSize().width)
117+
val windowHeightPx by rememberUpdatedState(getWindowSize().height)
118118
var dropdownOffsetPx by remember { mutableStateOf(0) }
119+
var dropdownHeightPx by remember { mutableStateOf(0) }
119120
var componentHeightPx by remember { mutableStateOf(0) }
121+
var componentWidthPx by remember { mutableStateOf(0) }
120122
var offsetPx by remember { mutableStateOf(0) }
121-
val windowHeightPx by rememberUpdatedState(getWindowSize().height)
122123
val statusBarPx by rememberUpdatedState(
123124
with(density) { WindowInsets.statusBars.asPaddingValues().calculateTopPadding().toPx() }.roundToInt()
124125
)
@@ -146,6 +147,7 @@ fun SuperDropdown(
146147
val positionInWindow = coordinates.positionInWindow()
147148
dropdownOffsetPx = positionInWindow.y.toInt()
148149
componentHeightPx = coordinates.size.height
150+
componentWidthPx = coordinates.size.width
149151
},
150152
insideMargin = insideMargin,
151153
title = title,
@@ -204,7 +206,7 @@ fun SuperDropdown(
204206
) {
205207
LazyColumn(
206208
modifier = Modifier
207-
.padding(horizontal = popupHorizontalPadding)
209+
.padding(horizontal = (windowWeightPx.dp - componentWidthPx.dp) / 2 / density.density)
208210
.onGloballyPositioned { layoutCoordinates ->
209211
dropdownHeightPx = layoutCoordinates.size.height
210212
offsetPx = calculateOffsetPx(
@@ -321,7 +323,7 @@ fun DropdownImpl(
321323
* @param windowHeightPx The height of the window.
322324
* @param dropdownOffsetPx The default offset of the dropdown.
323325
* @param dropdownHeightPx The height of the dropdown.
324-
* @param componentHeight The height of the click component.
326+
* @param componentHeightPx The height of the component.
325327
* @param insideHeightPx The height of the component inside.
326328
* @param statusBarPx The height of the status bar padding.
327329
* @param navigationBarPx The height of the navigation bar padding.
@@ -332,16 +334,16 @@ fun calculateOffsetPx(
332334
windowHeightPx: Int,
333335
dropdownOffsetPx: Int,
334336
dropdownHeightPx: Int,
335-
componentHeight: Int,
337+
componentHeightPx: Int,
336338
insideHeightPx: Int,
337339
statusBarPx: Int,
338340
navigationBarPx: Int,
339341
captionBarPx: Int
340342
): Int {
341-
return if (windowHeightPx - dropdownOffsetPx < dropdownHeightPx / 2 + captionBarPx + navigationBarPx + insideHeightPx + componentHeight / 2) {
343+
return if (windowHeightPx - dropdownOffsetPx < dropdownHeightPx / 2 + captionBarPx + navigationBarPx + insideHeightPx + componentHeightPx / 2) {
342344
windowHeightPx - dropdownHeightPx - insideHeightPx - captionBarPx - navigationBarPx
343345
} else {
344-
val offset = dropdownOffsetPx - dropdownHeightPx / 2 + componentHeight / 2
346+
val offset = dropdownOffsetPx - dropdownHeightPx / 2 + componentHeightPx / 2
345347
if (offset > insideHeightPx + statusBarPx) offset else insideHeightPx + statusBarPx
346348
}
347349
}

miuix/src/desktopMain/kotlin/top/yukonga/miuix/kmp/utils/Utils.desktop.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ object WindowProvider {
1919
fun getWindowSize(): WindowSize {
2020
return composeWindow?.let { window ->
2121
WindowSize(
22-
width = window.bounds.width,
22+
width = window.bounds.width - window.insets.left - window.insets.right,
2323
height = window.bounds.height - window.insets.top
2424
)
2525
} ?: WindowSize(0, 0)

0 commit comments

Comments
 (0)