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

Marcin Wisniowski

11/21/2023, 3:09 PM
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

Alexander Maryanovsky

11/21/2023, 3:18 PM
If the target element is gone after
Press
, there’s nothing to deliver the
Release
to.
m

Marcin Wisniowski

11/21/2023, 3:26 PM
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

Alexander Maryanovsky

11/21/2023, 3:29 PM
You can launch the closing of the window in a coroutine (effectively waiting for the current event processing to complete)
m

Marcin Wisniowski

11/21/2023, 3:37 PM
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

Alexander Maryanovsky

11/21/2023, 3:39 PM
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

Marcin Wisniowski

11/21/2023, 3:41 PM
When the button is clicked, I set
visible = false
on the
Window
, so the window is still composed, just not visible.
a

Alexander Maryanovsky

11/21/2023, 3:42 PM
You can wrap it in an
if
instead. Then it’ll be gone.
m

Marcin Wisniowski

11/21/2023, 3:42 PM
I know, this is on purpose though because it makes reopening the window much faster.
a

Alexander Maryanovsky

11/21/2023, 3:43 PM
Then key the button, or remove the indication from it.
It’ll still be stuck in hover, but it won’t be visible.
m

Marcin Wisniowski

11/21/2023, 3:53 PM
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

Alexander Maryanovsky

11/21/2023, 3:57 PM
Use
key(windowTimestampOpen) { Button() }
m

Marcin Wisniowski

11/21/2023, 4:03 PM
That does fix it completely 🎉