Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Commit

Permalink
Update wasm implementation too
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbanes committed Dec 11, 2023
1 parent 8fcf4a6 commit 41b8644
Showing 1 changed file with 7 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,25 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import kotlinx.browser.window
import org.w3c.dom.Window
import org.w3c.dom.events.Event

@ExperimentalMaterial3WindowSizeClassApi
@Composable
actual fun calculateWindowSizeClass(): WindowSizeClass {
val density = LocalDensity.current

var windowSizeClass by remember {
mutableStateOf(WindowSizeClass.calculateFromSize(window.getDpSize(density)))
mutableStateOf(
WindowSizeClass.calculateFromSize(window.getDpSize())
)
}

// Add a listener and listen for resize events
DisposableEffect(density) {
DisposableEffect(Unit) {
val callback: (Event) -> Unit = {
windowSizeClass = WindowSizeClass.calculateFromSize(window.getDpSize(density))
windowSizeClass = WindowSizeClass.calculateFromSize(window.getDpSize())
}

window.addEventListener("resize", callback)
Expand All @@ -41,6 +40,4 @@ actual fun calculateWindowSizeClass(): WindowSizeClass {
return windowSizeClass
}

private fun Window.getDpSize(density: Density): DpSize = with(density) {
DpSize(innerWidth.toDp(), innerHeight.toDp())
}
private fun Window.getDpSize(): DpSize = DpSize(innerWidth.dp, innerHeight.dp)

0 comments on commit 41b8644

Please sign in to comment.