You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.7 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package nova.monadic_sfx.ui
  2. import scala.concurrent.duration._
  3. import cats.effect.Resource
  4. import io.odin.Logger
  5. import monix.bio.Fiber
  6. import monix.bio.Task
  7. import monix.execution.CancelablePromise
  8. import nova.monadic_sfx.executors.Schedulers
  9. import nova.monadic_sfx.ui.DefaultUI
  10. import scalafx.application.JFXApp3
  11. import scalafx.application.JFXApp3.PrimaryStage
  12. class MyFxApp(val schedulers: Schedulers)(implicit logger: Logger[Task]) {
  13. private def internal(
  14. startSignal: CancelablePromise[Unit],
  15. stopSignal: CancelablePromise[Unit]
  16. ) =
  17. new JFXApp3 {
  18. def start(): Unit = {
  19. stage = new PrimaryStage {
  20. scene = DefaultUI.scene
  21. }
  22. startSignal.success(())
  23. }
  24. override def stopApp(): Unit = {
  25. stopSignal.success(())
  26. }
  27. }
  28. }
  29. object MyFxApp {
  30. def resource(
  31. schedulers: Schedulers,
  32. stage: => PrimaryStage,
  33. transitionDelay: FiniteDuration = 500.millis
  34. )(implicit
  35. logger: Logger[Task]
  36. ): Resource[Task, (Task[Unit], Fiber[Throwable, Unit])] =
  37. Resource.make(for {
  38. _ <- logger.info("Starting FX App")
  39. fxApp <- Task(new MyFxApp(schedulers))
  40. startSignal <- Task(CancelablePromise[Unit]())
  41. stopSignal <- Task(CancelablePromise[Unit]())
  42. delegate <- Task(fxApp.internal(startSignal, stopSignal))
  43. fib <-
  44. Task(delegate.main(Array.empty)).start.executeOn(schedulers.blocking)
  45. _ <- Task.fromCancelablePromise(startSignal)
  46. _ <- Task.sleep(transitionDelay)
  47. _ <- Task(delegate.stage = stage)
  48. .executeOn(schedulers.fx)
  49. .delayExecution(transitionDelay)
  50. } yield Task.fromCancelablePromise(stopSignal) -> fib) {
  51. case _ -> fib => fib.cancel
  52. }
  53. }