@@ -6,6 +6,7 @@ import androidx.compose.foundation.clickable
66import androidx.compose.foundation.gestures.detectTapGestures
77import androidx.compose.foundation.interaction.MutableInteractionSource
88import androidx.compose.foundation.layout.Arrangement
9+ import androidx.compose.foundation.layout.PaddingValues
910import androidx.compose.foundation.layout.Row
1011import androidx.compose.foundation.layout.WindowInsets
1112import androidx.compose.foundation.layout.WindowInsetsSides
@@ -57,7 +58,6 @@ import androidx.compose.ui.text.font.FontWeight
5758import androidx.compose.ui.text.rememberTextMeasurer
5859import androidx.compose.ui.text.style.TextAlign
5960import androidx.compose.ui.unit.Dp
60- import androidx.compose.ui.unit.DpSize
6161import androidx.compose.ui.unit.LayoutDirection
6262import androidx.compose.ui.unit.dp
6363import androidx.compose.ui.unit.sp
@@ -77,6 +77,7 @@ import top.yukonga.miuix.kmp.utils.MiuixPopupUtil.Companion.dismissPopup
7777import top.yukonga.miuix.kmp.utils.MiuixPopupUtil.Companion.showPopup
7878import top.yukonga.miuix.kmp.utils.SmoothRoundedCornerShape
7979import top.yukonga.miuix.kmp.utils.getWindowSize
80+ import kotlin.math.max
8081import kotlin.math.roundToInt
8182
8283/* *
@@ -110,7 +111,7 @@ fun SuperDropdown(
110111 summaryColor : BasicComponentColors = BasicComponentDefaults .summaryColor(),
111112 alwaysRight : Boolean = false,
112113 horizontalPadding : Dp = 0.dp,
113- insideMargin : DpSize = BasicComponentDefaults .InsideMargin ,
114+ insideMargin : PaddingValues = BasicComponentDefaults .InsideMargin ,
114115 defaultWindowInsetsPadding : Boolean = true,
115116 enabled : Boolean = true
116117) {
@@ -231,8 +232,18 @@ fun SuperDropdown(
231232 val dropdownElevation by rememberUpdatedState(with (density) {
232233 11 .dp.toPx()
233234 })
234- val insideWidthPx by rememberUpdatedState(with (density) { insideMargin.width.toPx() }.roundToInt())
235- val insideHeightPx by rememberUpdatedState(with (density) { insideMargin.height.toPx() }.roundToInt())
235+ val insideLeftPx by rememberUpdatedState(with (density) {
236+ insideMargin.calculateLeftPadding(LayoutDirection .Ltr ).toPx()
237+ }.roundToInt())
238+ val insideRightPx by rememberUpdatedState(with (density) {
239+ insideMargin.calculateRightPadding(LayoutDirection .Ltr ).toPx()
240+ }.roundToInt())
241+ val insideTopPx by rememberUpdatedState(with (density) {
242+ insideMargin.calculateTopPadding().toPx()
243+ }.roundToInt())
244+ val insideBottomPx by rememberUpdatedState(with (density) {
245+ insideMargin.calculateBottomPadding().toPx()
246+ }.roundToInt())
236247 val displayCutoutLeftSize = rememberUpdatedState(with (density) {
237248 WindowInsets .displayCutout.asPaddingValues(density).calculateLeftPadding(LayoutDirection .Ltr ).toPx()
238249 }.roundToInt())
@@ -265,16 +276,17 @@ fun SuperDropdown(
265276 modifier = Modifier
266277 .onGloballyPositioned { layoutCoordinates ->
267278 offsetXPx = if (alwaysRight || ! alignLeft) {
268- dropdownOffsetXPx + componentWidthPx - insideWidthPx - layoutCoordinates.size.width - paddingPx - if (defaultWindowInsetsPadding) displayCutoutLeftSize.value else 0
279+ dropdownOffsetXPx + componentWidthPx - insideRightPx - layoutCoordinates.size.width - paddingPx - if (defaultWindowInsetsPadding) displayCutoutLeftSize.value else 0
269280 } else {
270- dropdownOffsetXPx + paddingPx + insideWidthPx - if (defaultWindowInsetsPadding) displayCutoutLeftSize.value else 0
281+ dropdownOffsetXPx + paddingPx + insideLeftPx - if (defaultWindowInsetsPadding) displayCutoutLeftSize.value else 0
271282 }
272283 offsetYPx = calculateOffsetYPx(
273284 windowHeightPx,
274285 dropdownOffsetYPx,
275286 layoutCoordinates.size.height,
276287 componentHeightPx,
277- insideHeightPx,
288+ insideTopPx,
289+ insideBottomPx,
278290 statusBarPx,
279291 navigationBarPx,
280292 captionBarPx
@@ -385,7 +397,8 @@ fun DropdownImpl(
385397 * @param dropdownOffsetPx The default offset of the dropdown.
386398 * @param dropdownHeightPx The height of the dropdown.
387399 * @param componentHeightPx The height of the component.
388- * @param insideHeightPx The height of the component inside.
400+ * @param insideTopHeightPx The height of the top component inside.
401+ * @param insideBottomHeightPx The height of the bottom component inside.
389402 * @param statusBarPx The height of the status bar padding.
390403 * @param navigationBarPx The height of the navigation bar padding.
391404 * @param captionBarPx The height of the caption bar padding.
@@ -396,25 +409,29 @@ fun calculateOffsetYPx(
396409 dropdownOffsetPx : Int ,
397410 dropdownHeightPx : Int ,
398411 componentHeightPx : Int ,
399- insideHeightPx : Int ,
412+ insideTopHeightPx : Int ,
413+ insideBottomHeightPx : Int ,
400414 statusBarPx : Int ,
401415 navigationBarPx : Int ,
402416 captionBarPx : Int
403417): Int {
404418 return if (windowHeightPx - captionBarPx - navigationBarPx - dropdownOffsetPx - componentHeightPx > dropdownHeightPx) {
405419 // Show below
406- dropdownOffsetPx + componentHeightPx - insideHeightPx / 2
420+ dropdownOffsetPx + componentHeightPx - insideBottomHeightPx / 2
407421 } else if (dropdownOffsetPx - statusBarPx > dropdownHeightPx) {
408422 // Show above
409- dropdownOffsetPx - dropdownHeightPx + insideHeightPx / 2
423+ dropdownOffsetPx - dropdownHeightPx + insideTopHeightPx / 2
410424 } else if (windowHeightPx - statusBarPx - captionBarPx - navigationBarPx <= dropdownHeightPx) {
411425 // Special handling when the height of the popup is maxsize (== windowHeightPx)
412426 statusBarPx
413- } else if (windowHeightPx - dropdownOffsetPx < dropdownHeightPx / 2 + captionBarPx + navigationBarPx + insideHeightPx + componentHeightPx / 2 ) {
414- windowHeightPx - dropdownHeightPx - insideHeightPx - captionBarPx - navigationBarPx
415427 } else {
416- val offset = dropdownOffsetPx - dropdownHeightPx / 2 + componentHeightPx / 2
417- if (offset > insideHeightPx + statusBarPx) offset else insideHeightPx + statusBarPx
428+ val maxInsideHeight = max(insideTopHeightPx, insideBottomHeightPx)
429+ if (windowHeightPx - dropdownOffsetPx < dropdownHeightPx / 2 + captionBarPx + navigationBarPx + maxInsideHeight + componentHeightPx / 2 ) {
430+ windowHeightPx - dropdownHeightPx - maxInsideHeight - captionBarPx - navigationBarPx
431+ } else {
432+ val offset = dropdownOffsetPx - dropdownHeightPx / 2 + componentHeightPx / 2
433+ if (offset > maxInsideHeight + statusBarPx) offset else maxInsideHeight + statusBarPx
434+ }
418435 }
419436}
420437
0 commit comments