Jurriaan Mous
03/20/2023, 7:24 AM@OptIn(ExperimentalComposeUiApi::class)
fun main() = application {
val windows = rememberSaveable { mutableStateOf(listOf(0)) }
val newWindowId = rememberSaveable { mutableStateOf(1) }
fun onClose(windowId: Int) {
if (windows.value.isEmpty()) {
exitApplication()
}
windows.value = windows.value.filter { it != windowId }
}
windows.value.map { windowId ->
Window(onCloseRequest = { onClose(windowId) }, title = "Window") {
MenuBar {
Menu("File", mnemonic = 'F') {
Item(
"New Window",
onClick = {
windows.value = windows.value + newWindowId.value
newWindowId.value += 1
},
shortcut = KeyShortcut(Key.N, meta = true, alt = true)
)
}
}
App()
}
}
}
rememberSavable(key=windowId)
but it is still not closing the right window and maintaining the right state.Maik Liebing
03/20/2023, 8:08 AMfor (workspace in workspaces) {
key(workspace.id) {
WorkspaceWindow(...) {
...
}
}
}
WorkspaceWindow
is the function to create my windows. So it is similar to the body of your map
function. The important point here is the key
function as it makes sure, you always get the same state for the same identifier. Without the key function closing one window might reposition/resize other windows as they get different states now (while the content is still the same. So in your case, you should still have the correct windowIds). Maybe this solves your issue too.Jurriaan Mous
03/20/2023, 8:50 AMkey()
function was indeed the secret ingredient which I was missing. It works now. Thanks!!!