Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix WindowState and DialogState savers to not crash on unspecified window/dialog size. #1394

Merged
merged 1 commit into from
Jun 10, 2024

Conversation

m-sasha
Copy link

@m-sasha m-sasha commented Jun 7, 2024

Fix WindowState.Saver to correctly save/restore unspecified window size.

Fixes JetBrains/compose-multiplatform#4945

Testing

Manually with

fun main() = singleWindowApplication {
    LazyColumn {
        items(1000) { index ->
            val state = rememberWindowState(size = DpSize.Unspecified)
            Text("$index " + state.size.toString())
        }
    }
}

and also added unit tests for saving/restoring `WindowState` and `DialogState`.

Release Notes

Fixes - Desktop

  • Correctly save WindowState with unspecified size instead of crashing.

@m-sasha m-sasha requested a review from igordmn June 7, 2024 13:42
@iamcalledrob
Copy link

Hi, issue submitter here. Amazing turnaround!

It looks like you'll need to apply the same fix in at least rememberDialogState too, which uses the same logic and has the same crash.

@m-sasha m-sasha force-pushed the m-sasha/fix-windowstate-saver branch 2 times, most recently from 4a492c1 to 2c97866 Compare June 7, 2024 16:58
@m-sasha
Copy link
Author

m-sasha commented Jun 7, 2024

It looks like you'll need to apply the same fix in at least rememberDialogState too, which uses the same logic and has the same crash.

There doesn't seem to be documentation that DialogState.size can be unspecified, but since it works de-facto, I fixed its saver too.

@m-sasha m-sasha changed the title Fix WindowState.Saver to not crash on unspecified window size. Fix WindowState and DialogState savers to not crash on unspecified window/dialog size. Jun 7, 2024
@m-sasha m-sasha requested a review from igordmn June 7, 2024 17:08
@igordmn
Copy link
Collaborator

igordmn commented Jun 7, 2024

There doesn't seem to be documentation that DialogState.size can be unspecified

DialogState indeed doesn't contain it compare to WindowState 🤔. It was an oversight, and it should be in KDoc


index = 1
waitForIdle()
index = 2
Copy link
Collaborator

@igordmn igordmn Jun 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test doesn't check that state can be restored correctly.

To check that, we need to change the size, and switch back to index = 0

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the need to switch back to index = 0, but why change the size?

The latest commit seems to work (I purposely "broke" restoring in order to test it, and the test fails correctly).

Copy link
Collaborator

@igordmn igordmn Jun 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but why change the size?

If we don't change the size, then we can't be sure that value was restored instead of reusing the initial passed value (or reused the value saved in index=1).

To check it correctly, it seems we need to have the initial size not Unspecified:

        var index by mutableIntStateOf(0)
        lateinit var lastRecomposedState: WindowState
        setContent {
            val saveableStateHolder = rememberSaveableStateHolder()
            saveableStateHolder.SaveableStateProvider(index) {
                val state = rememberWindowState(size = DpSize(10f, 10f))
                lastRecomposedState = sate
            }
        }

        state.size = DpSize.Unspecified
        waitForIdle()
        assertWindowStateEquals(WindowState(size = DpSize.Unspecified), lastRecomposedState)

        index = 1
        waitForIdle()
        assertWindowStateEquals(WindowState(size = DpSize(10f, 10f)), lastRecomposedState)

        index = 0
        waitForIdle()
        assertWindowStateEquals(WindowState(size = DpSize.Unspecified), lastRecomposedState)

@m-sasha m-sasha force-pushed the m-sasha/fix-windowstate-saver branch from 2c97866 to d53e952 Compare June 9, 2024 09:21
@m-sasha m-sasha merged commit 2d2b9e1 into jb-main Jun 10, 2024
8 checks passed
@m-sasha m-sasha deleted the m-sasha/fix-windowstate-saver branch June 10, 2024 10:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Crash when using rememberWindowState: DpSize is unspecified
3 participants