forked from cljfx/cljfx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe27_selection_models.clj
317 lines (288 loc) · 13.3 KB
/
e27_selection_models.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
(ns e27-selection-models
(:require [cljfx.api :as fx]
[cljfx.ext.list-view :as fx.ext.list-view]
[cljfx.ext.tab-pane :as fx.ext.tab-pane]
[cljfx.ext.table-view :as fx.ext.table-view]
[cljfx.ext.tree-view :as fx.ext.tree-view])
(:import [javafx.scene.control Tab TreeItem]))
(set! *warn-on-reflection* true)
(defn init-state []
{:selection ["/etc"
"/users/vlaaad/.clojure"]
:tree {"dev" {}
"etc" {}
"users" {"vlaaad" {".clojure" {"deps.edn" {}}}}
"usr" {"bin" {}
"lib" {}}}})
(declare *state renderer)
; clean up renderer on file reload
(when (and (.hasRoot #'*state)
(.hasRoot #'renderer))
(fx/unmount-renderer *state renderer)
(reset! *state (init-state)))
(defonce *state
(atom (init-state)))
(defn list-view [{:keys [items selection selection-mode]}]
{:fx/type fx.ext.list-view/with-selection-props
:props (case selection-mode
:multiple {:selection-mode :multiple
:selected-items selection
:on-selected-items-changed {:event/type ::select-multiple}}
:single (cond-> {:selection-mode :single
:on-selected-item-changed {:event/type ::select-single}}
(seq selection)
(assoc :selected-item (-> selection sort first))))
:desc {:fx/type :list-view
:cell-factory {:fx/cell-type :list-cell
:describe (fn [path]
{:text path})}
:items items}})
(defn- let-refs [refs desc]
{:fx/type fx/ext-let-refs
:refs refs
:desc desc})
(defn- get-ref [ref]
{:fx/type fx/ext-get-ref
:ref ref})
(defn- with-tab-selection-props [props desc]
{:fx/type fx.ext.tab-pane/with-selection-props
:props props
:desc desc})
; Programatically setting tabs is somewhat buggy in JavaFX, so for
; demonstration purposes :selection-capabilities controls the selection model:
; - #{:read} the tab changes just based on the current state
; - #{:write} the tab changes just based the user selection (eg. clicking)
; - #{:read :write} both of the above (quite buggy, multiple tabs seem selected
; and the content of tabs blend together)
(defn tab-pane [{:keys [items selection selection-capabilities]}]
{:pre [(set? selection-capabilities)]}
(let [selected-tab-id (-> selection sort first)
_ (assert selected-tab-id)]
(let-refs (into {}
(map (fn [item]
{:pre [(string? item)]}
[item
(merge
{:fx/type :tab
:graphic {:fx/type :label
:text item}
:id item
:closable false}
(cond-> {}
; buggy for :read'ing tabs
(:write selection-capabilities)
(assoc :content {:fx/type :label
:text item})
(not (:write selection-capabilities))
(assoc :disable (if selected-tab-id
(not= item selected-tab-id)
true))))]))
items)
(with-tab-selection-props
(cond-> {}
(:read selection-capabilities) (assoc :selected-item (get-ref selected-tab-id))
(:write selection-capabilities) (assoc :on-selected-item-changed {:event/type ::select-tab}))
{:fx/type :tab-pane
:tabs (map #(-> (get-ref %)
(assoc :fx/id %))
items)}))))
(defn table-view [{:keys [path->value selection selection-mode]}]
{:fx/type fx.ext.table-view/with-selection-props
:props (case selection-mode
:multiple {:selection-mode :multiple
:selected-items selection
:on-selected-items-changed {:event/type ::select-multiple}}
:single (cond-> {:selection-mode :single
:on-selected-item-changed {:event/type ::select-single}}
(seq selection)
(assoc :selected-item (-> selection sort first))))
:desc {:fx/type :table-view
:columns [{:fx/type :table-column
:text "path"
:cell-value-factory identity
:cell-factory {:fx/cell-type :table-cell
:describe (fn [path]
{:text path})}}
{:fx/type :table-column
:text "elements"
:cell-value-factory identity
:cell-factory {:fx/cell-type :table-cell
:describe (fn [path]
{:text (str (count (path->value path)))})}}]
:items (keys path->value)}})
;; Situation with tree-view is complicated by the fact that it's selection model is
;; parameterized by TreeItems, and selection expects items. To workaround it we create
;; tree-item descriptions inside using `fx/ext-let-refs`, so we can refer to them later
;; with `fx/ext-get-ref` containing a "path", which is conveniently a value with use for
;; selection. Since defined tree-item descriptions can have children, which are also tree
;; items, we need to declare them first, in a separate `fx/ext-let-refs`, and then refer
;; to them in parent's `fx/ext-let-refs`, that makes code really hard to follow.
(defn children-refs [prefix m]
(->> m
keys
(mapv (fn [x] {:fx/type fx/ext-get-ref
:ref (str prefix "/" x)}))))
(defn- make-tree-item-refs [tree desc]
(letfn [(f [x prefix desc]
(let [children (for [[k vs] x
v vs]
[(str prefix "/" k) v])
new-desc {:fx/type fx/ext-let-refs
:refs (into {} (for [[k v] x
:let [path (str prefix "/" k)]]
[path {:fx/type :tree-item
:value path
:children (children-refs path v)}]))
:desc desc}]
(reduce (fn [desc [prefix e]]
(f (into {} [e]) prefix desc))
new-desc children)))]
(f tree "" desc)))
(defn tree-view [{:keys [tree selection selection-mode]}]
(make-tree-item-refs
tree
{:fx/type fx.ext.tree-view/with-selection-props
:props (case selection-mode
:multiple {:selection-mode :multiple
:selected-items (for [x selection]
{:fx/type fx/ext-get-ref
:ref x})
:on-selected-items-changed {:event/type ::select-tree-items}}
:single (cond-> {:selection-mode :single
:on-selected-item-changed {:event/type ::select-tree-item}}
(seq selection)
(assoc :selected-item {:fx/type fx/ext-get-ref
:ref (-> selection sort first)})))
:desc {:fx/type :tree-view
:show-root false
:root {:fx/type :tree-item
:children (children-refs "" tree)}}}))
;; For list-view and table view we don't need such complicated setup, since their
;; selection is described by the same values they have in `:items`, so we just create a
;; map from paths to "directory" contents on these paths
(defn- make-path->value [tree]
(letfn [(flatten-map [prefix x]
(mapcat (fn [[k v]]
(let [path (str prefix "/" k)]
(cons [path v] (flatten-map path v))))
x))]
(into (sorted-map)
(flatten-map "" tree))))
(defn add-header [title desc]
{:fx/type :v-box
:spacing 10
:children [{:fx/type :label
:text title}
desc]})
(defn on-grid [{:keys [rows columns descs]}]
{:pre [(vector? descs)
(<= (count descs)
(* rows columns))]}
{:fx/type :grid-pane
:hgap 20
:vgap 20
:padding 30
:row-constraints (repeat rows {:fx/type :row-constraints
:percent-height (/ 100 rows)})
:column-constraints (repeat columns {:fx/type :column-constraints
:percent-width (/ 100 columns)})
:children (for [row (range rows)
col (range columns)
:let [loc (+ (* row rows) col)]
:when (< loc (count descs))]
(-> (nth descs loc)
(assoc :grid-pane/row row
:grid-pane/column col)))})
(defn view [{{:keys [tree selection]} :state}]
(let [path->value (make-path->value tree)]
{:fx/type :stage
:showing true
:width 960
:scene {:fx/type :scene
:root {:fx/type :scroll-pane
:fit-to-width true
:fit-to-height true
:content
{:fx/type on-grid
:rows 3
:columns 3
:descs [(add-header
":list-view :multiple"
{:fx/type list-view
:items (keys path->value)
:selection-mode :multiple
:selection selection})
(add-header
":list-view :single"
{:fx/type list-view
:items (keys path->value)
:selection-mode :single
:selection selection})
(add-header
":table-view :multiple"
{:fx/type table-view
:path->value path->value
:selection-mode :multiple
:selection selection})
(add-header
":table-view :single"
{:fx/type table-view
:path->value path->value
:selection-mode :single
:selection selection})
(add-header
":tree-view :multiple"
{:fx/type tree-view
:tree tree
:selection-mode :multiple
:selection selection})
(add-header
":tree-view :single"
{:fx/type tree-view
:tree tree
:selection-mode :single
:selection selection})
(add-header
":tab-pane (opens current selection)"
{:fx/type tab-pane
:items (keys path->value)
:selection-capabilities #{:read}
:selection selection})
(add-header
":tab-pane (only changes on click)"
{:fx/type tab-pane
:items (keys path->value)
:selection-capabilities #{:write}
:selection selection})
; buggy, content from different tabs
; blend into eachother
#_
(add-header
":tab-pane (read+write)"
{:fx/type tab-pane
:items (keys path->value)
:selection-capabilities #{:read :write}
:selection selection})]}}}}))
(defn tree-item-value [^TreeItem x]
(.getValue x))
(defn tab-id [^Tab x]
{:post [(string? %)]}
(.getId x))
(def renderer
(fx/create-renderer
:middleware (fx/wrap-map-desc (fn [state]
{:fx/type view
:state state}))
:opts {:fx.opt/map-event-handler
#(case (:event/type %)
::select-tree-items
(swap! *state assoc :selection (->> % :fx/event (mapv tree-item-value)))
::select-tree-item
(swap! *state assoc :selection [(-> % :fx/event tree-item-value)])
::select-multiple
(swap! *state assoc :selection (:fx/event %))
::select-single
(swap! *state assoc :selection [(:fx/event %)])
::select-tab
(swap! *state assoc :selection [(-> % :fx/event tab-id)]))}))
(fx/mount-renderer *state renderer)