https://kotlinlang.org logo
#compose-desktop
Title
# compose-desktop
b

Brendan Campbell-hartzell

06/24/2022, 6:36 PM
If I'm wrapping a Swing component in a SwingPanel, how can I send native Swing events to modifiers that were passed in? For example, say, the onPreviewKeyEvent Modifier?
i

Igor Demin

06/29/2022, 9:27 PM
You can import awtEventOrNull extension. It is available for KeyEvent and for PointerEvent.
b

Brendan Campbell-hartzell

06/29/2022, 10:55 PM
No, the problem is that the modifiers aren't called when attached to the SwingPanel, so I'm assuming I'd have to do that myself in the Swing code by adding listeners and directly calling the modifiers to connect them together. Is it not possible to see what modifiers were received and work with them manually?
i

Igor Demin

06/29/2022, 11:18 PM
Ah, I understand. You are trying to implement something like this:
Copy code
SwingPanel(Modifier.onPreviewKeyEvent = {}, factory = ::SomeSwingComponent)
And clicks on SomeSwingComponent and key presses when SomeSwingComponent is in focus aren’t caught by the upper modifier. Yes, this isn’t implemented unfortunately. The swing event handling tree is detached from the Compose event handling tree at the moment, and the only way to catch the events on the Swing component is to set listeners manually. But I will investigate in the future, if it is possible to make a seamless event tree for Swing/Compose components.
Is it not possible to see what modifiers were received and work with them manually
There can be not only modifiers on SwingPanel, but also on some upper component:
Copy code
Box(Modifier.onPreviewKeyEvent) {
  SwingPanel
}
Such configuration isn’t supported too, and there is no way to pass the event to this modifier, because the event system in Swing doesn’t know about this Compose component, and connecting the Swing event system to this component is probably only possible inside the internals of Compose.
b

Brendan Campbell-hartzell

06/30/2022, 1:06 AM
That's unfortunate. I ended up just adding my own
on<SomeEvent>
- style inputs that I could end up using in the update method for the SwingPanel. It works, but it sort of breaks the interface from an external user's perspective. It'd be nice if we could inspect the Modifier object for the types that were passed in so that, when creating custom components, we could decide that certain parts of the modifier should be passed to some subcomponent while another is passed to something else.
10 Views