Kirill Grouchnikov
11/03/2023, 10:14 PMComponent.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.Alexander Maryanovsky
11/03/2023, 11:03 PMKirill Grouchnikov
11/04/2023, 12:23 AMAlexander Maryanovsky
11/04/2023, 10:41 AMprivate 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")
}
}
}
Alexander Maryanovsky
11/04/2023, 10:43 AMAlexander Maryanovsky
11/04/2023, 11:28 AMAlexander Maryanovsky
11/04/2023, 11:29 AM(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.Igor Demin
11/07/2023, 10:10 AMfindComponentAt
- it will return the right target for the event