From 37ad2e8bb88f43f10c90f2dede3627732e6aee6b Mon Sep 17 00:00:00 2001 From: Adit Lal Date: Sat, 29 Jun 2024 19:31:01 +0530 Subject: [PATCH] Adds step 6.b - Adds getWindowSize for measuring any components requiring measurements of screen --- .../app/academy/utils/WindowSize.android.kt | 12 +++++++++++ .../kotlin/app/academy/utils/WindowSize.kt | 7 +++++++ .../app/academy/utils/WindowSize.ios.kt | 20 +++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 composeApp/src/androidMain/kotlin/app/academy/utils/WindowSize.android.kt create mode 100644 composeApp/src/commonMain/kotlin/app/academy/utils/WindowSize.kt create mode 100644 composeApp/src/iosMain/kotlin/app/academy/utils/WindowSize.ios.kt diff --git a/composeApp/src/androidMain/kotlin/app/academy/utils/WindowSize.android.kt b/composeApp/src/androidMain/kotlin/app/academy/utils/WindowSize.android.kt new file mode 100644 index 0000000..3552715 --- /dev/null +++ b/composeApp/src/androidMain/kotlin/app/academy/utils/WindowSize.android.kt @@ -0,0 +1,12 @@ +package app.academy.utils + +import androidx.compose.runtime.Composable +import androidx.compose.ui.platform.LocalConfiguration +import androidx.compose.ui.unit.DpSize +import androidx.compose.ui.unit.dp + +@Composable +actual fun getWindowSize(): DpSize { + val screen = LocalConfiguration.current + return DpSize(screen.screenWidthDp.dp, screen.screenHeightDp.dp) +} diff --git a/composeApp/src/commonMain/kotlin/app/academy/utils/WindowSize.kt b/composeApp/src/commonMain/kotlin/app/academy/utils/WindowSize.kt new file mode 100644 index 0000000..7d03197 --- /dev/null +++ b/composeApp/src/commonMain/kotlin/app/academy/utils/WindowSize.kt @@ -0,0 +1,7 @@ +package app.academy.utils + +import androidx.compose.runtime.Composable +import androidx.compose.ui.unit.DpSize + +@Composable +expect fun getWindowSize(): DpSize diff --git a/composeApp/src/iosMain/kotlin/app/academy/utils/WindowSize.ios.kt b/composeApp/src/iosMain/kotlin/app/academy/utils/WindowSize.ios.kt new file mode 100644 index 0000000..41d4aa6 --- /dev/null +++ b/composeApp/src/iosMain/kotlin/app/academy/utils/WindowSize.ios.kt @@ -0,0 +1,20 @@ +package app.academy.utils + +import androidx.compose.runtime.Composable +import androidx.compose.ui.ExperimentalComposeUiApi +import androidx.compose.ui.platform.LocalWindowInfo +import androidx.compose.ui.unit.DpSize +import androidx.compose.ui.unit.dp +import kotlinx.cinterop.ExperimentalForeignApi +import platform.UIKit.UIScreen + +@Composable +@OptIn(ExperimentalComposeUiApi::class) +actual fun getWindowSize(): DpSize { + val screen = LocalWindowInfo.current.containerSize + val width = screen.width + val height = screen.height + // Convert points to dp + val density = UIScreen.mainScreen.scale + return DpSize((width / density).dp, (height / density).dp) +}