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

Perf/2.1.0 #33

Merged
merged 2 commits into from
Aug 12, 2024
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
<scm>
<connection>scm:git:git://github.com/saalfeldlab/saalfx</connection>
<developerConnection>scm:git:[email protected]:saalfeldlab/saalfx.git</developerConnection>
<tag>saalfx-1.3.0</tag>
<tag>saalfx-2.0.0</tag>
<url>https://github.com/saalfeldlab/saalfx</url>
</scm>

Expand Down
13 changes: 5 additions & 8 deletions src/main/kotlin/org/janelia/saalfeldlab/fx/actions/Action.kt
Original file line number Diff line number Diff line change
Expand Up @@ -192,18 +192,15 @@ open class Action<E : Event>(val eventType: EventType<E>) {

private fun testChecks(event: E?): Boolean {
return checks.isEmpty() || let {
var valid = true
for ((description, check) in checks) {
valid = valid && check(event)
if (!valid) {
val msg = description?.let {
"$it (${check::class.java})"
} ?: "(${check::class.java})"
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
}
}

Expand Down
41 changes: 16 additions & 25 deletions src/main/kotlin/org/janelia/saalfeldlab/fx/actions/ActionSet.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
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
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


Expand Down Expand Up @@ -64,21 +61,19 @@ open class ActionSet(val name: String, var keyTracker: () -> KeyTracker? = { nul
apply?.let { it(this) }
}

private fun testChecksForEventType(event: Event, eventType: EventType<out Event> = 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<out Event>.testChecksForEventType(event: Event, eventType: EventType<out Event> = 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<out Event>? = event.eventType): Boolean {
private tailrec fun Action<out Event>.testChecksForInheritedEventTypes(event: Event, eventType: EventType<out Event>? = event.eventType): Boolean {
if (eventType == null) return true
return if (!testChecksForEventType(event, eventType)) false else testChecksForInheritedEventTypes(event, eventType.superType)
}
Expand Down Expand Up @@ -194,7 +189,7 @@ open class ActionSet(val name: String, var keyTracker: () -> KeyTracker? = { nul

/* configure via the callback*/
withAction()
}.also { addAction(it)}
}.also { addAction(it) }
}

/**
Expand Down Expand Up @@ -300,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) }
}

/**
Expand Down Expand Up @@ -391,9 +386,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 {
Expand All @@ -410,7 +403,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 <E : Event> preInvokeCheck(action: Action<E>, event: E) = action.canHandleEvent(event.eventType) && testChecksForInheritedEventTypes(event)
protected open fun <E : Event> preInvokeCheck(action: Action<E>, event: E) = action.run {
canHandleEvent(event.eventType) && testChecksForInheritedEventTypes(event)
}

/**
* Optional callback for before the extension functions [installActionSet] are called.
Expand Down Expand Up @@ -441,10 +436,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]
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
*/
package org.janelia.saalfeldlab.fx.util

import javafx.application.Platform
import kotlinx.coroutines.*
import java.lang.Runnable

Expand All @@ -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 {
Expand Down
Loading