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

ShortcutsModifier - pressedKeys stuck when a child window is opened from a shortcut handler

Open Inego opened this issue 4 years ago • 2 comments

If a keyboard shortcut is assigned to an AppWindow and its handler leads to opening of another window, the pressed key stays in the ShortcutsInstance's pressedKeys set.

import androidx.compose.desktop.AppWindow
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import javax.swing.SwingUtilities


fun main() {

    SwingUtilities.invokeLater {

        var aIsPressed by mutableStateOf<Boolean>(false)

        AppWindow(title = "Parent").apply {

            show {
                var dialogShown by remember { mutableStateOf(false) }

                if (aIsPressed) {
                    dialogShown = true
                    aIsPressed = false
                }

                if (dialogShown) {
                    Dialog({ dialogShown = false }, DialogProperties(title = "Child")) { }
                }
            }

            keyboard.setShortcut(Key.A) {
                println("A was pressed.")
                aIsPressed = true
            }
        }
    }
}
  1. In the Parent window, press A.
  2. The Child window is opened.
  3. Close the Child window.
  4. Click the Parent window so it gets the focus.
  5. Press any key other than A. The keyboard shortcut handler is erroneously fired which leads to another opening of the Child window.

Inego avatar Mar 14 '21 06:03 Inego

There is a clumsy workaround for this bug: delay the opening of the Child window for a sufficient (100 ms) amount of time:

                val composeScope = rememberCoroutineScope()
                ...

                if (aIsPressed) {
                    aIsPressed = false
                    composeScope.launch {
                        delay(100)
                        dialogShown = true
                    }
                }

Inego avatar Mar 14 '21 07:03 Inego

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

okushnikov avatar Aug 26 '24 17:08 okushnikov