I have an `onPointerEvent(PointerEventType.Release...
# compose-desktop
m
I have an
onPointerEvent(PointerEventType.Release)
modifier, and an
onClick
modifier on the same element, which closes the window. The click release event is never delivered, regardless of the modifier order (the window closes before the event is delivered). How can I properly ensure that I get the click release event?
a
If the target element is gone after
Press
, there’s nothing to deliver the
Release
to.
m
onClick
fires when you release the mouse button, so they happen at the same time. Sometimes I get the Release event, sometimes I don't, seems like some race condition. It makes sense to me, but I'm looking for a solution. My problem is that my close button gets visually "stuck" in the pressed state when I reopen the window, because it was never told the cursor was released.
a
You can launch the closing of the window in a coroutine (effectively waiting for the current event processing to complete)
m
Hmm, that worked, thank you. Though now my close button gets "stuck" in the hover state, because
PointerEventType.Exit
is never delivered. The cursor technically never leaves the button before it's gone with the window, so it does make sense. 🤔
a
You can try `key`ing your button (or the entire UI) to something that changes when you re-open it.
Why is it still part of the composition after it’s “closed” though?
m
When the button is clicked, I set
visible = false
on the
Window
, so the window is still composed, just not visible.
a
You can wrap it in an
if
instead. Then it’ll be gone.
m
I know, this is on purpose though because it makes reopening the window much faster.
a
Then key the button, or remove the indication from it.
It’ll still be stuck in hover, but it won’t be visible.
m
to something that changes when you re-open it
Copy code
LaunchedEffect(windowOpenTimestamp) {
    state.isHovered = false // Window was just opened, clear hover
}
This worked. Though because it clears the hover state when the window is reopened, and the button hover state to normal state transition is animated, you can still see the button animating back to the normal state when you reopen the window. But it's barely noticeable so I can live with that! Thanks.
a
Use
key(windowTimestampOpen) { Button() }
m
That does fix it completely 🎉