Skip to content

Commit 2836b0b

Browse files
committed
Modify the position of the popup
1 parent 0499757 commit 2836b0b

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ fun SuperDropdown(
190190
val captionBarPx by rememberUpdatedState(
191191
with(density) { WindowInsets.captionBar.asPaddingValues().calculateBottomPadding().toPx() }.roundToInt()
192192
)
193+
val insideWidthPx by rememberUpdatedState(with(density){ insideMargin.width.toPx() }.roundToInt())
193194
val insideHeightPx by rememberUpdatedState(with(density) { insideMargin.height.toPx() }.roundToInt())
194195
val displayCutoutLeftSize = rememberUpdatedState(with(density) {
195196
WindowInsets.displayCutout.asPaddingValues(density).calculateLeftPadding(LayoutDirection.Ltr).toPx()
@@ -221,9 +222,9 @@ fun SuperDropdown(
221222
modifier = Modifier
222223
.onGloballyPositioned { layoutCoordinates ->
223224
offsetXPx = if (alwaysRight || !alignLeft) {
224-
dropdownOffsetXPx + componentWidthPx - layoutCoordinates.size.width - paddingPx - if (defaultWindowInsetsPadding) displayCutoutLeftSize.value else 0
225+
dropdownOffsetXPx + componentWidthPx - insideWidthPx - layoutCoordinates.size.width - paddingPx - if (defaultWindowInsetsPadding) displayCutoutLeftSize.value else 0
225226
} else {
226-
dropdownOffsetXPx + paddingPx - if (defaultWindowInsetsPadding) displayCutoutLeftSize.value else 0
227+
dropdownOffsetXPx + paddingPx + insideWidthPx - if (defaultWindowInsetsPadding) displayCutoutLeftSize.value else 0
227228
}
228229
offsetYPx = calculateOffsetYPx(
229230
windowHeightPx,
@@ -353,7 +354,16 @@ fun calculateOffsetYPx(
353354
navigationBarPx: Int,
354355
captionBarPx: Int
355356
): Int {
356-
return if (windowHeightPx - dropdownOffsetPx < dropdownHeightPx / 2 + captionBarPx + navigationBarPx + insideHeightPx + componentHeightPx / 2) {
357+
return if (windowHeightPx - captionBarPx - navigationBarPx - dropdownOffsetPx - componentHeightPx > dropdownHeightPx) {
358+
// Show below
359+
dropdownOffsetPx + componentHeightPx - insideHeightPx / 2
360+
} else if (dropdownOffsetPx - statusBarPx > dropdownHeightPx) {
361+
// Show above
362+
dropdownOffsetPx - dropdownHeightPx + insideHeightPx / 2
363+
} else if (windowHeightPx - statusBarPx - captionBarPx - navigationBarPx <= dropdownHeightPx) {
364+
// Special handling when the height of the popup is maxsize (== windowHeightPx)
365+
0
366+
} else if (windowHeightPx - dropdownOffsetPx < dropdownHeightPx / 2 + captionBarPx + navigationBarPx + insideHeightPx + componentHeightPx / 2) {
357367
windowHeightPx - dropdownHeightPx - insideHeightPx - captionBarPx - navigationBarPx
358368
} else {
359369
val offset = dropdownOffsetPx - dropdownHeightPx / 2 + componentHeightPx / 2

0 commit comments

Comments
 (0)