burin 0.1.2

A Rust GUI framework built on the auralis reactive signal kernel
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
//! Property-based differential harness for the incremental layout engine.
//!
//! Baseline phase (Task 2): laying out the SAME randomly-generated widget tree
//! twice must produce identical bounds (determinism). Later tasks compare
//! incremental-vs-full layout after mutation.

use proptest::prelude::*;

use crate::core::context::MountContext;
use crate::core::element::ElementId;
use crate::core::widget::Widget;
use crate::layout::taffy_bridge::TaffyBridge;
use crate::style::Rect;
use crate::style::{Dimension, Styled};
use crate::testing::TestHarness;
use crate::widgets::display::Text;
use crate::widgets::layout::{HStack, SizedBox, VStack};
use auralis_signal::Signal;
use std::collections::HashMap;

/// Type-erased wrapper so recursively-built `Box<dyn Widget>` children can be
/// pushed into stacks whose `push`/`child` take `impl Widget + 'static`.
struct BoxedWidget(Box<dyn Widget>);
impl Widget for BoxedWidget {
    fn mount_box(self: Box<Self>, ctx: &mut MountContext<'_>) -> ElementId {
        self.0.mount_box(ctx)
    }
}

#[derive(Debug, Clone, Copy)]
enum Dim {
    Fixed(f32),
    Pct(f32),
    Auto,
}

#[derive(Debug, Clone)]
enum Node {
    Leaf {
        w: f32,
        h: f32,
        grow: bool,
    },
    Stack {
        vertical: bool,
        w: Dim,
        h: Dim,
        gap: f32,
        grow: bool,
        min_w: Option<f32>,
        children: Vec<Node>,
    },
    Grid {
        cols: u32,
        grow: bool,
        children: Vec<Node>,
    },
}

fn arb_dim() -> impl Strategy<Value = Dim> {
    prop_oneof![
        (40f32..300.0).prop_map(Dim::Fixed),
        (0.3f32..1.0).prop_map(Dim::Pct),
        Just(Dim::Auto),
    ]
}

fn arb_node() -> impl Strategy<Value = Node> {
    let leaf = (20f32..120.0, 16f32..48.0, prop::bool::weighted(0.25))
        .prop_map(|(w, h, grow)| Node::Leaf { w, h, grow });
    leaf.prop_recursive(4, 40, 4, |inner| {
        prop_oneof![
            (
                any::<bool>(),
                arb_dim(),
                arb_dim(),
                0f32..12.0,
                prop::bool::weighted(0.25),
                prop::option::weighted(0.3, 40f32..160.0),
                prop::collection::vec(inner.clone(), 1..4)
            )
                .prop_map(|(vertical, w, h, gap, grow, min_w, children)| Node::Stack {
                    vertical,
                    w,
                    h,
                    gap,
                    grow,
                    min_w,
                    children
                }),
            (
                2u32..4,
                prop::bool::weighted(0.25),
                prop::collection::vec(inner, 1..6)
            )
                .prop_map(|(cols, grow, children)| Node::Grid {
                    cols,
                    grow,
                    children
                }),
        ]
    })
}

fn to_dim(d: Dim) -> Dimension {
    match d {
        Dim::Fixed(px) => Dimension::Pixels(px),
        Dim::Pct(p) => Dimension::Percent(p),
        Dim::Auto => Dimension::Auto,
    }
}

fn maybe_grow(w: Box<dyn Widget>, grow: bool) -> Box<dyn Widget> {
    if grow {
        Box::new(crate::widgets::layout::Expanded::new(BoxedWidget(w)))
    } else {
        w
    }
}

fn build_widget(node: &Node) -> Box<dyn Widget> {
    match node {
        Node::Leaf { w, h, grow } => {
            let leaf: Box<dyn Widget> =
                Box::new(SizedBox::new().width(*w).height(*h).child(Text::new("x")));
            maybe_grow(leaf, *grow)
        }
        Node::Stack {
            vertical,
            w,
            h,
            gap,
            grow,
            min_w,
            children,
        } => {
            let stack: Box<dyn Widget> = if *vertical {
                let mut s = VStack::new();
                for c in children {
                    s = s.push(BoxedWidget(build_widget(c)));
                }
                s = s.width(to_dim(*w)).height(to_dim(*h)).gap(*gap);
                if let Some(mw) = min_w {
                    s = s.min_width(*mw);
                }
                Box::new(s)
            } else {
                let mut s = HStack::new();
                for c in children {
                    s = s.push(BoxedWidget(build_widget(c)));
                }
                s = s.width(to_dim(*w)).height(to_dim(*h)).gap(*gap);
                if let Some(mw) = min_w {
                    s = s.min_width(*mw);
                }
                Box::new(s)
            };
            maybe_grow(stack, *grow)
        }
        Node::Grid {
            cols,
            grow,
            children,
        } => {
            use crate::widgets::layout::{GridItem, GridRow};
            let mut g = GridRow::new().columns(*cols);
            for c in children {
                g = g.push(GridItem::new(BoxedWidget(build_widget(c))));
            }
            maybe_grow(Box::new(g), *grow)
        }
    }
}

/// Collect element bounds in deterministic DFS pre-order from the root.
fn ordered_bounds(h: &TestHarness) -> Vec<Rect> {
    fn walk(h: &TestHarness, id: ElementId, out: &mut Vec<Rect>) {
        if let Some(el) = h.find(id) {
            out.push(el.screen_bounds);
            let children = el.children.clone();
            for c in children {
                walk(h, c, out);
            }
        }
    }
    let mut out = Vec::new();
    walk(h, h.root_id(), &mut out);
    out
}

/// Full-tree layout oracle: builds a fresh `TaffyBridge` and computes every
/// element's bounds from the harness's current arena, independent of the
/// incremental gate.
fn compute_full_bounds(harness: &TestHarness) -> HashMap<ElementId, Rect> {
    let mut taffy = TaffyBridge::new();
    let root_id = harness.root_id();
    taffy.clear();
    let root_node = taffy.build_full_tree(harness.root(), root_id);
    let size = harness.size();
    if let Ok(mut s) = taffy.tree.style(root_node).cloned() {
        s.size.width = taffy::style::Dimension::length(size.width);
        s.size.height = taffy::style::Dimension::length(size.height);
        let _ = taffy.tree.set_style(root_node, s);
    }
    taffy.compute_layout(root_node, size).into_iter().collect()
}

/// Collect all ElementIds in DFS pre-order.
fn dfs_ids(h: &TestHarness) -> Vec<ElementId> {
    fn walk(h: &TestHarness, id: ElementId, out: &mut Vec<ElementId>) {
        out.push(id);
        if let Some(el) = h.find(id) {
            let children = el.children.clone();
            for c in children {
                walk(h, c, out);
            }
        }
    }
    let mut out = Vec::new();
    walk(h, h.root_id(), &mut out);
    out
}

proptest! {
    #[test]
    fn full_layout_is_deterministic(node in arb_node()) {
        let mut a = TestHarness::new(800.0, 600.0);
        a.mount(BoxedWidget(build_widget(&node)));
        a.run_frame();

        let mut b = TestHarness::new(800.0, 600.0);
        b.mount(BoxedWidget(build_widget(&node)));
        b.run_frame();

        prop_assert_eq!(ordered_bounds(&a), ordered_bounds(&b));
    }

    #[test]
    fn incremental_matches_full_after_measure_change(node in arb_node()) {
        let sig = Signal::new(String::from("Hi"));
        let mut h = TestHarness::new(800.0, 600.0);
        h.mount(
            SizedBox::new().width(780.0).height(580.0).child(
                VStack::new()
                    .push(BoxedWidget(build_widget(&node)))
                    .push(Text::new(String::new()).bind(sig.clone())),
            ),
        );
        h.run_frame();

        h.set_signal(&sig, String::from("This is a considerably longer piece of text"));
        h.run_frame();
        h.run_frame();

        let full = compute_full_bounds(&h);

        for id in dfs_ids(&h) {
            let inc = h.find(id).unwrap().screen_bounds;
            if let Some(f) = full.get(&id) {
                prop_assert_eq!(inc, *f, "incremental != full at element {:?}", id);
            }
        }
    }

    #[test]
    fn incremental_matches_full_multi_mutation(
        node in arb_node(),
        muts in prop::collection::vec(30usize..260, 1..8),
    ) {
        let sig = Signal::new(String::from("x"));
        let mut h = TestHarness::new(800.0, 600.0);
        h.mount(SizedBox::new().width(780.0).height(580.0).child(
            VStack::new()
                .push(BoxedWidget(build_widget(&node)))
                .push(Text::new(String::new()).bind(sig.clone())),
        ));
        h.run_frame();

        for (i, m) in muts.iter().enumerate() {
            let s = "y".repeat((*m / 8).max(1) + (i % 3));
            h.set_signal(&sig, s);
            h.run_frame();
            h.run_frame();

            let full = compute_full_bounds(&h);
            for id in dfs_ids(&h) {
                if let Some(f) = full.get(&id) {
                    let inc = h.find(id).unwrap().screen_bounds;
                    prop_assert_eq!(inc, *f, "mut#{} incremental != full at {:?}", i, id);
                }
            }
        }
    }

    #[test]
    fn emitted_boundaries_own_size_stable(node in arb_node(), m in 30usize..260) {
        let sig = Signal::new(String::from("x"));
        let mut h = TestHarness::new(800.0, 600.0);
        h.mount(SizedBox::new().width(780.0).height(580.0).child(
            VStack::new()
                .push(BoxedWidget(build_widget(&node)))
                .push(Text::new(String::new()).bind(sig.clone())),
        ));
        h.run_frame();

        let before = compute_full_bounds(&h);
        h.set_signal(&sig, "z".repeat(m / 8 + 2));
        h.run_frame();
        h.run_frame();
        let after = compute_full_bounds(&h);

        for id in dfs_ids(&h) {
            if let (Some(b), Some(a)) = (before.get(&id), after.get(&id)) {
                let size_stable = (b.width - a.width).abs() < 0.001
                    && (b.height - a.height).abs() < 0.001;
                if size_stable {
                    let inc = h.find(id).unwrap().screen_bounds;
                    prop_assert!(
                        (inc.width - a.width).abs() < 0.001 && (inc.height - a.height).abs() < 0.001,
                        "stable-size element {:?} mis-sized by incremental: inc={:?} full={:?}",
                        id, inc, a
                    );
                }
            }
        }
    }

    #[test]
    fn incremental_full_interleaved_with_resize(
        node in arb_node(),
        ops in prop::collection::vec((any::<bool>(), 30usize..260usize, 820u32..1400u32), 1..8),
    ) {
        // Interleave incremental (text-measure) frames with resize frames (which
        // take the full path via root reposition). Validates that switching
        // between the incremental and full paths does not corrupt taffy state
        // (e.g. compute_subtree's temporary style-size override must be restored).
        let sig = Signal::new(String::from("x"));
        let mut h = TestHarness::new(800.0, 600.0);
        h.mount(SizedBox::new().width(780.0).height(580.0).child(
            VStack::new()
                .push(BoxedWidget(build_widget(&node)))
                .push(Text::new(String::new()).bind(sig.clone())),
        ));
        h.run_frame();
        // Settle initial text-measure feedback so op#0 starts from a quiescent
        // state (no pending measure entangled with the first op).
        h.run_frame();
        h.run_frame();

        for (i, (do_resize, m, dim)) in ops.iter().enumerate() {
            if *do_resize {
                // Integer dims: the harness sets the ROOT's own bounds to the
                // raw requested size (not the rounded collect_bounds value), so a
                // fractional size would show a harmless <1px root rounding diff
                // vs the fresh oracle — orthogonal to incremental correctness.
                let rh = (*dim * 3 / 4).max(200);
                h.resize(*dim as f32, rh as f32);
            } else {
                h.set_signal(&sig, "y".repeat(m / 8 + 1));
            }
            h.run_frame();
            h.run_frame();

            let full = compute_full_bounds(&h);
            for id in dfs_ids(&h) {
                if let Some(f) = full.get(&id) {
                    let inc = h.find(id).unwrap().screen_bounds;
                    prop_assert_eq!(inc, *f, "op#{} (resize={}) incremental != full at {:?}", i, do_resize, id);
                }
            }
        }
    }
}

#[test]
fn incremental_path_is_actually_exercised() {
    let sig = Signal::new(String::from("x"));
    let mut h = TestHarness::new(800.0, 600.0);
    h.mount(
        SizedBox::new()
            .width(400.0)
            .height(300.0)
            .child(VStack::new().push(Text::new(String::new()).bind(sig.clone()))),
    );
    h.run_frame();
    let base = h.incremental_taken();
    h.set_signal(&sig, "much longer text string here".into());
    h.run_frame();
    h.run_frame();
    assert!(
        h.incremental_taken() > base,
        "incremental layout path was never taken (base={}, now={}) — differential tests would be vacuous",
        base, h.incremental_taken()
    );
}

#[test]
fn single_axis_row_contained_takes_incremental() {
    use crate::style::{Dimension, Styled};
    let sig = Signal::new(String::from("Hi"));
    let mut h = TestHarness::new(800.0, 600.0);
    // HStack width:100% is a direct child of a definite SizedBox -> x-independent
    // (percent-of-definite), height auto -> single-axis boundary. A single-line
    // width change keeps the row height stable -> contained.
    h.mount(
        SizedBox::new().width(500.0).height(400.0).child(
            HStack::new()
                .width(Dimension::Percent(1.0))
                .push(
                    SizedBox::new()
                        .width(40.0)
                        .height(24.0)
                        .child(Text::new("x")),
                )
                .push(Text::new(String::new()).bind(sig.clone())),
        ),
    );
    h.run_frame();
    h.run_frame();
    let base = h.incremental_taken();
    h.set_signal(&sig, String::from("longer label"));
    h.run_frame();
    h.run_frame();
    println!(
        "[SA] contained: incremental delta = {}",
        h.incremental_taken() - base
    );
    let full = compute_full_bounds(&h);
    for id in dfs_ids(&h) {
        if let Some(f) = full.get(&id) {
            assert_eq!(
                h.find(id).unwrap().screen_bounds,
                *f,
                "contained mismatch at {:?}",
                id
            );
        }
    }
    assert!(
        h.incremental_taken() > base,
        "single-axis row width change should take incremental"
    );
}

#[test]
fn single_axis_wrapping_escalates() {
    use crate::style::{Dimension, Styled};
    let sig = Signal::new(String::from("short"));
    let mut h = TestHarness::new(800.0, 600.0);
    // A row (height stretched to a definite parent) whose child VStack is a
    // shrink-to-content column: the VStack is height-independent (stretched by
    // the row) but WIDTH-dependent (content). A text width change grows the
    // VStack's (dependent) width -> the verify must fail -> escalate.
    h.mount(
        SizedBox::new().width(600.0).height(300.0).child(
            HStack::new()
                .height(Dimension::Percent(1.0))
                .push(VStack::new().push(Text::new(String::new()).bind(sig.clone()))),
        ),
    );
    h.run_frame();
    h.run_frame();
    let base_esc = h.escalation_taken();
    h.set_signal(&sig, "a considerably longer label than before now".into());
    h.run_frame();
    h.run_frame();
    println!(
        "[SA] escalate: escalation delta = {}",
        h.escalation_taken() - base_esc
    );
    let full = compute_full_bounds(&h);
    for id in dfs_ids(&h) {
        if let Some(f) = full.get(&id) {
            assert_eq!(
                h.find(id).unwrap().screen_bounds,
                *f,
                "wrapping mismatch at {:?}",
                id
            );
        }
    }
    assert!(
        h.escalation_taken() > base_esc,
        "multi-line growth in a single-axis column must escalate"
    );
}

#[test]
fn stretch_tree_takes_incremental() {
    let sig = Signal::new(String::from("Hi"));
    let mut h = TestHarness::new(1000.0, 700.0);
    h.mount(
        VStack::new().push(
            HStack::new()
                .push(Text::new(String::new()).bind(sig.clone()))
                .push(Text::new("a"))
                .push(Text::new("b")),
        ),
    );
    h.run_frame();
    h.run_frame();
    let base = h.incremental_taken();
    h.set_signal(&sig, String::from("a much longer label than before"));
    h.run_frame();
    h.run_frame();
    let full = compute_full_bounds(&h);
    for id in dfs_ids(&h) {
        if let Some(f) = full.get(&id) {
            assert_eq!(
                h.find(id).unwrap().screen_bounds,
                *f,
                "stretch mismatch at {:?}",
                id
            );
        }
    }
    assert!(
        h.incremental_taken() > base,
        "stretch tree width change should be contained at a stretch boundary (incremental)"
    );
}