Guest User

Untitled

a guest
Jan 8th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. (ns getting-started.reactlike
  2. (:require [fn-fx.fx-dom :as dom]
  3. [fn-fx.diff :refer [component defui render should-update?]]
  4. [fn-fx.controls :as ui]))
  5.  
  6. (def firebrick
  7. (ui/color :red 0.69 :green 0.13 :blue 0.13))
  8.  
  9. ;; The main login window component, notice the authed? parameter, this defines a function
  10. ;; we can use to construct these ui components, named "login-form"
  11. (defui LoginWindow
  12. (render [this {:keys [authed? label]}]
  13. (ui/grid-pane
  14. :alignment :center
  15. :hgap 10
  16. :vgap 10
  17. :padding (ui/insets
  18. :bottom 25
  19. :left 25
  20. :right 25
  21. :top 25)
  22. :children [(ui/text
  23. :text "Welcome"
  24. :font (ui/font
  25. :family "Tahoma"
  26. :weight :normal
  27. :size 20)
  28. :grid-pane/column-index 0
  29. :grid-pane/row-index 0
  30. :grid-pane/column-span 2
  31. :grid-pane/row-span 1)
  32.  
  33. (ui/label
  34. :text "User:"
  35. :grid-pane/column-index 0
  36. :grid-pane/row-index 1)
  37.  
  38. (ui/text-field
  39. :id :user-name-field
  40. :grid-pane/column-index 1
  41. :grid-pane/row-index 1)
  42.  
  43. (ui/label :text "Password:"
  44. :grid-pane/column-index 0
  45. :grid-pane/row-index 2)
  46.  
  47. (ui/password-field
  48. :id :password-field
  49. :grid-pane/column-index 1
  50. :grid-pane/row-index 2)
  51.  
  52. (ui/h-box
  53. :spacing 10
  54. :alignment :bottom-right
  55. :children [(ui/button :text "Sign in"
  56. :on-action {:event :auth
  57. :fn-fx/include {:user-name-field #{:text}
  58. :password-field #{:text}}})]
  59. :grid-pane/column-index 1
  60. :grid-pane/row-index 4)
  61.  
  62. (ui/text
  63. :text (if authed? "Sign in was pressed" "")
  64. :fill firebrick
  65. :grid-pane/column-index 1
  66. :grid-pane/row-index 6)
  67. (ui/text
  68. :text (or label "No label present!")
  69. :fill firebrick
  70. :grid-pane/column-index 1
  71. :grid-pane/row-index 7)])))
  72.  
  73. ;; Wrap our login form in a stage/scene, and create a "stage" function
  74. (defui Stage
  75. (render [this args]
  76. (ui/stage
  77. :title "JavaFX Welcome"
  78. :shown true
  79. :scene (ui/scene
  80. :root (login-window args)))))
  81.  
  82.  
  83. (def app-state (atom nil))
  84. (defn reset-state! []
  85. (do (reset! app-state {:authed? false})
  86. app-state))
  87.  
  88. (defn set-label! [x]
  89. (swap! app-state assoc :label x))
  90.  
  91.  
  92. (defn -main []
  93. (let [;; Data State holds the business logic of our app
  94. ;; now it points to the global app-state....
  95. data-state (reset-state!)
  96.  
  97. ;; handler-fn handles events from the ui and updates the data state
  98. handler-fn (fn [{:keys [event] :as all-data}]
  99. (println "UI Event" event all-data)
  100. (case event
  101. :auth (swap! data-state assoc :authed? true)
  102. (println "Unknown UI event" event all-data)))
  103.  
  104. ;; ui-state holds the most recent state of the ui
  105. ui-state (agent (dom/app (stage @data-state) handler-fn))]
  106.  
  107. ;; Every time the data-state changes, queue up an update of the UI
  108. ;; Transitively, we can push changes to the DOM by munging with
  109. ;; the global app state now.
  110. (add-watch data-state :ui (fn [_ _ _ _]
  111. (send ui-state
  112. (fn [old-ui]
  113. (dom/update-app old-ui (stage @data-state))))))))
  114.  
  115. (comment
  116. (-main)
  117. ;;Let's see what happens when we change the time....
  118. (def res (future
  119. (while true
  120. (do (set-label! (str (System/currentTimeMillis) " ms"))
  121. (Thread/sleep 16)))))
  122. ;;we're now propogating changes to our model just modifying the
  123. ;;atom from another thread. You can do this a number of ways,
  124. ;;uncluding core.async. I used futures...
  125. ;;to stop the counter (manually), cancel the future.
  126.  
  127. ;;we'd probably use channels or something else for this.
  128. #_(future-cancel res))
Add Comment
Please, Sign In to add comment