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.

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