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.

71 lines
1.8 KiB

3 years ago
  1. package nova.monadic_sfx.ui
  2. import javafx.{scene => jfxc}
  3. import monix.bio.Task
  4. import monix.execution.Cancelable
  5. import monix.execution.cancelables.CompositeCancelable
  6. import monix.reactive.subjects.ConcurrentSubject
  7. import nova.monadic_sfx.implicits._
  8. import scalafx.beans.property.ObjectProperty
  9. import scalafx.scene.Node
  10. import scalafx.scene.Parent
  11. import scalafx.scene.control.TextField
  12. import scalafx.scene.text.Font
  13. import cats.effect.Resource
  14. final class FXComponent private (
  15. val rootNode: Parent,
  16. val cancelable: Cancelable
  17. )
  18. object FXComponent {
  19. def acquire(f: CompositeCancelable => Task[Parent]) =
  20. for {
  21. c <- Task(CompositeCancelable())
  22. p <- f(c)
  23. } yield new FXComponent(p, c)
  24. def fxComponent2Node(
  25. component: FXComponent
  26. )(implicit c: CompositeCancelable): Node = {
  27. c += component.cancelable
  28. component.rootNode
  29. }
  30. def resource(f: CompositeCancelable => Task[Parent]) =
  31. Resource.make(acquire(f))(comp => Task(comp.cancelable.cancel()))
  32. }
  33. object TestFXComp {
  34. val testComp =
  35. FXComponent.resource { implicit c =>
  36. Task.deferAction { implicit s =>
  37. Task {
  38. val sub = ConcurrentSubject.publish[jfxc.text.Font]
  39. val f = ObjectProperty(Font("hmm"))
  40. sub.onNext(f())
  41. new TextField {
  42. font <-- sub
  43. font <== f
  44. }
  45. }
  46. }
  47. }
  48. // val x = for {
  49. // comp <- testComp
  50. // res <- FXComponent.make { implicit c =>
  51. // Task.deferAction { implicit s =>
  52. // Task {
  53. // new BorderPane {
  54. // val sub = ConcurrentSubject.publish[jfxc.text.Font]
  55. // center = FXComponent.fxComponent2Node(comp)
  56. // bottom = new TextField {
  57. // font <-- sub
  58. // }
  59. // }
  60. // }
  61. // }
  62. // }
  63. // } yield res
  64. }