Skip to content

Instantly share code, notes, and snippets.

@vlaaad
Created February 25, 2019 23:10
Show Gist options
  • Save vlaaad/dc087f97dfd708f7cf6c02f9f8ac8a56 to your computer and use it in GitHub Desktop.
Save vlaaad/dc087f97dfd708f7cf6c02f9f8ac8a56 to your computer and use it in GitHub Desktop.
fn-fx dragging example
{:deps {;; does not work, needs JDK 8 with JavaFX
;; halgari/fn-fx {:mvn/version "0.4.0"}
;; works, needs JDK 11
fn-fx/fn-fx-openjfx11 {:mvn/version "0.5.0-SNAPSHOT"}}
:paths ["."]
:aliases {:wayland {:jvm-opts ["-Djdk.gtk.version=2"]}}}
(ns dragging-example
(:require [fn-fx.controls :as ui]
[fn-fx.diff :as fx]
[fn-fx.fx-dom :as dom]))
(def *state
(atom [{:x 100 :y 100}
{:x 200 :y 100}]))
(defmulti event-handler :event/type)
(defmethod event-handler ::mouse-dragged [{:keys [index fn-fx/includes]}]
(let [{:keys [scene-x scene-y]} (:fn-fx/event includes)]
(swap! *state update index assoc :x scene-x :y scene-y)))
(defmethod event-handler :default [e]
(prn e))
(fx/defui PointView
(render [this {:keys [point index]}]
(ui/circle
:layout-x (:x point)
:layout-y (:y point)
:center-x 0
:center-y 0
:radius 20
:on-mouse-dragged {:event/type ::mouse-dragged
:fn-fx/include {:fn-fx/event #{:scene-x :scene-y}}
:index index})))
(fx/defui RootView
(render [this points]
(ui/stage
:width 300
:height 200
:shown true
:scene (ui/scene
:root (ui/group
:children (map-indexed
(fn [index point]
(point-view {:index index :point point}))
points))))))
(def *app
(agent (dom/app (root-view @*state) event-handler)))
(add-watch *state :ui (fn [_ _ _ new-state]
(send *app dom/update-app (root-view new-state))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment