I am not sure how to adapt the code ```val userPr...
# compose-desktop
y
I am not sure how to adapt the code
Copy code
val userPreference = UserPreference()

Window(
  size = userPreference.windowSize,
  resizable = true,
  title = "Test",
  events = WindowEvents(onResize = userPreference::onWindowResize)
) {
  App(userPreference)
}
to the new Window api. It looks like I can use
val state = rememberWindowState(size = userPreference.windowSize)
, but I am not sure how to call
onWindowResize
when
state.size
changes...
i
It is a bit more code in a new API 🙂
Copy code
val state = rememberWindowState()

Window(state) {

}

LaunchedEffect(state) {
    snapshotFlow { state.size }
        .onEach(::onWindowResize)
        .launchIn(this)

    snapshotFlow { state.position }
        .filterNot { it.isInitial }
        .onEach(::onWindowRelocate)
        .launchIn(this)
}
(similar to listening the other hoisted states, like LazyListState)
y
thank you. I will try this. I don't mind that it is more code. It is just that I had no clue how to do it and clearly it is not trivial. I carefully read the tutorial for this new api (https://github.com/JetBrains/compose-jb/tree/master/tutorials/Window_API_new) but I couldn't find a description about it.
i
description about it
We forgot about it and added it recently. Thanks for bringing this 🙂
y
No problem. I guess that is what using cutting edge tech means... Glad to be able to help as well by providing feedback.
I do not know if I am doing something wrong, but when I copy/paste the code as-is, the application no longer quits when closing the window. Moving the
LaunchedEffect
section inside the
Window
callback allow the application to terminate properly...
i
Wow, thanks for noticing this, I changed the tutorials. Yes, the right approach is to write this in
Window
scope. Because when we close the window - we will cancel all launched inside
Window
coroutines. If we launch coroutines inside
application
- we can't cancel them.
👍 1