From a59e99ef12825f08f456d16a187955509950ca76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=99=E7=A9=BA?= <70465933+YuKongA@users.noreply.github.com> Date: Thu, 22 Aug 2024 19:41:15 +0800 Subject: [PATCH] library: Fix wasmJs get windowSize --- .../src/main/kotlin/module.publication.gradle.kts | 2 +- .../src/main/kotlin/root.publication.gradle.kts | 2 +- miuix/src/desktopMain/kotlin/expectUtils.desktop.kt | 7 ++++--- miuix/src/iosMain/kotlin/expectUtils.ios.kt | 3 ++- miuix/src/wasmJsMain/kotlin/expectUtils.wasmJs.kt | 7 ++++++- 5 files changed, 14 insertions(+), 7 deletions(-) diff --git a/convention-plugins/src/main/kotlin/module.publication.gradle.kts b/convention-plugins/src/main/kotlin/module.publication.gradle.kts index 237b757..797f851 100644 --- a/convention-plugins/src/main/kotlin/module.publication.gradle.kts +++ b/convention-plugins/src/main/kotlin/module.publication.gradle.kts @@ -17,7 +17,7 @@ publishing { // Provide artifacts information required by Maven Central pom { - name.set("Miuix-like UI") + name.set("miuix") description.set("A MiuiX-like UI for Kotlin Multiplatform") url.set("https://github.com/miuix-kotlin-multiplatform/miuix") diff --git a/convention-plugins/src/main/kotlin/root.publication.gradle.kts b/convention-plugins/src/main/kotlin/root.publication.gradle.kts index d535b05..30730b3 100644 --- a/convention-plugins/src/main/kotlin/root.publication.gradle.kts +++ b/convention-plugins/src/main/kotlin/root.publication.gradle.kts @@ -1,4 +1,4 @@ allprojects { group = "top.yukonga.miuix.kmp" - version = "0.0.1" + version = "0.0.2" } \ No newline at end of file diff --git a/miuix/src/desktopMain/kotlin/expectUtils.desktop.kt b/miuix/src/desktopMain/kotlin/expectUtils.desktop.kt index f0939aa..5ba7fdf 100644 --- a/miuix/src/desktopMain/kotlin/expectUtils.desktop.kt +++ b/miuix/src/desktopMain/kotlin/expectUtils.desktop.kt @@ -1,6 +1,7 @@ import androidx.compose.runtime.Composable import androidx.compose.ui.awt.ComposeWindow import androidx.compose.ui.platform.LocalDensity +import kotlin.math.roundToInt object WindowSizeProvider { private var composeWindow: ComposeWindow? = null @@ -24,9 +25,9 @@ actual fun getWindowSize(): WindowSize { val density = LocalDensity.current.density val windowSize = WindowSizeProvider.getWindowSize() return WindowSize( - width = (windowSize.width * density).toInt(), - height = (windowSize.height * density).toInt() + width = (windowSize.width * density).roundToInt(), + height = (windowSize.height * density).roundToInt() ) } -actual fun platform(): Platform = Platform.Desktop \ No newline at end of file +actual fun platform(): Platform = Platform.Desktop \ No newline at end of file diff --git a/miuix/src/iosMain/kotlin/expectUtils.ios.kt b/miuix/src/iosMain/kotlin/expectUtils.ios.kt index 6c6fc6c..cd2e4bb 100644 --- a/miuix/src/iosMain/kotlin/expectUtils.ios.kt +++ b/miuix/src/iosMain/kotlin/expectUtils.ios.kt @@ -3,6 +3,7 @@ import androidx.compose.ui.platform.LocalDensity import kotlinx.cinterop.ExperimentalForeignApi import kotlinx.cinterop.useContents import platform.UIKit.UIScreen +import kotlin.math.roundToInt @OptIn(ExperimentalForeignApi::class) @Composable @@ -11,7 +12,7 @@ actual fun getWindowSize(): WindowSize { val density = LocalDensity.current.density val width = screenBounds.useContents { size.width } * density val height = screenBounds.useContents { size.height } * density - return WindowSize(width.toInt(), height.toInt()) + return WindowSize(width.roundToInt(), height.roundToInt()) } actual fun platform(): Platform = Platform.IOS \ No newline at end of file diff --git a/miuix/src/wasmJsMain/kotlin/expectUtils.wasmJs.kt b/miuix/src/wasmJsMain/kotlin/expectUtils.wasmJs.kt index b731aee..7cece64 100644 --- a/miuix/src/wasmJsMain/kotlin/expectUtils.wasmJs.kt +++ b/miuix/src/wasmJsMain/kotlin/expectUtils.wasmJs.kt @@ -1,9 +1,14 @@ import androidx.compose.runtime.Composable +import androidx.compose.ui.platform.LocalDensity import kotlinx.browser.window +import kotlin.math.roundToInt @Composable actual fun getWindowSize(): WindowSize { - return WindowSize(window.innerWidth, window.innerHeight) + val density = LocalDensity.current.density + val width = window.innerWidth * density + val height = window.innerHeight * density + return WindowSize(width.roundToInt(), height.roundToInt()) } actual fun platform(): Platform = Platform.WasmJs \ No newline at end of file