JetBrains / compose-multiplatform

Compose Multiplatform, a modern UI framework for Kotlin that makes building performant and beautiful user interfaces easy and enjoyable.
https://jetbrains.com/lp/compose-multiplatform
Apache License 2.0
16.21k stars 1.17k forks source link

Restore focus when window becomes visible #4355

Closed m-sasha closed 1 month ago

m-sasha commented 8 months ago

Describe the bug When a window is hidden and then visible again, the focus is not restored to the element that had it before.

Affected platforms Select one of the platforms below:

Versions

To Reproduce

fun main() = application {
    var windowVisible by remember { mutableStateOf(true) }
    Window(
        onCloseRequest = { windowVisible = false },
        visible = windowVisible
    ) {
        val focusRequester = remember { FocusRequester() }
        var text by remember { mutableStateOf("") }
        TextField(
            value = text,
            onValueChange = { text = it },
            modifier = Modifier.focusRequester(focusRequester)
        )

        LaunchedEffect(Unit) {
            focusRequester.requestFocus()
        }

        LaunchedEffect(windowVisible) {
            if (!windowVisible) {
                delay(2000)
                windowVisible = true
            }
        }
    }
}

It seems the focus is actively moved away from the currently focused element by ComposeSceneMediator.focusListener.focusGained.

https://github.com/JetBrains/compose-multiplatform/assets/5230206/56f6930f-23bf-4d35-97ea-891560912d82

Expected behavior Focus should be kept/restored to the element that had it when the window became hidden.

m-sasha commented 8 months ago

This is part of the problem reported here: https://github.com/JetBrains/compose-multiplatform/issues/4231

okushnikov commented 2 months ago

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.