serge-native 0.1.2

Native runtime of serge: retained view tree, layout, scene collection, input and UI tasks
Documentation
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
//! Layout and scene collection: the retained tree becomes `taffy` input,
//! then a `ScenePrimitive` list in paint order (`docs/03-adapters-and-pipeline.md`
//! §1.2).

use serge_core::{Attr, Kind, PaintCmd, Point, Rect, ScenePrimitive};
use serge_native_layout::{LayoutNode, WidthHint, compute};
use serge_native_text::TextSystem;
use serge_style::{Color, Length, Style, Theme};

use crate::tree::{NKind, NNode, Tree};

/// The checkbox square's side.
pub(crate) const CHECK: f32 = 18.0;
/// The slider thumb's radius.
pub(crate) const THUMB: f32 = 9.0;

fn px(l: Length) -> f32 {
    l.px().unwrap_or(0.0)
}

/// The content box (inside padding and border).
pub(crate) fn content(n: &NNode) -> Rect {
    let s = &n.style;
    let bx = px(s.padding_x) + px(s.border_width);
    let by = px(s.padding_y) + px(s.border_width);
    Rect::new(
        n.rect.x + bx,
        n.rect.y + by,
        (n.rect.width - 2.0 * bx).max(0.0),
        (n.rect.height - 2.0 * by).max(0.0),
    )
}

fn label(n: &NNode) -> (&str, bool) {
    match n.kind {
        NKind::Widget(Kind::Input) if n.input.is_empty() => {
            (n.attr(Attr::Placeholder).map_or("", |v| v.as_str()), true)
        }
        NKind::Widget(Kind::Input) => (n.input.as_str(), false),
        _ => (n.text(), false),
    }
}

fn measured(kind: NKind) -> bool {
    matches!(
        kind,
        NKind::Widget(Kind::Text | Kind::Link | Kind::Button | Kind::Input | Kind::Checkbox)
    )
}

/// Lays out the tree in `width` × `height`; returns canvases whose size
/// changed (their `bounds` signals are set by the caller, outside the
/// borrow).
pub(crate) fn layout(tree: &mut Tree, text: &mut TextSystem) -> Vec<(usize, Rect)> {
    let (width, height) = tree.size;
    // Flatten groups: layout sees widgets only.
    let mut nodes: Vec<LayoutNode> = Vec::new();
    let mut ids: Vec<usize> = Vec::new();
    fn flat_children(tree: &Tree, id: usize, out: &mut Vec<usize>) {
        if let Some(n) = tree.node(id) {
            for &c in &n.children {
                match tree.node(c).map(|n| n.kind) {
                    Some(NKind::Group) => flat_children(tree, c, out),
                    Some(_) => out.push(c),
                    None => {}
                }
            }
        }
    }
    fn add(tree: &Tree, id: usize, nodes: &mut Vec<LayoutNode>, ids: &mut Vec<usize>) -> usize {
        let n = tree.node(id).expect("alive");
        let index = nodes.len();
        nodes.push(LayoutNode {
            style: n.style.clone(),
            measured: measured(n.kind),
            children: Vec::new(),
        });
        ids.push(id);
        let mut kids = Vec::new();
        flat_children(tree, id, &mut kids);
        let children = kids.into_iter().map(|c| add(tree, c, nodes, ids)).collect();
        nodes[index].children = children;
        index
    }
    let root = tree.root;
    if let Some(r) = tree.node_mut(root) {
        r.style = Style {
            padding_x: Length::Px(0.0),
            padding_y: Length::Px(0.0),
            ..Style::default()
        };
    }
    add(tree, root, &mut nodes, &mut ids);
    let boxes = compute(&nodes, 0, width, Some(height), |i, hint| {
        let n = tree.node(ids[i]).expect("alive");
        let s = &n.style;
        let max = match hint {
            WidthHint::Exact(w) | WidthHint::AtMost(w) => Some(w),
            WidthHint::Min => Some(1.0),
            WidthHint::Max => None,
        };
        match n.kind {
            NKind::Widget(Kind::Checkbox) => {
                let (w, h) = text.measure(
                    n.text(),
                    s.font_px(),
                    s.font_weight,
                    max.map(|m| (m - CHECK - px(s.gap)).max(1.0)),
                );
                (CHECK + px(s.gap) + w, h.max(CHECK))
            }
            NKind::Widget(Kind::Input) => {
                let (t, _) = label(n);
                let (w, h) = text.measure(
                    if t.is_empty() { " " } else { t },
                    s.font_px(),
                    s.font_weight,
                    None,
                );
                (w + 2.0, h)
            }
            _ => {
                let (t, _) = label(n);
                text.measure(t, s.font_px(), s.font_weight, max)
            }
        }
    });
    // Absolute rects.
    let mut resized = Vec::new();
    #[allow(clippy::too_many_arguments)]
    fn place(
        tree: &mut Tree,
        nodes: &[LayoutNode],
        ids: &[usize],
        boxes: &[serge_native_layout::LayoutBox],
        i: usize,
        ox: f32,
        oy: f32,
        resized: &mut Vec<(usize, Rect)>,
    ) {
        let b = boxes[i];
        let rect = Rect::new(ox + b.x, oy + b.y, b.width, b.height);
        if let Some(n) = tree.node_mut(ids[i]) {
            n.rect = rect;
            if let Some(c) = &n.canvas {
                let local = Rect::new(0.0, 0.0, rect.width, rect.height);
                if c.bounds.get_untracked() != local {
                    resized.push((ids[i], local));
                }
            }
        }
        for &c in &nodes[i].children {
            place(tree, nodes, ids, boxes, c, rect.x, rect.y, resized);
        }
    }
    place(
        tree,
        &nodes,
        &ids,
        &boxes,
        0,
        tree.origin.0,
        tree.origin.1,
        &mut resized,
    );
    tree.content_height = boxes[0].height;
    let max_scroll = (tree.content_height - height).max(0.0);
    tree.scroll_y = tree.scroll_y.clamp(0.0, max_scroll);
    resized
}

/// The primitives of the whole window.
pub(crate) fn collect(tree: &Tree, text: &mut TextSystem, theme: &Theme) -> Vec<ScenePrimitive> {
    let (w, h) = tree.size;
    let mut out = vec![ScenePrimitive::Quad {
        rect: Rect::new(tree.origin.0, tree.origin.1, w, h),
        color: theme.background,
        radius: 0.0,
    }];
    let dy = -tree.scroll_y;
    visit(tree, tree.root, text, theme, dy, &mut out);
    out
}

fn shift(r: Rect, dy: f32) -> Rect {
    r.offset(0.0, dy)
}

fn visit(
    tree: &Tree,
    id: usize,
    text: &mut TextSystem,
    theme: &Theme,
    dy: f32,
    out: &mut Vec<ScenePrimitive>,
) {
    let Some(n) = tree.node(id) else { return };
    if let NKind::Widget(kind) = n.kind {
        draw(tree, id, n, kind, text, theme, dy, out);
    }
    for &c in &n.children {
        visit(tree, c, text, theme, dy, out);
    }
    if matches!(n.kind, NKind::Widget(Kind::Canvas)) {
        out.push(ScenePrimitive::PopClip);
    }
}

#[allow(clippy::too_many_arguments)]
fn draw(
    tree: &Tree,
    id: usize,
    n: &NNode,
    kind: Kind,
    text: &mut TextSystem,
    theme: &Theme,
    dy: f32,
    out: &mut Vec<ScenePrimitive>,
) {
    let s = &n.style;
    let rect = shift(n.rect, dy);
    let radius = px(s.radius);
    let hovered = tree.hover == Some(id);
    let pressed = tree.pressed == Some(id) && hovered;
    let disabled = n.flag(Attr::Disabled);
    let mut bg = s.background;
    if kind == Kind::Button && !disabled {
        if pressed {
            bg = bg.mix(Color::BLACK, 0.18);
        } else if hovered {
            bg = bg.mix(Color::BLACK, 0.08);
        }
    }
    if disabled {
        bg = bg.with_alpha(f32::from(bg.a) / 255.0 * 0.5);
    }
    if bg.a > 0 && kind != Kind::Slider {
        out.push(ScenePrimitive::Quad {
            rect,
            color: bg,
            radius,
        });
    }
    let focused = tree.focus == Some(id);
    let (bw, bc) = if focused && n.focusable() {
        (2.0, theme.primary)
    } else {
        (px(s.border_width), s.border_color)
    };
    if bw > 0.0 && bc.a > 0 {
        out.push(ScenePrimitive::Border {
            rect,
            color: bc,
            width: bw,
            radius,
        });
    }
    let c = shift(content(n), dy);
    let size = s.font_px();
    let weight = s.font_weight;
    match kind {
        Kind::View | Kind::Stack | Kind::Card => {}
        Kind::Text | Kind::Link => {
            let l = text.layout_ref(n.text(), size, weight, Some(c.width.max(1.0)));
            out.extend(l.primitives(Point::new(c.x, c.y), s.color));
            if kind == Kind::Link && hovered {
                out.push(ScenePrimitive::Quad {
                    rect: Rect::new(c.x, c.y + l.height - 2.0, l.width, 1.0),
                    color: s.color,
                    radius: 0.0,
                });
            }
        }
        Kind::Button => {
            let l = text.layout_ref(n.text(), size, weight, Some(c.width.max(1.0)));
            let origin = Point::new(
                c.x + (c.width - l.width) / 2.0,
                c.y + (c.height - l.height) / 2.0,
            );
            let color = if disabled {
                s.color.with_alpha(0.6)
            } else {
                s.color
            };
            out.extend(l.primitives(origin, color));
        }
        Kind::Input => {
            let (t, placeholder) = label(n);
            let l = text.layout_ref(t, size, weight, None);
            let y = c.y + (c.height - l.height) / 2.0;
            out.push(ScenePrimitive::PushClip(c));
            out.extend(l.primitives(
                Point::new(c.x, y),
                if placeholder { theme.muted } else { s.color },
            ));
            if focused {
                let x = if placeholder {
                    c.x
                } else {
                    c.x + l.width + 1.0
                };
                out.push(ScenePrimitive::Quad {
                    rect: Rect::new(x, y + 2.0, 1.5, (l.height - 4.0).max(8.0)),
                    color: s.color,
                    radius: 0.0,
                });
            }
            out.push(ScenePrimitive::PopClip);
        }
        Kind::Checkbox => {
            let checked = n.flag(Attr::Checked);
            let b = Rect::new(c.x, c.y + (c.height - CHECK) / 2.0, CHECK, CHECK);
            if checked {
                out.push(ScenePrimitive::Quad {
                    rect: b,
                    color: theme.primary,
                    radius: 4.0,
                });
                out.push(ScenePrimitive::Line {
                    points: vec![
                        Point::new(b.x + 4.0, b.y + 9.5),
                        Point::new(b.x + 7.5, b.y + 13.0),
                        Point::new(b.x + 14.0, b.y + 5.5),
                    ],
                    color: theme.on_primary,
                    width: 2.0,
                });
            } else {
                out.push(ScenePrimitive::Quad {
                    rect: b,
                    color: theme.surface,
                    radius: 4.0,
                });
                out.push(ScenePrimitive::Border {
                    rect: b,
                    color: if hovered { theme.primary } else { theme.border },
                    width: 1.5,
                    radius: 4.0,
                });
            }
            let x = c.x + CHECK + px(s.gap);
            let l = text.layout_ref(n.text(), size, weight, Some((c.right() - x).max(1.0)));
            out.extend(l.primitives(Point::new(x, c.y + (c.height - l.height) / 2.0), s.color));
        }
        Kind::Slider => {
            let (lo, hi) = (n.num(Attr::Min, 0.0), n.num(Attr::Max, 1.0));
            let v = n.num(Attr::Value, lo);
            let t = if hi > lo {
                ((v - lo) / (hi - lo)).clamp(0.0, 1.0)
            } else {
                0.0
            };
            let track = slider_track(n, dy);
            let cy = track.y + track.height / 2.0;
            out.push(ScenePrimitive::Quad {
                rect: track,
                color: theme.border,
                radius: 2.0,
            });
            let fx = track.x + t * track.width;
            out.push(ScenePrimitive::Quad {
                rect: Rect::new(track.x, track.y, fx - track.x, track.height),
                color: theme.primary,
                radius: 2.0,
            });
            let thumb = Rect::new(fx - THUMB, cy - THUMB, THUMB * 2.0, THUMB * 2.0);
            out.push(ScenePrimitive::Quad {
                rect: thumb,
                color: theme.surface,
                radius: THUMB,
            });
            out.push(ScenePrimitive::Border {
                rect: thumb,
                color: theme.primary,
                width: if hovered || tree.drag == Some(id) {
                    3.0
                } else {
                    2.0
                },
                radius: THUMB,
            });
        }
        Kind::ProgressBar => {
            let v = n.num(Attr::Value, 0.0).clamp(0.0, 1.0);
            out.push(ScenePrimitive::Quad {
                rect: Rect::new(rect.x, rect.y, rect.width * v, rect.height),
                color: theme.primary,
                radius,
            });
        }
        Kind::Canvas => {
            out.push(ScenePrimitive::PushClip(rect));
            if let Some(slot) = &n.viewport {
                let size = serge_core::viewport::Extent::pixels(rect, tree.scale);
                out.push(ScenePrimitive::ExternalTexture {
                    key: slot.frame(size),
                    rect,
                });
            }
            if let Some(cv) = &n.canvas {
                for cmd in &cv.commands {
                    paint_cmd(cmd, rect.x, rect.y, text, out);
                }
            }
            // PopClip after the children (see `visit`).
        }
    }
}

/// The slider's track in window coordinates.
pub(crate) fn slider_track(n: &NNode, dy: f32) -> Rect {
    let c = content(n);
    let cy = c.y + c.height / 2.0 + dy;
    Rect::new(c.x + THUMB, cy - 2.0, (c.width - 2.0 * THUMB).max(1.0), 4.0)
}

fn paint_cmd(
    cmd: &PaintCmd,
    ox: f32,
    oy: f32,
    text: &mut TextSystem,
    out: &mut Vec<ScenePrimitive>,
) {
    let p = |q: &Point| Point::new(q.x + ox, q.y + oy);
    match cmd {
        PaintCmd::FillRect {
            rect,
            color,
            radius,
        } => out.push(ScenePrimitive::Quad {
            rect: rect.offset(ox, oy),
            color: *color,
            radius: *radius,
        }),
        PaintCmd::StrokeRect { rect, color, width } => out.push(ScenePrimitive::Border {
            rect: rect.offset(ox, oy),
            color: *color,
            width: *width,
            radius: 0.0,
        }),
        PaintCmd::Polyline {
            points,
            color,
            width,
        } => out.push(ScenePrimitive::Line {
            points: points.iter().map(p).collect(),
            color: *color,
            width: *width,
        }),
        PaintCmd::FillPolygon { points, color } => out.push(ScenePrimitive::Polygon {
            points: points.iter().map(p).collect(),
            color: *color,
        }),
        PaintCmd::Text {
            origin,
            text: t,
            size,
            color,
        } => {
            let l = text.layout_ref(t, *size, 400, None);
            out.extend(l.primitives(p(origin), *color));
        }
    }
}

/// The interactive widget under `(x, y)` (window coordinates), innermost
/// first.
pub(crate) fn hit(tree: &Tree, x: f32, y: f32) -> Option<usize> {
    let p = Point::new(x, y + tree.scroll_y);
    let mut found = None;
    tree.walk(tree.root, &mut |id, n| {
        if n.focusable() && n.rect.contains(p) {
            found = Some(id);
        }
    });
    found
}