cljfx

Caio Cascaes 2023-05-18T23:56:52.570019Z

Hello guys! Do you have an example of how a app can show an alert, for confirming actions? I looked into the documentation, everything I have is this:

(defn alert [_]
  {:fx/type :alert
   :alert-type :warning
   :showing true
   :on-close-request (fn [^DialogEvent e]
                       (when (nil? (.getResult ^Dialog (.getSource e)))
                         (.consume e)))
   :on-hidden (fn [_]
                (reset! *state :choose-vault))
   :header-text "Nuke launch inevitable"
   :content-text "Please press Yes"
   :button-types [:yes]})
But how is it called/added into, say:
(s/defn login
  []
  {:fx/type   :v-box
   :padding   10
   :spacing   10
   :alignment :top-center
   :children  [{:fx/type :label
                :style   "-fx-font-size: 40px;"
                :text    "Login"}
               {:fx/type         :text-field
                :prompt-text     "E-mail"
                :disable         (get state/*state :login-waiting false)
                :max-width       200
                :on-text-changed #(swap! state/*state assoc :login-email %)
                :on-key-pressed  {:event/type ::press}}
               {:fx/type         :password-field
                :prompt-text     "Password"
                :disable         (get state/*state :login-waiting false)
                :max-width       200
                :on-text-changed #(swap! state/*state assoc :login-password %)
                :on-key-pressed  {:event/type ::press}}
               {:fx/type    :button
                :text       (if (get state/*state :login-waiting false) "Verifying..." "Submit")
                :pref-width 200
                :on-action  controllers.tower/authenticate!}]})
Thanks!

vlaaad 2023-05-19T07:40:44.084519Z

If you want it super simple, you can do it like that:

(= javafx.scene.control.ButtonType/YES
   (.get
     @(fx/on-fx-thread
        (.showAndWait
          (fx/instance
            (fx/create-component
              {:fx/type :alert
               :alert-type :information
               :content-text "Hello?"
               :button-types [:no :yes]}))))))
(^ you can try it in the REPL)

vlaaad 2023-05-19T07:41:45.277019Z

You don’t need fx/on-fx-thread if you run this code on fx thread (e.g. in a on-action callback)

vlaaad 2023-05-19T07:42:11.364269Z

You will need a typehint for showAndWait

vlaaad 2023-05-19T07:43:00.433209Z

this code create a dialog outside of the main app cljfx app lifecycle, and blocks until user responds, which is what you want with dialogs

👍 1
vlaaad 2023-05-19T07:45:45.753339Z

if you want the dialog to be a part of the whole app lifecycle, you can achieve this using fx/ext-many lifecycle by defining many windows in your app, e.g. one main window with your app and extra dialog windows that appear or disappear as needed. There is example here https://github.com/cljfx/cljfx/blob/master/examples/e21_extension_lifecycles.clj#L78-L103

vlaaad 2023-05-19T07:46:16.491049Z

or you can achieve this with fx/ext-let-refs

vlaaad 2023-05-19T07:46:53.907379Z

there is an example of a button with confirmation dialog and how it’s managed inside an app here https://github.com/cljfx/cljfx/blob/master/examples/e22_button_with_confirmation_dialog.clj

Caio Cascaes 2023-05-19T13:11:55.213079Z

Many thanks master! I'll give this try!