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

[#1318] Fix leaking fragments #1319

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 37 additions & 45 deletions src/main/java/tornadofx/Component.kt
Original file line number Diff line number Diff line change
Expand Up @@ -528,61 +528,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, oldValue, 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 { o, oldValue, newValue -> op(newValue) } }
fun ObservableBooleanValue.onChange(op: (Boolean) -> Unit) = apply { addListener { o, old, new -> op(new ?: false) } }
Expand Down