<@U015TKX93PD> - is there a compose desktop equiva...
# compose-desktop
k
@Igor Demin - is there a compose desktop equivalent of AWT’s
Component.dispatchEvent()
? I’m trying to figure out how to port this code to Compose. That block sends a pair of
MOUSE_PRESSED
/
MOUSE_RELEASED
events to the center of the specific component, essentially emulating a mouse click in the center of that component - whatever it happens to be, a checkbox (to toggle it), a combobox (to show its popup), etc. Of course, there’s no guarantee that the centered click is the only possible interaction with that component, but that is the best approximation that I am trying to achieve to implement keyboard based activation of generic components in the ribbon (components have key tips, and activating the key tip via keyboard should activate that component). Now, before I embark on a more complicated path of requiring every single component that can be shown in the ribbon to provide that functionality on its own (via
MutableInteractionSource
or other mechanisms), is there a way to dispatch mouse press / release events in Compose Desktop and have them delivered to the right target? I’ve tried dispatching them to either
JRootPane
of the window, and to the inner
org.jetbrains.skiko.SkiaLayer$1
canvas layer, in both cases offsetting them to account for the component position in the window, but it doesn’t seem to be working.
a
Would using the semantic action work for you?
☝🏻 1
k
Do you have an example I can look at?
a
Ah, I don’t think you have official access to the semantic tree at runtime. You can get to it through accessibility, but it’s a hack, and we don’t guarantee it’ll keep working:
Copy code
private fun AccessibleContext.findChildWithName(name: String): AccessibleContext? {
    if (accessibleName == name)
        return this
    for (childIndex in 0 until accessibleChildrenCount) {
        val childResult = getAccessibleChild(childIndex).accessibleContext.findChildWithName(name)
        if (childResult != null)
            return childResult
    }

    return null
}


fun main() = singleWindowApplication {
    Column {
        Button(
            modifier = Modifier.semantics {
                text = AnnotatedString("button")
            },
            onClick = { println("Clicked") }
        ) {
            Text("Button")
        }

        Button(
            onClick = {
                val action = window.accessibleContext.findChildWithName("button")
                action?.accessibleAction?.doAccessibleAction(0)
            }
        ) {
            Text("Click me")
        }
    }
}
I think dispatching on the SkiaLayer should work…
Hmm, problem is I don’t see how to actually get Swing/AWT to send the event.
Copy code
(window.contentPane.getComponent(0) as Container).getComponent(1)
is the component on which Compose listens to mouse events. But I can’t seem to manage to get it to call the listeners.