Can anyone share how to remember window state (in ...
# compose-desktop
s
Can anyone share how to remember window state (in particular size) of the main windows across app restarts? I tried to wrap
rememberWindowState
in
rememberSaveable
but keep getting "@Composable invocations can only happen from the context of a @Composable function" although I've moved the code already to a `@Composable`function:
Copy code
fun main() = application {
    createMainWindow { ::exitApplication }
}

@Composable
fun createMainWindow(onCloseRequest: () -> Unit) {
    val rememberedState =  rememberSaveable { rememberWindowState(width = Dp.Unspecified, height = Dp.Unspecified) }
    Window(
        onCloseRequest = onCloseRequest,
        state = rememberedState,
        title = "Stan"
    ) {
        var text by remember { mutableStateOf("Hello, World!") }

        Button(onClick = { text = "Hello, Desktop!" }) {
            Text(text)
        }
    }
}
z
You can't do a remember inside remember What do you mean by app restarts? Completely exiting and reopening? If that's what you mean then you'll have to store the window state to a file
s
> What do you mean by app restarts? Completely exiting and reopening? Yes. > If that's what you mean then you'll have to store the window state to a file Ok, bummer, I thought that's exactly what
rememberSaveable
saves be to do manually.
z
yea rememberSaveable is really only useful on Android
i
rememberSaveable
wipes the data when user explicitly closes the app on android. It's for things like restart due to config changes or memory optimizations. If you need a custom behaviour - it can be provided via
LocalSaveableStateRegistry
rememberSaveable is really only useful on Android
Nope - it's used for saving things like scroll position during navigation back
z
Oh, didn't know that
m
There is a native macOS system for this
You could use it on that platform