frust-widgets 0.5.2

Baseline widget set for Frust: text, buttons, images, flex, stack and scroll containers, and the authoring helpers for custom widgets.
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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
//! Integration tests for [`frust_widgets::scaffold`]'s layout contract,
//! driven through a real [`RenderRoot`] rebuild + layout so window insets can
//! be simulated the same way [`RenderRoot::set_insets`]'s other consumers are
//! tested (`safe_area.rs`'s `#[cfg(test)]` module is the sibling precedent).
//!
//! Fixtures are hand-written (no `test-support` feature dependency) mirroring
//! this crate's other integration tests (`visit_children.rs`,
//! `semantics_tree.rs`): a fixed-size leaf per slot role (kept as distinct
//! types so [`find`] can pick one out unambiguously by its type name) plus
//! [`SelfInsetBar`], a bar that reads a window inset edge in its own
//! `layout` and grows by it — the minimal stand-in for
//! `frust_glyph::appbar::AppBarWidget`'s self-sizing contract, which
//! R-B4-inset (`scaffold.rs`'s module docs) is written against.
//!
//! Every fixture's `rebuild` is diff-aware (compares `prev` against `self`,
//! mirroring `frust_widgets::test_support::Leaf`'s own contract) rather than
//! unconditionally returning `ChangeFlags::LAYOUT` — the single-build tests in
//! this file never invoke `rebuild` at all (their one pass is always the
//! unconditional first-build arm), but the second-rebuild coverage further
//! down does, and an unconditionally-dirty fixture would mask whatever
//! `ScaffoldWidget`'s own slot reconciliation (`scaffold.rs`'s private
//! `rebuild_optional_slot`) actually reports.

use frust_core::{
    BoxConstraints, BuildCtx, ChangeFlags, InspectNode, LayoutCtx, PaintCtx, PaintScene,
    RenderRoot, View, Widget, WindowEdgeInsets, WindowInsets, any,
};
use frust_widgets::{Alignment, ScaffoldView, scaffold};
use kurbo::{Point, Size};

// -- Fixtures --------------------------------------------------------------

/// The scaffold's required `body` slot: a fixed-size leaf.
struct Body {
    intrinsic: Size,
}
fn body(width: f64, height: f64) -> Body {
    Body {
        intrinsic: Size::new(width, height),
    }
}
struct BodyWidget {
    intrinsic: Size,
}
impl View<()> for Body {
    type Element = BodyWidget;
    fn build(&self, _ctx: &mut BuildCtx<'_>) -> BodyWidget {
        BodyWidget {
            intrinsic: self.intrinsic,
        }
    }
    fn rebuild(&self, prev: &Self, el: &mut BodyWidget, _ctx: &mut BuildCtx<'_>) -> ChangeFlags {
        if prev.intrinsic != self.intrinsic {
            el.intrinsic = self.intrinsic;
            ChangeFlags::LAYOUT
        } else {
            ChangeFlags::NONE
        }
    }
}
impl Widget for BodyWidget {
    fn layout(&mut self, _ctx: &mut LayoutCtx, bc: &BoxConstraints) -> Size {
        bc.constrain(self.intrinsic)
    }
    fn paint(&mut self, _ctx: &mut PaintCtx, _scene: &mut dyn PaintScene) {}
}

/// A plain (non-self-insetting) bar, for tests that don't exercise
/// R-B4-inset — a distinct type from [`Body`]/[`Fab`] so [`find`] can locate
/// it unambiguously.
struct Bar {
    height: f64,
}
fn bar(height: f64) -> Bar {
    Bar { height }
}
struct BarWidget {
    height: f64,
}
impl View<()> for Bar {
    type Element = BarWidget;
    fn build(&self, _ctx: &mut BuildCtx<'_>) -> BarWidget {
        BarWidget {
            height: self.height,
        }
    }
    fn rebuild(&self, prev: &Self, el: &mut BarWidget, _ctx: &mut BuildCtx<'_>) -> ChangeFlags {
        if prev.height != self.height {
            el.height = self.height;
            ChangeFlags::LAYOUT
        } else {
            ChangeFlags::NONE
        }
    }
}
impl Widget for BarWidget {
    fn layout(&mut self, _ctx: &mut LayoutCtx, bc: &BoxConstraints) -> Size {
        let width = if bc.max().width.is_finite() {
            bc.max().width
        } else {
            0.0
        };
        bc.constrain(Size::new(width, self.height))
    }
    fn paint(&mut self, _ctx: &mut PaintCtx, _scene: &mut dyn PaintScene) {}
}

/// The `fab` slot: a fixed-size leaf, a distinct type from [`Body`]/[`Bar`].
struct Fab {
    intrinsic: Size,
}
fn fab_leaf(width: f64, height: f64) -> Fab {
    Fab {
        intrinsic: Size::new(width, height),
    }
}
struct FabWidget {
    intrinsic: Size,
}
impl View<()> for Fab {
    type Element = FabWidget;
    fn build(&self, _ctx: &mut BuildCtx<'_>) -> FabWidget {
        FabWidget {
            intrinsic: self.intrinsic,
        }
    }
    fn rebuild(&self, prev: &Self, el: &mut FabWidget, _ctx: &mut BuildCtx<'_>) -> ChangeFlags {
        if prev.intrinsic != self.intrinsic {
            el.intrinsic = self.intrinsic;
            ChangeFlags::LAYOUT
        } else {
            ChangeFlags::NONE
        }
    }
}
impl Widget for FabWidget {
    fn layout(&mut self, _ctx: &mut LayoutCtx, bc: &BoxConstraints) -> Size {
        bc.constrain(self.intrinsic)
    }
    fn paint(&mut self, _ctx: &mut PaintCtx, _scene: &mut dyn PaintScene) {}
}

/// Which window edge [`SelfInsetBar`] reads.
#[derive(Clone, Copy, PartialEq)]
enum InsetEdge {
    Top,
    Bottom,
}

/// A bar that self-sizes exactly like
/// `frust_glyph::appbar::AppBarWidget::layout` does: a fixed `content_height`
/// plus whichever inset edge it reads from `ctx.window_insets()` — the
/// minimal fixture R-B4-inset's contract needs, without a `frust-glyph`
/// dependency (`frust-widgets` has none).
struct SelfInsetBar {
    content_height: f64,
    edge: InsetEdge,
}
fn self_inset_bar(content_height: f64, edge: InsetEdge) -> SelfInsetBar {
    SelfInsetBar {
        content_height,
        edge,
    }
}
struct SelfInsetBarWidget {
    content_height: f64,
    edge: InsetEdge,
}
impl View<()> for SelfInsetBar {
    type Element = SelfInsetBarWidget;
    fn build(&self, _ctx: &mut BuildCtx<'_>) -> SelfInsetBarWidget {
        SelfInsetBarWidget {
            content_height: self.content_height,
            edge: self.edge,
        }
    }
    fn rebuild(
        &self,
        prev: &Self,
        el: &mut SelfInsetBarWidget,
        _ctx: &mut BuildCtx<'_>,
    ) -> ChangeFlags {
        if prev.content_height != self.content_height || prev.edge != self.edge {
            el.content_height = self.content_height;
            el.edge = self.edge;
            ChangeFlags::LAYOUT
        } else {
            ChangeFlags::NONE
        }
    }
}
impl Widget for SelfInsetBarWidget {
    fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints) -> Size {
        let inset = match self.edge {
            InsetEdge::Top => ctx.window_insets().padding().top,
            InsetEdge::Bottom => ctx.window_insets().padding().bottom,
        };
        let width = if bc.max().width.is_finite() {
            bc.max().width
        } else {
            0.0
        };
        bc.constrain(Size::new(width, self.content_height + inset))
    }
    fn paint(&mut self, _ctx: &mut PaintCtx, _scene: &mut dyn PaintScene) {}
}

// -- Harness -----------------------------------------------------------------

/// Rebuild, push `insets`, lay out against `window`, and return the inspect
/// snapshot — mirrors `safe_area.rs`'s own test ordering (`set_insets` after
/// `rebuild`, before `layout`).
fn inspect<V: View<()>>(
    build: &mut impl FnMut(&mut ()) -> V,
    window: Size,
    insets: WindowInsets,
) -> Vec<InspectNode> {
    let mut root: RenderRoot<(), V> = RenderRoot::new();
    let mut state = ();
    root.rebuild(build, &mut state);
    root.set_insets(insets);
    root.layout(window);
    root.inspect()
}

/// Like [`inspect`], but rebuilds *twice* against `first` then `second` — each
/// a fresh view the way a build closure re-runs every frame — running a
/// REAL second [`RenderRoot::layout`] pass in between, rather than the single
/// build-then-layout pass every test above exercises. Returns both inspect
/// snapshots plus the [`ChangeFlags`] the **second** rebuild reported (the
/// first pass's flags are always the unconditional `LAYOUT | PAINT` of a first
/// build, so only the second pass's flags say anything about `ScaffoldWidget`'s
/// own slot reconciliation — the surface this file's second-rebuild tests
/// exist to cover, `scaffold.rs`'s `rebuild_optional_slot`).
fn two_pass<V: View<()>>(
    mut first: impl FnMut(&mut ()) -> V,
    mut second: impl FnMut(&mut ()) -> V,
    window: Size,
    insets: WindowInsets,
) -> (Vec<InspectNode>, Vec<InspectNode>, ChangeFlags) {
    let mut root: RenderRoot<(), V> = RenderRoot::new();
    let mut state = ();
    root.rebuild(&mut first, &mut state);
    root.set_insets(insets);
    root.layout(window);
    let before = root.inspect();

    let flags = root.rebuild(&mut second, &mut state);
    root.layout(window);
    let after = root.inspect();
    (before, after, flags)
}

/// Each node's bare type name (no module path/generics) — `visit_children.rs`'s
/// helper of the same shape.
fn short_names(nodes: &[InspectNode]) -> Vec<&str> {
    nodes
        .iter()
        .map(|n| {
            let bare = n.type_name.split('<').next().unwrap_or(n.type_name);
            bare.rsplit("::").next().unwrap_or(bare)
        })
        .collect()
}

/// The one node whose bare type name is `name` — panics (with the observed
/// names) if there isn't exactly the fixture the caller expects to find.
fn find<'a>(nodes: &'a [InspectNode], name: &str) -> &'a InspectNode {
    nodes
        .iter()
        .find(|n| {
            let bare = n.type_name.split('<').next().unwrap_or(n.type_name);
            bare.rsplit("::").next().unwrap_or(bare) == name
        })
        .unwrap_or_else(|| panic!("no {name} node found; nodes were {:?}", short_names(nodes)))
}

// -- Empty-slot case ---------------------------------------------------------

#[test]
fn body_only_scaffold_has_no_optional_slot_nodes() {
    let nodes = inspect(
        &mut |_: &mut ()| -> ScaffoldView<()> { scaffold(body(50.0, 50.0)) },
        Size::new(300.0, 400.0),
        WindowInsets::default(),
    );
    assert_eq!(short_names(&nodes), vec!["ScaffoldWidget", "BodyWidget"]);
    assert_eq!(nodes[0].children.len(), 1);
    let body_node = find(&nodes, "BodyWidget");
    assert_eq!(body_node.bounds.origin(), Point::ZERO);
    assert_eq!(body_node.bounds.width(), 300.0);
    assert_eq!(body_node.bounds.height(), 400.0);
}

// -- Slot sizing/order, all four together ------------------------------------

#[test]
fn all_four_slots_are_present_sized_and_positioned_consistently() {
    let nodes = inspect(
        &mut |_: &mut ()| -> ScaffoldView<()> {
            scaffold(body(0.0, 0.0))
                .app_bar(any(bar(56.0)))
                .bottom_bar(any(bar(48.0)))
                .fab(any(fab_leaf(56.0, 56.0)))
        },
        Size::new(300.0, 400.0),
        WindowInsets::default(),
    );
    // visit_children! declares (body, app_bar, bottom_bar, fab) — pre-order
    // in `inspect()` mirrors paint order, so this also pins the z-stack.
    assert_eq!(
        short_names(&nodes),
        vec![
            "ScaffoldWidget",
            "BodyWidget",
            "BarWidget",
            "BarWidget",
            "FabWidget"
        ]
    );

    let body_node = &nodes[1];
    let app_bar_node = &nodes[2];
    let bottom_bar_node = &nodes[3];
    let fab_node = &nodes[4];

    assert_eq!(app_bar_node.bounds.origin(), Point::ZERO);
    assert_eq!(app_bar_node.bounds.height(), 56.0);

    assert_eq!(body_node.bounds.origin(), Point::new(0.0, 56.0));
    assert_eq!(body_node.bounds.width(), 300.0);
    assert_eq!(body_node.bounds.height(), 400.0 - 56.0 - 48.0);

    assert_eq!(
        bottom_bar_node.bounds.origin(),
        Point::new(0.0, 400.0 - 48.0)
    );
    assert_eq!(bottom_bar_node.bounds.height(), 48.0);

    // Default bottom-right alignment, `fab_margin` inset from each edge; the
    // fab's box already stops above the bottom bar, so no window inset is
    // added on top of it (zero here regardless — no inset was pushed).
    assert_eq!(
        fab_node.bounds.origin(),
        Point::new(300.0 - 16.0 - 56.0, 400.0 - 48.0 - 16.0 - 56.0)
    );
}

#[test]
fn fab_alignment_moves_the_fab_within_its_box() {
    let nodes = inspect(
        &mut |_: &mut ()| -> ScaffoldView<()> {
            scaffold(body(0.0, 0.0))
                .fab(any(fab_leaf(56.0, 56.0)))
                .fab_alignment(Alignment::TOP_LEFT)
        },
        Size::new(300.0, 400.0),
        WindowInsets::default(),
    );
    let fab_node = find(&nodes, "FabWidget");
    assert_eq!(fab_node.bounds.origin(), Point::new(16.0, 16.0));
}

// -- R-B4-inset: the Scaffold never pre-insets a self-insetting app bar ------

#[test]
fn r_b4_inset_a_self_insetting_app_bar_is_never_pre_inset_by_the_scaffold() {
    // A 44px status-bar-style top inset, no IME.
    let insets = WindowInsets::new(
        WindowEdgeInsets::new(0.0, 44.0, 0.0, 0.0),
        WindowEdgeInsets::ZERO,
    );
    let nodes = inspect(
        &mut |_: &mut ()| -> ScaffoldView<()> {
            scaffold(body(0.0, 0.0)).app_bar(any(self_inset_bar(56.0, InsetEdge::Top)))
        },
        Size::new(300.0, 400.0),
        insets,
    );
    let app_bar_node = find(&nodes, "SelfInsetBarWidget");
    let body_node = find(&nodes, "BodyWidget");

    // The bar already grew by the 44px inset in its own layout.
    assert_eq!(app_bar_node.bounds.height(), 56.0 + 44.0);
    assert_eq!(app_bar_node.bounds.origin(), Point::ZERO);

    // The body sits exactly at the bar's own (already-inset) height — never
    // `44 + bar_height`, which would double-consume the inset.
    assert_eq!(body_node.bounds.origin().y, 100.0);
    assert_eq!(body_node.bounds.origin().y, app_bar_node.bounds.height());
}

// -- FAB bottom-inset consumption --------------------------------------------

#[test]
fn fab_consumes_the_bottom_window_inset_when_no_bottom_bar_is_present() {
    // A 34px home-indicator-style bottom inset, no IME.
    let insets = WindowInsets::new(
        WindowEdgeInsets::new(0.0, 0.0, 0.0, 34.0),
        WindowEdgeInsets::ZERO,
    );
    let nodes = inspect(
        &mut |_: &mut ()| -> ScaffoldView<()> {
            scaffold(body(0.0, 0.0)).fab(any(fab_leaf(56.0, 56.0)))
        },
        Size::new(300.0, 400.0),
        insets,
    );
    let fab_node = find(&nodes, "FabWidget");
    // `fab_margin` (16) + the bottom inset (34), off the raw bottom edge.
    assert_eq!(
        fab_node.bounds.origin().y,
        400.0 - 16.0 - 34.0 - 56.0,
        "with no bottom_bar, the fab must consume the window's bottom inset itself"
    );
}

// -- bottom_bar: self-insets, and the fab floats above it with no double inset --

#[test]
fn bottom_bar_self_insets_and_the_fab_floats_above_it_without_double_consuming() {
    // The same 34px bottom inset as above, but now claimed by a self-insetting
    // bottom_bar instead of the fab.
    let insets = WindowInsets::new(
        WindowEdgeInsets::new(0.0, 0.0, 0.0, 34.0),
        WindowEdgeInsets::ZERO,
    );
    let nodes = inspect(
        &mut |_: &mut ()| -> ScaffoldView<()> {
            scaffold(body(0.0, 0.0))
                .bottom_bar(any(self_inset_bar(48.0, InsetEdge::Bottom)))
                .fab(any(fab_leaf(56.0, 56.0)))
        },
        Size::new(300.0, 400.0),
        insets,
    );
    let bottom_bar_node = find(&nodes, "SelfInsetBarWidget");
    let body_node = find(&nodes, "BodyWidget");
    let fab_node = find(&nodes, "FabWidget");

    // The bottom bar grew by the 34px inset in its own layout (mirroring the
    // app-bar convention).
    let bar_height = 48.0 + 34.0;
    assert_eq!(bottom_bar_node.bounds.height(), bar_height);
    assert_eq!(
        bottom_bar_node.bounds.origin(),
        Point::new(0.0, 400.0 - bar_height)
    );

    // The body stops exactly above the (already-inset) bottom bar.
    assert_eq!(body_node.bounds.height(), 400.0 - bar_height);

    // The fab floats `fab_margin` above the bottom bar — no *additional*
    // window inset on top of it (that would double-consume the same 34px the
    // bottom bar already self-sized for).
    assert_eq!(
        fab_node.bounds.origin().y,
        400.0 - bar_height - 16.0 - 56.0,
        "the fab must not double-consume the bottom inset once a bottom_bar has self-inset for it"
    );
}

// -- Second-rebuild slot reconciliation ---------------------------------------
//
// Every test above builds and lays out exactly once. The three optional slots
// (`app_bar`/`bottom_bar`/`fab`) are reconciled by `scaffold.rs`'s private
// `rebuild_optional_slot` — the route-derived-chrome pattern `app_bar_opt`/
// `bottom_bar_opt` exist for — which a single build+layout pass never
// exercises at all (the first rebuild always takes the unconditional
// first-build arm). These run a REAL second rebuild, with a REAL second
// layout pass, through [`two_pass`].

#[test]
fn app_bar_removed_on_rebuild_expands_the_body_to_full_height() {
    let (before, after, flags) = two_pass(
        |_: &mut ()| -> ScaffoldView<()> {
            scaffold(body(0.0, 0.0)).app_bar_opt(Some(any(bar(56.0))))
        },
        |_: &mut ()| -> ScaffoldView<()> { scaffold(body(0.0, 0.0)).app_bar_opt(None) },
        Size::new(300.0, 400.0),
        WindowInsets::default(),
    );
    let body_before = find(&before, "BodyWidget");
    assert_eq!(body_before.bounds.origin(), Point::new(0.0, 56.0));
    assert_eq!(body_before.bounds.height(), 400.0 - 56.0);

    assert_eq!(
        short_names(&after),
        vec!["ScaffoldWidget", "BodyWidget"],
        "the app_bar node must be torn down, not merely emptied"
    );
    let body_after = find(&after, "BodyWidget");
    assert_eq!(body_after.bounds.origin(), Point::ZERO);
    assert_eq!(body_after.bounds.height(), 400.0);
    assert!(flags.needs_layout());
}

#[test]
fn app_bar_added_on_rebuild_shrinks_the_body_below_it() {
    let (before, after, flags) = two_pass(
        |_: &mut ()| -> ScaffoldView<()> { scaffold(body(0.0, 0.0)).app_bar_opt(None) },
        |_: &mut ()| -> ScaffoldView<()> {
            scaffold(body(0.0, 0.0)).app_bar_opt(Some(any(bar(56.0))))
        },
        Size::new(300.0, 400.0),
        WindowInsets::default(),
    );
    assert_eq!(short_names(&before), vec!["ScaffoldWidget", "BodyWidget"]);
    let body_before = find(&before, "BodyWidget");
    assert_eq!(body_before.bounds.origin(), Point::ZERO);
    assert_eq!(body_before.bounds.height(), 400.0);

    let bar_after = find(&after, "BarWidget");
    assert_eq!(bar_after.bounds.origin(), Point::ZERO);
    assert_eq!(bar_after.bounds.height(), 56.0);
    let body_after = find(&after, "BodyWidget");
    assert_eq!(body_after.bounds.origin(), Point::new(0.0, 56.0));
    assert_eq!(body_after.bounds.height(), 400.0 - 56.0);
    assert!(flags.needs_layout());
}

#[test]
fn bottom_bar_removed_on_rebuild_expands_the_body_to_full_height() {
    let (before, after, flags) = two_pass(
        |_: &mut ()| -> ScaffoldView<()> {
            scaffold(body(0.0, 0.0)).bottom_bar_opt(Some(any(bar(48.0))))
        },
        |_: &mut ()| -> ScaffoldView<()> { scaffold(body(0.0, 0.0)).bottom_bar_opt(None) },
        Size::new(300.0, 400.0),
        WindowInsets::default(),
    );
    let body_before = find(&before, "BodyWidget");
    assert_eq!(body_before.bounds.origin(), Point::ZERO);
    assert_eq!(body_before.bounds.height(), 400.0 - 48.0);

    assert_eq!(
        short_names(&after),
        vec!["ScaffoldWidget", "BodyWidget"],
        "the bottom_bar node must be torn down, not merely emptied"
    );
    let body_after = find(&after, "BodyWidget");
    assert_eq!(body_after.bounds.origin(), Point::ZERO);
    assert_eq!(body_after.bounds.height(), 400.0);
    assert!(flags.needs_layout());
}

#[test]
fn bottom_bar_added_on_rebuild_shrinks_the_body_above_it() {
    let (before, after, flags) = two_pass(
        |_: &mut ()| -> ScaffoldView<()> { scaffold(body(0.0, 0.0)).bottom_bar_opt(None) },
        |_: &mut ()| -> ScaffoldView<()> {
            scaffold(body(0.0, 0.0)).bottom_bar_opt(Some(any(bar(48.0))))
        },
        Size::new(300.0, 400.0),
        WindowInsets::default(),
    );
    let body_before = find(&before, "BodyWidget");
    assert_eq!(body_before.bounds.height(), 400.0);

    let bar_after = find(&after, "BarWidget");
    assert_eq!(bar_after.bounds.height(), 48.0);
    assert_eq!(bar_after.bounds.origin(), Point::new(0.0, 400.0 - 48.0));
    let body_after = find(&after, "BodyWidget");
    assert_eq!(body_after.bounds.origin(), Point::ZERO);
    assert_eq!(body_after.bounds.height(), 400.0 - 48.0);
    assert!(flags.needs_layout());
}

#[test]
fn fab_appearing_on_rebuild_positions_the_fab_without_disturbing_the_body() {
    let (before, after, flags) = two_pass(
        |_: &mut ()| -> ScaffoldView<()> { scaffold(body(0.0, 0.0)) },
        |_: &mut ()| -> ScaffoldView<()> {
            scaffold(body(0.0, 0.0)).fab(any(fab_leaf(56.0, 56.0)))
        },
        Size::new(300.0, 400.0),
        WindowInsets::default(),
    );
    assert_eq!(short_names(&before), vec!["ScaffoldWidget", "BodyWidget"]);
    let body_before = find(&before, "BodyWidget");

    assert_eq!(
        short_names(&after),
        vec!["ScaffoldWidget", "BodyWidget", "FabWidget"]
    );
    let body_after = find(&after, "BodyWidget");
    let fab_after = find(&after, "FabWidget");
    assert_eq!(
        body_before.bounds, body_after.bounds,
        "the body must not move or resize when the fab appears"
    );
    assert_eq!(
        fab_after.bounds.origin(),
        Point::new(300.0 - 16.0 - 56.0, 400.0 - 16.0 - 56.0)
    );
    assert!(flags.needs_layout());
}

#[test]
fn fab_disappearing_on_rebuild_leaves_the_body_undisturbed() {
    let (before, after, flags) = two_pass(
        |_: &mut ()| -> ScaffoldView<()> {
            scaffold(body(0.0, 0.0)).fab(any(fab_leaf(56.0, 56.0)))
        },
        |_: &mut ()| -> ScaffoldView<()> { scaffold(body(0.0, 0.0)) },
        Size::new(300.0, 400.0),
        WindowInsets::default(),
    );
    assert_eq!(
        short_names(&before),
        vec!["ScaffoldWidget", "BodyWidget", "FabWidget"]
    );
    let body_before = find(&before, "BodyWidget");

    assert_eq!(
        short_names(&after),
        vec!["ScaffoldWidget", "BodyWidget"],
        "the fab node must be torn down, not merely emptied"
    );
    let body_after = find(&after, "BodyWidget");
    assert_eq!(
        body_before.bounds, body_after.bounds,
        "the body must not move or resize when the fab disappears"
    );
    assert!(flags.needs_layout());
}

#[test]
fn app_bar_content_swap_with_a_different_height_relayouts_the_body() {
    // Same slot, same view *type* (`Bar`), different `height` — the reconciler
    // takes the in-place-rebuild arm (`AnyView`'s type stays the same), so this
    // exercises `rebuild_optional_slot`'s `(Some, Some, Some(pod))` arm, not a
    // teardown/build swap.
    let (before, after, flags) = two_pass(
        |_: &mut ()| -> ScaffoldView<()> { scaffold(body(0.0, 0.0)).app_bar(any(bar(56.0))) },
        |_: &mut ()| -> ScaffoldView<()> { scaffold(body(0.0, 0.0)).app_bar(any(bar(80.0))) },
        Size::new(300.0, 400.0),
        WindowInsets::default(),
    );
    let bar_before = find(&before, "BarWidget");
    assert_eq!(bar_before.bounds.height(), 56.0);
    let body_before = find(&before, "BodyWidget");
    assert_eq!(body_before.bounds.origin(), Point::new(0.0, 56.0));

    let bar_after = find(&after, "BarWidget");
    assert_eq!(bar_after.bounds.height(), 80.0);
    let body_after = find(&after, "BodyWidget");
    assert_eq!(body_after.bounds.origin(), Point::new(0.0, 80.0));
    assert_eq!(body_after.bounds.height(), 400.0 - 80.0);
    assert!(flags.needs_layout());
}

#[test]
fn rebuild_with_unchanged_slots_reports_no_change_flags() {
    // All three optional slots present, and every field identical between the
    // two passes — the skip path REVIEW_FOCUS calls out for this hot-spot
    // class: a rebuild that changes nothing must not report LAYOUT/PAINT, or a
    // shell driven off `RenderRoot::has_pending_change_flags`/
    // `take_change_flags` would relayout every frame for nothing.
    let (before, after, flags) = two_pass(
        |_: &mut ()| -> ScaffoldView<()> {
            scaffold(body(50.0, 50.0))
                .app_bar_opt(Some(any(bar(56.0))))
                .bottom_bar_opt(Some(any(bar(48.0))))
                .fab(any(fab_leaf(56.0, 56.0)))
        },
        |_: &mut ()| -> ScaffoldView<()> {
            scaffold(body(50.0, 50.0))
                .app_bar_opt(Some(any(bar(56.0))))
                .bottom_bar_opt(Some(any(bar(48.0))))
                .fab(any(fab_leaf(56.0, 56.0)))
        },
        Size::new(300.0, 400.0),
        WindowInsets::default(),
    );
    assert_eq!(
        before, after,
        "no node should move, resize, appear, or disappear when every slot is unchanged"
    );
    assert!(
        flags.is_empty(),
        "a rebuild with unchanged slots must report no ChangeFlags at all, got {flags:?}"
    );
}