Skip to content

Commit

Permalink
Backport (as much as possible) edvin/tornadofx2#40
Browse files Browse the repository at this point in the history
  • Loading branch information
guizmaii committed Aug 5, 2021
1 parent bb4bfe2 commit 2cae596
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 47 deletions.
82 changes: 37 additions & 45 deletions src/main/java/tornadofx/Component.kt
Original file line number Diff line number Diff line change
Expand Up @@ -529,61 +529,53 @@ abstract class UIComponent(viewTitle: String? = "", icon: Node? = null) : Compon
properties["tornadofx.closeable"] = SimpleBooleanProperty(false)
}

private val rootParentChangeListener: ChangeListener<Parent>
get() {
val key = "tornadofx.rootParentChangeListener"
if (properties[key] == null) {
properties[key] = ChangeListener<Parent> { _, oldParent, newParent ->
if (modalStage != null) return@ChangeListener
if (newParent == null && oldParent != null && isDocked) callOnUndock()
if (newParent != null && newParent != oldParent && !isDocked) {
callOnDock()
// Call `onTabSelected` if/when we are connected to a Tab and it's selected
// Note that this only works for builder constructed tabpanes
owningTab?.let {
it.selectedProperty()?.onChange { if (it) onTabSelected() }
if (it.isSelected) onTabSelected()
}
}
private val rootParentChangeListener: ChangeListener<Parent> by lazy {
ChangeListener<Parent> { _, oldParent, newParent ->
if (modalStage != null) return@ChangeListener
if (newParent == null && oldParent != null && isDocked) callOnUndock()
if (newParent != null && newParent != oldParent && !isDocked) {
callOnDock()
// Call `onTabSelected` if/when we are connected to a Tab and it's selected
// Note that this only works for builder constructed tabpanes
owningTab?.let {
it.selectedProperty()?.onChange { if (it) onTabSelected() }
if (it.isSelected) onTabSelected()
}
}
return properties[key] as ChangeListener<Parent>
}
}

private val rootSceneChangeListener: ChangeListener<Scene>
get() {
val key = "tornadofx.rootSceneChangeListener"
if (properties[key] == null) {
properties[key] = ChangeListener<Scene> { _, oldParent, newParent ->
if (modalStage != null || root.parent != null) return@ChangeListener
if (newParent == null && oldParent != null && isDocked) callOnUndock()
if (newParent != null && newParent != oldParent && !isDocked) {
// Calls dock or undock when window opens or closes
newParent.windowProperty().onChangeOnce {
it?.showingProperty()?.addListener(rootSceneWindowShowingPropertyChangeListener)
}
callOnDock()
}
private val rootSceneChangeListener: ChangeListener<Scene> by lazy {
ChangeListener<Scene> { _, oldParent, newParent ->
val key = "tornadofx.rootSceneWindowPropertyChangeListener"

if (modalStage != null || root.parent != null) return@ChangeListener
if (newParent == null && oldParent != null && isDocked) {
(properties[key] as? ChangeListener<Window>)?.run {
oldParent.windowProperty().removeListener(this)
}
callOnUndock()
}
if (newParent != null && newParent != oldParent && !isDocked) {
// Calls dock or undock when window opens or closes
properties[key] = newParent.windowProperty().onChangeOnce {
it?.showingProperty()?.addListener(rootSceneWindowShowingPropertyChangeListener)
}
callOnDock()
}
return properties[key] as ChangeListener<Scene>
}
}

private val rootSceneWindowShowingPropertyChangeListener: ChangeListener<Boolean>
get() {
val key = "tornadofx.rootSceneWindowShowingPropertyChangeListener"
if (properties[key] == null) {
properties[key] = ChangeListener<Boolean> { property, oldValue, newValue ->
if (!isInitialized) {
property.removeListener(rootSceneWindowShowingPropertyChangeListener)
return@ChangeListener
}
if (!newValue && isDocked) callOnUndock()
if (newValue && !isDocked) callOnDock()
}
private val rootSceneWindowShowingPropertyChangeListener: ChangeListener<Boolean> by lazy {
ChangeListener<Boolean> { property, _, newValue ->
if (!isInitialized) {
property.removeListener(rootSceneWindowShowingPropertyChangeListener)
return@ChangeListener
}
return properties[key] as ChangeListener<Boolean>
if (!newValue && isDocked) callOnUndock()
if (newValue && !isDocked) callOnDock()
}
}

internal fun init() {
if (isInitialized) return
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/tornadofx/Lib.kt
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ inline fun <T> ChangeListener(crossinline listener: (observable: ObservableValue
* Listen for changes to this observable. Optionally only listen x times.
* The lambda receives the changed value when the change occurs, which may be null,
*/
fun <T> ObservableValue<T>.onChangeTimes(times: Int, op: (T?) -> Unit) {
fun <T> ObservableValue<T>.onChangeTimes(times: Int, op: (T?) -> Unit): ChangeListener<T> {
var counter = 0
val listener = object : ChangeListener<T> {
override fun changed(observable: ObservableValue<out T>?, oldValue: T, newValue: T) {
Expand All @@ -244,9 +244,10 @@ fun <T> ObservableValue<T>.onChangeTimes(times: Int, op: (T?) -> Unit) {
}
}
addListener(listener)
return listener
}

fun <T> ObservableValue<T>.onChangeOnce(op: (T?) -> Unit) = onChangeTimes(1, op)
fun <T> ObservableValue<T>.onChangeOnce(op: (T?) -> Unit): ChangeListener<T> = onChangeTimes(1, op)

fun <T> ObservableValue<T>.onChange(op: (T?) -> Unit) = apply { addListener { _, _, newValue -> op(newValue) } }
fun ObservableBooleanValue.onChange(op: (Boolean) -> Unit) = apply { addListener { _, _, new -> op(new ?: false) } }
Expand Down

0 comments on commit 2cae596

Please sign in to comment.