compose-multiplatform icon indicating copy to clipboard operation
compose-multiplatform copied to clipboard

`window.toFront()` doesn't make a window active when the app is running in a tray

Open yevhenii-nadtochii opened this issue 1 year ago • 4 comments

A shown window fails to become active and gain focus. The window is created with alwaysOnTop = true, so it should gain focus automatically as shown. But direct calls to window.requestFocus() and window.toFront also have no effect.

Maybe it is because the app is a background application from the start (Tray + LSUIElement = true for MacOS). Tray menu has Show window item. And a user usually have another active application at the moment he/she clicks a Show window item from a tray menu.

Code snippet
fun main() = application {
    var isVisible by remember { mutableStateOf(true) }
    Tray(
        icon = stopwatch(),
        menu = {
            Item("Show Window", onClick = { isVisible = true })
            Item("Quit", onClick = ::exitApplication)
        }
    )
    Window(
        onCloseRequest = { isVisible = false },
        visible = isVisible,
        title = "Stopwatch App",
        icon = stopwatch(),
        alwaysOnTop = true,
    ) {
        var textValue by remember { mutableStateOf("") }
        val focus = remember { FocusRequester() }

        TextField(
            value = textValue,
            onValueChange = { textValue = it },
            modifier = Modifier.focusRequester(focus)
        )

        LaunchedEffect(isVisible) {
            if (isVisible) {
                window.toFront() // Can't gain focus, the window remains inactive.
                focus.requestFocus()  // Thus, the focus is NOT passed to the text field.
            }
        }
    }
}

Affected platforms

  • Desktop (macOS)

Versions

  • Kotlin version*: 1.9.20
  • Compose Multiplatform version*: 1.5.12
  • OS version(s)* (required for Desktop and iOS issues): Sonoma 14.2.1 (23C71)
  • OS architecture (x86 or arm64): both.
  • JDK (for desktop issues): openjdk 17.0.10

To Reproduce

window-focus-reproducer.zip

  1. Run the application from the archive, wait until the tray icon appears in the top bar.
  2. Click on an empty space on your Desktop to make sure the currently active app is Finder.
  3. Go to tray icon and show a window from the tray menu.
  4. The shown window is not active, its text field is not focused.

When the currently active app is NOT MainKt, the window never gets focused on showing up. When the currently active app is MainKt, the window gets focused half the time.

Expected behavior Top most window is active when it is shown, especially when this is requested explicitly by window.toFront() or window.requestFocus().

Screenshot 2024-02-05 at 12 12 10 PM

yevhenii-nadtochii avatar Feb 05 '24 11:02 yevhenii-nadtochii

Hello, @yevhenii-nadtochii, thanks for submitting the issue. Unfortunately, for now we don't have such API for desired behavior. window.toFront() don't do that work that you expect, a real window isn't visible at that moment. Right now, as a solution it seems like you may use window.addHierarchyListener in your case.

mazunin-v-jb avatar Feb 07 '24 12:02 mazunin-v-jb

I've tried this:

window.addHierarchyListener {
    window.toFront()
    window.requestFocus()
}

But the window remains inactive.

I've also tried with runDistributable (i.e., alwaysOnTop doesn't work if I run the app from IDEA). The result is the same.

yevhenii-nadtochii avatar Feb 07 '24 12:02 yevhenii-nadtochii

Is there any way to check if the window is on the front, a boolean function?

RafaelAthosPrime avatar Feb 08 '24 22:02 RafaelAthosPrime

You can use this to cause your window to move to front and become focusable when shown:

        LaunchedEffect(Unit) {
            focus.requestFocus()
        }

        DisposableEffect(window) {
            val listener = object: ComponentAdapter() {
                override fun componentShown(e: ComponentEvent?) {
                    super.componentShown(e)
                    window.toFront()
                    window.requestFocus()
                }
            }

            window.addComponentListener(listener)

            onDispose {
                window.removeComponentListener(listener)
            }
        }

m-sasha avatar Feb 19 '24 11:02 m-sasha

@m-sasha unfortunately this doesn't help.

yevhenii-nadtochii avatar Feb 21 '24 14:02 yevhenii-nadtochii

Can you post a new reproducer that uses that workaround?

m-sasha avatar Feb 21 '24 14:02 m-sasha

Try this:

fun main() = application {
    var isVisible by remember { mutableStateOf(false) }
    Tray(
        icon = stopwatch(),
        menu = {
            Item("Show Window", onClick = { isVisible = true })
            Item("Quit", onClick = ::exitApplication)
        }
    )
    Window(
        onCloseRequest = { isVisible = false },
        visible = isVisible,
        title = "Stopwatch App",
        icon = stopwatch(),
        alwaysOnTop = true,
    ) {
        var textValue by remember { mutableStateOf("") }
        val focus = remember { FocusRequester() }

        TextField(
            value = textValue,
            onValueChange = { textValue = it },
            modifier = Modifier.focusRequester(focus)
        )

        LaunchedEffect(Unit) {
            focus.requestFocus()
        }

        DisposableEffect(window) {
            val componentListener = object: ComponentAdapter() {
                override fun componentShown(e: ComponentEvent?) {
                    window.toFront()
                }
            }
            val windowListener = object: WindowAdapter() {
                override fun windowActivated(e: WindowEvent?) {
                    SwingUtilities.invokeLater {
                        focus.requestFocus()
                    }
                }
            }

            window.addComponentListener(componentListener)
            window.addWindowListener(windowListener)

            onDispose {
                window.removeComponentListener(componentListener)
                window.removeWindowListener(windowListener)
            }
        }
    }
}

m-sasha avatar Feb 23 '24 10:02 m-sasha

This workaround does better.

If the currently active app is AppKt, then both the window and the field become focused. So, you can hit Show window and start typing in the field right away.

But still no changes if the currently active app is another one, which is usually the case for tray apps.

yevhenii-nadtochii avatar Feb 23 '24 11:02 yevhenii-nadtochii

Ok, I found the magic incantation, it's Desktop.getDesktop().requestForeground():

fun main() = application {
    var isVisible by remember { mutableStateOf(false) }
    Tray(
        icon = stopwatch(),
        menu = {
            Item("Show Window", onClick = { isVisible = true })
            Item("Quit", onClick = ::exitApplication)
        }
    )
    Window(
        onCloseRequest = { isVisible = false },
        visible = isVisible,
        title = "Stopwatch App",
        icon = stopwatch(),
        alwaysOnTop = true,
    ) {
        var textValue by remember { mutableStateOf("") }
        val focus = remember { FocusRequester() }

        TextField(
            value = textValue,
            onValueChange = { textValue = it },
            modifier = Modifier.focusRequester(focus)
        )

        LaunchedEffect(Unit) {
            focus.requestFocus()
        }

        LaunchedEffect(isVisible) {
            if (isVisible) {
                Desktop.getDesktop().requestForeground(true)
            }
        }

        DisposableEffect(window) {
            val windowListener = object: WindowAdapter() {
                override fun windowActivated(e: WindowEvent?) {
                    SwingUtilities.invokeLater {
                        focus.requestFocus()
                    }
                }
            }

            window.addWindowListener(windowListener)

            onDispose {
                window.removeWindowListener(windowListener)
            }
        }
    }
}

m-sasha avatar Feb 23 '24 12:02 m-sasha

@m-sasha The magic did the trick! Thank you 🙂

There's interesting detail. I've noticed that sometimes a window still can't get active. One out of 3–7 attempts fails. Turns out that the launched effect is not always executed upon updates of isVisible variable (why?) and requestForeground() is not called at all.

Moving it out of Window composable finally solved the problem.

The final snippet
fun main() = application {
    var isVisible by remember { mutableStateOf(false) }

    Tray(
        icon = stopwatch(),
        menu = {
            Item("Show Window", onClick = { isVisible = true })
            Item("Quit", onClick = ::exitApplication)
        }
    )

    Window(
        onCloseRequest = { isVisible = false },
        visible = isVisible,
        title = "Stopwatch App",
        icon = stopwatch(),
        alwaysOnTop = true,
    ) {
        var textValue by remember { mutableStateOf("") }
        val focus = remember { FocusRequester() }

        TextField(
            value = textValue,
            onValueChange = { textValue = it },
            modifier = Modifier.focusRequester(focus)
        )

        DisposableEffect(window) {
            val listener = object : WindowAdapter() {
                override fun windowActivated(e: WindowEvent?) {
                    SwingUtilities.invokeLater {
                        focus.requestFocus()
                    }
                }
            }
            window.addWindowListener(listener)
            onDispose {
                window.removeWindowListener(listener)
            }
        }
    }

    LaunchedEffect(isVisible) {
        if (isVisible) {
            Desktop.getDesktop().requestForeground(true)
        }
    }
}

yevhenii-nadtochii avatar Feb 23 '24 18:02 yevhenii-nadtochii

Closing this, as it's not an issue with Compose, but with AWT.

m-sasha avatar Apr 18 '24 22:04 m-sasha

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

okushnikov avatar Jul 14 '24 15:07 okushnikov