mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-24 11:31:28 +01:00
craft-ui: replay records compare as replayed; replayed members take their stack positions from the arena
`Transcript` equality compares the form a replay registers (`Transcript::placed`, which `focus::replay` uses): a record under the scope open around the run goes under the open scope, and a replayed scope is no new scope. A replayed member join and `over` placement take their stack positions from the arena they join in the replaying pass, as a real run does: the journal notes the members as declared with the layer and clip, and the join and the `over` placement record their shapes themselves. The `journal` doc says the capture assert fires in a build, not in `cargo check`.
This commit is contained in:
1 parent
2f8832b099
commit
17adf30a85
5 files changed
+192
-69
No files matched your search
@@ -440,41 +440,61 @@ fn register_root_again(ctx: &Context, arena: ArenaId, registration: RootRegistra
|
||||
});
|
||||
}
|
||||
|
||||
/// Notes in the open records the members that joined `arena`, as they joined.
|
||||
fn journal_join(ctx: &Context, arena: ArenaId, joined: &[MemberDecl]) {
|
||||
crate::runtime::replay::journal(ctx, || (arena, joined.to_vec()), |ctx, (arena, joined)| join_again(ctx, *arena, joined.clone()));
|
||||
/// Notes in the open records the members that joined `arena` on `layer` through `clip`, as declared: a replay joins
|
||||
/// them at the stack positions the arena gives them then.
|
||||
fn journal_join(ctx: &Context, arena: ArenaId, layer: Layer, clip: Rect, joined: &[MemberDecl]) {
|
||||
crate::runtime::replay::journal(
|
||||
ctx,
|
||||
|| (arena, layer, clip, joined.to_vec()),
|
||||
|ctx, (arena, layer, clip, joined)| join_again(ctx, *arena, *layer, *clip, joined),
|
||||
);
|
||||
}
|
||||
|
||||
/// Replayed members join their arena again.
|
||||
fn join_again(ctx: &Context, arena: ArenaId, joined: Vec<MemberDecl>) {
|
||||
journal_join(ctx, arena, &joined);
|
||||
let pass_nr = pass::pass_nr(ctx, pass::current_viewport(ctx));
|
||||
with_arena(ctx, arena, pass_nr, |state| {
|
||||
rotate(state, pass_nr);
|
||||
/// Replayed members join their arena again, after the members that joined before them in this pass.
|
||||
fn join_again(ctx: &Context, arena: ArenaId, layer: Layer, clip: Rect, joined: &[MemberDecl]) {
|
||||
journal_join(ctx, arena, layer, clip, joined);
|
||||
let read = PassRead::of(ctx);
|
||||
let mut shapes = Vec::new();
|
||||
with_arena_in(ctx, read.viewport, arena, read.pass_nr, |state| {
|
||||
rotate(state, read.pass_nr);
|
||||
for decl in joined {
|
||||
state.union_sense |= decl.parts.iter().fold(decl.sense, |sense, part| sense | part.sense);
|
||||
state.members.push(decl);
|
||||
join_member(state, decl.clone(), layer, read.target(), &mut shapes);
|
||||
}
|
||||
});
|
||||
pointer::add_shapes_in(ctx, read.viewport, layer, arena.0, clip, shapes);
|
||||
}
|
||||
|
||||
/// Places the `over` parts of the root of `arena` (`member` None) or of its member `member` above the members joined
|
||||
/// so far, as `place_over_widgets` does; journaled for a replay.
|
||||
fn place_over(ctx: &Context, arena: ArenaId, member: Option<Id>) {
|
||||
crate::runtime::replay::journal(ctx, || (arena, member), |ctx, &(arena, member)| place_over(ctx, arena, member));
|
||||
let pass_nr = pass::pass_nr(ctx, pass::current_viewport(ctx));
|
||||
with_arena(ctx, arena, pass_nr, |state| {
|
||||
rotate(state, pass_nr);
|
||||
/// Joins `decl`, as declared, to the arena `state` on `layer`: at the next stack position, its `over` parts just above
|
||||
/// it, its shapes added to `shapes`.
|
||||
fn join_member(state: &mut ArenaState, mut decl: MemberDecl, layer: Layer, target: f32, shapes: &mut Vec<Shape<Id>>) {
|
||||
decl.local = state.layer == Some(layer);
|
||||
let position = member_position(state.members.len());
|
||||
decl.over_at = position + 1;
|
||||
shapes.extend(member_shapes(&decl, position, target));
|
||||
state.union_sense |= decl.parts.iter().fold(decl.sense, |sense, part| sense | part.sense);
|
||||
state.members.push(decl);
|
||||
}
|
||||
|
||||
/// Places the `over` parts of the root `id` of `arena` (`member` false) or of its member `id` above the members joined
|
||||
/// so far, as `place_over_widgets` does, and records their shapes on `layer` through `clip`; journaled, so a replay
|
||||
/// places them above the members joined in the replaying pass.
|
||||
fn place_over(ctx: &Context, (arena, id, member, layer, clip): (ArenaId, Id, bool, Layer, Rect)) {
|
||||
crate::runtime::replay::journal(ctx, || (arena, id, member, layer, clip), |ctx, &args| place_over(ctx, args));
|
||||
let read = PassRead::of(ctx);
|
||||
let shapes = with_arena_in(ctx, read.viewport, arena, read.pass_nr, |state| {
|
||||
rotate(state, read.pass_nr);
|
||||
let at = over_position(state.members.len());
|
||||
match member {
|
||||
None => state.own_over_at = at,
|
||||
Some(id) => {
|
||||
if let Some(joined) = state.members.iter_mut().rev().find(|joined| joined.id == id) {
|
||||
joined.over_at = at;
|
||||
}
|
||||
}
|
||||
if !member {
|
||||
state.own_over_at = at;
|
||||
return Some(own_over_shapes(&state.own, id, at, read.target()));
|
||||
}
|
||||
let joined = state.members.iter_mut().rev().find(|joined| joined.id == id)?;
|
||||
joined.over_at = at;
|
||||
Some(member_over_shapes(joined, read.target()).collect())
|
||||
});
|
||||
if let Some(shapes) = shapes {
|
||||
pointer::add_shapes_in(ctx, read.viewport, layer, arena.0, clip, shapes);
|
||||
}
|
||||
}
|
||||
|
||||
/// What step 8 reads of the running pass, fetched once per root: the viewport, the pass number and the pointer
|
||||
@@ -689,20 +709,10 @@ pub(crate) fn place_over_widgets(ui: &Ui, id: Id, member: Option<ArenaId>, decls
|
||||
}
|
||||
let arena = member.unwrap_or_else(|| ArenaId::new(id));
|
||||
let layer = ui.layer_id();
|
||||
let read = PassRead::of(ctx);
|
||||
place_over(ctx, arena, member.map(|_| id));
|
||||
let shapes = with_arena_in(ctx, read.viewport, arena, read.pass_nr, |state| {
|
||||
if member.is_none() {
|
||||
return Some(own_over_shapes(&state.own, id, state.own_over_at, read.target()));
|
||||
}
|
||||
let joined = state.members.iter().rev().find(|joined| joined.id == id)?;
|
||||
Some(member_over_shapes(joined, read.target()).collect::<Vec<_>>())
|
||||
});
|
||||
let Some(shapes) = shapes else { return };
|
||||
pointer::record_shapes_in(ctx, read.viewport, layer, arena.0, clip, shapes);
|
||||
place_over(ctx, (arena, id, member.is_some(), layer, clip));
|
||||
// A root's widgets are in its widget record from step 8.
|
||||
if member.is_some() && !widgets.is_empty() {
|
||||
pointer::record_widgets_in(ctx, read.viewport, layer, arena.0, &widgets, decls.iter().any(|part| part.sense.contains(Sense::CLICK)));
|
||||
pointer::record_widgets(ctx, layer, arena.0, &widgets, decls.iter().any(|part| part.sense.contains(Sense::CLICK)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -756,27 +766,19 @@ pub(crate) fn sense_members(ui: &Ui, arena: ArenaId, specs: &[MemberSpec<'_>]) -
|
||||
let outcome = with_arena(ctx, arena, pass_nr, |state| {
|
||||
rotate(state, pass_nr);
|
||||
for spec in specs {
|
||||
let mut decl = MemberDecl::new(spec.id, spec.rect, spec.sense, spec.parts);
|
||||
decl.local = state.layer == Some(layer);
|
||||
let position = member_position(state.members.len());
|
||||
decl.over_at = position + 1;
|
||||
shapes.extend(member_shapes(&decl, position, target));
|
||||
state.union_sense |= decl.sense;
|
||||
for part in &decl.parts {
|
||||
state.union_sense |= part.sense;
|
||||
}
|
||||
joined.push((decl.local, known_in(state, spec.id)));
|
||||
let decl = MemberDecl::new(spec.id, spec.rect, spec.sense, spec.parts);
|
||||
if recording {
|
||||
recorded.push(decl.clone());
|
||||
}
|
||||
state.members.push(decl);
|
||||
join_member(state, decl, layer, target, &mut shapes);
|
||||
joined.push((state.layer == Some(layer), known_in(state, spec.id)));
|
||||
}
|
||||
state.outcome.filter(|outcome| outcome.pass == pass_nr).unwrap_or_default()
|
||||
});
|
||||
if recording {
|
||||
journal_join(ctx, arena, &recorded);
|
||||
journal_join(ctx, arena, layer, ui.clip_rect(), &recorded);
|
||||
}
|
||||
pointer::record_shapes(ctx, layer, arena.0, ui.clip_rect(), shapes);
|
||||
pointer::add_shapes_in(ctx, pass::current_viewport(ctx), layer, arena.0, ui.clip_rect(), shapes);
|
||||
specs
|
||||
.iter()
|
||||
.zip(joined)
|
||||
|
||||
@@ -260,8 +260,9 @@ pub(crate) struct Mark {
|
||||
|
||||
/// What one run of a child registered with the focus service: its stops, the scopes it pushed, the roving groups
|
||||
/// its stops belong to and its collections' row groups. A child out of view registers it again without running
|
||||
/// (`runtime::lazy`), so its stops keep their place in Tab and F6 order.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
/// (`runtime::lazy`), so its stops keep their place in Tab and F6 order. Two transcripts are equal when their replays
|
||||
/// register the same records under the same open scope (`Transcript::placed`).
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub(crate) struct Transcript {
|
||||
stops: Vec<StopRec>,
|
||||
scopes: Vec<(Id, ScopeRec)>,
|
||||
@@ -276,8 +277,34 @@ impl Transcript {
|
||||
pub(crate) fn stop_ids(&self) -> impl Iterator<Item = Id> + '_ {
|
||||
self.stops.iter().map(|rec| rec.stop.id)
|
||||
}
|
||||
|
||||
/// The transcript as a replay registers it with `outer` the innermost open scope: a record under the scope that
|
||||
/// was open around the run goes under `outer`, and a replayed scope is no new scope (`entering` is false).
|
||||
fn placed(&self, outer: Option<Id>) -> Transcript {
|
||||
let place = |scope: Option<Id>| if scope == self.outer { outer } else { scope };
|
||||
Transcript {
|
||||
stops: self.stops.iter().map(|rec| StopRec { stop: rec.stop.clone(), scope: place(rec.scope) }).collect(),
|
||||
scopes: self
|
||||
.scopes
|
||||
.iter()
|
||||
.map(|(id, scope)| (*id, ScopeRec { parent: place(scope.parent), entering: scope.entering.map(|_| false), ..scope.clone() }))
|
||||
.collect(),
|
||||
groups: self.groups.clone(),
|
||||
row_sets: self.row_sets.clone(),
|
||||
outer,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Transcript {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
let [a, b] = [self.placed(None), other.placed(None)];
|
||||
(a.stops, a.scopes, a.groups, a.row_sets) == (b.stops, b.scopes, b.groups, b.row_sets)
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for Transcript {}
|
||||
|
||||
/// Where the running pass's registrations stand.
|
||||
pub(crate) fn mark(ctx: &Context) -> Mark {
|
||||
with(ctx, |state| Mark {
|
||||
@@ -310,21 +337,19 @@ pub(crate) fn transcript(ctx: &Context, mark: Mark) -> Transcript {
|
||||
pub(crate) fn replay(ctx: &Context, transcript: &Transcript) {
|
||||
let frame = crate::runtime::egui::pass::frame_nr(ctx, viewport(ctx));
|
||||
with(ctx, |state| {
|
||||
let outer = state.open.last().map(|(id, _)| *id);
|
||||
let place = |scope: Option<Id>| if scope == transcript.outer { outer } else { scope };
|
||||
for (id, scope) in &transcript.scopes {
|
||||
let parent = place(scope.parent);
|
||||
state.scopes.insert(*id, ScopeRec { parent, entering: scope.entering.map(|_| false), ..scope.clone() });
|
||||
state.scope_order.push(*id);
|
||||
let placed = transcript.placed(state.open.last().map(|(id, _)| *id));
|
||||
for (id, scope) in placed.scopes {
|
||||
if scope.entering.is_none() {
|
||||
state.seen.insert(*id, frame);
|
||||
state.seen.insert(id, frame);
|
||||
}
|
||||
state.scopes.insert(id, scope);
|
||||
state.scope_order.push(id);
|
||||
}
|
||||
state.stops.extend(transcript.stops.iter().map(|rec| StopRec { stop: rec.stop.clone(), scope: place(rec.scope) }));
|
||||
for group in &transcript.groups {
|
||||
state.groups.insert(group.id, *group);
|
||||
state.stops.extend(placed.stops);
|
||||
for group in placed.groups {
|
||||
state.groups.insert(group.id, group);
|
||||
}
|
||||
state.row_sets.extend(transcript.row_sets.iter().cloned());
|
||||
state.row_sets.extend(placed.row_sets);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -374,6 +374,12 @@ pub(crate) fn record_shapes_in(ctx: &Context, viewport: ViewportId, layer: Layer
|
||||
|| (layer, root, clip, shapes.clone()),
|
||||
|ctx, (layer, root, clip, shapes)| record_shapes(ctx, *layer, *root, *clip, shapes.iter().cloned()),
|
||||
);
|
||||
add_shapes_in(ctx, viewport, layer, root, clip, shapes);
|
||||
}
|
||||
|
||||
/// `record_shapes_in` with no note in the run records, for a caller whose own note makes the shapes again: shapes at
|
||||
/// stack positions a replay takes from the arena it joins (`sensing`), not from the recorded run.
|
||||
pub(crate) fn add_shapes_in(ctx: &Context, viewport: ViewportId, layer: Layer, root: Id, clip: Rect, shapes: Vec<Shape<Id>>) {
|
||||
PerViewport::<Shapes>::with(ctx, viewport, |record| {
|
||||
let order = record.next;
|
||||
record.next += 1;
|
||||
|
||||
@@ -213,10 +213,11 @@ fn with_open(ctx: &Context, f: impl FnOnce(&mut Facts)) {
|
||||
|
||||
/// Notes, in the records open around the running code, the registration the running code made: `again(ctx, &args)`
|
||||
/// registers it again. `args` builds the arguments only while a record is open. `again` captures nothing (a function
|
||||
/// or a closure without captures; a capture does not compile), so everything the registration depends on is in the
|
||||
/// arguments, and two notes of the same `again` with equal arguments are the same registration. A service outside
|
||||
/// this crate that keeps registrations per pass (a dock's drop spots) notes each one here, so a settled region's replay
|
||||
/// makes it again.
|
||||
/// or a closure without captures), so everything the registration depends on is in the arguments. A capture fails
|
||||
/// the build, not `cargo check`: the size assert is a constant that is evaluated only when the code is generated.
|
||||
/// Two notes of the same `again` with equal arguments are the same registration. A service outside this crate that
|
||||
/// keeps registrations per pass (a dock's drop spots) notes each one here, so a settled region's replay makes it
|
||||
/// again.
|
||||
pub fn journal<A, F>(ctx: &Context, args: impl FnOnce() -> A, again: F)
|
||||
where
|
||||
A: PartialEq + fmt::Debug + Send + Sync + 'static,
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
//! Trials of the replay records: their notes and their comparison.
|
||||
|
||||
use crate::primitives::sensing::{self, MemberSpec};
|
||||
use crate::runtime::egui::{fixture, repaint};
|
||||
use crate::runtime::focus::{self, KeyUse, Stop};
|
||||
use crate::runtime::env::ArenaId;
|
||||
use crate::runtime::focus::{self, FocusPolicy, KeyUse, Stop};
|
||||
use crate::runtime::replay;
|
||||
use crate::testing::{Frames, trial};
|
||||
use crate::testing::{Frames, pointer, trial};
|
||||
use craft_behavior::Sense;
|
||||
use egui::{Pos2, Rect, Vec2};
|
||||
|
||||
// A job's thread asks for a pass (a finished job's wake, a progress report) while a pass of the same context records a
|
||||
// region on the UI thread: the request notes nothing in that pass's records and leaves its pass state alone.
|
||||
@@ -83,3 +87,88 @@ fn a_transcript_with_one_changed_stop_compares_unequal() {
|
||||
assert_eq!(first.focus, again.focus);
|
||||
assert_ne!(first.focus, changed.focus, "the second stop's keys changed");
|
||||
}
|
||||
|
||||
/// Records `stops` run inside a record, with `entering` the first-pass flag of the layer scope it pushes, inside the
|
||||
/// scope `outer` when one is given.
|
||||
fn layer_scope_record(frames: &mut Frames, outer: Option<egui::Id>, entering: bool) -> replay::Record {
|
||||
let [scope, stop] = [fixture::named_id("layer scope"), fixture::named_id("stop")];
|
||||
let mut record = None;
|
||||
frames.frame(Vec::new(), |ui| {
|
||||
let ctx = ui.ctx().clone();
|
||||
if let Some(outer) = outer {
|
||||
focus::push_scope(&ctx, outer, FocusPolicy::NONE, None);
|
||||
}
|
||||
replay::open(&ctx);
|
||||
focus::register_stop(&ctx, Stop::new(fixture::named_id("beside"), KeyUse::CODE));
|
||||
focus::push_scope_from(&ctx, scope, FocusPolicy::NONE, None, None, false, Some(entering));
|
||||
focus::register_stop(&ctx, Stop::new(stop, KeyUse::CODE));
|
||||
focus::pop_scope(&ctx);
|
||||
record = Some(replay::close(&ctx, fixture::layer_of(ui)));
|
||||
if outer.is_some() {
|
||||
focus::pop_scope(&ctx);
|
||||
}
|
||||
});
|
||||
record.expect("the frame ran")
|
||||
}
|
||||
|
||||
// A view recorded in the pass its layer appeared (`entering`) and the real run of the next pass register the same
|
||||
// once replayed: a replayed scope is no new scope.
|
||||
#[trial]
|
||||
fn a_first_pass_transcript_equals_the_next_real_run() {
|
||||
let mut frames = Frames::new();
|
||||
let first = layer_scope_record(&mut frames, None, true);
|
||||
let next = layer_scope_record(&mut frames, None, false);
|
||||
assert_eq!(first.focus, next.focus);
|
||||
}
|
||||
|
||||
// The same view shown inside two different scopes registers the same records under each.
|
||||
#[trial]
|
||||
fn a_transcript_under_another_open_scope_compares_equal() {
|
||||
let mut frames = Frames::new();
|
||||
let one = layer_scope_record(&mut frames, Some(fixture::named_id("outer one")), false);
|
||||
let other = layer_scope_record(&mut frames, Some(fixture::named_id("outer other")), false);
|
||||
assert_eq!(one.focus, other.focus);
|
||||
}
|
||||
|
||||
// A child recorded as the arena's first member and replayed after two members that cover it stacks above them, as a
|
||||
// real run after them would: its stack positions come from the arena it joins, not from the recorded run.
|
||||
#[trial]
|
||||
fn a_replayed_member_stacks_above_the_members_before_it() {
|
||||
let mut frames = Frames::new();
|
||||
let arena = ArenaId::new(fixture::named_id("arena"));
|
||||
let [before, cover, child] = [fixture::named_id("before"), fixture::named_id("cover"), fixture::named_id("child")];
|
||||
let rect = Rect::from_min_size(Pos2::new(20.0, 20.0), Vec2::splat(40.0));
|
||||
let spec = |id, rect| MemberSpec { id, rect, sense: Sense::HOVER | Sense::CLICK, parts: &[], enabled: true };
|
||||
let mut recorded = None;
|
||||
frames.frame(Vec::new(), |ui| {
|
||||
let ctx = ui.ctx().clone();
|
||||
replay::open(&ctx);
|
||||
sensing::sense_members(ui, arena, &[spec(child, rect)]);
|
||||
recorded = Some(replay::close(&ctx, fixture::layer_of(ui)));
|
||||
});
|
||||
let recorded = recorded.expect("the frame ran");
|
||||
let shown = |ui: &mut egui::Ui, replayed: bool| {
|
||||
sensing::sense_members(ui, arena, &[spec(before, Rect::from_min_size(Pos2::ZERO, Vec2::splat(10.0))), spec(cover, rect)]);
|
||||
if replayed {
|
||||
replay::replay(ui.ctx(), &recorded);
|
||||
return None;
|
||||
}
|
||||
let ctx = ui.ctx().clone();
|
||||
replay::open(&ctx);
|
||||
sensing::sense_members(ui, arena, &[spec(child, rect)]);
|
||||
Some(replay::close(&ctx, fixture::layer_of(ui)))
|
||||
};
|
||||
let mut real = None;
|
||||
frames.frame(Vec::new(), |ui| real = shown(ui, false));
|
||||
let real = real.expect("the frame ran");
|
||||
assert_eq!(recorded.journal, real.journal, "the join is noted as declared, without its stack positions");
|
||||
frames.frame(Vec::new(), |ui| {
|
||||
let _ = shown(ui, true);
|
||||
});
|
||||
let mut hovered = None;
|
||||
frames.frame(vec![pointer::moved(rect.center())], |ui| {
|
||||
hovered = Some([crate::runtime::pointer::hovers(ui.ctx(), child), crate::runtime::pointer::hovers(ui.ctx(), cover)]);
|
||||
let _ = shown(ui, true);
|
||||
});
|
||||
assert_eq!(hovered, Some([true, false]), "the replayed child is above the member that covers it");
|
||||
}
|
||||
Reference in new issue
Block a user