Skip to content

Commit 2780fa6

Browse files
committed
Use material3-window-size-class to get current window size
1 parent 8032d5a commit 2780fa6

File tree

13 files changed

+67
-74
lines changed

13 files changed

+67
-74
lines changed

composeApp/src/commonMain/kotlin/SecondPage.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import androidx.compose.runtime.Composable
55
import androidx.compose.runtime.mutableStateOf
66
import androidx.compose.runtime.remember
77
import androidx.compose.ui.Modifier
8+
import androidx.compose.ui.unit.dp
9+
import top.yukonga.miuix.kmp.basic.LazyColumn
810
import top.yukonga.miuix.kmp.basic.ScrollBehavior
911
import top.yukonga.miuix.kmp.extra.SuperDropdown
10-
import top.yukonga.miuix.kmp.basic.LazyColumn
1112

1213
@Composable
1314
fun SecondPage(
@@ -28,6 +29,7 @@ fun SecondPage(
2829
items = dropdownOptions,
2930
selectedIndex = dropdownSelectedOption.value,
3031
onSelectedIndexChange = { newOption -> dropdownSelectedOption.value = newOption },
32+
horizontalPadding = 12.dp
3133
)
3234
}
3335
item {

composeApp/src/commonMain/kotlin/ThirdPage.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import androidx.compose.runtime.Composable
55
import androidx.compose.runtime.MutableState
66
import androidx.compose.ui.Modifier
77
import androidx.compose.ui.unit.dp
8+
import top.yukonga.miuix.kmp.basic.LazyColumn
89
import top.yukonga.miuix.kmp.basic.ScrollBehavior
910
import top.yukonga.miuix.kmp.extra.SuperDropdown
1011
import top.yukonga.miuix.kmp.extra.SuperSwitch
11-
import top.yukonga.miuix.kmp.basic.LazyColumn
1212
import top.yukonga.miuix.kmp.utils.Platform
1313
import top.yukonga.miuix.kmp.utils.getWindowSize
1414
import top.yukonga.miuix.kmp.utils.platform
@@ -65,7 +65,8 @@ fun ThirdPage(
6565
title = "Color Mode",
6666
items = listOf("System", "Light", "Dark"),
6767
selectedIndex = colorMode.value,
68-
onSelectedIndexChange = { colorMode.value = it }
68+
onSelectedIndexChange = { colorMode.value = it },
69+
horizontalPadding = 12.dp
6970
)
7071
}
7172
item {

composeApp/src/desktopMain/kotlin/main.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import androidx.compose.ui.window.Window
55
import androidx.compose.ui.window.WindowPosition
66
import androidx.compose.ui.window.application
77
import androidx.compose.ui.window.rememberWindowState
8-
import top.yukonga.miuix.kmp.utils.WindowProvider
98

109
fun main() = application {
1110
val state = rememberWindowState(
@@ -17,7 +16,6 @@ fun main() = application {
1716
onCloseRequest = ::exitApplication,
1817
title = "Miuix",
1918
) {
20-
WindowProvider.init(window)
2119
App()
2220
}
2321
}

gradle/libs.versions.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ kotlin = "2.0.20"
1212
[libraries]
1313
androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "androidx-activity-compose" }
1414
androidx-window = { group = "androidx.window", name = "window", version.ref = "androidx-window" }
15+
jetbrains-compose-window-size = { module = "org.jetbrains.compose.material3:material3-window-size-class", version.ref = "compose-plugin" }
1516

1617
[plugins]
1718
android-application = { id = "com.android.application", version.ref = "androidGradlePlugin" }

miuix/build.gradle.kts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ kotlin {
5050
}
5151
desktopMain.dependencies {
5252
implementation(compose.desktop.currentOs)
53+
implementation(libs.jetbrains.compose.window.size)
54+
}
55+
jsMain.dependencies {
56+
implementation(libs.jetbrains.compose.window.size)
57+
}
58+
macosMain.dependencies {
59+
implementation(libs.jetbrains.compose.window.size)
60+
}
61+
wasmJsMain.dependencies {
62+
implementation(libs.jetbrains.compose.window.size)
5363
}
5464
}
5565
}

miuix/src/androidMain/kotlin/top/yukonga/miuix/kmp/utils/Utils.android.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ import androidx.window.layout.WindowMetricsCalculator
1616
actual fun getWindowSize(): WindowSize {
1717
val context = LocalContext.current
1818
val windowMetrics: WindowMetrics = WindowMetricsCalculator.getOrCreate().computeCurrentWindowMetrics(context)
19-
2019
val widthPx = windowMetrics.bounds.width()
2120
val heightPx = windowMetrics.bounds.height()
22-
2321
return WindowSize(widthPx, heightPx)
2422
}
2523

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,12 @@ fun SuperDialog(
7171
) {
7272
val density = LocalDensity.current
7373
val getWindowSize by rememberUpdatedState(getWindowSize())
74-
println(getWindowSize.width.dp / density.density)
74+
val windowWidth = getWindowSize.width.dp / density.density
75+
val windowHeight = getWindowSize.height.dp / density.density
7576
val paddingModifier = remember(outsideMargin) { Modifier.padding(horizontal = outsideMargin.width).padding(bottom = outsideMargin.height) }
7677
val roundedCorner by rememberUpdatedState(getRoundedCorner())
7778
val bottomCornerRadius by remember { derivedStateOf { if (roundedCorner != 0.dp) roundedCorner - outsideMargin.width else 32.dp } }
78-
val contentAlignment by remember { derivedStateOf { if (getWindowSize.width > getWindowSize.height && getWindowSize.width.dp / density.density > 1200.dp) Alignment.Center else Alignment.BottomCenter } }
79+
val contentAlignment by rememberUpdatedState { derivedStateOf { if (windowHeight >= 480.dp && windowWidth >= 840.dp) Alignment.Center else Alignment.BottomCenter } }
7980

8081
if (!dialogStates.contains(show)) dialogStates.add(show)
8182
LaunchedEffect(show.value) {
@@ -114,7 +115,7 @@ fun SuperDialog(
114115
.pointerInput(Unit) {
115116
detectTapGestures { /* Do nothing to consume the click */ }
116117
}
117-
.align(contentAlignment)
118+
.align(contentAlignment.invoke().value)
118119
.graphicsLayer(
119120
shape = SquircleShape(bottomCornerRadius),
120121
clip = false

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ import kotlin.math.roundToInt
7979
* @param summary The summary of the [SuperDropdown].
8080
* @param summaryColor The color of the summary.
8181
* @param items The options of the [SuperDropdown].
82+
* @param horizontalPadding The horizontal padding of the [SuperDropdown].
8283
* @param alwaysRight Whether the popup is always show on the right side.
8384
* @param insideMargin The margin inside the [SuperDropdown].
8485
* @param defaultWindowInsetsPadding Whether to apply default window insets padding to the [SuperDropdown].
@@ -95,6 +96,7 @@ fun SuperDropdown(
9596
summaryColor: Color = MiuixTheme.colorScheme.onSurfaceVariantSummary,
9697
items: List<String>,
9798
alwaysRight: Boolean = false,
99+
horizontalPadding: Dp = 0.dp,
98100
insideMargin: DpSize = DpSize(16.dp, 16.dp),
99101
defaultWindowInsetsPadding: Boolean = true,
100102
selectedIndex: Int,
@@ -207,7 +209,7 @@ fun SuperDropdown(
207209
LazyColumn(
208210
modifier = Modifier
209211
.padding(
210-
horizontal = (windowWeightPx.dp - componentWidthPx.dp) / 2 / density.density - if (defaultWindowInsetsPadding)
212+
horizontal = horizontalPadding + (windowWeightPx.dp - componentWidthPx.dp) / 2 / density.density - if (defaultWindowInsetsPadding)
211213
(WindowInsets.displayCutout.asPaddingValues(density).calculateLeftPadding(LayoutDirection.Ltr) +
212214
WindowInsets.displayCutout.asPaddingValues(density).calculateRightPadding(LayoutDirection.Ltr)) / 2 else 0.dp
213215
)

miuix/src/commonMain/kotlin/top/yukonga/miuix/kmp/utils/Utils.kt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@ package top.yukonga.miuix.kmp.utils
33
import androidx.compose.runtime.Composable
44
import androidx.compose.ui.unit.Dp
55

6+
/**
7+
* Window size data class.
8+
*/
9+
data class WindowSize(val width: Int, val height: Int)
10+
11+
/**
12+
* Returns the current window size.
13+
*/
14+
@Composable
15+
expect fun getWindowSize(): WindowSize
16+
617
/**
718
* Platform enum class.
819
*/
@@ -20,17 +31,6 @@ enum class Platform {
2031
*/
2132
expect fun platform(): Platform
2233

23-
/**
24-
* Window size data class.
25-
*/
26-
data class WindowSize(val width: Int, val height: Int)
27-
28-
/**
29-
* Returns the current window size.
30-
*/
31-
@Composable
32-
expect fun getWindowSize(): WindowSize
33-
3434
/**
3535
* Returns the rounded corner of the current device.
3636
*/

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

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,20 @@
11
package top.yukonga.miuix.kmp.utils
22

33
import androidx.compose.runtime.Composable
4-
import androidx.compose.ui.awt.ComposeWindow
5-
import androidx.compose.ui.platform.LocalDensity
4+
import androidx.compose.ui.ExperimentalComposeUiApi
5+
import androidx.compose.ui.platform.LocalWindowInfo
66
import androidx.compose.ui.unit.Dp
77
import androidx.compose.ui.unit.dp
88
import java.awt.KeyboardFocusManager
99
import java.awt.event.KeyEvent
10-
import kotlin.math.roundToInt
11-
12-
object WindowProvider {
13-
private var composeWindow: ComposeWindow? = null
14-
15-
fun init(window: ComposeWindow) {
16-
composeWindow = window
17-
}
18-
19-
fun getWindowSize(): WindowSize {
20-
return composeWindow?.let { window ->
21-
WindowSize(
22-
width = window.bounds.width - window.insets.left - window.insets.right,
23-
height = window.bounds.height - window.insets.top
24-
)
25-
} ?: WindowSize(0, 0)
26-
}
27-
}
2810

2911
@Composable
12+
@OptIn(ExperimentalComposeUiApi::class)
3013
actual fun getWindowSize(): WindowSize {
31-
val density = LocalDensity.current.density
32-
val windowSize = WindowProvider.getWindowSize()
14+
val window = LocalWindowInfo.current
3315
return WindowSize(
34-
width = (windowSize.width * density).roundToInt(),
35-
height = (windowSize.height * density).roundToInt()
16+
width = window.containerSize.width,
17+
height = window.containerSize.height
3618
)
3719
}
3820

0 commit comments

Comments
 (0)