org.marid.fx.extensions.OtherExtensions.kt Maven / Gradle / Ivy
package org.marid.fx.extensions
import javafx.application.Platform
import java.io.InputStreamReader
import java.nio.charset.StandardCharsets
import java.util.*
import java.util.concurrent.CompletableFuture
typealias ProgressTracker = (progress: Double) -> Unit
fun Properties.loadFromResource(resource: String): Properties {
val loader = Thread.currentThread().contextClassLoader
InputStreamReader(
loader.getResourceAsStream(resource)!!,
StandardCharsets.UTF_8
).use {
load(it)
}
return this
}
fun T.callFx(f: (T) -> R): R =
if (Platform.isFxApplicationThread()) {
f(this)
} else {
val future = CompletableFuture()
Platform.runLater {
try {
future.complete(f(this))
} catch (e: Throwable) {
future.completeExceptionally(e)
}
}
future.get()
}
fun T.runFx(f: (T) -> Unit): Unit {
if (Platform.isFxApplicationThread()) {
f(this)
} else {
Platform.runLater { f(this) }
}
}