Files
craft-libs/crates/ui-shell/tests/it/shell/app_shell.rs
T
aiongg b257b150fb craft-ui: the replay audit runs every memoized view that would replay and compares it with the replay
Test builds only. With CRAFT_UI_AUDIT_REPLAY=1 (or testing::audit_replays), the outermost view that would replay in a
real pass runs for real inside an audit record; the kept run stays as it was. The run is compared with what the replay
adds: the view's rect, paint, widget rects and AccessKit nodes (PassRecords::first_difference), focus registrations,
the journal and the output. A difference panics with the view's name and the first difference of each kind. Counters
count nothing inside an audit run, so the count trials hold under the audit.

A record that paints a paint callback is blocked: its view runs every pass, and the audit names it.
2026-11-20 00:10:09 +00:00

234 lines
10 KiB
Rust

//! The app shell's named slots: a bar in a top or bottom slot fills it with its members on the slot's middle, and a
//! slot whose content is a memoized view runs only when its props change.
use craft_ui::bars::{Bar, BarKind};
use craft_ui::buttons::Button;
use craft_ui::contract::{MemoProps, MemoView, Show as _, ShowContainer as _};
use craft_ui::display::Text;
use craft_ui::layout_components::{Row, Stack};
use craft_ui::runtime::env::{Env, RevealEdge, ShellEntry, WindowKey};
use craft_ui::shell::{AppShell, Slot, Title, TitleBar};
use craft_ui::testing::in_audit_run;
use craft_ui::testing::pointer::{press, release};
use craft_ui::testing::{Fixture, Frames, MemoRun, last_pass_memo_runs, trial};
use egui::{Rect, Ui, Vec2};
use std::cell::{Cell, RefCell};
use std::rc::Rc;
/// What the options-bar trial saw: the slot, the bar, a member of the bar and a member of a row nested in it.
#[derive(Clone, Copy)]
struct InSlot {
slot: Rect,
bar: Rect,
member: Rect,
nested: Rect,
}
/// A bar in the options slot is as high as the slot, whatever its own padding and members take, and its members sit
/// on the slot's middle: a member of the bar's own row and a member of a row nested in it beside a child of two lines.
#[trial]
fn a_bar_in_a_top_slot_fills_it_and_centres_its_members_on_one_line() {
let seen = Rc::new(Cell::new(None::<InSlot>));
let out = Rc::clone(&seen);
let mut fixture = Fixture::new(move |ui| {
let (mut bar, mut member, mut nested) = (Rect::NOTHING, Rect::NOTHING, Rect::NOTHING);
let shown = AppShell::new(WindowKey::main())
.options_bar(true, |ui, _, _: &mut ()| {
bar = Bar::new("Options", BarKind::Options)
.show(ui, |ui, _| {
member = Button::new("Alpha").show(ui).rect;
Row::new().show(ui, |ui, _| {
nested = Button::new("Beta").show(ui).rect;
Stack::new().show(ui, |ui, _| {
Text::new("Above").show(ui);
Text::new("Below").show(ui);
});
});
})
.output
.response
.rect;
})
.body(|_, _, _| {})
.show(ui, &mut ());
let slot = shown.regions.slots.iter().find(|(key, _)| *key == "options").map(|(_, rect)| *rect);
if let Some(slot) = slot {
out.set(Some(InSlot { slot, bar, member, nested }));
}
});
fixture.room(Vec2::new(600.0, 300.0)).settle();
let InSlot { slot, bar, member, nested } = seen.get().expect("the options slot shows");
const SLACK: f32 = 1.0;
assert!((bar.min.y - slot.min.y).abs() <= SLACK && (bar.max.y - slot.max.y).abs() <= SLACK, "the bar {bar:?} fills the slot {slot:?}");
for (name, rect) in [("the bar's member", member), ("the nested row's member", nested)] {
assert!((rect.center().y - slot.center().y).abs() <= SLACK, "{name} {rect:?} sits on the middle of the slot {slot:?}");
}
}
/// A title bar in the title slot shows its leading content inside the slot, on the slot's middle.
#[trial]
fn a_title_bar_in_the_title_slot_shows_its_content_there() {
let seen = Rc::new(Cell::new(None::<(Rect, Rect, Rect)>));
let out = Rc::clone(&seen);
let mut fixture = Fixture::new(move |ui| {
let (mut bar, mut member) = (Rect::NOTHING, Rect::NOTHING);
let shown = AppShell::new(WindowKey::main())
.title_bar(|ui, _, _: &mut ()| {
bar = TitleBar::new()
.show_titled(
ui,
Title::new("S"),
|ui, _| {
member = Button::new("File").show(ui).rect;
},
|_, _| {},
)
.response
.rect;
})
.options_bar(true, |ui, _, _: &mut ()| {
let _ = Bar::new("Options", BarKind::Options).show(ui, |ui, _| {
Button::new("Alpha").show(ui);
});
})
.body(|_, _, _| {})
.show(ui, &mut ());
if let Some(slot) = shown.regions.slots.iter().find(|(key, _)| *key == "title").map(|(_, rect)| *rect) {
out.set(Some((slot, bar, member)));
}
});
fixture.room(Vec2::new(600.0, 300.0)).settle();
let (slot, bar, member) = seen.get().expect("the title slot shows");
assert!(member.width() > 0.0 && slot.contains_rect(member), "the bar's member {member:?} shows in the slot {slot:?} (bar {bar:?})");
assert!((member.center().y - slot.center().y).abs() <= 1.0, "the member {member:?} sits on the slot's middle {slot:?}");
}
thread_local! {
/// The memoized slots that ran on this trial's thread, in order.
static SLOT_RUNS: RefCell<Vec<&'static str>> = const { RefCell::new(Vec::new()) };
}
/// Takes the memoized slots that ran since the last call.
fn slot_runs() -> Vec<&'static str> {
SLOT_RUNS.with(|runs| std::mem::take(&mut *runs.borrow_mut()))
}
/// What a memoized slot shows.
#[derive(MemoProps)]
struct SlotText {
name: &'static str,
text: String,
}
fn slot_text(props: &mut SlotText, ui: &mut Ui) {
// An audit run is the replay audit's, not the pass's: the trials count none.
if !in_audit_run(ui.ctx()) {
SLOT_RUNS.with(|runs| runs.borrow_mut().push(props.name));
}
Text::new(props.text.clone()).show(ui);
}
/// Slots whose content is a memoized view run only when their props change: at rest and for a click in the body they
/// replay, a new status runs the status slot alone (one real run, one measuring run), and a slot the app does not
/// memoize runs every pass.
#[trial]
fn memoized_slots_run_only_when_their_props_change() {
let (version, plain, body) = (Cell::new(1), Cell::new(0), Cell::new(Rect::NOTHING));
let frame = |frames: &mut Frames, events| {
plain.set(0);
frames.frame(events, |ui| {
let out = AppShell::new(WindowKey::main())
.options_bar(true, |ui, _, _: &mut ()| {
let _ = MemoView::new("options", SlotText { name: "options", text: "Options".to_owned() }, slot_text).show(ui);
})
.status(true, |ui, _, _| {
let _ = MemoView::new("status", SlotText { name: "status", text: format!("Ready {}", version.get()) }, slot_text).show(ui);
})
.start(Slot::new("tools"), |ui, _, _| {
plain.set(plain.get() + 1);
Text::new("Tools").show(ui);
})
.body(|ui, _, _| {
Button::new("Body").show(ui);
})
.show(ui, &mut ());
body.set(out.regions.body);
});
(slot_runs(), plain.get())
};
let mut frames = Frames::new();
for _ in 0..30 {
frame(&mut frames, vec![]);
}
let (ran, plain_runs) = frame(&mut frames, vec![]);
assert_eq!(ran, Vec::<&str>::new(), "the memoized slots replay at rest");
assert!(plain_runs > 0, "the slot the app does not memoize runs every pass");
let at = body.get().center();
for events in [vec![press(at)], vec![release(at)], vec![]] {
assert_eq!(frame(&mut frames, events.clone()).0, Vec::<&str>::new(), "a click in the body ({events:?}) leaves the slots replaying");
}
version.set(2);
// One real run, then one measuring run in the first sizing pass that measures the new content: the discard pass its
// new size asks for, whose real run replays the first. A container that measures before the real run of one pass
// makes the same two runs in the other order (ui-widgets body::memo_view, the stack measured first).
assert_eq!(
frame(&mut frames, vec![]).0,
vec!["status", "status"],
"a new status runs the status slot alone: one real run and one measuring run in the sizing pass that measures it"
);
assert!(
last_pass_memo_runs(&frames.ctx).iter().all(|(_, _, run)| *run == MemoRun::Replayed),
"the discard pass's real run replays every slot: {:?}",
last_pass_memo_runs(&frames.ctx)
);
assert_eq!(frame(&mut frames, vec![]).0, Vec::<&str>::new(), "and it replays at it");
}
/// What a slot that registers a revealed rect shows: the rect.
#[derive(MemoProps)]
struct Revealing {
at: Rect,
}
fn revealing(props: &mut Revealing, ui: &mut Ui) {
// An audit run is the replay audit's, not the pass's: the trials count none.
if !in_audit_run(ui.ctx()) {
SLOT_RUNS.with(|runs| runs.borrow_mut().push("revealing"));
}
if let Some(shell) = Env::of(ui).view().get::<ShellEntry>().0 {
shell.reveal_rect(ui.ctx(), RevealEdge::Start, props.at);
}
Text::new("Tools").show(ui);
}
/// A memoized view in a slot that registers a revealed rect registers it again when it replays: the shell reads the
/// same rects in a replayed pass as in the run.
#[trial]
fn a_replayed_view_in_a_slot_registers_its_revealed_rects() {
let at = Rect::from_min_size(egui::pos2(0.0, 40.0), Vec2::new(40.0, 120.0));
let seen = RefCell::new(Vec::new());
let frame = |frames: &mut Frames| {
frames.frame(vec![], |ui| {
AppShell::new(WindowKey::main())
.start(Slot::new("tools"), |ui, _, _: &mut ()| {
let _ = MemoView::new("tools", Revealing { at }, revealing).show(ui);
})
.body(|ui, bcx, _| {
*seen.borrow_mut() = bcx.shell().map(|shell| shell.revealed_rects(ui.ctx())).unwrap_or_default();
})
.show(ui, &mut ());
});
(slot_runs(), seen.borrow().clone())
};
let mut frames = Frames::new();
let (ran, first) = frame(&mut frames);
assert!(ran.contains(&"revealing"), "the slot runs in its first frame");
assert_eq!(first, vec![(RevealEdge::Start, at)], "its run registers the rect");
for _ in 0..30 {
frame(&mut frames);
}
let (ran, replayed) = frame(&mut frames);
assert_eq!(ran, Vec::<&str>::new(), "the slot replays");
assert_eq!(replayed, first, "the replay registers the same rects as the run");
}