From 08a442aeaf67710900109e8b71a6ba6e5fe81142 Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 19 Nov 2026 07:37:16 +0000 Subject: [PATCH 1/6] craft-ui: journal entries are data, comparable by call and arguments `runtime::journal(ctx, args, again)` notes a registration as its arguments and a capture-free `again` (a function or a closure without captures; a capture fails to compile). Two entries are equal when they come from the same `again` with equal arguments, and `Debug` names the call and its arguments, so a replay can be compared with a real run. The focus `Transcript` and the records it holds (`StopRec`, `ScopeRec`, `RowGroups`, `Stop`, `Group`, `KeyUse`, `KeyRole`) derive `PartialEq`. The journal's argument types gain `PartialEq` or `Debug` where they lacked them; `UiTokens` compares by its content key. The docking generics that journal drop spots require `PartialEq + Debug` of the panel type. --- crates/behavior/src/keys.rs | 2 +- crates/ui-contract/src/contract/lazy.rs | 2 +- crates/ui-contract/src/primitives/sensing.rs | 26 +++---- .../ui-contract/src/runtime/commands/board.rs | 11 +-- .../ui-contract/src/runtime/commands/mod.rs | 2 +- crates/ui-contract/src/runtime/drag.rs | 9 ++- crates/ui-contract/src/runtime/egui/access.rs | 6 +- crates/ui-contract/src/runtime/focus.rs | 10 +-- .../ui-contract/src/runtime/focus/policy.rs | 2 +- .../src/runtime/focus/registration.rs | 4 +- crates/ui-contract/src/runtime/keys/nodes.rs | 10 ++- .../ui-contract/src/runtime/layout_memory.rs | 14 ++-- crates/ui-contract/src/runtime/memory.rs | 2 +- crates/ui-contract/src/runtime/pointer.rs | 22 +++--- crates/ui-contract/src/runtime/reads.rs | 7 +- crates/ui-contract/src/runtime/replay.rs | 77 ++++++++++++++++--- crates/ui-contract/src/runtime/wheel.rs | 2 +- crates/ui-contract/src/runtime/windows/hit.rs | 2 +- crates/ui-contract/src/style/tokens.rs | 9 +++ crates/ui-panels/src/docking/dock_column.rs | 18 +++-- crates/ui-panels/src/docking/frame.rs | 3 +- crates/ui-panels/src/docking/group.rs | 9 ++- crates/ui-panels/src/docking/iconic.rs | 9 ++- crates/ui-panels/src/docking/targets.rs | 38 +++++---- crates/ui-patterns/src/patterns/dock/area.rs | 7 +- 25 files changed, 202 insertions(+), 101 deletions(-) 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/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..7397d0fe5 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. @@ -441,10 +442,7 @@ 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()) - }); + crate::runtime::replay::journal(ctx, || (arena, joined.to_vec()), |ctx, (arena, joined)| join_again(ctx, *arena, joined.clone())); } /// Replayed members join their arena again. @@ -463,7 +461,7 @@ fn join_again(ctx: &Context, arena: ArenaId, joined: Vec) { /// 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)); + 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); diff --git a/crates/ui-contract/src/runtime/commands/board.rs b/crates/ui-contract/src/runtime/commands/board.rs index 0c3d7f8a7..05853c05a 100644 --- a/crates/ui-contract/src/runtime/commands/board.rs +++ b/crates/ui-contract/src/runtime/commands/board.rs @@ -696,7 +696,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| match board.instances.iter_mut().find(|i| i.id == id) { @@ -1019,10 +1019,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 1570c4ae1..40c43af07 100644 --- a/crates/ui-contract/src/runtime/commands/mod.rs +++ b/crates/ui-contract/src/runtime/commands/mod.rs @@ -341,7 +341,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)] +#[derive(Clone, Debug, Default, PartialEq, Eq)] 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 8eee24e44..030a76695 100644 --- a/crates/ui-contract/src/runtime/egui/access.rs +++ b/crates/ui-contract/src/runtime/egui/access.rs @@ -139,7 +139,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`. @@ -147,7 +147,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`. @@ -155,7 +155,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..803cd2572 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, @@ -261,7 +261,7 @@ 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)] +#[derive(Clone, Debug, Default, PartialEq, Eq)] pub(crate) struct Transcript { stops: Vec, scopes: Vec<(Id, ScopeRec)>, @@ -431,7 +431,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..b2511074e 100644 --- a/crates/ui-contract/src/runtime/pointer.rs +++ b/crates/ui-contract/src/runtime/pointer.rs @@ -369,10 +369,11 @@ 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()), + ); PerViewport::::with(ctx, viewport, |record| { let order = record.next; record.next += 1; @@ -385,7 +386,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 +404,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 +418,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..0c248849e 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,20 @@ 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; 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. +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-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