Skip to content

Instantly share code, notes, and snippets.

@mhuebert
Forked from jvtrigueros/example.clj
Last active May 16, 2022 15:16
Show Gist options
  • Save mhuebert/d28cf5fd0536f9effadd86b8a94813c0 to your computer and use it in GitHub Desktop.
Save mhuebert/d28cf5fd0536f9effadd86b8a94813c0 to your computer and use it in GitHub Desktop.
Choosing a directory and setting setting a text label
(ns example
(:require
[cljfx.api :as fx])
(:import
(javafx.stage DirectoryChooser)))
(def *state (atom {:directory nil}))
(defmulti event-handler :event/type)
(defmethod event-handler ::select-directory [{:keys [fx/event]}]
(let [window (.getWindow (.getScene (.getTarget event)))
chooser (doto (DirectoryChooser.)
(.setTitle "Open Directory"))]
(when-let [file (.showDialog chooser window)]
{:state {:directory file}})))
(defn directory-input
[{:keys [text]}]
{:fx/type :h-box
:children [{:fx/type :text-field
:text text
:style {:-fx-background-radius "0px"}}
{:fx/type :button
:text "..."
:on-action {:event/type ::select-directory}
:style {:-fx-background-radius "0px"}}]})
(defn root-view
[{:keys [directory] :as state}]
{:fx/type :stage
:showing true
:scene {:fx/type :scene
:root {:fx/type directory-input
:text (str directory)}}})
(def renderer
(fx/create-renderer
:middleware (fx/wrap-map-desc #'root-view)
:opts {:fx.opt/map-event-handler
(-> event-handler
(fx/wrap-co-effects {:state (fx/make-deref-co-effect *state)})
(fx/wrap-effects {:state (fx/make-reset-effect *state)
:dispatch fx/dispatch-effect}))}))
(comment
;; Render the application using the current *state
(fx/mount-renderer *state renderer)
(renderer)
;; Inspect state
(str (:directory @*state));; => "/home/jvtrigueros/workspace"
,)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment