From 8ec745d0e1105500e8225206eb2ff21a3b15b4dd Mon Sep 17 00:00:00 2001 From: Caleb Hulbert Date: Wed, 7 Aug 2024 16:25:39 -0400 Subject: [PATCH 1/2] refactor: remove ACTION and FILTER Logger in favor of action loggers --- pom.xml | 2 +- .../janelia/saalfeldlab/fx/actions/Action.kt | 5 ++- .../saalfeldlab/fx/actions/ActionSet.kt | 34 ++++++++----------- .../util/InvokeOnJavaFXApplicationThread.kt | 6 +--- 4 files changed, 18 insertions(+), 29 deletions(-) diff --git a/pom.xml b/pom.xml index ce7a5b2..61ef960 100644 --- a/pom.xml +++ b/pom.xml @@ -117,7 +117,7 @@ scm:git:git://github.com/saalfeldlab/saalfx scm:git:git@github.com:saalfeldlab/saalfx.git - saalfx-1.3.0 + saalfx-2.0.0 https://github.com/saalfeldlab/saalfx diff --git a/src/main/kotlin/org/janelia/saalfeldlab/fx/actions/Action.kt b/src/main/kotlin/org/janelia/saalfeldlab/fx/actions/Action.kt index 73ac209..7b9d929 100644 --- a/src/main/kotlin/org/janelia/saalfeldlab/fx/actions/Action.kt +++ b/src/main/kotlin/org/janelia/saalfeldlab/fx/actions/Action.kt @@ -196,9 +196,8 @@ open class Action(val eventType: EventType) { for ((description, check) in checks) { valid = valid && check(event) if (!valid) { - val msg = description?.let { - "$it (${check::class.java})" - } ?: "(${check::class.java})" + val startMsg = description?.let { "$it " } ?: "" + val msg ="$startMsg(${check::class.java})" logger.trace { "Check: $msg did not pass" } break } diff --git a/src/main/kotlin/org/janelia/saalfeldlab/fx/actions/ActionSet.kt b/src/main/kotlin/org/janelia/saalfeldlab/fx/actions/ActionSet.kt index 1e713ed..bd25818 100644 --- a/src/main/kotlin/org/janelia/saalfeldlab/fx/actions/ActionSet.kt +++ b/src/main/kotlin/org/janelia/saalfeldlab/fx/actions/ActionSet.kt @@ -64,21 +64,19 @@ open class ActionSet(val name: String, var keyTracker: () -> KeyTracker? = { nul apply?.let { it(this) } } - private fun testChecksForEventType(event: Event, eventType: EventType = event.eventType): Boolean { - return checks[eventType]?.let { checks -> - var pass = true - for ((reason, check) in checks) { - if (!check(event)) { - ACTION_SET_LOGGER.debug { "Verify All Failed: $reason" } - pass = false - break - } + private fun Action.testChecksForEventType(event: Event, eventType: EventType = event.eventType): Boolean { + val checks = checks[eventType] ?: return true + + for ((reason, check) in checks) { + if (!check(event)) { + logger.debug { "Verify All Failed: $reason" } + return false } - pass - } ?: true + } + return true } - private tailrec fun testChecksForInheritedEventTypes(event: Event, eventType: EventType? = event.eventType): Boolean { + private tailrec fun Action.testChecksForInheritedEventTypes(event: Event, eventType: EventType? = event.eventType): Boolean { if (eventType == null) return true return if (!testChecksForEventType(event, eventType)) false else testChecksForInheritedEventTypes(event, eventType.superType) } @@ -391,9 +389,7 @@ open class ActionSet(val name: String, var keyTracker: () -> KeyTracker? = { nul try { action(event) } catch (e: Exception) { - val logger = if (action.filter) FILTER_LOGGER else ACTION_LOGGER - val nameText = action.name?.let { "$name: $it" } ?: name - logger.error(e) { "$nameText (${event.eventType} was valid, but failed" } + action.logger.error(e) {"${event.eventType} was valid, but failed" } throw e } } else { @@ -410,7 +406,9 @@ open class ActionSet(val name: String, var keyTracker: () -> KeyTracker? = { nul * @param action the [Action] to handle [event] * @param event to handle */ - protected open fun preInvokeCheck(action: Action, event: E) = action.canHandleEvent(event.eventType) && testChecksForInheritedEventTypes(event) + protected open fun preInvokeCheck(action: Action, event: E) = action.run { + canHandleEvent(event.eventType) && testChecksForInheritedEventTypes(event) + } /** * Optional callback for before the extension functions [installActionSet] are called. @@ -441,10 +439,6 @@ open class ActionSet(val name: String, var keyTracker: () -> KeyTracker? = { nul companion object { - private val ACTION_SET_LOGGER: KLogger = KotlinLogging.logger {} - private val ACTION_LOGGER: KLogger = KotlinLogging.logger("${MethodHandles.lookup().lookupClass().name}-Action") - private val FILTER_LOGGER: KLogger = KotlinLogging.logger("${MethodHandles.lookup().lookupClass().name}-Filter") - /** * Install [actionSet] in the receiver [Node] * diff --git a/src/main/kotlin/org/janelia/saalfeldlab/fx/util/InvokeOnJavaFXApplicationThread.kt b/src/main/kotlin/org/janelia/saalfeldlab/fx/util/InvokeOnJavaFXApplicationThread.kt index 8eb93fe..e72d3b7 100644 --- a/src/main/kotlin/org/janelia/saalfeldlab/fx/util/InvokeOnJavaFXApplicationThread.kt +++ b/src/main/kotlin/org/janelia/saalfeldlab/fx/util/InvokeOnJavaFXApplicationThread.kt @@ -28,7 +28,6 @@ */ package org.janelia.saalfeldlab.fx.util -import javafx.application.Platform import kotlinx.coroutines.* import java.lang.Runnable @@ -42,10 +41,7 @@ class InvokeOnJavaFXApplicationThread { operator fun invoke(task: suspend CoroutineScope.() -> Unit) = sharedMainScope.launch(block = task) @JvmStatic - operator fun invoke(task: Runnable) { - if (Platform.isFxApplicationThread()) task.run() - else invoke { task.run() } - } + operator fun invoke(task: Runnable) = invoke { task.run() } @Throws(InterruptedException::class) fun invokeAndWait(task: suspend CoroutineScope.() -> Unit) = runBlocking { From 2b7a2c4e544cdc29dc270c350423555f7973a1a0 Mon Sep 17 00:00:00 2001 From: Caleb Hulbert Date: Wed, 7 Aug 2024 22:07:37 -0400 Subject: [PATCH 2/2] refactor: some boolean logic --- .../kotlin/org/janelia/saalfeldlab/fx/actions/Action.kt | 8 +++----- .../org/janelia/saalfeldlab/fx/actions/ActionSet.kt | 9 +++------ 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/org/janelia/saalfeldlab/fx/actions/Action.kt b/src/main/kotlin/org/janelia/saalfeldlab/fx/actions/Action.kt index 7b9d929..06609f1 100644 --- a/src/main/kotlin/org/janelia/saalfeldlab/fx/actions/Action.kt +++ b/src/main/kotlin/org/janelia/saalfeldlab/fx/actions/Action.kt @@ -192,17 +192,15 @@ open class Action(val eventType: EventType) { private fun testChecks(event: E?): Boolean { return checks.isEmpty() || let { - var valid = true for ((description, check) in checks) { - valid = valid && check(event) - if (!valid) { + if (!check(event)) { val startMsg = description?.let { "$it " } ?: "" val msg ="$startMsg(${check::class.java})" logger.trace { "Check: $msg did not pass" } - break + return false } } - valid + true } } diff --git a/src/main/kotlin/org/janelia/saalfeldlab/fx/actions/ActionSet.kt b/src/main/kotlin/org/janelia/saalfeldlab/fx/actions/ActionSet.kt index bd25818..c5e5ec5 100644 --- a/src/main/kotlin/org/janelia/saalfeldlab/fx/actions/ActionSet.kt +++ b/src/main/kotlin/org/janelia/saalfeldlab/fx/actions/ActionSet.kt @@ -1,7 +1,5 @@ package org.janelia.saalfeldlab.fx.actions -import io.github.oshai.kotlinlogging.KLogger -import io.github.oshai.kotlinlogging.KotlinLogging import javafx.event.Event import javafx.event.EventHandler import javafx.event.EventType @@ -9,7 +7,6 @@ import javafx.scene.Node import javafx.scene.input.* import javafx.stage.Window import org.janelia.saalfeldlab.fx.event.KeyTracker -import java.lang.invoke.MethodHandles import java.util.function.Consumer @@ -192,7 +189,7 @@ open class ActionSet(val name: String, var keyTracker: () -> KeyTracker? = { nul /* configure via the callback*/ withAction() - }.also { addAction(it)} + }.also { addAction(it) } } /** @@ -298,7 +295,7 @@ open class ActionSet(val name: String, var keyTracker: () -> KeyTracker? = { nul /* configure based on the callback */ withAction() - }.also { addAction(it)} + }.also { addAction(it) } } /** @@ -389,7 +386,7 @@ open class ActionSet(val name: String, var keyTracker: () -> KeyTracker? = { nul try { action(event) } catch (e: Exception) { - action.logger.error(e) {"${event.eventType} was valid, but failed" } + action.logger.error(e) { "${event.eventType} was valid, but failed" } throw e } } else {