Skip to content

Instantly share code, notes, and snippets.

@vlaaad

vlaaad/e30.clj Secret

Last active June 20, 2019 23:18
Show Gist options
  • Save vlaaad/ec3793f55b654a8b1e4dc81b3cddc74b to your computer and use it in GitHub Desktop.
Save vlaaad/ec3793f55b654a8b1e4dc81b3cddc74b to your computer and use it in GitHub Desktop.
(ns e30
(:require [cljfx.api :as fx]
[cljfx.prop :as prop]
[cljfx.lifecycle :as lifecycle]
[cljfx.mutator :as mutator])
(:import [java.util UUID Collection]
[javafx.scene.control TabPane Tab]))
(def *state
(atom []))
(def ext-with-tabs
(fx/make-ext-with-props
{:tabs (prop/make (mutator/setter (fn [^TabPane pane ^Collection tabs]
(doto (.getTabs pane)
(.clear)
(.setAll tabs))))
lifecycle/dynamics)}))
(fx/mount-renderer
*state
(fx/create-renderer
:opts {:fx.opt/map-event-handler
(fn [e]
(case (:event/type e)
::add-tab (swap! *state conj (str (UUID/randomUUID)))
::set-tabs (reset! *state (mapv #(.getId ^Tab %) (:fx/event e)))))}
:middleware (fx/wrap-map-desc
(fn [tab-ids]
{:fx/type :stage
:width 960
:height 540
:showing true
:scene {:fx/type :scene
:root {:fx/type :v-box
:children [{:fx/type :button
:text "Add tab"
:on-action {:event/type ::add-tab}}
{:fx/type :label
:text (str (mapv #(subs % 0 3) tab-ids))}
{:fx/type ext-with-tabs
:props {:tabs (for [id tab-ids]
{:fx/type :tab
:fx/key id
:id id
:text (subs id 0 3)})}
:desc {:fx/type :tab-pane
:style {:-fx-open-tab-animation "NONE"
:-fx-close-tab-animation "NONE"}
:tab-closing-policy :all-tabs
:tab-drag-policy :reorder
:on-tabs-changed {:event/type ::set-tabs}}}]}}}))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment