lgui-core 0.2.0

Platform-neutral runtime and UI primitives for LGUI
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
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
use super::{primitive::*, scene::*, scroll_cache::*, signature::*, transform::*, *};

pub fn compile_scene(tree: &HostTree) -> Scene {
    let mut list = Scene::new();
    let mut skip = HashSet::new();
    for node in tree.nodes() {
        if skip.contains(&node.id) {
            continue;
        }
        let command_start = list.commands.len();
        let include_popup_subtree = node.render_phase == RenderPhase::Popup;
        if node.shadow.is_some() {
            push_node_and_children(
                tree,
                node,
                Arc::make_mut(&mut list.commands),
                &mut skip,
                include_popup_subtree,
            );
            translate_popup_root_commands(&mut list, tree, node, command_start);
            continue;
        }
        if matches!(
            node.kind,
            UiNodeKind::CompositingLayer
                | UiNodeKind::StaticLayer
                | UiNodeKind::ScrollRaster
                | UiNodeKind::Clip
                | UiNodeKind::ClipPath
        ) {
            if let Some(spec) = node.compositing_layer {
                let (commands, content_signature) = compile_compositing_layer_commands(
                    tree,
                    node,
                    &mut skip,
                    include_popup_subtree,
                );
                list.push(ScenePrimitive::CompositingLayer {
                    id: node.id.clone(),
                    rect: node.layout_rect,
                    spec,
                    commands,
                    content_signature,
                    phase: node.render_phase,
                });
            }
            if let Some(spec) = node.static_layer.clone() {
                let commands =
                    compile_static_layer_commands(tree, node, &mut skip, include_popup_subtree);
                let child_signature = command_signature(&commands);
                list.push(ScenePrimitive::StaticLayer {
                    id: node.id.clone(),
                    rect: node.layout_rect,
                    spec,
                    commands,
                    child_signature,
                    phase: node.render_phase,
                });
            }
            if let Some(spec) = node.scroll_raster.clone() {
                let (commands, child_signature) = compile_scroll_raster_commands(
                    tree,
                    node,
                    &spec,
                    &mut skip,
                    include_popup_subtree,
                );
                list.push(ScenePrimitive::ScrollRaster {
                    id: node.id.clone(),
                    viewport: node.layout_rect,
                    spec,
                    commands,
                    child_signature,
                    phase: node.render_phase,
                });
            }
            if node.kind == UiNodeKind::Clip {
                let commands = compile_clip_commands(tree, node, &mut skip, include_popup_subtree);
                let child_signature = command_signature(&commands);
                list.push(ScenePrimitive::Clip {
                    id: node.id.clone(),
                    rect: node.clip_rect.unwrap_or(node.layout_rect),
                    commands,
                    child_signature,
                    phase: node.render_phase,
                });
            }
            if node.kind == UiNodeKind::ClipPath {
                if let Some(path) = node.path.clone() {
                    let commands =
                        compile_clip_commands(tree, node, &mut skip, include_popup_subtree);
                    let child_signature = command_signature(&commands);
                    list.push(ScenePrimitive::ClipPath {
                        id: node.id.clone(),
                        rect: node.clip_rect.unwrap_or(node.layout_rect),
                        path,
                        commands,
                        child_signature,
                        phase: node.render_phase,
                    });
                }
            }
            translate_popup_root_commands(&mut list, tree, node, command_start);
            continue;
        }
        if matches!(node.kind, UiNodeKind::Group | UiNodeKind::Root) {
            continue;
        }
        push_node_commands(&mut list, node);
        translate_popup_root_commands(&mut list, tree, node, command_start);
    }
    list.move_popup_commands_to_end();
    list
}

/// Returns the retained scene roots in backend paint order without compiling their primitives.
/// Descendants owned by clip/static/raster containers are represented by that container root;
/// popup descendants remain independent roots so they preserve popup z-order semantics.
pub fn scene_root_ids(tree: &HostTree) -> Vec<UiId> {
    let mut roots = Vec::new();
    let mut skip = HashSet::new();
    for node in tree.nodes() {
        if skip.contains(&node.id) {
            continue;
        }
        if node.shadow.is_some()
            || matches!(
                node.kind,
                UiNodeKind::CompositingLayer
                    | UiNodeKind::StaticLayer
                    | UiNodeKind::ScrollRaster
                    | UiNodeKind::Clip
                    | UiNodeKind::ClipPath
            )
        {
            roots.push(node.id.clone());
            mark_node_and_descendants_skipped(
                tree,
                node,
                &mut skip,
                node.render_phase == RenderPhase::Popup,
            );
        } else if !matches!(node.kind, UiNodeKind::Group | UiNodeKind::Root) {
            roots.push(node.id.clone());
        }
    }
    roots.sort_by_key(|id| {
        tree.node(id)
            .is_some_and(|node| node.render_phase == RenderPhase::Popup)
    });
    roots
}

/// Compiles one retained scene root. Normal commits call this only for inserted or paint-dirty
/// roots; unchanged roots keep their previously committed primitive vectors.
pub fn compile_scene_root(tree: &HostTree, id: &UiId) -> Scene {
    let Some(node) = tree.node(id) else {
        return Scene::new();
    };
    let mut list = Scene::new();
    let mut skip = HashSet::new();
    let include_popup_subtree = node.render_phase == RenderPhase::Popup;
    if node.shadow.is_some() {
        push_node_and_children(
            tree,
            node,
            Arc::make_mut(&mut list.commands),
            &mut skip,
            include_popup_subtree,
        );
        translate_popup_root_commands(&mut list, tree, node, 0);
        return list;
    }
    if matches!(
        node.kind,
        UiNodeKind::CompositingLayer
            | UiNodeKind::StaticLayer
            | UiNodeKind::ScrollRaster
            | UiNodeKind::Clip
            | UiNodeKind::ClipPath
    ) {
        if let Some(spec) = node.compositing_layer {
            let (commands, content_signature) =
                compile_compositing_layer_commands(tree, node, &mut skip, include_popup_subtree);
            list.push(ScenePrimitive::CompositingLayer {
                id: node.id.clone(),
                rect: node.layout_rect,
                spec,
                commands,
                content_signature,
                phase: node.render_phase,
            });
        }
        if let Some(spec) = node.static_layer.clone() {
            let commands =
                compile_static_layer_commands(tree, node, &mut skip, include_popup_subtree);
            let child_signature = command_signature(&commands);
            list.push(ScenePrimitive::StaticLayer {
                id: node.id.clone(),
                rect: node.layout_rect,
                spec,
                commands,
                child_signature,
                phase: node.render_phase,
            });
        }
        if let Some(spec) = node.scroll_raster.clone() {
            let (commands, child_signature) =
                compile_scroll_raster_commands(tree, node, &spec, &mut skip, include_popup_subtree);
            list.push(ScenePrimitive::ScrollRaster {
                id: node.id.clone(),
                viewport: node.layout_rect,
                spec,
                commands,
                child_signature,
                phase: node.render_phase,
            });
        }
        if node.kind == UiNodeKind::Clip {
            let commands = compile_clip_commands(tree, node, &mut skip, include_popup_subtree);
            let child_signature = command_signature(&commands);
            list.push(ScenePrimitive::Clip {
                id: node.id.clone(),
                rect: node.clip_rect.unwrap_or(node.layout_rect),
                commands,
                child_signature,
                phase: node.render_phase,
            });
        }
        if node.kind == UiNodeKind::ClipPath {
            if let Some(path) = node.path.clone() {
                let commands = compile_clip_commands(tree, node, &mut skip, include_popup_subtree);
                let child_signature = command_signature(&commands);
                list.push(ScenePrimitive::ClipPath {
                    id: node.id.clone(),
                    rect: node.clip_rect.unwrap_or(node.layout_rect),
                    path,
                    commands,
                    child_signature,
                    phase: node.render_phase,
                });
            }
        }
    } else if !matches!(node.kind, UiNodeKind::Group | UiNodeKind::Root) {
        push_node_commands(&mut list, node);
    }
    translate_popup_root_commands(&mut list, tree, node, 0);
    list
}

fn translate_popup_root_commands(
    list: &mut Scene,
    tree: &HostTree,
    node: &UiNode,
    command_start: usize,
) {
    if node.render_phase != RenderPhase::Popup {
        return;
    }
    let (offset_x, offset_y) = tree.ancestor_content_offset(node);
    if offset_x == 0.0 && offset_y == 0.0 {
        return;
    }
    for command in &mut Arc::make_mut(&mut list.commands)[command_start..] {
        *command = translate_command(command, offset_x, offset_y);
    }
}

fn compile_static_layer_commands(
    tree: &HostTree,
    root: &UiNode,
    skip: &mut HashSet<UiId>,
    include_popup_subtree: bool,
) -> Vec<ScenePrimitive> {
    let mut commands = Vec::new();
    for child_id in &root.children {
        compile_static_layer_child(tree, child_id, &mut commands, skip, include_popup_subtree);
    }
    commands
}

fn compile_compositing_layer_commands(
    tree: &HostTree,
    root: &UiNode,
    skip: &mut HashSet<UiId>,
    include_popup_subtree: bool,
) -> (Vec<ScenePrimitive>, u64) {
    let commands = compile_static_layer_commands(tree, root, skip, include_popup_subtree);
    let commands = translate_commands(commands, -root.layout_rect.left, -root.layout_rect.top);
    let content_signature = command_signature(&commands);
    (commands, content_signature)
}

fn compile_scroll_raster_commands(
    tree: &HostTree,
    root: &UiNode,
    spec: &ScrollRasterSpec,
    skip: &mut HashSet<UiId>,
    include_popup_subtree: bool,
) -> (Vec<ScenePrimitive>, u64) {
    if let Some(snapshot) = load_scroll_raster_command_snapshot(root, spec) {
        mark_node_and_descendants_skipped(tree, root, skip, include_popup_subtree);
        return snapshot;
    }
    let commands = compile_static_layer_commands(tree, root, skip, include_popup_subtree);
    let child_signature = command_signature(&commands);
    store_scroll_raster_command_snapshot(root, spec, commands.clone(), child_signature);
    (commands, child_signature)
}

fn mark_node_and_descendants_skipped(
    tree: &HostTree,
    node: &UiNode,
    skip: &mut HashSet<UiId>,
    include_popup_subtree: bool,
) {
    skip.insert(node.id.clone());
    for child_id in &node.children {
        if let Some(child) = tree.node(child_id) {
            if child.render_phase == RenderPhase::Popup && !include_popup_subtree {
                continue;
            }
            mark_node_and_descendants_skipped(tree, child, skip, include_popup_subtree);
        }
    }
}

fn compile_static_layer_child(
    tree: &HostTree,
    id: &UiId,
    commands: &mut Vec<ScenePrimitive>,
    skip: &mut HashSet<UiId>,
    include_popup_subtree: bool,
) {
    let Some(node) = tree.node(id) else {
        return;
    };
    if node.render_phase == RenderPhase::Popup && !include_popup_subtree {
        return;
    }
    skip.insert(node.id.clone());
    push_node_and_children(tree, node, commands, skip, include_popup_subtree);
}

fn compile_clip_commands(
    tree: &HostTree,
    root: &UiNode,
    skip: &mut HashSet<UiId>,
    include_popup_subtree: bool,
) -> Vec<ScenePrimitive> {
    let mut commands = Vec::new();
    let (offset_x, offset_y) = root.content_offset;
    for child_id in &root.children {
        compile_clip_child(
            tree,
            child_id,
            &mut commands,
            skip,
            offset_x,
            offset_y,
            include_popup_subtree,
        );
    }
    commands
}

fn compile_clip_child(
    tree: &HostTree,
    id: &UiId,
    commands: &mut Vec<ScenePrimitive>,
    skip: &mut HashSet<UiId>,
    offset_x: f32,
    offset_y: f32,
    include_popup_subtree: bool,
) {
    let Some(node) = tree.node(id) else {
        return;
    };
    if node.render_phase == RenderPhase::Popup && !include_popup_subtree {
        return;
    }
    let before = commands.len();
    push_node_and_children(tree, node, commands, skip, include_popup_subtree);
    for command in &mut commands[before..] {
        *command = translate_command(command, offset_x, offset_y);
    }
}

fn push_node_and_children(
    tree: &HostTree,
    node: &UiNode,
    commands: &mut Vec<ScenePrimitive>,
    skip: &mut HashSet<UiId>,
    include_popup_subtree: bool,
) {
    if node.render_phase == RenderPhase::Popup && !include_popup_subtree {
        return;
    }
    skip.insert(node.id.clone());
    if let Some(shadow) = node.shadow {
        let mut source = node.clone();
        source.shadow = None;
        let mut nested = Vec::new();
        push_node_and_children(tree, &source, &mut nested, skip, include_popup_subtree);
        let Some(content_bounds) = nested
            .iter()
            .map(ScenePrimitive::paint_bounds)
            .reduce(UiRect::union)
        else {
            return;
        };
        // A shadowed compositing node needs two independent retained surfaces.
        for command in &mut nested {
            if let ScenePrimitive::CompositingLayer { id, .. } = command {
                if *id == node.id {
                    *id = UiId::owned(format!("{}.__shadow_content", node.id.as_str()));
                }
            }
        }
        let rect = shadow.paint_bounds(content_bounds.union(node.paint_bounds));
        let rect = UiRect::new(
            rect.left.floor(),
            rect.top.floor(),
            rect.right.ceil(),
            rect.bottom.ceil(),
        );
        let nested = translate_commands(nested, -rect.left, -rect.top);
        let content_signature = command_signature(&nested);
        let mut spec = CompositingLayerSpec::new();
        spec.shadow = Some(shadow);
        commands.push(ScenePrimitive::CompositingLayer {
            id: node.id.clone(),
            rect,
            spec,
            commands: nested,
            content_signature,
            phase: node.render_phase,
        });
        return;
    }
    if node.kind == UiNodeKind::Clip {
        let nested = compile_clip_commands(tree, node, skip, include_popup_subtree);
        let child_signature = command_signature(&nested);
        commands.push(ScenePrimitive::Clip {
            id: node.id.clone(),
            rect: node.clip_rect.unwrap_or(node.layout_rect),
            commands: nested,
            child_signature,
            phase: node.render_phase,
        });
        return;
    }
    if node.kind == UiNodeKind::ClipPath {
        if let Some(path) = node.path.clone() {
            let nested = compile_clip_commands(tree, node, skip, include_popup_subtree);
            let child_signature = command_signature(&nested);
            commands.push(ScenePrimitive::ClipPath {
                id: node.id.clone(),
                rect: node.clip_rect.unwrap_or(node.layout_rect),
                path,
                commands: nested,
                child_signature,
                phase: node.render_phase,
            });
        }
        return;
    }
    if let UiNodeKind::CompositingLayer = node.kind {
        if let Some(spec) = node.compositing_layer {
            let (layer_commands, content_signature) =
                compile_compositing_layer_commands(tree, node, skip, include_popup_subtree);
            commands.push(ScenePrimitive::CompositingLayer {
                id: node.id.clone(),
                rect: node.layout_rect,
                spec,
                commands: layer_commands,
                content_signature,
                phase: node.render_phase,
            });
        }
        return;
    }
    if let UiNodeKind::StaticLayer = node.kind {
        if let Some(spec) = node.static_layer.clone() {
            let layer_commands =
                compile_static_layer_commands(tree, node, skip, include_popup_subtree);
            let child_signature = command_signature(&layer_commands);
            commands.push(ScenePrimitive::StaticLayer {
                id: node.id.clone(),
                rect: node.layout_rect,
                spec,
                commands: layer_commands,
                child_signature,
                phase: node.render_phase,
            });
        }
        return;
    }
    if let UiNodeKind::ScrollRaster = node.kind {
        if let Some(spec) = node.scroll_raster.clone() {
            let (raster_commands, child_signature) =
                compile_scroll_raster_commands(tree, node, &spec, skip, include_popup_subtree);
            commands.push(ScenePrimitive::ScrollRaster {
                id: node.id.clone(),
                viewport: node.layout_rect,
                spec,
                commands: raster_commands,
                child_signature,
                phase: node.render_phase,
            });
        }
        return;
    }
    if !matches!(
        node.kind,
        UiNodeKind::Group
            | UiNodeKind::Root
            | UiNodeKind::CompositingLayer
            | UiNodeKind::StaticLayer
            | UiNodeKind::ScrollRaster
            | UiNodeKind::ClipPath
    ) {
        push_node_commands_vec(commands, node);
    }
    for child_id in &node.children {
        let Some(child) = tree.node(child_id) else {
            continue;
        };
        if child.render_phase == RenderPhase::Popup && !include_popup_subtree {
            continue;
        }
        push_node_and_children(tree, child, commands, skip, include_popup_subtree);
    }
}

fn push_node_commands(list: &mut Scene, node: &UiNode) {
    push_node_commands_into(|command| list.push(command), node);
}

fn push_node_commands_vec(commands: &mut Vec<ScenePrimitive>, node: &UiNode) {
    push_node_commands_into(|command| commands.push(command), node);
}

fn push_node_commands_into(mut push: impl FnMut(ScenePrimitive), node: &UiNode) {
    if !matches!(node.kind, UiNodeKind::Ellipse)
        && (node.style.fill.is_some() || node.style.stroke.is_some())
    {
        push(ScenePrimitive::Rect {
            id: node.id.clone(),
            rect: node.layout_rect,
            style: node.style,
            phase: node.render_phase,
        });
    }
    if let UiNodeKind::Ellipse = node.kind {
        if node.style.fill.is_some() || node.style.stroke.is_some() {
            push(ScenePrimitive::Ellipse {
                id: node.id.clone(),
                rect: node.layout_rect,
                style: node.style,
                phase: node.render_phase,
            });
        }
    }
    if let (Some(text), Some(style)) = (node.text.clone(), node.text_style) {
        push(ScenePrimitive::Text {
            id: node.id.clone(),
            rect: node.layout_rect,
            text,
            style,
            phase: node.render_phase,
        });
    }
    if let UiNodeKind::Custom(key) = node.kind {
        push(ScenePrimitive::Custom {
            id: node.id.clone(),
            rect: node.layout_rect,
            key,
            style: node.custom_style,
            phase: node.render_phase,
        });
    }
    if let UiNodeKind::Line = node.kind {
        if let Some(stroke) = node.style.stroke {
            push(ScenePrimitive::Line {
                id: node.id.clone(),
                start: super::Point::new(node.layout_rect.left, node.layout_rect.top),
                end: super::Point::new(node.layout_rect.right, node.layout_rect.bottom),
                stroke,
                phase: node.render_phase,
            });
        }
    }
    if let UiNodeKind::Path = node.kind {
        if let Some(path) = node.path.clone() {
            push(ScenePrimitive::Path {
                id: node.id.clone(),
                rect: node.layout_rect,
                path,
                style: node.path_style,
                phase: node.render_phase,
            });
        }
    }
    if let UiNodeKind::Image = node.kind {
        if let Some(request) = node.image_request.as_ref() {
            push(ScenePrimitive::Image {
                id: node.id.clone(),
                rect: node.layout_rect,
                source: request.source().clone(),
                request: request.clone(),
                fit: node.image_fit,
                phase: node.render_phase,
            });
        }
    }
    if let UiNodeKind::Icon = node.kind {
        if let Some(key) = node.icon_key {
            push(ScenePrimitive::Icon {
                id: node.id.clone(),
                rect: node.layout_rect,
                key,
                style: node.icon_style,
                phase: node.render_phase,
            });
        }
    }
    if let UiNodeKind::Glow = node.kind {
        if let Some((color, alpha)) = node.glow {
            push(ScenePrimitive::Glow {
                id: node.id.clone(),
                rect: node.layout_rect,
                color,
                alpha,
                phase: node.render_phase,
            });
        }
    }
    if let UiNodeKind::BackdropBlur = node.kind {
        if let Some(style) = node.backdrop_blur_style {
            push(ScenePrimitive::BackdropBlur {
                id: node.id.clone(),
                rect: node.layout_rect,
                style,
                phase: node.render_phase,
            });
        }
    }
    if let UiNodeKind::BackdropBlurPath = node.kind {
        if let (Some(style), Some(path)) = (node.backdrop_blur_style, node.path.clone()) {
            push(ScenePrimitive::BackdropBlurPath {
                id: node.id.clone(),
                rect: node.layout_rect,
                path,
                style,
                phase: node.render_phase,
            });
        }
    }
    if let UiNodeKind::Overlay = node.kind {
        if let Some(style) = node.overlay_style.clone() {
            push(ScenePrimitive::Overlay {
                id: node.id.clone(),
                rect: node.layout_rect,
                style,
                phase: node.render_phase,
            });
        }
    }
}