Fork me on GitHub
#cljfx
<
2023-05-18
>
Caio Cascaes23:05:52

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!

vlaaad07:05:44

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)

vlaaad07:05:45

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

vlaaad07:05:11

You will need a typehint for showAndWait

vlaaad07:05:00

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

👍 2
vlaaad07:05:45

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

vlaaad07:05:16

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

vlaaad07:05:53

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 Cascaes13:05:55

Many thanks master! I'll give this try!