Skip to content

Commit ade661c

Browse files
committed
update: Use PaddingValues instead of DpSize
update: Allow setting Scaffold's popupHost to null (using Scaffold and MiuixPopupHost separately)
1 parent c67349b commit ade661c

File tree

6 files changed

+45
-34
lines changed

6 files changed

+45
-34
lines changed

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import androidx.compose.foundation.clickable
55
import androidx.compose.foundation.interaction.MutableInteractionSource
66
import androidx.compose.foundation.layout.Arrangement
77
import androidx.compose.foundation.layout.Column
8+
import androidx.compose.foundation.layout.PaddingValues
89
import androidx.compose.foundation.layout.Row
910
import androidx.compose.foundation.layout.RowScope
1011
import androidx.compose.foundation.layout.fillMaxWidth
@@ -23,7 +24,6 @@ import androidx.compose.ui.graphics.Color
2324
import androidx.compose.ui.input.pointer.PointerEventType
2425
import androidx.compose.ui.input.pointer.pointerInput
2526
import androidx.compose.ui.text.font.FontWeight
26-
import androidx.compose.ui.unit.DpSize
2727
import androidx.compose.ui.unit.dp
2828
import top.yukonga.miuix.kmp.theme.MiuixTheme
2929

@@ -45,7 +45,7 @@ import top.yukonga.miuix.kmp.theme.MiuixTheme
4545
@Composable
4646
fun BasicComponent(
4747
modifier: Modifier = Modifier,
48-
insideMargin: DpSize = BasicComponentDefaults.InsideMargin,
48+
insideMargin: PaddingValues = BasicComponentDefaults.InsideMargin,
4949
title: String? = null,
5050
titleColor: BasicComponentColors = BasicComponentDefaults.titleColor(),
5151
summary: String? = null,
@@ -80,19 +80,12 @@ fun BasicComponent(
8080
}
8181
.heightIn(min = 56.dp)
8282
.fillMaxWidth()
83-
.padding(
84-
horizontal = insideMargin.width,
85-
vertical = insideMargin.height
86-
),
83+
.padding(insideMargin),
8784
verticalAlignment = Alignment.CenterVertically,
8885
horizontalArrangement = Arrangement.SpaceBetween
8986
) {
9087
leftAction?.let {
91-
Box(
92-
modifier = Modifier.padding(end = 16.dp)
93-
) {
94-
it()
95-
}
88+
it()
9689
}
9790
Column(
9891
modifier = Modifier.weight(1f)
@@ -130,7 +123,7 @@ object BasicComponentDefaults {
130123
/**
131124
* The default margin inside the [BasicComponent].
132125
*/
133-
val InsideMargin = DpSize(16.dp, 16.dp)
126+
val InsideMargin = PaddingValues(16.dp)
134127

135128
/**
136129
* The default color of the title.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ fun Scaffold(
6565
floatingActionButton: @Composable () -> Unit = {},
6666
floatingActionButtonPosition: MiuixFabPosition = MiuixFabPosition.End,
6767
snackbarHost: @Composable () -> Unit = {},
68+
popupHost: @Composable () -> Unit = { MiuixPopupHost() },
6869
containerColor: Color = MiuixTheme.colorScheme.background,
6970
contentWindowInsets: WindowInsets = WindowInsets.statusBars,
7071
content: @Composable (PaddingValues) -> Unit
@@ -91,7 +92,7 @@ fun Scaffold(
9192
snackbar = snackbarHost,
9293
fab = floatingActionButton,
9394
fabPosition = floatingActionButtonPosition,
94-
popup = { MiuixPopupHost() },
95+
popup = popupHost,
9596
contentWindowInsets = safeInsets,
9697
)
9798
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package top.yukonga.miuix.kmp.extra
22

33
import androidx.compose.foundation.Image
4+
import androidx.compose.foundation.layout.PaddingValues
45
import androidx.compose.foundation.layout.padding
56
import androidx.compose.foundation.layout.size
67
import androidx.compose.runtime.Composable
@@ -12,7 +13,6 @@ import androidx.compose.ui.Modifier
1213
import androidx.compose.ui.graphics.Color
1314
import androidx.compose.ui.graphics.ColorFilter
1415
import androidx.compose.ui.text.style.TextAlign
15-
import androidx.compose.ui.unit.DpSize
1616
import androidx.compose.ui.unit.dp
1717
import top.yukonga.miuix.kmp.basic.BasicComponent
1818
import top.yukonga.miuix.kmp.basic.BasicComponentColors
@@ -48,7 +48,7 @@ fun SuperArrow(
4848
rightText: String? = null,
4949
rightActionColor: RightActionColors = SuperArrowDefaults.rightActionColors(),
5050
onClick: (() -> Unit)? = null,
51-
insideMargin: DpSize = BasicComponentDefaults.InsideMargin,
51+
insideMargin: PaddingValues = BasicComponentDefaults.InsideMargin,
5252
enabled: Boolean = true
5353
) {
5454
val updatedOnClick by rememberUpdatedState(onClick)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package top.yukonga.miuix.kmp.extra
22

3+
import androidx.compose.foundation.layout.PaddingValues
34
import androidx.compose.foundation.layout.RowScope
45
import androidx.compose.runtime.Composable
56
import androidx.compose.runtime.getValue
@@ -8,7 +9,6 @@ import androidx.compose.runtime.remember
89
import androidx.compose.runtime.rememberUpdatedState
910
import androidx.compose.runtime.setValue
1011
import androidx.compose.ui.Modifier
11-
import androidx.compose.ui.unit.DpSize
1212
import top.yukonga.miuix.kmp.basic.BasicComponent
1313
import top.yukonga.miuix.kmp.basic.BasicComponentColors
1414
import top.yukonga.miuix.kmp.basic.BasicComponentDefaults
@@ -43,7 +43,7 @@ fun SuperCheckbox(
4343
checkboxColors: CheckboxColors = CheckboxDefaults.checkboxColors(),
4444
rightActions: @Composable RowScope.() -> Unit = {},
4545
checkboxLocation: CheckboxLocation = CheckboxLocation.Left,
46-
insideMargin: DpSize = BasicComponentDefaults.InsideMargin,
46+
insideMargin: PaddingValues = BasicComponentDefaults.InsideMargin,
4747
enabled: Boolean = true
4848
) {
4949
var isChecked by remember { mutableStateOf(checked) }

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

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import androidx.compose.foundation.clickable
66
import androidx.compose.foundation.gestures.detectTapGestures
77
import androidx.compose.foundation.interaction.MutableInteractionSource
88
import androidx.compose.foundation.layout.Arrangement
9+
import androidx.compose.foundation.layout.PaddingValues
910
import androidx.compose.foundation.layout.Row
1011
import androidx.compose.foundation.layout.WindowInsets
1112
import androidx.compose.foundation.layout.WindowInsetsSides
@@ -57,7 +58,6 @@ import androidx.compose.ui.text.font.FontWeight
5758
import androidx.compose.ui.text.rememberTextMeasurer
5859
import androidx.compose.ui.text.style.TextAlign
5960
import androidx.compose.ui.unit.Dp
60-
import androidx.compose.ui.unit.DpSize
6161
import androidx.compose.ui.unit.LayoutDirection
6262
import androidx.compose.ui.unit.dp
6363
import androidx.compose.ui.unit.sp
@@ -77,6 +77,7 @@ import top.yukonga.miuix.kmp.utils.MiuixPopupUtil.Companion.dismissPopup
7777
import top.yukonga.miuix.kmp.utils.MiuixPopupUtil.Companion.showPopup
7878
import top.yukonga.miuix.kmp.utils.SmoothRoundedCornerShape
7979
import top.yukonga.miuix.kmp.utils.getWindowSize
80+
import kotlin.math.max
8081
import 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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package top.yukonga.miuix.kmp.extra
22

3+
import androidx.compose.foundation.layout.PaddingValues
34
import androidx.compose.foundation.layout.RowScope
45
import androidx.compose.runtime.Composable
56
import androidx.compose.runtime.getValue
@@ -10,7 +11,6 @@ import androidx.compose.runtime.setValue
1011
import androidx.compose.ui.Modifier
1112
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
1213
import androidx.compose.ui.platform.LocalHapticFeedback
13-
import androidx.compose.ui.unit.DpSize
1414
import top.yukonga.miuix.kmp.basic.BasicComponent
1515
import top.yukonga.miuix.kmp.basic.BasicComponentColors
1616
import top.yukonga.miuix.kmp.basic.BasicComponentDefaults
@@ -46,7 +46,7 @@ fun SuperSwitch(
4646
switchColors: SwitchColors = SwitchDefaults.switchColors(),
4747
leftAction: @Composable (() -> Unit)? = null,
4848
rightActions: @Composable RowScope.() -> Unit = {},
49-
insideMargin: DpSize = BasicComponentDefaults.InsideMargin,
49+
insideMargin: PaddingValues = BasicComponentDefaults.InsideMargin,
5050
enabled: Boolean = true
5151
) {
5252
var isChecked by remember { mutableStateOf(checked) }

0 commit comments

Comments
 (0)