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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
|
(ns jvui.widgets
"The widgets, in the calling convention dvui made popular: you call them, and
what they answer is what the person did.
(when (w/button \"Save\") (save!))
(reset! on? (w/checkbox @on? \"Enabled\"))
There is no widget object to hold, no handler to register and nothing to
free — a button that is not called this frame is not on the screen, which is
the property the whole style is for.
Containers are macros so their contents read as a body rather than as a
closure. They are thin: each is `core/box*` with a different set of defaults
from the theme."
(:require [jvui.core :as c]
[jvui.theme :as theme]
[jvui.frames :as frames]
[clojure.string :as str]))
;; -------------------------------------------------------------- containers
(defmacro box
"The general container. See `core/box*` for the options."
[opts & body]
`(c/box* ~opts (fn [~'_id ~'_rect] ~@body)))
(defmacro vbox [opts & body] `(box (assoc ~opts :dir :vertical) ~@body))
(defmacro hbox [opts & body] `(box (assoc ~opts :dir :horizontal) ~@body))
(defn card*
"A surface with a border and a radius: the thing a group of controls sits on."
[opts body]
(c/box* (merge {:dir :vertical
:padding (c/th :padding)
:spacing (c/th :spacing)
:radius (c/th :radius)
:fill (c/th :surface)
:border {:colour (c/th :border) :width (c/th :border-width)}
:expand :horizontal}
opts)
body))
(defmacro card [opts & body] `(card* ~opts (fn [~'_id ~'_rect] ~@body)))
(defn page*
"The root container: the window's background, padded, with its contents kept
to `:max-width` and centred in whatever is left."
[opts body]
(c/box* (merge {:dir :vertical :expand :both :fill (c/th :bg)
:padding (c/th :padding)}
(dissoc opts :max-width))
(fn [_ _]
;; The column expands vertically and is *given* its width, so the
;; gravity has something left over to centre it in.
(c/box* {:dir :vertical
:spacing (c/th :spacing)
:expand :vertical
:gravity [0.5 0.0]
:min-size [(min (double (or (:max-width opts) 1.0e9))
(max 0.0 (- (double (first (:size (c/ui))))
(* 2 (double (c/th :padding))))))
0.0]}
body))))
(defmacro page [opts & body] `(page* ~opts (fn [~'_id ~'_rect] ~@body)))
;; -------------------------------------------------------------------- text
(defn- break-word
"Cut a word too long for `width` into pieces that fit.
Character-wise and greedy. Only ever reached for a word that would not
fit on a line of its own — an ordinary sentence never comes here."
[w size width]
(loop [rest* w out []]
(if (or (empty? rest*) (<= (first (c/measure rest* size)) width))
(if (seq rest*) (conj out rest*) out)
(let [n (loop [i 1]
(cond (>= i (count rest*)) (count rest*)
(> (first (c/measure (subs rest* 0 i) size)) width) (max 1 (dec i))
:else (recur (inc i))))]
(recur (subs rest* n) (conj out (subs rest* 0 n)))))))
(defn- wrap-lines
"Break `s` into lines that each fit `width`, on spaces.
Greedy and word-wise, and a word that cannot fit on a line of its own
is broken at a character. That second half was left out first time
round on the grounds that a URL cut in half reads worse than one that
overflows — which was wrong, and the sign-in screen showed why: an
OAuth login URL is one unbreakable word, so it made its container
wider than the window and pushed the whole page off both edges. A
broken URL is worse than an unbroken one; a window you cannot read is
worse than both.
Measured through the same `c/measure` the drawing uses, so a line that
is said to fit does fit — a wrap computed against a different metric
than the renderer's is off by a word at the worst moments."
[s size width]
(let [words (mapcat (fn [w]
(if (> (first (c/measure w size)) width)
(break-word w size width)
[w]))
(str/split (str s) #" "))]
(loop [[w & more] words line nil out []]
(cond
(nil? w) (if line (conj out line) out)
(nil? line) (recur more w out)
:else
(let [try* (str line " " w)]
(if (<= (first (c/measure try* size)) width)
(recur more try* out)
(recur more w (conj out line))))))))
(defn label
"A line of text, wrapped to the space it was given. Answers its rectangle.
Wrapping is on when the text does not fit and the box has told us how
much room there is. That is not a style choice: without it one long
sentence makes its container wider than the window, and a page that
centres its content then hangs off BOTH edges — which is exactly what
frq's sign-in screen did the first time it was painted here.
`:wrap false` turns it off for a caller that would rather overflow, and
a width of zero — the first frame, before any box knows its size — is
read as \"not yet\" rather than \"no room\"."
([s] (label s {}))
([s {:keys [size colour expand gravity align wrap]
:or {expand :horizontal gravity [0.0 0.5] align :left wrap true}}]
(let [size (or size (c/th :font-size))
colour (or colour (c/th :text))
[tw0 th0] (c/measure s size)
avail (c/avail-width)
lines (if (and wrap (pos? avail) (> tw0 avail))
(wrap-lines s size avail)
[(str s)])
many? (> (count lines) 1)
widths (mapv #(first (c/measure % size)) lines)
tw (if many? (reduce max 0.0 widths) tw0)
lh (max th0 (c/line-height size))
rect (c/leaf [(double tw) (double (* lh (count lines)))]
expand gravity)
[x y w h] rect]
(dotimes [i (count lines)]
(let [ln (nth lines i)
lw (nth widths i)
tx (case align
:center (+ x (/ (- w lw) 2.0))
:right (+ x (- w lw))
x)]
(c/draw-text! ln tx (+ y (* i lh) (/ (- lh th0) 2.0)) size colour)))
rect)))
(defn title [s & [opts]]
(label s (merge {:size (c/th :font-size-title)} opts)))
(defn dim-label [s & [opts]]
(label s (merge {:colour (c/th :text-dim)} opts)))
;; ------------------------------------------------------------------ button
(defn button
"Answers true on the frame the button is released inside itself.
opts: :kind (:normal or :primary) :expand :gravity :key :min-width"
([s] (button s {}))
([s {:keys [kind expand gravity key min-width]
:or {kind :normal expand :none gravity [0.0 0.5]}}]
(let [size (c/th :font-size)
pad (c/th :padding)
[tw th*] (c/measure s size)
w (max (double (or min-width 0.0)) (+ tw (* 2 pad) (* 2 pad)))
h (max (double (c/th :control-height)) (+ th* pad))
id (c/next-id key)
rect (c/leaf [w h] expand gravity)
{:keys [hover? pressed? clicked? focused?]} (c/interact! id rect)
primary? (= kind :primary)
base (cond primary? (c/th :accent)
:else (c/th :surface-alt))
fill (cond pressed? (if primary?
(theme/mix base [0 0 0 255] 0.25)
(c/th :press))
hover? (if primary?
(theme/mix base [255 255 255 255] 0.12)
(c/th :hover))
:else base)
text-colour (if primary? (c/th :accent-text) (c/th :text))
[rx ry rw rh] rect]
(c/fill! rect fill (c/th :radius)
(if focused? (c/th :focus) (c/th :border))
(if focused? 2.0 (c/th :border-width)))
(c/draw-text! s (+ rx (/ (- rw tw) 2.0)) (+ ry (/ (- rh th*) 2.0))
size text-colour)
clicked?)))
;; ---------------------------------------------------------------- checkbox
(defn checkbox
"Answers the value the checkbox should have after this frame."
([value s] (checkbox value s {}))
([value s {:keys [key expand] :or {expand :horizontal}}]
(let [size (c/th :font-size)
gap 8.0
side 18.0
[tw th*] (c/measure s size)
h (max side (double th*))
id (c/next-id key)
rect (c/leaf [(+ side gap tw) h] expand [0.0 0.5])
{:keys [hover? clicked? focused?]} (c/interact! id rect)
[rx ry _ rh] rect
by (+ ry (/ (- rh side) 2.0))
boxr [rx by side side]
on? (if clicked? (not value) value)]
(c/fill! boxr
(cond on? (c/th :accent)
hover? (c/th :hover)
:else (c/th :surface-alt))
4.0
(if focused? (c/th :focus) (c/th :border))
(if focused? 2.0 (c/th :border-width)))
(when on?
;; the tick, as two strokes rather than a glyph, so it does not depend
;; on the font having one
(let [cx (+ rx 4.0) cy (+ by (/ side 2.0))]
(c/draw-line! cx cy (+ rx 7.5) (+ by side -5.0) (c/th :accent-text) 2.0)
(c/draw-line! (+ rx 7.5) (+ by side -5.0) (+ rx side -4.0) (+ by 5.0)
(c/th :accent-text) 2.0)))
(c/draw-text! s (+ rx side gap) (+ ry (/ (- rh th*) 2.0)) size (c/th :text))
on?)))
;; ------------------------------------------------------------------ slider
(defn slider
"A horizontal slider. Answers the value after this frame.
The drag is tracked through `capture`, so the value keeps following the
pointer after it leaves the track — which is what a person expects when they
drag fast, and what a plain hit-test gets wrong."
([value] (slider value {}))
([value {:keys [min max key expand step]
:or {min 0.0 max 1.0 expand :horizontal}}]
(let [lo (double min) hi (double max)
h (double (c/th :control-height))
id (c/next-id key)
rect (c/leaf [120.0 h] expand [0.0 0.5])
{:keys [hover? pressed? focused?]} (c/interact! id rect)
[rx ry rw rh] rect
knob 16.0
track-h 6.0
travel (clojure.core/max 1.0 (- rw knob))
mx (first (:mouse (c/ui)))
raw (if pressed?
(+ lo (* (- hi lo)
(clojure.core/max 0.0
(clojure.core/min 1.0 (/ (- mx rx (/ knob 2.0)) travel)))))
(double value))
v (clojure.core/max lo (clojure.core/min hi
(if step (* step (Math/round (/ raw (double step)))) raw)))
t (if (= hi lo) 0.0 (/ (- v lo) (- hi lo)))
ty (+ ry (/ (- rh track-h) 2.0))
kx (+ rx (* t travel))]
(c/fill! [rx ty rw track-h] (c/th :surface-alt) (/ track-h 2.0))
(c/fill! [rx ty (+ (* t travel) (/ knob 2.0)) track-h] (c/th :accent)
(/ track-h 2.0))
(c/fill! [kx (+ ry (/ (- rh knob) 2.0)) knob knob]
(if (or hover? pressed?)
(theme/mix (c/th :accent) [255 255 255 255] 0.2)
(c/th :accent))
(/ knob 2.0)
(if focused? (c/th :focus) (c/th :border))
(if focused? 2.0 1.0))
v)))
;; ---------------------------------------------------------------- progress
(defn progress
([fraction] (progress fraction {}))
([fraction {:keys [expand height] :or {expand :horizontal height 8.0}}]
(let [t (max 0.0 (min 1.0 (double fraction)))
rect (c/leaf [120.0 (double height)] expand [0.0 0.5])
[rx ry rw rh] rect]
(c/fill! rect (c/th :surface-alt) (/ rh 2.0))
(when (pos? t) (c/fill! [rx ry (* t rw) rh] (c/th :accent) (/ rh 2.0)))
rect)))
;; ------------------------------------------------------------------- filler
(defn spacer
([] (spacer {}))
([{:keys [size expand] :or {size 0.0 expand :none}}]
(c/leaf [(double size) (double size)] expand [0.0 0.0])))
(defn separator
[]
(let [rect (c/leaf [1.0 (double (c/th :border-width))] :horizontal [0.0 0.5])]
(c/fill! rect (c/th :border))
rect))
;; -------------------------------------------------------------- text entry
(defn- clamp [v lo hi] (max lo (min hi v)))
(defn entry-activated?
"Did the field under `id` see Enter on the frame just walked?
Asked after `text-entry`, which records it. Enter is the one key a text
field must NOT treat as input — the client sends on it — and a widget
that answered only its text gave a caller no way to know."
[id]
(boolean (c/state id :activated false)))
(defn text-entry
"A single-line editable string. Answers the text after this frame.
The caret is an index into the string kept under the widget's id, which is
the one piece of state a text field cannot recompute from its value."
([value] (text-entry value {}))
([value {:keys [key expand placeholder min-width]
:or {expand :horizontal}}]
(let [size (c/th :font-size)
pad (c/th :padding)
h (double (c/th :control-height))
id (c/next-id key)
rect (c/leaf [(double (or min-width 160.0)) h] expand [0.0 0.5])
{:keys [hover? focused?]} (c/interact! id rect)
[rx ry rw rh] rect
s (str value)
caret0 (clamp (c/state id :caret (count s)) 0 (count s))
evs (c/key-events id)
[s' caret']
(reduce
(fn [[s caret] e]
(case (:kind e)
:text (let [t (:text e)]
[(str (subs s 0 caret) t (subs s caret))
(+ caret (count t))])
:key-down
(case (:key e)
:backspace (if (pos? caret)
[(str (subs s 0 (dec caret)) (subs s caret))
(dec caret)]
[s caret])
:delete (if (< caret (count s))
[(str (subs s 0 caret) (subs s (inc caret))) caret]
[s caret])
:left [s (max 0 (dec caret))]
:right [s (min (count s) (inc caret))]
:home [s 0]
:end [s (count s)]
[s caret])
[s caret]))
[s caret0] (or evs []))
caret' (clamp caret' 0 (count s'))]
(c/state! id :caret caret')
;; Enter is not an edit and must not be swallowed as one: a client
;; sends its message on it. Recorded as state rather than returned,
;; because `text-entry` already answers the text and a second return
;; value would change every existing call.
(c/state! id :activated
(boolean (some #(and (= :key-down (:kind %)) (= :return (:key %)))
(or evs []))))
(c/fill! rect (c/th :surface) (c/th :radius)
(if focused? (c/th :focus) (c/th :border))
(if focused? 2.0 (c/th :border-width)))
(let [[_ th*] (c/measure (if (= s' "") "M" s') size)
ty (+ ry (/ (- rh th*) 2.0))
inner (- rw (* 2 pad))
;; How far the text is slid left so the caret stays in view. A
;; field narrower than its contents is the ordinary case — a
;; handle, a URL, a password — and without this the text simply
;; runs out past the border and over whatever is beside it.
caret-x (first (c/measure (subs s' 0 caret') size))
shift (max 0.0 (- caret-x inner))]
(c/with-clip [(+ rx pad) ry inner rh]
(fn []
(if (and (= s' "") placeholder (not focused?))
(c/draw-text! placeholder (+ rx pad) ty size (c/th :text-dim))
(c/draw-text! s' (- (+ rx pad) shift) ty size (c/th :text)))))
;; The caret rides the same shift, and inside the same clip: a caret
;; drawn at the untranslated offset sits past the border on a full
;; field, pointing at where the text would have been.
(when focused?
(c/with-clip [(+ rx pad) ry inner rh]
(fn []
(c/fill! [(- (+ rx pad caret-x) shift) (+ ty 1.0) 1.5 (- th* 2.0)]
(c/th :text))))))
s')))
;; ------------------------------------------------------------------- scroll
(defonce ^:private scroll-areas
;; Offsets kept by NAME, outliving the node that showed them.
;;
;; A widget id is a position in this frame's tree, so two lists that are
;; never on screen together — one channel's messages and another's —
;; share one id and therefore one offset, and switching between them
;; carries the wrong scroll across. `:scroll-key` names the area instead,
;; and the name outlives the node.
(atom {}))
(defn scroll-offset
"How far the area named `k` is scrolled, or nil if it has never shown.
Public because it is the only way to ask a viewport what it did — the
widget answers its rectangle, not its state — and a caller restoring a
position, or a test asking whether a list actually followed its
content, has nowhere else to look."
[k]
(:offset (get @scroll-areas k)))
(defn scroll*
"A clipped viewport that scrolls its contents vertically.
The viewport reports the height it was given, not the height of what is
in it; the difference between the two is what there is to scroll, which
is why `core/box*` keeps both numbers.
opts beyond `core/box*`'s:
:height the viewport's own height
:scroll-key a name for the offset, so it survives the node
:reserve leave this much of the available height behind
:stick-to-bottom follow new content while the reader is at the end
:scroll-to-bottom a number the caller BUMPS to ask for a jump
:on-at-end called with true/false as the reader arrives at or
leaves the end"
[opts body]
(let [{:keys [scroll-key reserve stick-to-bottom scroll-to-bottom on-at-end]} opts
id (c/next-id (:key opts))
area (or scroll-key id)
prev (c/data id)
;; A viewport with no height of its own FILLS what is left rather
;; than ASKING for it. Asking is what it did first — a minimum of
;; the whole column — and then the column had nothing left for
;; the separator and compose bar under it, which went off the
;; bottom of the window.
;;
;; So the height is the one it was GIVEN last frame, and it
;; reports a minimum of nothing. Two hundred is the answer on the
;; first frame only, before it has been given anything.
given (nth (:rect prev) 3 nil)
h (double (cond
(:height opts) (:height opts)
(and given (pos? (double given)))
(max 0.0 (- (double given) (double (or reserve 0.0))))
:else 200.0))
content-h (second (or (:content-min prev) [0.0 0.0]))
view (or (:rect prev) [0.0 0.0 0.0 0.0])
maxoff (max 0.0 (- content-h h))
st (get @scroll-areas area {})
off0 (min maxoff (max 0.0 (or (:offset st) 0.0)))
wheel (or (c/wheel-events view) [])
;; A jump is asked for by CHANGING the number, not by setting it:
;; a caller that wanted to jump twice to the same place would
;; otherwise have nothing to say the second time.
jump? (and scroll-to-bottom
(pos? (double scroll-to-bottom))
(not= scroll-to-bottom (:jumped st)))
;; The wheel is applied FIRST, and a wheel that moved the list
;; breaks the stick for that frame. Deciding to stick before
;; reading it pins the offset at the end every frame and the
;; reader can never scroll up at all — they drag and it snaps
;; back, which reads as the window being broken.
wheeled (reduce (fn [o e] (min maxoff (max 0.0 (- o (* 40.0 (:dy e))))))
off0 wheel)
moved? (not= wheeled off0)
;; Sticking and jumping are the same control pulling opposite
;; ways. Sticking wins over a jump, so a jump to an old message
;; is not snatched back by the next arrival — but a hand on the
;; wheel wins over both.
stick? (and stick-to-bottom (not jump?) (not moved?) (:at-end st true))
off (cond
jump? maxoff
stick? maxoff
:else wheeled)
;; The gutter the bar is drawn in, taken off the content whether or
;; not there is anything to scroll yet. Always, because the
;; alternative is that a list reflows the moment it grows past the
;; window — every line rewrapping under the reader as the message
;; that overflowed arrives. And taken at all because the bar is
;; painted over the viewport's right edge: without it the last
;; characters of every wrapped line sit under the bar, which is
;; where "the sidebar truncates" comes from.
;; The bar's own width and a gap beside it. Reserving the bar alone
;; leaves the wrap boundary exactly at its left edge, so a line ends
;; flush against it — legible, but it reads as text running into the
;; bar, and a glyph whose drawn width runs a hair past its measured
;; one touches it. A gap is what puts the last word beside the bar
;; rather than on it.
gutter (+ (double (c/th :scrollbar)) (double (c/th :spacing)))
r (c/box* (merge {:dir :vertical
:expand :both
:pad-right gutter
;; An explicit :height IS a demand and is
;; reported as one. Without it the viewport
;; fills what is left and asks for nothing —
;; asking is what left the compose bar off the
;; bottom of the window.
:min-size [0.0 (if (:height opts) h 0.0)]
:fixed true
:clip? true
:spacing (c/th :spacing)
:key (:key opts)
:offset [0.0 (- off)]}
(dissoc opts :height :scroll-key :reserve :stick-to-bottom
:scroll-to-bottom :on-at-end))
body)
;; Within a couple of lines of the end counts as at it: a reader
;; who has not moved should not stop being followed because the
;; last message was a pixel taller than the one before.
at-end? (>= off (- maxoff 24.0))
;; Arriving at the end is reported at once; leaving it has to hold
;; for a few frames. A burst of messages grows the content faster
;; than the offset follows, and reporting that honestly would
;; blink "scrolled away" whenever a channel is busy.
away (if at-end? 0 (inc (or (:away st) 0)))
settled (cond at-end? true (>= away 3) false :else nil)
was (:reported st)]
(swap! scroll-areas assoc area
(cond-> (assoc st :offset off :at-end at-end? :away away)
jump? (assoc :jumped scroll-to-bottom)
(some? settled) (assoc :reported settled)))
;; Not on the first report: the opening one arrives before the content
;; has a height, and every list would announce itself as at its end.
(when (and on-at-end (some? settled) (some? was) (not= was settled))
(on-at-end settled))
(when (pos? maxoff)
(let [[vx vy vw vh] (or (:rect (c/data id)) view)
bw (double (c/th :scrollbar))
bh (max 24.0 (* vh (/ vh (max 1.0 content-h))))
by (+ vy (* (/ off maxoff) (- vh bh)))]
(c/fill! [(- (+ vx vw) bw) vy bw vh] (c/th :surface-alt) (/ bw 2.0))
(c/fill! [(- (+ vx vw) bw) by bw bh] (c/th :border) (/ bw 2.0))))
r))
(defmacro scroll [opts & body] `(scroll* ~opts (fn [~'_id ~'_rect] ~@body)))
(defn- picture
"The texture for either kind of source: a live `feed` or a file `src`."
[{:keys [feed src]}]
(cond feed (frames/lookup feed)
src (frames/from-file src)))
(defn image
"A picture: live pixels under `:feed`, or a file at `:src`.
One widget and not two, because everything downstream — the fit, the
bounds, the click — is the same for both. That is libvidya's arrangement
too, and frq writes [:image {:feed k}] for a call tile and
[:image {:src p}] for an attachment.
`:fit` gives it every point of the space it has been handed; otherwise it
asks for its own size, bounded by `:max-width` and `:max-height`. Either
way the picture keeps its shape — a 16:9 camera in a square tile is the
ordinary case, and stretching it is the one thing nobody wants.
A source with no picture yet — a feed before its first frame, a file that
will not decode — draws the empty tile and nothing else. Not a
broken-image glyph: the text beside it already says what it was meant to
be, and a tile that appears only once a frame lands rearranges the wall
under the person every time somebody joins."
([opts] (image opts {}))
([{:keys [feed src] :as source}
{:keys [fit max-width max-height size expand gravity placeholder?]
:or {gravity [0.5 0.5] placeholder? true}}]
(let [t (picture source)
[tw th*] (if t [(:w t) (:h t)] [0 0])
want (cond
size size
(and fit) [320.0 180.0]
(pos? tw)
(let [k (min (if max-width (/ (double max-width) tw) 1.0)
(if max-height (/ (double max-height) th*) 1.0))]
[(* tw k) (* th* k)])
:else [(double (or max-width 160)) (double (or max-height 90))])
rect (c/leaf (mapv double want)
(or expand (if fit :both :none))
gravity)]
(when placeholder? (c/fill! rect (c/th :surface-alt) 6.0))
(when (and t (:tex t)) (c/draw-picture! (:tex t) (:w t) (:h t) rect))
rect)))
(defn title-2
"A second-level heading — smaller than `title`, still bold."
[s]
(label s {:size (* 1.15 (c/th :font-size)) :colour (c/th :text)}))
(defn status
"A line of text with a dot in front of it saying whether the thing is live."
([s] (status s false))
([s live?]
(c/box* {:dir :horizontal :spacing 6 :gravity [0.0 0.5]}
(fn [_ _]
(let [line (c/th :font-size)
d (max 7.0 (min 12.0 (* line 0.55)))
r (c/leaf [(+ d 4.0) line] :none [0.0 0.5])
[x y w h] r]
(c/fill! [(+ x 2.0) (+ y (/ (- h d) 2.0)) d d]
(if live? (c/th :accent) (c/th :text-dim))
(/ d 2.0)))
(label s)))))
(defn spinner
"A turning mark, and a word beside it if there is one.
Turned from the wall clock rather than a frame counter: a loop that
dropped frames would otherwise show a spinner that stutters in a way that
reads as the work having stalled, which is the one thing it is there to
deny."
([] (spinner ""))
([s]
(c/box* {:dir :horizontal :spacing 6 :gravity [0.0 0.5]}
(fn [_ _]
(let [line (c/th :font-size)
r (c/leaf [line line] :none [0.0 0.5])
[x y w h] r
cx (+ x (/ w 2.0)) cy (+ y (/ h 2.0))
rad (* 0.4 (min w h))
t (/ (double (mod (System/currentTimeMillis) 1000)) 1000.0)
a (* t 2.0 Math/PI)]
;; Three ticks around a circle: enough to read as turning, and no
;; arc primitive needed.
(dotimes [i 3]
(let [th* (+ a (* i (/ (* 2.0 Math/PI) 3.0)))]
(c/fill! [(+ cx (* rad (Math/cos th*)) -1.5)
(+ cy (* rad (Math/sin th*)) -1.5) 3.0 3.0]
(c/th :accent) 1.5))))
(when (seq s) (label s))))))
(defn link
"Text that is a place to go. Answers true on the frame it was clicked."
([s] (link s {}))
([s {:keys [key]}]
(let [id (c/next-id key)
rect (label s {:colour (c/th :accent)})]
(:clicked? (c/interact! id rect)))))
(defn emoji
"One emoji, drawn as a character and sized to sit level with the words
either side.
In colour where there is a colour face to draw it from, and as text
where there is not.
The two paths differ in more than the picture: a colour emoji face is a
bitmap with one 128-pixel strike, so it cannot be a fallback font and is
drawn as an image scaled into the line — which means the glyph occupies a
SQUARE of the size asked for, decided here rather than measured. The text
path stays exactly what it was, a label in the UI font, and is what a
machine with no colour face gets."
([s] (emoji s nil))
([s size]
(let [sz (double (or size (c/th :font-size)))]
(if (c/colour-emoji? s)
(let [rect (c/leaf [sz sz] :none [0.5 0.5])]
(c/draw-emoji! s rect)
rect)
(label s {:size sz})))))
(defn avatar
"A round picture for somebody, or their initial on a colour if there is
none.
The fallback is not a placeholder to be replaced later — most people in
most rooms have no picture, so the initial IS the avatar, and its colour
comes from the name so the same person is the same colour everywhere."
([nick] (avatar nick {}))
([nick {:keys [src size] :or {size 24.0}}]
(let [rect (c/leaf [size size] :none [0.5 0.5])
t (when src (frames/from-file src))]
(if (and t (:tex t))
(c/draw-picture! (:tex t) (:w t) (:h t) rect)
(let [name (str nick)
bare (str/replace name #"^[#&@+%~]+" "")
initial (if (seq bare) (str/upper-case (subs bare 0 1)) "?")
[x y w h] rect]
(c/fill! rect (c/name-colour name) (/ size 2.0))
(let [sz (* size 0.5)
[tw th*] (c/measure initial sz)]
(c/draw-text! initial (+ x (/ (- w tw) 2.0)) (+ y (/ (- h th*) 2.0))
sz (c/th :accent-text)))))
rect)))
(defn reaction
"A tally wearing a pill: an emoji, how many people, and whether you are one
of them.
The same glyph `emoji` draws, and the same two ways of drawing it: a
colour face gives a square picture, and everything else is text. So the
glyph and the tally are measured and drawn apart rather than as one
string — a picture cannot be concatenated onto a number.
A reaction is not a character otherwise either: it answers the pointer
and it is a count. `mine?` is bordered rather than filled differently,
because the pill has to stay readable at the size a line of them ends
up."
([glyph] (reaction glyph {}))
([glyph {:keys [count mine? size key]
:or {count 0 mine? false}}]
(let [sz (double (or size (c/th :font-size)))
colour? (c/colour-emoji? (str glyph))
tally (when (pos? count) (str " " count))
[gw gh] (if colour? [sz sz] (c/measure (str glyph) sz))
[cw ch] (if tally (c/measure tally sz) [0.0 0.0])
tw (+ (double gw) (double cw))
th* (max (double gh) (double ch))
pad 6.0
rect (c/leaf [(+ tw (* 2 pad)) (+ th* 4.0)] :none [0.0 0.5])
id (c/next-id key)
[x y w h] rect]
(c/fill! rect (if mine? (c/th :press) (c/th :surface-alt))
(/ h 2.0)
(when mine? (c/th :accent)) (if mine? 1.0 0.0))
(if colour?
(c/draw-emoji! (str glyph) [(+ x pad) (+ y (/ (- h sz) 2.0)) sz sz])
(c/draw-text! (str glyph) (+ x pad) (+ y (/ (- h (double gh)) 2.0))
sz (c/th :text)))
(when tally
(c/draw-text! tally (+ x pad (double gw)) (+ y (/ (- h (double ch)) 2.0))
sz (c/th :text)))
(:clicked? (c/interact! id rect)))))
|