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.

91 lines
2.7 KiB

  1. package nova.monadic_sfx.ui.components.todo
  2. import monix.bio.Task
  3. import nova.monadic_sfx.implicits.FontIcon
  4. import nova.monadic_sfx.implicits.IconLiteral
  5. import nova.monadic_sfx.implicits.JFXListView
  6. import nova.monadic_sfx.implicits.JavaFXMonixObservables._
  7. import scalafx.Includes._
  8. import scalafx.beans.property.StringProperty
  9. import scalafx.scene.control.ContextMenu
  10. import scalafx.scene.control.ListCell
  11. import scalafx.scene.control.SelectionMode
  12. import scalafx.scene.layout.HBox
  13. import scalafx.scene.text.Text
  14. import nova.monadic_sfx.util.reactive._
  15. import org.gerweck.scalafx.util._
  16. import scalafx.beans.property.ObjectProperty
  17. import nova.monadic_sfx.implicits.MenuItem
  18. import monix.execution.cancelables.CompositeCancelable
  19. object TodoListView {
  20. def apply(
  21. store: MonixProSubject[
  22. TodoListStore.Command,
  23. (TodoListStore.Command, TodoListStore.State)
  24. ]
  25. ): Task[JFXListView[Todo]] =
  26. Task.deferAction(implicit s =>
  27. Task {
  28. val cc = CompositeCancelable()
  29. val todos =
  30. store.map { case (_, state) => state.todos }
  31. new JFXListView[Todo] {
  32. def selectedItems = selectionModel().selectedItems.view
  33. cc += items <-- todos
  34. val emptyCell = ObjectProperty(new HBox)
  35. cellFactory = _ =>
  36. new ListCell[Todo] {
  37. val _text = StringProperty("")
  38. val _graphic = ObjectProperty(
  39. new HBox {
  40. children = Seq(
  41. new FontIcon {
  42. iconSize = 10
  43. iconLiteral = IconLiteral.Gmi10k
  44. },
  45. new Text {
  46. text <== _text
  47. }
  48. )
  49. }
  50. )
  51. item.asOption.map(
  52. _.fold("")(todo => s"${todo.id} - ${todo.content}")
  53. ) --> _text
  54. graphic <== item.asOption.flatMap(
  55. _.fold(emptyCell)(_ => _graphic)
  56. )
  57. }
  58. selectionModel().selectionMode = SelectionMode.Multiple
  59. contextMenu = new ContextMenu {
  60. items ++= Seq(
  61. new MenuItem {
  62. text = "Add"
  63. obsAction.useLazy(TodoListStore.Add("blah3")) --> store
  64. },
  65. new MenuItem {
  66. text = "Delete"
  67. obsAction.useIterable(_ =>
  68. selectedItems
  69. .map(todo => TodoListStore.Delete(todo.id))
  70. .toList
  71. ) --> store
  72. },
  73. new MenuItem {
  74. text = "Edit"
  75. }
  76. )
  77. }
  78. }
  79. }
  80. )
  81. }