diff --git a/Cargo.lock b/Cargo.lock index 625ed4613..76a8ff488 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5324,7 +5324,7 @@ dependencies = [ [[package]] name = "ecolor" version = "0.37.0" -source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d" +source = "git+https://github.com/storytold/egui.git?rev=7e1c30d142bc7879d043e74b5ceb3ecfe7f51329#7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" dependencies = [ "bytemuck", "emath", @@ -5380,7 +5380,7 @@ dependencies = [ [[package]] name = "eframe" version = "0.37.0" -source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d" +source = "git+https://github.com/storytold/egui.git?rev=7e1c30d142bc7879d043e74b5ceb3ecfe7f51329#7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" dependencies = [ "ahash", "bytemuck", @@ -5415,7 +5415,7 @@ dependencies = [ [[package]] name = "egui" version = "0.37.0" -source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d" +source = "git+https://github.com/storytold/egui.git?rev=7e1c30d142bc7879d043e74b5ceb3ecfe7f51329#7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" dependencies = [ "accesskit", "ahash", @@ -5434,7 +5434,7 @@ dependencies = [ [[package]] name = "egui-wgpu" version = "0.37.0" -source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d" +source = "git+https://github.com/storytold/egui.git?rev=7e1c30d142bc7879d043e74b5ceb3ecfe7f51329#7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" dependencies = [ "ahash", "bytemuck", @@ -5453,7 +5453,7 @@ dependencies = [ [[package]] name = "egui-winit" version = "0.37.0" -source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d" +source = "git+https://github.com/storytold/egui.git?rev=7e1c30d142bc7879d043e74b5ceb3ecfe7f51329#7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" dependencies = [ "block2 0.6.2", "bytemuck", @@ -5473,7 +5473,7 @@ dependencies = [ [[package]] name = "egui_glow" version = "0.37.0" -source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d" +source = "git+https://github.com/storytold/egui.git?rev=7e1c30d142bc7879d043e74b5ceb3ecfe7f51329#7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" dependencies = [ "bytemuck", "egui", @@ -5486,7 +5486,7 @@ dependencies = [ [[package]] name = "egui_kittest" version = "0.37.0" -source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d" +source = "git+https://github.com/storytold/egui.git?rev=7e1c30d142bc7879d043e74b5ceb3ecfe7f51329#7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" dependencies = [ "eframe", "egui", @@ -5563,7 +5563,7 @@ dependencies = [ [[package]] name = "emath" version = "0.37.0" -source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d" +source = "git+https://github.com/storytold/egui.git?rev=7e1c30d142bc7879d043e74b5ceb3ecfe7f51329#7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" dependencies = [ "bytemuck", ] @@ -5636,7 +5636,7 @@ dependencies = [ [[package]] name = "epaint" version = "0.37.0" -source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d" +source = "git+https://github.com/storytold/egui.git?rev=7e1c30d142bc7879d043e74b5ceb3ecfe7f51329#7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" dependencies = [ "ahash", "bytemuck", @@ -5662,7 +5662,7 @@ dependencies = [ [[package]] name = "epaint_default_fonts" version = "0.37.0" -source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d" +source = "git+https://github.com/storytold/egui.git?rev=7e1c30d142bc7879d043e74b5ceb3ecfe7f51329#7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" [[package]] name = "equator" @@ -14980,19 +14980,19 @@ dependencies = [ [[patch.unused]] name = "egui_demo_lib" version = "0.37.0" -source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d" +source = "git+https://github.com/storytold/egui.git?rev=7e1c30d142bc7879d043e74b5ceb3ecfe7f51329#7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" [[patch.unused]] name = "egui_extras" version = "0.37.0" -source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d" +source = "git+https://github.com/storytold/egui.git?rev=7e1c30d142bc7879d043e74b5ceb3ecfe7f51329#7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" [[patch.unused]] name = "egui_inspection" version = "0.37.0" -source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d" +source = "git+https://github.com/storytold/egui.git?rev=7e1c30d142bc7879d043e74b5ceb3ecfe7f51329#7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" [[patch.unused]] name = "egui_system_font_provider" version = "0.37.0" -source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d" +source = "git+https://github.com/storytold/egui.git?rev=7e1c30d142bc7879d043e74b5ceb3ecfe7f51329#7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" diff --git a/Cargo.toml b/Cargo.toml index 4c4920066..c8a661561 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -294,20 +294,20 @@ hayro-jbig2 = { path = "vendor/hayro-jbig2" } hayro-cmap = { path = "vendor/hayro-cmap" } hayro-postscript = { path = "vendor/hayro-postscript" } linesweeper = { path = "vendor/linesweeper" } -ecolor = { git = "https://github.com/storytold/egui.git", rev = "e9711ca9b69772f875ae0d1cef1977ff8c16234d" } -emath = { git = "https://github.com/storytold/egui.git", rev = "e9711ca9b69772f875ae0d1cef1977ff8c16234d" } -epaint = { git = "https://github.com/storytold/egui.git", rev = "e9711ca9b69772f875ae0d1cef1977ff8c16234d" } -epaint_default_fonts = { git = "https://github.com/storytold/egui.git", rev = "e9711ca9b69772f875ae0d1cef1977ff8c16234d" } -egui = { git = "https://github.com/storytold/egui.git", rev = "e9711ca9b69772f875ae0d1cef1977ff8c16234d" } -egui-winit = { git = "https://github.com/storytold/egui.git", rev = "e9711ca9b69772f875ae0d1cef1977ff8c16234d" } -egui-wgpu = { git = "https://github.com/storytold/egui.git", rev = "e9711ca9b69772f875ae0d1cef1977ff8c16234d" } -egui_glow = { git = "https://github.com/storytold/egui.git", rev = "e9711ca9b69772f875ae0d1cef1977ff8c16234d" } -egui_extras = { git = "https://github.com/storytold/egui.git", rev = "e9711ca9b69772f875ae0d1cef1977ff8c16234d" } -egui_kittest = { git = "https://github.com/storytold/egui.git", rev = "e9711ca9b69772f875ae0d1cef1977ff8c16234d" } -egui_system_font_provider = { git = "https://github.com/storytold/egui.git", rev = "e9711ca9b69772f875ae0d1cef1977ff8c16234d" } -eframe = { git = "https://github.com/storytold/egui.git", rev = "e9711ca9b69772f875ae0d1cef1977ff8c16234d" } -egui_demo_lib = { git = "https://github.com/storytold/egui.git", rev = "e9711ca9b69772f875ae0d1cef1977ff8c16234d" } -egui_inspection = { git = "https://github.com/storytold/egui.git", rev = "e9711ca9b69772f875ae0d1cef1977ff8c16234d" } +ecolor = { git = "https://github.com/storytold/egui.git", rev = "7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" } +emath = { git = "https://github.com/storytold/egui.git", rev = "7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" } +epaint = { git = "https://github.com/storytold/egui.git", rev = "7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" } +epaint_default_fonts = { git = "https://github.com/storytold/egui.git", rev = "7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" } +egui = { git = "https://github.com/storytold/egui.git", rev = "7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" } +egui-winit = { git = "https://github.com/storytold/egui.git", rev = "7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" } +egui-wgpu = { git = "https://github.com/storytold/egui.git", rev = "7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" } +egui_glow = { git = "https://github.com/storytold/egui.git", rev = "7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" } +egui_extras = { git = "https://github.com/storytold/egui.git", rev = "7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" } +egui_kittest = { git = "https://github.com/storytold/egui.git", rev = "7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" } +egui_system_font_provider = { git = "https://github.com/storytold/egui.git", rev = "7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" } +eframe = { git = "https://github.com/storytold/egui.git", rev = "7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" } +egui_demo_lib = { git = "https://github.com/storytold/egui.git", rev = "7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" } +egui_inspection = { git = "https://github.com/storytold/egui.git", rev = "7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" } winit = { git = "https://github.com/storytold/winit.git", rev = "a517b3846ee8457d84ee4975110bfda6797c44dc" } # symphonia's FLAC decoder restores LPC samples as `residual + (prediction as i32)`. In a valid diff --git a/crates/behavior/src/keys.rs b/crates/behavior/src/keys.rs index 5c8bf19da..a24e52bf5 100644 --- a/crates/behavior/src/keys.rs +++ b/crates/behavior/src/keys.rs @@ -8,7 +8,7 @@ use craft_host::target::Os; use craft_keymap::{Chord, Key, Mods}; /// What the focused stop is, as far as the table's rows differ. -#[derive(Clone, Copy, Debug, Default)] +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] #[non_exhaustive] pub struct KeyRole { /// A value control with a reset value and no text part: Delete resets it. diff --git a/crates/craft/src/egui_fork.rs b/crates/craft/src/egui_fork.rs index 2e5c61777..9ddb438ce 100644 --- a/crates/craft/src/egui_fork.rs +++ b/crates/craft/src/egui_fork.rs @@ -3,7 +3,7 @@ /// The egui fork the library is built and tested with. Apps patch egui to exactly this. pub const GIT: &str = "https://github.com/storytold/egui.git"; /// The commit of [`GIT`]. -pub const REV: &str = "e9711ca9b69772f875ae0d1cef1977ff8c16234d"; +pub const REV: &str = "7e1c30d142bc7879d043e74b5ceb3ecfe7f51329"; /// The packages the workspace patches to the fork (the `[patch.crates-io]` entries that name [`GIT`]). pub const PACKAGES: &[&str] = &[ diff --git a/crates/ui-contract/src/contract/lazy.rs b/crates/ui-contract/src/contract/lazy.rs index c65a6cf75..1e9045c55 100644 --- a/crates/ui-contract/src/contract/lazy.rs +++ b/crates/ui-contract/src/contract/lazy.rs @@ -137,7 +137,7 @@ impl Lazy { let real = !sizing && !crate::runtime::measure::active(&ctx); let owner = view.map(|view| view.owner); // A run around this one that is replayed later keeps the child's entry. - replay::journal(&ctx, || move |ctx: &egui::Context| lazy::keep(ctx, owner, id)); + replay::journal(&ctx, || (owner, id), |ctx, &(owner, id)| lazy::keep(ctx, owner, id)); if let Some(view) = view { let at = |size: Vec2| { let x = if rtl { available.max.x - size.x } else { available.min.x }; diff --git a/crates/ui-contract/src/primitives/sensing.rs b/crates/ui-contract/src/primitives/sensing.rs index 8a6fe8269..7cce1927b 100644 --- a/crates/ui-contract/src/primitives/sensing.rs +++ b/crates/ui-contract/src/primitives/sensing.rs @@ -113,7 +113,7 @@ impl TextOutcome { /// A part as the sensing core registers it. // F2-MERGE: F3 (`PartBuilder` adds tooltip and `pointer_only`) -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq)] pub(crate) struct PartDecl { pub key: PartKey, pub index: Option, @@ -153,7 +153,7 @@ impl PartDecl { } /// One member's registration for this pass. -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq)] pub(crate) struct MemberDecl { pub id: Id, pub rect: Rect, @@ -174,7 +174,7 @@ impl MemberDecl { } /// What a root registers at step 8: its id and rect. -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Copy, Debug, PartialEq)] struct RootInfo { id: Id, rect: Rect, @@ -396,7 +396,7 @@ pub fn cancel_press_of(ctx: &Context, owner: Id) { /// Records the transformed body's egui layer. pub(crate) fn set_layer(ctx: &Context, arena: ArenaId, layer: Layer) { - crate::runtime::replay::journal(ctx, || move |ctx: &Context| set_layer(ctx, arena, layer)); + crate::runtime::replay::journal(ctx, || (arena, layer), |ctx, &(arena, layer)| set_layer(ctx, arena, layer)); let pass_nr = pass::pass_nr(ctx, pass::current_viewport(ctx)); with_arena(ctx, arena, pass_nr, |state| state.layer = Some(layer)); } @@ -408,7 +408,7 @@ pub(crate) fn set_layer(ctx: &Context, arena: ArenaId, layer: Layer) { // names their shapes as the press winner. /// What a root registers in its arena at step 8, kept for a replay. -#[derive(Clone)] +#[derive(Clone, Debug, PartialEq)] struct RootRegistration { info: RootInfo, own: Vec, @@ -419,10 +419,11 @@ struct RootRegistration { /// Notes a root's step-8 registration in the open records. fn journal_root(ctx: &Context, arena: ArenaId, info: RootInfo, own: &[PartDecl], widgets: &[Id], (layer, sense): (Layer, Sense)) { - crate::runtime::replay::journal(ctx, || { - let registration = RootRegistration { info, own: own.to_vec(), widgets: widgets.to_vec(), layer, sense }; - move |ctx: &Context| register_root_again(ctx, arena, registration.clone()) - }); + crate::runtime::replay::journal( + ctx, + || (arena, RootRegistration { info, own: own.to_vec(), widgets: widgets.to_vec(), layer, sense }), + |ctx, (arena, registration)| register_root_again(ctx, *arena, registration.clone()), + ); } /// A replayed root's step-8 registration: its own parts, rect, widgets, layer and sense, with no press run. @@ -439,44 +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, || { - let joined = joined.to_vec(); - move |ctx: &Context| 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) { - 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) { - crate::runtime::replay::journal(ctx, || move |ctx: &Context| 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>) { + 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 @@ -691,20 +709,10 @@ pub(crate) fn place_over_widgets(ui: &Ui, id: Id, member: Option, 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::>()) - }); - 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))); } } @@ -758,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) diff --git a/crates/ui-contract/src/runtime/commands/board.rs b/crates/ui-contract/src/runtime/commands/board.rs index ca350161d..85673f2e0 100644 --- a/crates/ui-contract/src/runtime/commands/board.rs +++ b/crates/ui-contract/src/runtime/commands/board.rs @@ -762,7 +762,7 @@ pub(crate) fn register_instance(ctx: &Context, scope: CommandScope, id: Id) { /// `register_instance` in the layer `layer`, which a replay of the registration keeps. fn register_in(ctx: &Context, scope: CommandScope, id: Id, layer: Option) { - crate::runtime::replay::journal(ctx, || move |ctx: &Context| register_in(ctx, scope, id, layer)); + crate::runtime::replay::journal(ctx, || (scope, id, layer), |ctx, &(scope, id, layer)| register_in(ctx, scope, id, layer)); let viewport = pass::current_viewport(ctx); let stamp = pass::pass_nr(ctx, viewport); with(ctx, |board| board.instances.upsert(Instance { id, scope, viewport, stamp, layer })); @@ -1079,10 +1079,11 @@ pub(crate) fn publish(ctx: &Context, chain: &CommandChain, command: &CommandRef, if !measure::effect(ctx, "commands::publish") { return; } - replay::journal(ctx, || { - let (chain, command, state) = (chain.clone(), command.clone(), state.clone()); - move |ctx: &Context| publish(ctx, &chain, &command, state.clone()) - }); + replay::journal( + ctx, + || (chain.clone(), command.clone(), state.clone()), + |ctx, (chain, command, state)| publish(ctx, chain, command, state.clone()), + ); let changed = with(ctx, |board| { let scope = board.scope_of(command); let declared = board.def(&command.id).is_some_and(|def| def.modal == ModalRule::Declared); diff --git a/crates/ui-contract/src/runtime/commands/mod.rs b/crates/ui-contract/src/runtime/commands/mod.rs index a4cf272e8..3c9592467 100644 --- a/crates/ui-contract/src/runtime/commands/mod.rs +++ b/crates/ui-contract/src/runtime/commands/mod.rs @@ -343,7 +343,7 @@ impl CommandInput { /// The chain of container instances that receive scoped commands, innermost last. A private inherited field of /// the env. #[doc(hidden)] -#[derive(Clone, Default, PartialEq, Eq, Hash)] +#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)] pub struct CommandChain(Arc<[(CommandScope, Id)]>); impl CommandChain { diff --git a/crates/ui-contract/src/runtime/drag.rs b/crates/ui-contract/src/runtime/drag.rs index 3e161c614..fdfea4948 100644 --- a/crates/ui-contract/src/runtime/drag.rs +++ b/crates/ui-contract/src/runtime/drag.rs @@ -505,10 +505,11 @@ pub(crate) fn show_mark(ctx: &Context, mark: Option) { /// Records that `owner`'s `ordinal`th target on `layer` accepts at the pointer, with its mark in layer space and the /// tokens of the target's region. A `window` target covers the whole window. pub(crate) fn register(ctx: &Context, owner: Id, layer: Layer, mark: DropMark, tokens: Arc, window: bool) { - crate::runtime::replay::journal(ctx, || { - let tokens = tokens.clone(); - move |ctx: &Context| register(ctx, owner, layer, mark, tokens.clone(), window) - }); + crate::runtime::replay::journal( + ctx, + || (owner, layer, mark, tokens.clone(), window), + |ctx, (owner, layer, mark, tokens, window)| register(ctx, *owner, *layer, *mark, tokens.clone(), *window), + ); let mark = match areas::layer_map(ctx, layer) { Some(map) => mark.mapped(|point| map.to_screen(point)), None => mark, diff --git a/crates/ui-contract/src/runtime/egui/access.rs b/crates/ui-contract/src/runtime/egui/access.rs index a996c6550..78d35ab30 100644 --- a/crates/ui-contract/src/runtime/egui/access.rs +++ b/crates/ui-contract/src/runtime/egui/access.rs @@ -137,7 +137,7 @@ pub(crate) fn register_parent(ctx: &Context, id: Id, parent: Id) { /// Notes that the runtime emitted the node of `id`. fn emit(ctx: &Context, id: Id) { with_emitted(ctx, |emitted| emitted.emit(id)); - crate::runtime::replay::journal(ctx, || move |ctx: &Context| emit(ctx, id)); + crate::runtime::replay::journal(ctx, || id, |ctx, &id| emit(ctx, id)); } /// Notes that the node of the text widget `widget` folds into the node of `owner`. @@ -145,7 +145,7 @@ fn note_folded(ctx: &Context, widget: Id, owner: Id) { with_emitted(ctx, |emitted| { emitted.folded.insert(widget, owner); }); - crate::runtime::replay::journal(ctx, || move |ctx: &Context| note_folded(ctx, widget, owner)); + crate::runtime::replay::journal(ctx, || (widget, owner), |ctx, &(widget, owner)| note_folded(ctx, widget, owner)); } /// Notes that the runtime wrote the node of `id`. @@ -153,7 +153,7 @@ pub(crate) fn note_written(ctx: &Context, id: Id) { with_emitted(ctx, |emitted| { emitted.written.insert(id); }); - crate::runtime::replay::journal(ctx, || move |ctx: &Context| note_written(ctx, id)); + crate::runtime::replay::journal(ctx, || id, |ctx, &id| note_written(ctx, id)); } /// The pass-end step: the children of each node the runtime wrote follow the order the runtime emitted them in, diff --git a/crates/ui-contract/src/runtime/focus.rs b/crates/ui-contract/src/runtime/focus.rs index e4615e280..03b786668 100644 --- a/crates/ui-contract/src/runtime/focus.rs +++ b/crates/ui-contract/src/runtime/focus.rs @@ -42,7 +42,7 @@ pub enum FocusShape { pub(crate) type StopKey = (Id, Option); /// A registered stop with its place in the scope tree. -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq, Eq)] #[doc(hidden)] pub struct StopRec { #[doc(hidden)] @@ -50,7 +50,7 @@ pub struct StopRec { pub(crate) scope: Option, } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq, Eq)] pub(crate) struct ScopeRec { pub(crate) policy: FocusPolicy, pub(crate) parent: Option, @@ -116,7 +116,7 @@ pub(crate) struct BeginIn { /// The row groups of one collection's visible rows in a pass: every one is a flat roving group of the same owner /// and direction, so the collection registers them as one record. -#[derive(Clone)] +#[derive(Clone, Debug, PartialEq, Eq)] pub(crate) struct RowGroups { owner: Id, rtl: bool, @@ -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, Default)] +/// (`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, scopes: Vec<(Id, ScopeRec)>, @@ -276,8 +277,34 @@ impl Transcript { pub(crate) fn stop_ids(&self) -> impl Iterator + '_ { 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) -> Transcript { + let place = |scope: Option| 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| 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); }); } @@ -431,7 +456,7 @@ pub(crate) fn pop_scope(ctx: &Context) { /// Registers that the menu bar `bar` takes the menu-bar keys of the next input hook (spec 9.7). A settled bar /// registers again when it replays its last run, and the keys it takes run it (`keys::routed_to`). pub(crate) fn register_menu_bar(ctx: &Context, bar: Id) { - crate::runtime::replay::journal(ctx, || move |ctx: &Context| register_menu_bar(ctx, bar)); + crate::runtime::replay::journal(ctx, || bar, |ctx, &bar| register_menu_bar(ctx, bar)); with(ctx, |state| state.menu_bar = Some(bar)); } diff --git a/crates/ui-contract/src/runtime/focus/policy.rs b/crates/ui-contract/src/runtime/focus/policy.rs index 93e8500b0..db028b674 100644 --- a/crates/ui-contract/src/runtime/focus/policy.rs +++ b/crates/ui-contract/src/runtime/focus/policy.rs @@ -250,7 +250,7 @@ pub enum EndKey { } /// A component's declared keys. -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[non_exhaustive] pub struct KeyUse { /// The keys kept from the key stage. diff --git a/crates/ui-contract/src/runtime/focus/registration.rs b/crates/ui-contract/src/runtime/focus/registration.rs index 5c525d8f7..f2d9fb531 100644 --- a/crates/ui-contract/src/runtime/focus/registration.rs +++ b/crates/ui-contract/src/runtime/focus/registration.rs @@ -8,7 +8,7 @@ use craft_behavior::{KeyRole, PartRef}; use egui::{Context, Id}; /// A stop as registered at step 7 of the pipeline. -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq, Eq)] #[non_exhaustive] pub struct Stop { /// The component. @@ -124,7 +124,7 @@ impl Stop { } /// A roving group's registration: one Tab stop whose members take moves. -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[non_exhaustive] pub struct Group { /// The group. diff --git a/crates/ui-contract/src/runtime/keys/nodes.rs b/crates/ui-contract/src/runtime/keys/nodes.rs index f6e13fbac..2266a989b 100644 --- a/crates/ui-contract/src/runtime/keys/nodes.rs +++ b/crates/ui-contract/src/runtime/keys/nodes.rs @@ -10,7 +10,7 @@ use egui::{Context, Id}; pub(crate) fn redirect_node(ctx: &Context, node: Id, owner: Id, part: PartRef) { // Journaled after the `register_node` it amends, so a settled region's replay leaves the row's node with the // collection, as a run does. - crate::runtime::replay::journal(ctx, || move |ctx: &Context| redirect_node(ctx, node, owner, part)); + crate::runtime::replay::journal(ctx, || (node, owner, part), |ctx, &(node, owner, part)| redirect_node(ctx, node, owner, part)); current(ctx, |stage| { if let Some(entry) = stage.nodes.get_mut(&node_key(node)) { entry.owner = owner; @@ -22,7 +22,7 @@ pub(crate) fn redirect_node(ctx: &Context, node: Id, owner: Id, part: PartRef) { /// Records the expanded state of the node registered by `register_node`: an `Expand` or `Collapse` request on a node /// with the state asks to change it, and acts as the node's activation. pub(crate) fn register_expanded(ctx: &Context, node: Id, expanded: Option) { - crate::runtime::replay::journal(ctx, || move |ctx: &Context| register_expanded(ctx, node, expanded)); + crate::runtime::replay::journal(ctx, || (node, expanded), |ctx, &(node, expanded)| register_expanded(ctx, node, expanded)); current(ctx, |stage| { if let Some(entry) = stage.nodes.get_mut(&node_key(node)) { entry.expanded = expanded; @@ -32,7 +32,11 @@ pub(crate) fn register_expanded(ctx: &Context, node: Id, expanded: Option) /// Registers an AccessKit node so a request that targets it reaches `owner`. pub(crate) fn register_node(ctx: &Context, node: Id, owner: Id, part: Option, focusable: bool) { - crate::runtime::replay::journal(ctx, || move |ctx: &Context| register_node(ctx, node, owner, part, focusable)); + crate::runtime::replay::journal( + ctx, + || (node, owner, part, focusable), + |ctx, &(node, owner, part, focusable)| register_node(ctx, node, owner, part, focusable), + ); current(ctx, |stage| { stage.nodes.insert(node_key(node), Node { owner, part, focusable, expanded: None }); }); diff --git a/crates/ui-contract/src/runtime/layout_memory.rs b/crates/ui-contract/src/runtime/layout_memory.rs index e2c4c79a3..a1b6b9832 100644 --- a/crates/ui-contract/src/runtime/layout_memory.rs +++ b/crates/ui-contract/src/runtime/layout_memory.rs @@ -1117,19 +1117,21 @@ pub(crate) fn ask_discard(ctx: &Context) { /// Notes in the run records open around the running code that a replay keeps the entry and the kept transcript of /// `id`: the replayed run does not reach them, and an entry no pass touched in a frame is dropped. fn journal_keep(ctx: &Context, id: Id) { - crate::runtime::replay::journal(ctx, || { - move |ctx: &Context| { + crate::runtime::replay::journal( + ctx, + || id, + |ctx, id| { let moment = Moment::of(ctx); with(ctx, &moment, |memory| { - if let Some(entry) = memory.entries.get_mut(&id) { + if let Some(entry) = memory.entries.get_mut(id) { entry.touched = moment.frame; } - if let Some(kept) = memory.kept.get_mut(&id) { + if let Some(kept) = memory.kept.get_mut(id) { kept.touched = moment.frame; } }); - } - }); + }, + ); } /// The end hook: asks egui for at most one discard pass per frame, and for a repaint when a change cannot get one, so diff --git a/crates/ui-contract/src/runtime/memory.rs b/crates/ui-contract/src/runtime/memory.rs index 4ebedc657..4088ec89c 100644 --- a/crates/ui-contract/src/runtime/memory.rs +++ b/crates/ui-contract/src/runtime/memory.rs @@ -230,7 +230,7 @@ struct Seen(u64); /// Registers `owner`'s pass, at step 3: eviction tests only owners. Shown entries of an owner not registered /// for 3 passes of the viewport that last showed it and 0.5 s are evicted by `evict`. pub(crate) fn register_owner(ctx: &Context, owner: Id) { - crate::runtime::replay::journal(ctx, || move |ctx: &Context| register_owner(ctx, owner)); + crate::runtime::replay::journal(ctx, || owner, |ctx, &owner| register_owner(ctx, owner)); let facts = frame::facts(ctx); pass_state::with::(ctx, |reg| { reg.owners.insert(owner, OwnerRecord { viewport: facts.viewport, pass: facts.pass_nr, at: facts.now }); diff --git a/crates/ui-contract/src/runtime/pointer.rs b/crates/ui-contract/src/runtime/pointer.rs index beeb9b20f..68665ed38 100644 --- a/crates/ui-contract/src/runtime/pointer.rs +++ b/crates/ui-contract/src/runtime/pointer.rs @@ -369,10 +369,17 @@ pub(crate) fn record_shapes(ctx: &Context, layer: Layer, root: Id, clip: Rect, s /// `record_shapes` for a caller that already read the running viewport. pub(crate) fn record_shapes_in(ctx: &Context, viewport: ViewportId, layer: Layer, root: Id, clip: Rect, shapes: impl IntoIterator>) { let shapes: Vec> = shapes.into_iter().collect(); - crate::runtime::replay::journal(ctx, || { - let shapes = shapes.clone(); - move |ctx: &Context| record_shapes(ctx, layer, root, clip, shapes.iter().cloned()) - }); + crate::runtime::replay::journal( + ctx, + || (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>) { PerViewport::::with(ctx, viewport, |record| { let order = record.next; record.next += 1; @@ -385,7 +392,7 @@ pub(crate) fn record_shapes_in(ctx: &Context, viewport: ViewportId, layer: Layer /// Records the egui widget `widget` that the component `owner` made for its own text (a field's text, a selection /// part): a press egui gives it is the widget's own and goes to no root, and the hover goes to `owner`. pub(crate) fn record_text_widget(ctx: &Context, widget: Id, owner: Id) { - crate::runtime::replay::journal(ctx, || move |ctx: &Context| record_text_widget(ctx, widget, owner)); + crate::runtime::replay::journal(ctx, || (widget, owner), |ctx, &(widget, owner)| record_text_widget(ctx, widget, owner)); PerViewport::::with(ctx, pass::current_viewport(ctx), |record| { record.this_texts.insert(widget, owner); }); @@ -403,7 +410,7 @@ pub(crate) fn record_layer_widget(ctx: &Context, widget: Id) { /// Records that `layer` shows the transformed body of the root `root`, which is itself shown on `parent`: a press on /// the body layer reaches the root's own shapes beneath the body's. pub(crate) fn record_body_layer(ctx: &Context, layer: Layer, parent: Layer, root: Id) { - crate::runtime::replay::journal(ctx, || move |ctx: &Context| record_body_layer(ctx, layer, parent, root)); + crate::runtime::replay::journal(ctx, || (layer, parent, root), |ctx, &(layer, parent, root)| record_body_layer(ctx, layer, parent, root)); PerViewport::::with(ctx, pass::current_viewport(ctx), |record| { record.this_bodies.insert(layer, (root, parent)); }); @@ -417,10 +424,11 @@ pub(crate) fn record_widgets(ctx: &Context, layer: Layer, root: Id, widgets: &[I /// `record_widgets` for a caller that already read the running viewport. pub(crate) fn record_widgets_in(ctx: &Context, viewport: ViewportId, layer: Layer, root: Id, widgets: &[Id], click: bool) { - crate::runtime::replay::journal(ctx, || { - let widgets = widgets.to_vec(); - move |ctx: &Context| record_widgets(ctx, layer, root, &widgets, click) - }); + crate::runtime::replay::journal( + ctx, + || (layer, root, widgets.to_vec(), click), + |ctx, (layer, root, widgets, click)| record_widgets(ctx, *layer, *root, widgets, *click), + ); PerViewport::::with(ctx, viewport, |record| { let entry = record.this.entry(layer).or_default().entry(root).or_default(); entry.widgets.extend_from_slice(widgets); diff --git a/crates/ui-contract/src/runtime/reads.rs b/crates/ui-contract/src/runtime/reads.rs index ce184f479..4b3cf72e1 100644 --- a/crates/ui-contract/src/runtime/reads.rs +++ b/crates/ui-contract/src/runtime/reads.rs @@ -35,7 +35,7 @@ pub(crate) struct Sample { pub took: Vec2, } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq)] struct Read { key: BindKey, reader: Id, @@ -94,10 +94,7 @@ pub(crate) fn note(ctx: &Context, key: BindKey, reader: Id, measuring: bool, sam } else { let read = Read { key, reader, measuring, runs, samples }; crate::runtime::replay::note_read(ctx, key); - crate::runtime::replay::journal(ctx, || { - let read = read.clone(); - move |ctx: &Context| with(ctx, |reads| reads.reads.push(read.clone())) - }); + crate::runtime::replay::journal(ctx, || read.clone(), |ctx, read| with(ctx, |reads| reads.reads.push(read.clone()))); with(ctx, |reads| reads.reads.push(read)); } } diff --git a/crates/ui-contract/src/runtime/replay.rs b/crates/ui-contract/src/runtime/replay.rs index f4a317b18..3b970c883 100644 --- a/crates/ui-contract/src/runtime/replay.rs +++ b/crates/ui-contract/src/runtime/replay.rs @@ -16,12 +16,63 @@ use crate::runtime::frame; use crate::runtime::pass_state; use crate::runtime::reads::BindKey; use egui::{Context, IdSet}; +use std::any::Any; use std::collections::HashSet; +use std::fmt; use std::sync::Arc; use std::sync::atomic::{AtomicUsize, Ordering}; -/// A registration of the recorded run, made again by calling it. -type Entry = Arc; +/// A registration of the recorded run: the call that makes it again and what it is called with. Two entries are equal +/// when they come from the same call (the same `again` of [`journal`]) with equal arguments. +#[derive(Clone)] +pub(crate) struct Entry(Arc); + +trait Noted: Send + Sync { + fn call(&self, ctx: &Context); + fn as_any(&self) -> &dyn Any; + fn same(&self, other: &dyn Noted) -> bool; + fn describe(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result; +} + +struct Note { + args: A, + again: F, +} + +impl Noted for Note +where + A: PartialEq + fmt::Debug + Send + Sync + 'static, + F: Fn(&Context, &A) + Send + Sync + 'static, +{ + fn call(&self, ctx: &Context) { + (self.again)(ctx, &self.args); + } + + fn as_any(&self) -> &dyn Any { + self + } + + fn same(&self, other: &dyn Noted) -> bool { + // `F` is capture-free, so its type names the call: the same type is the same call. + other.as_any().downcast_ref::().is_some_and(|other| other.args == self.args) + } + + fn describe(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}({:?})", std::any::type_name::(), self.args) + } +} + +impl PartialEq for Entry { + fn eq(&self, other: &Self) -> bool { + self.0.same(&*other.0) + } +} + +impl fmt::Debug for Entry { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.describe(f) + } +} /// Records open across all contexts, so a registration made while none is open takes no lock. static OPEN: AtomicUsize = AtomicUsize::new(0); @@ -68,7 +119,7 @@ pub(crate) struct Facts { /// What one real run of a region added to its pass. pub(crate) struct Record { pub(crate) focus: Transcript, - journal: Vec, + pub(crate) journal: Vec, egui: egui::PassRecords, /// The egui widgets of the run. pub(crate) widgets: IdSet, @@ -136,7 +187,7 @@ pub(crate) fn replay(ctx: &Context, record: &Record) { ctx.replay_pass_records(&record.egui); focus::replay(ctx, &record.focus); for entry in &record.journal { - entry(ctx); + entry.0.call(ctx); } if let Some(at) = record.facts.wake { crate::runtime::egui::repaint::request_after(ctx, crate::runtime::egui::pass::current_viewport(ctx), at - frame::clock(ctx)); @@ -160,12 +211,21 @@ fn with_open(ctx: &Context, f: impl FnOnce(&mut Facts)) { } } -/// Notes, in the records open around the running code, the registration `make` builds: called again, it registers -/// what the running code registered. 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(ctx: &Context, make: impl FnOnce() -> F) { +/// 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), 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(ctx: &Context, args: impl FnOnce() -> A, again: F) +where + A: PartialEq + fmt::Debug + Send + Sync + 'static, + F: Fn(&Context, &A) + Send + Sync + 'static, +{ + const { assert!(std::mem::size_of::() == 0, "`journal`'s `again` captures nothing: pass its state in `args`") }; if recording(ctx) { - let entry: Entry = Arc::new(make()); + let entry = Entry(Arc::new(Note { args: args(), again })); pass_state::with::(ctx, |recorder| recorder.journal.push(entry)); } } diff --git a/crates/ui-contract/src/runtime/wheel.rs b/crates/ui-contract/src/runtime/wheel.rs index f9763b1aa..6ad4df5e2 100644 --- a/crates/ui-contract/src/runtime/wheel.rs +++ b/crates/ui-contract/src/runtime/wheel.rs @@ -25,7 +25,7 @@ pub(crate) struct State { /// Registers a taker for next pass's split. pub(crate) fn register(ctx: &Context, taker: WheelTaker) { - crate::runtime::replay::journal(ctx, || move |ctx: &Context| register(ctx, taker)); + crate::runtime::replay::journal(ctx, || taker, |ctx, &taker| register(ctx, taker)); PerViewport::::with(ctx, pass::current_viewport(ctx), |state| state.this.push(taker)); } diff --git a/crates/ui-contract/src/runtime/windows/hit.rs b/crates/ui-contract/src/runtime/windows/hit.rs index c4dadbb42..c09d143a5 100644 --- a/crates/ui-contract/src/runtime/windows/hit.rs +++ b/crates/ui-contract/src/runtime/windows/hit.rs @@ -311,7 +311,7 @@ pub(super) fn with_exclusive(ctx: &Context, viewport: ViewportId, f: impl FnO /// A part declared again in the same pass replaces its entry. A settled region that replays its last run declares /// its parts again, so a settled title bar keeps its caption regions. pub(crate) fn note_exclusive(ctx: &Context, owner: Id, part: PartRef, hit: WindowHit, rect: Rect) { - crate::runtime::replay::journal(ctx, || move |ctx: &Context| note_exclusive(ctx, owner, part, hit, rect)); + crate::runtime::replay::journal(ctx, || (owner, part, hit, rect), |ctx, &(owner, part, hit, rect)| note_exclusive(ctx, owner, part, hit, rect)); with_exclusive(ctx, pass::current_viewport(ctx), |exclusive| { let entry = ExclusivePart { owner, part, hit, rect }; match exclusive.current.iter_mut().find(|held| held.owner == owner && held.part == part) { diff --git a/crates/ui-contract/src/style/tokens.rs b/crates/ui-contract/src/style/tokens.rs index 504731b04..2f0cc8428 100644 --- a/crates/ui-contract/src/style/tokens.rs +++ b/crates/ui-contract/src/style/tokens.rs @@ -42,6 +42,15 @@ pub struct UiTokens { key: u64, } +/// Two sets are equal when their content keys are: equal sets share a key. +impl PartialEq for UiTokens { + fn eq(&self, other: &Self) -> bool { + self.key == other.key + } +} + +impl Eq for UiTokens {} + impl UiTokens { /// A token set resolved under `contrast` and `density`, with no theme values for token groups, drawing the default /// icon set. diff --git a/crates/ui-contract/src/testing/replay_trials.rs b/crates/ui-contract/src/testing/replay_trials.rs index 69b9f5c35..83eb6fa47 100644 --- a/crates/ui-contract/src/testing/replay_trials.rs +++ b/crates/ui-contract/src/testing/replay_trials.rs @@ -1,8 +1,15 @@ -//! Trials of the replay records' notes. +//! Trials of the replay records: their notes and their comparison. +#![allow(clippy::expect_used, reason = "trial code: clippy.toml allows expect in tests, and trials are compiled into the library")] + +use crate::primitives::sensing::{self, MemberSpec}; use crate::runtime::egui::{fixture, repaint}; +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. @@ -19,3 +26,151 @@ fn a_repaint_from_a_job_thread_leaves_the_running_pass_s_records_alone() { }); assert_eq!(joined, Some(true), "the job thread's repaint request returns"); } + +/// Records one frame's run of `run` in `frames`. +fn record(frames: &mut Frames, mut run: impl FnMut(&egui::Context)) -> replay::Record { + let mut record = None; + frames.frame(Vec::new(), |ui| { + let ctx = ui.ctx().clone(); + replay::open(&ctx); + run(&ctx); + record = Some(replay::close(&ctx, fixture::layer_of(ui))); + }); + record.expect("the frame ran") +} + +fn note_a(_: &egui::Context, _: &(egui::Id, u32)) {} + +fn note_b(_: &egui::Context, _: &(egui::Id, u32)) {} + +// A journal entry is the same registration as another only when both come from the same call with equal arguments: a +// replay and a real run compare by it. +#[trial] +fn journal_entries_compare_by_their_call_and_arguments() { + let mut frames = Frames::new(); + let id = fixture::named_id("owner"); + let notes = |calls: [(bool, u32); 2]| { + move |ctx: &egui::Context| { + for (a, n) in calls { + if a { + replay::journal(ctx, || (id, n), note_a); + } else { + replay::journal(ctx, || (id, n), note_b); + } + } + } + }; + let first = record(&mut frames, notes([(true, 1), (true, 2)])); + let again = record(&mut frames, notes([(true, 1), (true, 2)])); + let changed_argument = record(&mut frames, notes([(true, 1), (true, 3)])); + let other_call = record(&mut frames, notes([(true, 1), (false, 2)])); + assert_eq!(first.journal.len(), 2, "both notes are in the record"); + assert_eq!(first.journal, again.journal, "the same calls with the same arguments"); + assert_ne!(first.journal[1], changed_argument.journal[1], "one argument changed"); + assert_eq!(first.journal[0], changed_argument.journal[0]); + assert_ne!(first.journal[1], other_call.journal[1], "another call with the same arguments"); +} + +// A focus transcript with one changed stop compares unequal to the run before it; one with the same stops equal. +#[trial] +fn a_transcript_with_one_changed_stop_compares_unequal() { + let mut frames = Frames::new(); + let [one, two] = [fixture::named_id("one"), fixture::named_id("two")]; + let stops = |second: KeyUse| { + move |ctx: &egui::Context| { + focus::register_stop(ctx, Stop::new(one, KeyUse::CODE)); + focus::register_stop(ctx, Stop::new(two, second)); + } + }; + let first = record(&mut frames, stops(KeyUse::CODE)); + let again = record(&mut frames, stops(KeyUse::CODE)); + let changed = record(&mut frames, stops(KeyUse::TEXT_MULTILINE)); + assert_eq!(first.focus.stop_ids().count(), 2, "both stops are in the transcript"); + 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, 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"); +} diff --git a/crates/ui-panels/src/docking/dock_column.rs b/crates/ui-panels/src/docking/dock_column.rs index 190d78a49..36c90de32 100644 --- a/crates/ui-panels/src/docking/dock_column.rs +++ b/crates/ui-panels/src/docking/dock_column.rs @@ -20,6 +20,7 @@ use craft_behavior::divider::{self as push, Segment}; use craft_behavior::dock::{Column, DockEdge, Group}; use craft_behavior::{Axis, DockSide, Flow, Metrics, PartKey, Sense, Value}; use egui::{Context, Id, Pos2, Rect, Response, Ui}; +use std::fmt::Debug; use std::hash::Hash; #[cfg(feature = "testing")] @@ -207,7 +208,7 @@ pub struct DockColumn<'a, P: Value + Hash + 'static> { collapse: bool, } -impl<'a, P: Value + Hash + 'static> DockColumn<'a, P> { +impl<'a, P: Value + Hash + Debug + 'static> DockColumn<'a, P> { /// A column named `label` of the dock `dock`, over `column`; `spec` reads each panel's limits and heights. pub fn new(label: impl Into