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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
(ns frq.hiccup
"glimmer's hiccup, painted by Flutter.
This is a backend, not a port. `frq.app` is 1,834 lines of
`[:vbox {:spacing 6} ...]` over about twenty tags, and it names no toolkit
anywhere — the same trick `frq.cosmic` and `frq.tui` rely on, where the
components do not know what is under the reconciler. So the screens do not
get rewritten for the phone; this interprets them.
The tags are glimmer's and so is the styling: every arm below is what
`crates/jolt-cosmic` asks libcosmic for, in `frq.theme`'s tokens. `:title` is
title3 and `:title-2` is title4; `:card` is Container::Card — padding 12,
spacing 8, a surface a step from the background rather than an elevation,
because COSMIC does not float things; `:button` is suggested, destructive or
standard; `:dim-label` is the caption class, which is the body colour stepped
back rather than a colour of its own.
Material is underneath and deliberately not visible. No elevation, no ripple
shadows, no Material 3 pill heights — a screen laid out against libcosmic's
4/8/12/16/24 spacing lands at the same proportions here.
What this does NOT do is glimmer's reconciliation. glimmer patches the tree
it painted last; Flutter rebuilds from the top and diffs its own element
tree, which is the same job done by the framework instead of by us. The cost
is that a cell firing rebuilds the whole screen rather than the subtree that
read it — fine at this size, and the thing to revisit if a message list ever
feels it."
(:require ["dart:io" :as io]
["package:flutter/material.dart" :as m]
[cljd.flutter :as f]
[frq.theme :as t]))
(defn- dbl [x default]
(cond (number? x) (double x)
:else default))
;; ------------------------------------------------------------- text entry
(defonce ^:private controllers
;; TextEditingController per `:key`, because a controller made during a build
;; is a new one every rebuild — the cursor jumps to the start and the
;; selection is lost on the keystroke after it.
;;
;; Keyed rather than positional: glimmer's own reconciler matches children by
;; `:key` for the same reason, and an entry that moves in the tree should
;; keep what is typed in it.
(atom {}))
(defn- controller-for [k text]
(let [k (or k ::anonymous)
c (or (get @controllers k)
(let [c (m/TextEditingController .text (or text ""))]
(swap! controllers assoc k c)
c))]
;; Only when it differs, and never while it would move the caret: the cell
;; is the authority for what the entry says, but the person typing is the
;; authority for where they are in it.
(when (and text (not= text (.-text c)))
(set! (.-text c) text))
c))
;; ------------------------------------------------------------------ props
(defn- props [node]
(let [p (second node)] (if (map? p) p {})))
(defn- body [node]
(let [p (second node)] (if (map? p) (drop 2 node) (drop 1 node))))
(declare render)
(defn- children
"Flatten seqs, drop nils. `(for [...] ...)` in a component yields a seq in
the child position and glimmer splices it; so does this."
[nodes]
(persistent!
(reduce (fn [acc n]
(cond (nil? n) acc
(seq? n) (reduce conj! acc (children n))
:else (conj! acc (render n))))
(transient []) nodes)))
;; ------------------------------------------------------------------ text
(defn- txt
[ctx s size color & {:keys [weight]}]
(m/Text (str s)
.style (m/TextStyle .fontSize size
.color color
.height 1.35
.fontWeight (or weight m/FontWeight.w400))))
;; ---------------------------------------------------------------- buttons
(defn- cosmic-button
"libcosmic's three button classes. Filled accent for suggested, filled red
for destructive, and a component-coloured fill for standard — COSMIC's
standard button is a filled surface, not an outline."
[ctx p on]
(let [kind (cond (:destructive p) :destructive
(or (:primary p) (= "primary" (:kind p))) :suggested
:else :standard)
bg (case kind
:suggested t/accent
:destructive t/destructive
t/component)
;; COSMIC names the foreground for each role, and for this theme both
;; accent.on and destructive.on are black — a cream accent with white
;; text on it is unreadable, which is exactly what guessing produced.
fg (case kind
:suggested t/on-accent
:destructive t/on-destructive
t/on-bg)]
(m/Material
.color bg
.borderRadius (m/BorderRadius.circular t/radius-m)
.child (m/InkWell
.borderRadius (m/BorderRadius.circular t/radius-m)
.onTap (when on #(on))
.child (m/Padding
.padding (m/EdgeInsets.symmetric .horizontal t/space-s
.vertical t/space-xxs)
.child (txt ctx (:label p "") t/text-body fg
:weight m/FontWeight.w500))))))
;; ----------------------------------------------------------------- widget
(defn- render-tag [tag node]
(f/widget
:context ctx
(let [p (props node)
kids (children (body node))
col (fn [sp cs] (m/Column .crossAxisAlignment m/CrossAxisAlignment.start
.mainAxisSize m/MainAxisSize.min
.spacing sp
.children cs))]
(case tag
:vbox
(let [c (col (dbl (:spacing p) 0.0) kids)]
(if-let [w (:width-request p)]
(m/SizedBox .width (dbl w 0.0) .child c)
c))
;; `page` is jolt-cosmic's scrollable container, centred and capped.
:page
(m/Center
.child (m/ConstrainedBox
.constraints (m/BoxConstraints .maxWidth (dbl (:max-width p) 520.0))
.child (m/Padding
.padding (m/EdgeInsets.all t/space-s)
.child (col t/space-xs kids))))
:hbox
(m/Row
.crossAxisAlignment m/CrossAxisAlignment.center
.mainAxisSize m/MainAxisSize.min
.spacing (dbl (:spacing p) 0.0)
.children kids)
:label (txt ctx (:label p "") t/text-body t/on-bg)
:dim-label (txt ctx (:label p "") t/text-caption t/dim)
:title (txt ctx (:label p "") t/text-title-3 t/on-bg
:weight m/FontWeight.w600)
:title-2 (txt ctx (:label p "") t/text-title-4 t/on-bg
:weight m/FontWeight.w600)
:button (cosmic-button ctx p (:on-click p))
;; button::link — accent text, no underline. COSMIC links are buttons.
:link
(m/InkWell
.onTap (when-let [on (:on-click p)] #(on))
.child (txt ctx (:label p "") t/text-body t/accent))
;; Container::Card: padding 12, spacing 8, fills its width unless it is
;; sitting in a row.
:card
(m/Container
.width double/infinity
.padding (m/EdgeInsets.all t/space-xs)
.decoration (m/BoxDecoration
.color t/card
.borderRadius (m/BorderRadius.circular t/radius-s))
.child (col (dbl (:spacing p) t/space-xxs) kids))
:separator (m/Divider .height 1.0 .thickness 1.0 .color t/divider)
;; A 16px indeterminate circle, with the label as a caption beside it.
:spinner
(m/Row
.mainAxisSize m/MainAxisSize.min
.spacing t/space-xxs
.children (into [(m/SizedBox
.width 16.0 .height 16.0
.child (m/CircularProgressIndicator
.strokeWidth 2.0
.color t/accent))]
(when-let [l (not-empty (str (:label p "")))]
[(txt ctx l t/text-caption t/dim)])))
;; A dot that says whether the thing is live, and the words beside it.
:status
(m/Row
.mainAxisSize m/MainAxisSize.min
.spacing 6.0
.children [(m/Container
.width 8.0 .height 8.0
.decoration (m/BoxDecoration
.color (if (:live p) t/success t/dim)
.borderRadius (m/BorderRadius.circular t/radius-xs)))
(txt ctx (:label p "") t/text-caption t/dim)])
:spacer
(let [size (dbl (or (:size p) (:gap p) (:width-request p)) t/space-xxs)]
(m/SizedBox .width size .height size))
:checkbutton
(let [on (:on-toggled p)]
(m/Row
.mainAxisSize m/MainAxisSize.min
.spacing t/space-xxxs
.children [(m/SizedBox
.width 20.0 .height 20.0
.child (m/Checkbox
.value (boolean (:active p))
.activeColor t/accent
.onChanged (when on (fn [v] (on (boolean v))))))
(txt ctx (:label p "") t/text-body t/on-bg)]))
;; text_input: a filled rounded field, no outline. COSMIC entries sit in
;; the component colour rather than behind a border.
:entry
(let [on-change (:on-change p)
on-activate (:on-activate p)
rows (:rows p)]
(m/TextField
.controller (controller-for (:key p) (:text p))
.onChanged (when on-change #(on-change %))
.onSubmitted (when on-activate (fn [_] (on-activate)))
.maxLines (if rows (int rows) 1)
.style (m/TextStyle .fontSize t/text-body .color t/on-bg)
.cursorColor t/accent
.decoration (m/InputDecoration
.isDense true
.filled true
.fillColor t/component
.hintText (:placeholder p)
.hintStyle (m/TextStyle .fontSize t/text-body
.color t/dim)
.contentPadding (m/EdgeInsets.symmetric
.horizontal t/space-xs
.vertical t/space-xxs)
.border (m/OutlineInputBorder
.borderRadius (m/BorderRadius.circular t/radius-s)
.borderSide m/BorderSide.none)
.enabledBorder (m/OutlineInputBorder
.borderRadius (m/BorderRadius.circular t/radius-s)
.borderSide m/BorderSide.none)
.focusedBorder (m/OutlineInputBorder
.borderRadius (m/BorderRadius.circular t/radius-s)
.borderSide (m/BorderSide .color t/accent
.width 1.0)))))
:emoji
(m/Text (str (:emoji p "")) .style (m/TextStyle .fontSize (dbl (:size p) 16.0)))
:avatar
(let [s (dbl (:size p) 32.0)
src (:src p)]
(m/CircleAvatar
.radius (/ s 2.0)
.backgroundColor t/component
.backgroundImage (when (and src (not= "" src)) (m/NetworkImage src))
.child (when (or (nil? src) (= "" src))
(txt ctx (let [l (str (:label p ""))]
(if (pos? (count l)) (.toUpperCase (subs l 0 1)) "?"))
t/text-body t/on-bg))))
:image
(let [src (or (:src p) (:path p))
w (:max-width p)
h (:max-height p)
img (cond
(nil? src) (m/SizedBox .width 0.0 .height 0.0)
(or (.startsWith (str src) "http://")
(.startsWith (str src) "https://"))
(m/Image.network (str src) .fit m/BoxFit.contain)
:else (m/Image.file (io/File. (str src)) .fit m/BoxFit.contain))
img (m/ClipRRect .borderRadius (m/BorderRadius.circular t/radius-s)
.child img)
img (if (or w h)
(m/ConstrainedBox
.constraints (m/BoxConstraints
.maxWidth (dbl w double/infinity)
.maxHeight (dbl h double/infinity))
.child img)
img)]
(if-let [on (:on-click p)]
(m/InkWell .onTap #(on) .child img)
img))
:scroll
(m/Expanded
.child (m/SingleChildScrollView
.child (col (dbl (:spacing p) 0.0) kids)))
;; A tag this backend has not grown yet still shows its children — which
;; is what libvidya did and what jolt-cosmic kept. The marker is here so
;; it is obvious which ones are missing: glimmer-cosmic's spike painted
;; every unknown tag as a silent column, and that is why most of frq
;; came out of it as stacked text.
(m/Column
.crossAxisAlignment m/CrossAxisAlignment.start
.mainAxisSize m/MainAxisSize.min
.children (into [(txt ctx (str "?" (name tag)) 10.0 m/Colors.orange)]
kids))))))
(defn render
"One hiccup node as a Flutter widget.
A vector whose head is a function is a component: glimmer calls it with the
rest of the vector as arguments and renders what comes back, and so does
this. Everything else is a tag."
[node]
(cond
(nil? node) (m/SizedBox .width 0.0 .height 0.0)
(string? node) (m/Text node)
(vector? node)
(let [head (first node)]
(if (keyword? head)
(render-tag head node)
(render (apply head (rest node)))))
(seq? node)
(m/Column .crossAxisAlignment m/CrossAxisAlignment.start
.mainAxisSize m/MainAxisSize.min
.children (children node))
:else (m/Text (str node))))
|