Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport https://github.com/edvin/tornadofx/pull/1319 to Tornado2 #40

Merged
merged 1 commit into from
Aug 5, 2021
Merged
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 @@ -542,61 +542,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()
}
}
internal 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 { _, _, newValue -> op(newValue) } }
fun ObservableBooleanValue.onChange(op: (Boolean) -> Unit) = apply { addListener { _, _, new -> op(new ?: false) } }
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/tornadofx/Workspace.kt
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ open class Workspace(title: String = "Workspace", navigationMode: NavigationMode
dockedComponent?.also {
dockedComponentProperty.value = null
if (oldMode == Stack && newMode == Tabs) {
val listener = it.properties["tornadofx.rootParentChangeListener"] as ChangeListener<Parent>
val listener = it.rootParentChangeListener
it.root.parentProperty().removeListener(listener)
it.callOnUndock()
dock(it, true)
Expand Down