Skip to content
This repository has been archived by the owner on Sep 28, 2024. It is now read-only.

whenDockedOnce & whenUndockedOnce don't work #1026

Open
ykrasik opened this issue Jul 6, 2019 · 2 comments
Open

whenDockedOnce & whenUndockedOnce don't work #1026

ykrasik opened this issue Jul 6, 2019 · 2 comments

Comments

@ykrasik
Copy link

ykrasik commented Jul 6, 2019

/~https://github.com/edvin/tornadofx/blob/master/src/main/java/tornadofx/Component.kt#L1261

The implementation looks wrong - the listener which is added to the onDockListeners is a lambda which wraps the original listener, but inside the lambda the code tries to remove the original listener (which quietly returns false).
The result - the listener gets called on each dock.

@edvin
Copy link
Owner

edvin commented Jul 8, 2019

Thanks for reporting. I've committed a fix now. I wanted to find a way to avoid the intermediary wrapped variable, but was unable to find a smoother way than this:

fun <U : UIComponent> U.whenDockedOnce(listener: (U) -> Unit) {
    if (onDockListeners == null) onDockListeners = mutableListOf()
    var wrapped: (U) -> Unit = {}
    wrapped = {
        runLater {
            onDockListeners!!.remove(wrapped)
        }
        listener(it)
    }
    whenDocked(wrapped)
}

The runLater is needed to avoid removing the listener from the list as it is being iterated over.

@ykrasik
Copy link
Author

ykrasik commented Jul 16, 2019

Yeah I couldn't figure out any nicer way either, seems like there's no way to reference the this of a lambda.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants