All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.marid.fx.extensions.OtherExtensions.kt Maven / Gradle / Ivy

There is a newer version: 0.9.8.10
Show newest version
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) }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy