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.

163 lines
4.6 KiB

  1. package nova.monadic_sfx
  2. import com.softwaremill.macwire._
  3. import io.odin.Logger
  4. import monix.bio.Task
  5. import nova.monadic_sfx.executors.Schedulers
  6. import nova.monadic_sfx.implicits.JFXButton
  7. import nova.monadic_sfx.implicits.JavaFXMonixObservables._
  8. import nova.monadic_sfx.ui.MyFxApp
  9. import nova.monadic_sfx.ui.components.todo.TodoListView
  10. import org.gerweck.scalafx.util._
  11. import scalafx.Includes._
  12. import scalafx.application.JFXApp.PrimaryStage
  13. import scalafx.beans.property.ObjectProperty
  14. import scalafx.beans.property.StringProperty
  15. import scalafx.collections.ObservableBuffer
  16. import scalafx.geometry.Insets
  17. import scalafx.scene.Scene
  18. import scalafx.scene.control.TableColumn
  19. import scalafx.scene.control.TableView
  20. import scalafx.scene.layout.HBox
  21. import scalafx.scene.paint.Color
  22. import scalafx.scene.shape.Rectangle
  23. import nova.monadic_sfx.ui.components.todo.TodoListStore
  24. class MainApp(
  25. // spawnProtocol: ActorSystem[SpawnProtocol.Command],
  26. schedulers: Schedulers,
  27. startTime: Long
  28. )(implicit logger: Logger[Task]) {
  29. lazy val addTodoButton = new JFXButton {
  30. text = "Add"
  31. }
  32. lazy val addTodoObs = addTodoButton.observableAction()
  33. // lazy val todoListView = TodoListView.defaultListView
  34. lazy val _scene = new Scene {
  35. root = new HBox {
  36. padding = Insets(20)
  37. content = new Rectangle {
  38. width = 400
  39. height = 200
  40. fill = Color.DeepSkyBlue
  41. }
  42. children ++= Seq(
  43. new JFXButton {
  44. text = "DummyButton"
  45. },
  46. new JFXButton {
  47. text = "DummyButton2"
  48. },
  49. addTodoButton,
  50. Test.ttv
  51. // todoListView
  52. )
  53. }
  54. }
  55. private lazy val stage = new PrimaryStage {
  56. title = "Simple ScalaFX App"
  57. scene = _scene
  58. width = 800
  59. height = 400
  60. }
  61. // implicit val l = logger
  62. // implicit val sp = spawnProtocol
  63. val program = for {
  64. (fxApp, fxAppFib) <- wire[MyFxApp].init(stage)
  65. // _ <- Task(fxApp.stage = stage)
  66. // .executeOn(schedulers.fx)
  67. // .delayExecution(2000.millis)
  68. // todoComponent <- createTodoComponent
  69. // _ <- toIO(
  70. // addTodoObs
  71. // .mapEval(_ =>
  72. // toTask(todoComponent.send(TodoListComponent.Add(Todo(1, "blah"))))
  73. // )
  74. // .completedL
  75. // .executeOn(schedulers.fx)
  76. // .startAndForget
  77. // )
  78. _ <- createTodoComponent.executeOn(schedulers.fx)
  79. _ <- logger.info(
  80. s"Application started in ${(System.currentTimeMillis() - startTime) / 1000f} seconds"
  81. )
  82. _ <- fxAppFib.join
  83. } yield ()
  84. // def createTodoComponent: Task[TodoListComponent] = {
  85. // for {
  86. // channel <-
  87. // ConcurrentChannel
  88. // .of[Task, TodoListComponent.Complete, TodoListComponent.Command]
  89. // scheduler = schedulers.fx
  90. // lv <- TodoListView.defaultListView2.executeOn(scheduler)
  91. // // todoLV = new TodoListView(lv)
  92. // todoComponent <- wire[TodoListComponent.Props].create
  93. // // TODO make this a "message pass" instead of mutating directly
  94. // _ <- Task(_scene.getChildren += lv).executeOn(scheduler)
  95. // // _ <- toIO(
  96. // // delObs
  97. // // .doOnNext(_ => toTask(logger.debug("Pressed delete")))
  98. // // .doOnNext(todo =>
  99. // // toTask(
  100. // // for {
  101. // // _ <- logger.debug(s"Got todo $todo")
  102. // // _ <- todoComponent.send(TodoListComponent.Delete(todo.id))
  103. // // // _ <- Task.sequence(
  104. // // // lst.map(todo =>
  105. // // // todoComponent.send(TodoListComponent.Delete(todo.id))
  106. // // // )
  107. // // // )
  108. // // } yield ()
  109. // // )
  110. // // )
  111. // // .completedL
  112. // // ).startAndForget
  113. // // _ <- toIO(
  114. // // editObs
  115. // // .doOnNext(_ => toTask(logger.debug("Pressed edit")))
  116. // // .completedL
  117. // // ).startAndForget
  118. // } yield todoComponent
  119. // }
  120. def createTodoComponent: Task[Unit] =
  121. for {
  122. store <- TodoListStore(logger)
  123. lv <- TodoListView(store)
  124. _ <- Task(_scene.getChildren += lv)
  125. } yield ()
  126. }
  127. class TestModel(_name: String, _age: Int) {
  128. val name = StringProperty(_name).readOnly
  129. val age = ObjectProperty(_age).readOnly
  130. }
  131. object Test {
  132. val items = ObservableBuffer(
  133. new TestModel("hmm", 1),
  134. new TestModel("hmm2", 2)
  135. )
  136. val ttv = new TableView[TestModel](items) {
  137. columns ++= Seq(
  138. new TableColumn[TestModel, String] {
  139. text = "Name"
  140. cellValueFactory = { _.value.name }
  141. },
  142. new TableColumn[TestModel, Int] {
  143. text = "Age"
  144. cellValueFactory = { _.value.age }
  145. }
  146. )
  147. }
  148. }