From f451e60ceb954e801e4b286f00b9960e56ec59cf Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Fri, 13 Nov 2026 23:42:18 -0500 Subject: [PATCH 1/5] M5: a chain of After delay variant changes plays to the end (#2352) Change to swapped the instance's variant in place, so the next variant's After delay kept the instance's node id and was already recorded as fired: the chain stopped after one step. The swap now clears the fired keys inside the instance and records when it happened; next_delay counts a timer from the later of the screen's arrival and that swap. Co-Authored-By: Claude Sonnet 5.5 Model-Effort: Opus 5.5 drove; Opus 5.5 briefed; Sonnet 5.5 wrote at medium --- crates/ui-egui/src/present.rs | 6 ++ crates/ui-egui/src/present/actions.rs | 3 + .../src/present/afterdelay_chain_tests.rs | 98 +++++++++++++++++++ crates/ui-egui/src/present/navigate.rs | 5 +- docs/prototyping-parity.md | 3 +- 5 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 crates/ui-egui/src/present/afterdelay_chain_tests.rs diff --git a/crates/ui-egui/src/present.rs b/crates/ui-egui/src/present.rs index 8f23069f2..8f8570342 100644 --- a/crates/ui-egui/src/present.rs +++ b/crates/ui-egui/src/present.rs @@ -290,6 +290,8 @@ pub struct Presenter { bar_thumb: Option<(NodeId, usize, egui::TextureHandle)>, arrived: f64, delays_fired: HashSet<(NodeId, usize)>, + /// When each instance last changed variant (Change to): its After delay timers count from then. + swapped: HashMap, /// Mouse triggers waiting out their delay. waiting: Vec, hovered: Vec, @@ -370,6 +372,7 @@ impl Presenter { bar_thumb: None, arrived: -1.0, delays_fired: HashSet::new(), + swapped: HashMap::new(), waiting: Vec::new(), hovered: Vec::new(), press: None, @@ -460,6 +463,9 @@ mod tail_tests; #[cfg(test)] mod delay_tests; +#[cfg(test)] +mod afterdelay_chain_tests; + #[cfg(test)] mod overlay_anchor_tests; diff --git a/crates/ui-egui/src/present/actions.rs b/crates/ui-egui/src/present/actions.rs index cfc3f26a8..017003025 100644 --- a/crates/ui-egui/src/present/actions.rs +++ b/crates/ui-egui/src/present/actions.rs @@ -218,6 +218,9 @@ impl Presenter { } figcraft_engine::components::sync_instance(d, inst); self.settle(&before); + // The new variant's After delay interactions are fresh: they count from now. + self.delays_fired.retain(|(id, _)| !doc.is_ancestor(inst, *id)); + self.swapped.insert(inst, now); true } Action::SetVariable { variable, value, expr } => { diff --git a/crates/ui-egui/src/present/afterdelay_chain_tests.rs b/crates/ui-egui/src/present/afterdelay_chain_tests.rs new file mode 100644 index 000000000..e8a5551f1 --- /dev/null +++ b/crates/ui-egui/src/present/afterdelay_chain_tests.rs @@ -0,0 +1,98 @@ +//! A chain of variant After delay steps (Change to) plays to the end, each step's delay counted +//! from when its variant appeared (#2352). Driven through egui headless on the harness's clock. + +use figcraft_doc::{NodeId, NodeKind}; +use figcraft_engine::Session; +use serde_json::{Value, json}; + +use crate::present_tests::{Ui as Harness, id}; + +fn ex(s: &mut Session, cmd: &str, p: Value) -> Value { + s.execute(cmd, &p).unwrap_or_else(|e| panic!("{cmd} {p}: {e}")) +} + +/// Variants A to D of one set, an instance on A in a screen, each step an After delay of `ms` +/// changing to the next variant. (harness presenting the screen, the instance, the variants) +fn chain(ms: u32) -> (Harness, NodeId, Vec) { + let mut s = Session::new(); + let variants: Vec = ["A", "B", "C", "D"] + .iter() + .enumerate() + .map(|(k, state)| { + let f = id(&ex( + &mut s, + "frame.create", + json!({"x": k as f64 * 150.0, "y": 600, "width": 100, "height": 60, "name": format!("State={state}")}), + )); + id(&ex(&mut s, "component.create", json!({"ids": [f]}))) + }) + .collect(); + ex(&mut s, "component.combineVariants", json!({"ids": variants.clone()})); + for pair in variants.windows(2) { + let it = json!({"id": pair[0], "trigger": {"type": "afterDelay", "ms": ms}, "action": {"type": "changeTo", "dest": pair[1]}, "transition": {"type": "instant"}, "durationMs": 0}); + ex(&mut s, "proto.interaction.add", it); + } + let set = s.doc().unwrap().node(variants[0]).unwrap().parent.unwrap(); + let screen = id(&ex(&mut s, "frame.create", json!({"x": 0, "y": 0, "width": 400, "height": 300, "name": "Screen"}))); + let inst = id(&ex(&mut s, "instance.create", json!({"component": set, "x": 20, "y": 20, "parent": screen}))); + ex(&mut s, "instance.swap", json!({"ids": [inst], "component": variants[0]})); + let mut u = Harness::new(s); + u.run("proto.present", json!({"start": screen})); + (u, inst, variants) +} + +/// The variant (index) the instance shows in the presenter. +fn shown(u: &Harness, inst: NodeId, variants: &[NodeId]) -> Option { + let p = crate::tabs::shown_or_paused(&u.app)?; + let d = p.doc.as_ref()?; + match d.node(inst)?.kind { + NodeKind::Instance { component, .. } => variants.iter().position(|v| *v == component), + _ => None, + } +} + +#[test] +fn a_chain_of_variant_after_delays_plays_to_the_end() { + let (mut u, inst, variants) = chain(0); + u.frames(30, Vec::new); + assert_eq!(shown(&u, inst, &variants), Some(3), "A -> B -> C -> D"); +} + +#[test] +fn each_step_waits_its_own_delay_from_when_its_variant_appeared() { + let (mut u, inst, variants) = chain(500); + // Step until the instance shows B, then note the time. + let mut guard = 0; + while shown(&u, inst, &variants) != Some(1) { + u.frame(Vec::new()); + guard += 1; + assert!(guard < 120, "A never changed to B"); + } + let appeared = u.time; + while u.time < appeared + 0.4 { + u.frame(Vec::new()); + } + assert_eq!(shown(&u, inst, &variants), Some(1), "400 ms after B appeared: still B"); + while u.time < appeared + 0.6 { + u.frame(Vec::new()); + } + assert_eq!(shown(&u, inst, &variants), Some(2), "600 ms after B appeared: C"); +} + +#[test] +fn a_single_after_delay_still_fires_once() { + let mut s = Session::new(); + let a = id(&ex(&mut s, "frame.create", json!({"x": 0, "y": 0, "width": 300, "height": 400, "name": "A"}))); + let b = id(&ex(&mut s, "frame.create", json!({"x": 400, "y": 0, "width": 300, "height": 400, "name": "B"}))); + let it = json!({"id": a, "trigger": {"type": "afterDelay", "ms": 100}, "action": {"type": "navigate", "dest": b}, "transition": {"type": "instant"}, "durationMs": 0}); + ex(&mut s, "proto.interaction.add", it); + let mut u = Harness::new(s); + u.run("proto.present", json!({"start": a})); + u.frames(3, Vec::new); + let name = |u: &Harness| u.app.inspect()["presenter"]["name"].as_str().unwrap_or_default().to_string(); + assert_eq!(name(&u), "A", "not before the delay"); + u.frames(30, Vec::new); + assert_eq!(name(&u), "B", "once the delay is over"); + u.frames(60, Vec::new); + assert_eq!(name(&u), "B", "and not again"); +} diff --git a/crates/ui-egui/src/present/navigate.rs b/crates/ui-egui/src/present/navigate.rs index b5ae19cec..676a32f03 100644 --- a/crates/ui-egui/src/present/navigate.rs +++ b/crates/ui-egui/src/present/navigate.rs @@ -170,6 +170,7 @@ impl Presenter { } self.arrived = now; self.delays_fired.clear(); + self.swapped.clear(); self.waiting.clear(); self.hovered.clear(); self.rev += 1; @@ -245,7 +246,9 @@ pub(crate) fn next_delay(p: &Presenter, doc: &Document, root: NodeId, now: f64) if let Trigger::AfterDelay { ms } = it.trigger && !p.delays_fired.contains(&(id, k)) { - let due = since + f64::from(ms) / 1000.0 - now; + // Inside an instance that changed variant, the timer starts when it did. + let from = p.swapped.iter().filter(|(inst, _)| doc.is_ancestor(**inst, id)).fold(since, |a, (_, t)| a.max(*t)); + let due = from + f64::from(ms) / 1000.0 - now; if next.is_none_or(|(_, d)| due < d) { next = Some(((id, k), due)); } diff --git a/docs/prototyping-parity.md b/docs/prototyping-parity.md index 6ca217eec..d9f5d51b0 100644 --- a/docs/prototyping-parity.md +++ b/docs/prototyping-parity.md @@ -39,7 +39,7 @@ tool; opt-in), **silent** (the documentation doesn't say; the case pins the curr | Key/gamepad on a controller: the trigger takes a controller button in place of a key, pressed anywhere on the screen | **added** | `Gamepad A` … `Gamepad Home` (the standard layout's 17 buttons, face buttons by position) as the trigger's key, picked from the Controller menu beside the key field; the player polls controllers only while the prototype has such a trigger and the window has the focus, and plays each press once (held buttons don't repeat); Windows reads Xbox-compatible controllers, the browser its Gamepad API's standard-mapped ones; `controller_buttons_fire_key_gamepad_triggers`, `controller_buttons_play_key_gamepad_triggers` | | A controller button is not the keyboard key of the same name; the d-pad is not the arrow keys; a keyboard modifier held makes a button press another input | silent | pinned as stated | | REST files: controller triggers (`device` other than `KEYBOARD`) | **added** | they were skipped; their code reads as the standard layout's button index (silent: the schema doesn't say what the codes count); two buttons at once or an unknown code is skipped with a warning; `controller_triggers_import_as_their_button` | -| After delay: timed from when the screen shows | pass | `triggers_fire_on_their_gesture` | +| After delay: timed from when the screen shows; a chain of variant changes on After delay plays to the end, each step's delay counted from when its variant appears (#2352) | pass | `triggers_fire_on_their_gesture`, `a_chain_of_variant_after_delays_plays_to_the_end` | | After delay on an overlay: timed from when the overlay opens, again each time it opens | **fixed** | timed from when the screen under it arrived (a toast opened late closed at once) and fired only the first time; `an_overlays_after_delay_counts_from_when_it_opens` | | Nested layers with the same trigger: the top-most layer's interaction wins; a layer without that trigger lets it through to the one under it | pass | `nested_hotspots_and_click_through`, `hotspots_priority_and_click_through` | | A layer without interactions over a hotspot | silent | it doesn't block it: clicks reach the hotspot under it | @@ -108,6 +108,7 @@ tool; opt-in), **silent** (the documentation doesn't say; the case pins the curr | Date | Change | Summary | |---|---|---| +| 2026-11-14 | patch | M5, #2352: a chain of variant changes on After delay plays to the end (A to B to C to D); before, the step a variant had fired stayed marked as fired when the instance changed variant, so the chain stopped after one step, and now each step's delay counts from when its variant appears (`crates/ui-egui/src/present/afterdelay_chain_tests.rs`). Numbers unchanged | | 2026-11-09 | patch | M7, #2158: a connection dragged from a variant (or a layer in it) onto a sibling variant of its set did nothing, because the drop looked only for top-level frames and a variant's top-level frame is its set. It now creates Change to the sibling (On click, Instant, as measured in Figma's web app) and outlines the sibling while dragging; the set's background and the variant itself still add nothing, and a frame outside the set still gets Navigate to | | 2026-11-06 | patch | M5, #2027: `proto.settings` took any string as the device (`"Pixel Nonexistent"`, `"iphone 16"`) and saved it in the file; the presenter then drew no device while its picker showed the name as if it were one, and the settings couldn't be read back over MCP (`proto.settings {}` answered `{ok: true}`). An unknown name (or a non-string) is now refused with every name the picker offers (`None`, `Custom size` and the devices), as an unknown colour is refused with the device's colours, and nothing changes; a known name is taken in any case and stored as the picker spells it. Every call answers the page's settings after it: device, background, rotation, the colour shown, the screen as presented, the custom size and responsive. Test: `proto_present_tests::unknown_devices_are_refused_and_settings_read_back` | | 2026-10-31 | minor | Gap 15: the Controller menu names buttons as PlayStation and Switch controllers do; that item leaves the missing list. Estimate unchanged. Test: `present::tail_tests`. | From d3f275fa709ffd6e454e41fd66a16c1b2675bb1b Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Sat, 14 Nov 2026 01:48:54 -0500 Subject: [PATCH 2/5] M5: a temporary variant change that reverts keeps the After delay deadline (#2352) The swap times now travel with the while-hovering / while-pressing snapshot, so reverting a hover's variant change restores them and the variant shown again fires on its own deadline. Test a_hover_variant_that_reverts_keeps_the_after_delay_deadline (red before: left Some(0), right Some(2) at 1.3 s); present 102 passed, clippy clean (CodeRabbit on #2362). Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/present.rs | 3 ++ .../src/present/afterdelay_chain_tests.rs | 53 +++++++++++++++++++ crates/ui-egui/src/present/navigate.rs | 2 + 3 files changed, 58 insertions(+) diff --git a/crates/ui-egui/src/present.rs b/crates/ui-egui/src/present.rs index 8f8570342..6ac376fb1 100644 --- a/crates/ui-egui/src/present.rs +++ b/crates/ui-egui/src/present.rs @@ -248,6 +248,9 @@ struct Snapshot { overlays: Vec, history: Vec, scroll: HashMap, + /// When each instance last changed variant: a temporary change that reverts restores it, so + /// the variant shown again keeps its After delay deadline. + swapped: HashMap, } /// A "while hovering / while pressing" state that reverts when the pointer leaves / lifts. diff --git a/crates/ui-egui/src/present/afterdelay_chain_tests.rs b/crates/ui-egui/src/present/afterdelay_chain_tests.rs index e8a5551f1..945f0f57a 100644 --- a/crates/ui-egui/src/present/afterdelay_chain_tests.rs +++ b/crates/ui-egui/src/present/afterdelay_chain_tests.rs @@ -79,6 +79,59 @@ fn each_step_waits_its_own_delay_from_when_its_variant_appeared() { assert_eq!(shown(&u, inst, &variants), Some(2), "600 ms after B appeared: C"); } +/// A hover that changes the variant and reverts leaves the variant shown again on its own After +/// delay deadline (counted from the screen's arrival), not from the hover. +#[test] +fn a_hover_variant_that_reverts_keeps_the_after_delay_deadline() { + let mut s = Session::new(); + let variants: Vec = ["A", "Hover", "Next"] + .iter() + .enumerate() + .map(|(k, state)| { + let f = id(&ex( + &mut s, + "frame.create", + json!({"x": k as f64 * 150.0, "y": 600, "width": 100, "height": 60, "name": format!("State={state}")}), + )); + id(&ex(&mut s, "component.create", json!({"ids": [f]}))) + }) + .collect(); + ex(&mut s, "component.combineVariants", json!({"ids": variants.clone()})); + let instant = json!({"type": "instant"}); + let delay = json!({"id": variants[0], "trigger": {"type": "afterDelay", "ms": 1000}, "action": {"type": "changeTo", "dest": variants[2]}, "transition": instant, "durationMs": 0}); + ex(&mut s, "proto.interaction.add", delay); + let hover = json!({"id": variants[0], "trigger": {"type": "onHover"}, "action": {"type": "changeTo", "dest": variants[1]}, "transition": instant, "durationMs": 0}); + ex(&mut s, "proto.interaction.add", hover); + let set = s.doc().unwrap().node(variants[0]).unwrap().parent.unwrap(); + let screen = id(&ex(&mut s, "frame.create", json!({"x": 0, "y": 0, "width": 400, "height": 300, "name": "Screen"}))); + let inst = id(&ex(&mut s, "instance.create", json!({"component": set, "x": 20, "y": 20, "parent": screen}))); + ex(&mut s, "instance.swap", json!({"ids": [inst], "component": variants[0]})); + let mut u = Harness::new(s); + u.run("proto.present", json!({"start": screen})); + u.frame(Vec::new()); + let start = u.time; + let hot = u.app.inspect()["presenter"]["hotspots"].as_array().cloned().unwrap_or_default(); + let h = hot.iter().find(|h| h["id"] == json!(inst)).unwrap_or_else(|| panic!("no hotspot on the instance: {hot:?}")); + let r: Vec = h["rect"].as_array().unwrap().iter().map(|v| v.as_f64().unwrap() as f32).collect(); + let (on, away) = (egui::pos2((r[0] + r[2]) / 2.0, (r[1] + r[3]) / 2.0), egui::pos2(r[0] - 40.0, r[1] - 40.0)); + while u.time < start + 0.8 { + u.frame(vec![egui::Event::PointerMoved(away)]); + } + u.frame(vec![egui::Event::PointerMoved(on)]); + u.frame(Vec::new()); + assert_eq!(shown(&u, inst, &variants), Some(1), "while hovering: the hover variant"); + while u.time < start + 0.9 { + u.frame(vec![egui::Event::PointerMoved(on)]); + } + u.frame(vec![egui::Event::PointerMoved(away)]); + u.frame(Vec::new()); + assert_eq!(shown(&u, inst, &variants), Some(0), "the hover left: A again"); + while u.time < start + 1.3 { + u.frame(vec![egui::Event::PointerMoved(away)]); + } + assert_eq!(shown(&u, inst, &variants), Some(2), "A's 1,000 ms delay from the arrival fired, not 1,000 ms after the hover"); +} + #[test] fn a_single_after_delay_still_fires_once() { let mut s = Session::new(); diff --git a/crates/ui-egui/src/present/navigate.rs b/crates/ui-egui/src/present/navigate.rs index 676a32f03..b76ac8844 100644 --- a/crates/ui-egui/src/present/navigate.rs +++ b/crates/ui-egui/src/present/navigate.rs @@ -150,6 +150,7 @@ impl Presenter { overlays: self.overlays.clone(), history: self.history.clone(), scroll: self.scroll.clone(), + swapped: self.swapped.clone(), }) } @@ -228,6 +229,7 @@ impl Presenter { self.overlays = t.snap.overlays; self.history = t.snap.history; self.scroll = t.snap.scroll; + self.swapped = t.snap.swapped; self.stop_scrolling(); self.hovered.clear(); self.rev += 1; From 24c2988bd5f4cc5492c46bd9796acd9e68cdb9cb Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Sat, 14 Nov 2026 02:59:37 -0500 Subject: [PATCH 3/5] M5: a reverted hover keeps the delays that ran; an edit drops the swap times (#2352) The while-hovering / while-pressing snapshot also keeps the fired After delays, and the revert puts back the ones the temporary variant change cleared (keeping any that fired during it), so a delay that already ran on the variant doesn't run again. When an edit in the editor replaces the presented document, the run-time swap times are cleared with the rest of that state. Tests: a_hover_variant_that_reverts_does_not_rerun_a_fired_delay (red before: count left 2, right 1) and an_edit_while_presenting_drops_the_swap_times (red before: left Some(0), right Some(1)); present 104 passed, clippy clean (CodeRabbit on #2362). Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/present.rs | 3 + .../src/present/afterdelay_chain_tests.rs | 83 +++++++++++++++++++ crates/ui-egui/src/present/navigate.rs | 5 ++ 3 files changed, 91 insertions(+) diff --git a/crates/ui-egui/src/present.rs b/crates/ui-egui/src/present.rs index 6ac376fb1..f7af47c5f 100644 --- a/crates/ui-egui/src/present.rs +++ b/crates/ui-egui/src/present.rs @@ -251,6 +251,9 @@ struct Snapshot { /// When each instance last changed variant: a temporary change that reverts restores it, so /// the variant shown again keeps its After delay deadline. swapped: HashMap, + /// The After delays that had fired: a temporary variant change clears the swapped instance's, + /// and the revert puts them back so a delay that already ran doesn't run again. + delays_fired: HashSet<(NodeId, usize)>, } /// A "while hovering / while pressing" state that reverts when the pointer leaves / lifts. diff --git a/crates/ui-egui/src/present/afterdelay_chain_tests.rs b/crates/ui-egui/src/present/afterdelay_chain_tests.rs index 945f0f57a..e407e7322 100644 --- a/crates/ui-egui/src/present/afterdelay_chain_tests.rs +++ b/crates/ui-egui/src/present/afterdelay_chain_tests.rs @@ -132,6 +132,89 @@ fn a_hover_variant_that_reverts_keeps_the_after_delay_deadline() { assert_eq!(shown(&u, inst, &variants), Some(2), "A's 1,000 ms delay from the arrival fired, not 1,000 ms after the hover"); } +/// A delay that already ran on a variant stays run after a hover changes the variant and reverts: +/// the keys the temporary change cleared come back with the revert. +#[test] +fn a_hover_variant_that_reverts_does_not_rerun_a_fired_delay() { + let mut s = Session::new(); + ex(&mut s, "variable.createCollection", json!({"name": "Run", "modes": ["Default"]})); + ex(&mut s, "variable.create", json!({"collection": "Run", "name": "count", "type": "number", "value": 0})); + let count = s.doc().unwrap().variables.get("count").map(|v| v.id.clone()).unwrap(); + let variants: Vec = ["A", "Hover"] + .iter() + .enumerate() + .map(|(k, state)| { + let f = id(&ex( + &mut s, + "frame.create", + json!({"x": k as f64 * 150.0, "y": 600, "width": 100, "height": 60, "name": format!("State={state}")}), + )); + id(&ex(&mut s, "component.create", json!({"ids": [f]}))) + }) + .collect(); + ex(&mut s, "component.combineVariants", json!({"ids": variants.clone()})); + let instant = json!({"type": "instant"}); + // A's delay leaves A on screen: it counts its runs (count = count + 1). + let inc = json!({"op": {"op": "add", "args": [{"value": {"type": "alias", "value": count}}, {"value": {"type": "number", "value": 1}}]}}); + let delay = json!({"id": variants[0], "trigger": {"type": "afterDelay", "ms": 100}, "action": {"type": "setVariable", "variable": count, "value": null, "expr": inc}, "transition": instant, "durationMs": 0}); + ex(&mut s, "proto.interaction.add", delay); + let hover = json!({"id": variants[0], "trigger": {"type": "onHover"}, "action": {"type": "changeTo", "dest": variants[1]}, "transition": instant, "durationMs": 0}); + ex(&mut s, "proto.interaction.add", hover); + let set = s.doc().unwrap().node(variants[0]).unwrap().parent.unwrap(); + let screen = id(&ex(&mut s, "frame.create", json!({"x": 0, "y": 0, "width": 400, "height": 300, "name": "Screen"}))); + let inst = id(&ex(&mut s, "instance.create", json!({"component": set, "x": 20, "y": 20, "parent": screen}))); + ex(&mut s, "instance.swap", json!({"ids": [inst], "component": variants[0]})); + let mut u = Harness::new(s); + u.run("proto.present", json!({"start": screen})); + u.frame(Vec::new()); + let start = u.time; + let hot = u.app.inspect()["presenter"]["hotspots"].as_array().cloned().unwrap_or_default(); + let h = hot.iter().find(|h| h["id"] == json!(inst)).unwrap_or_else(|| panic!("no hotspot on the instance: {hot:?}")); + let r: Vec = h["rect"].as_array().unwrap().iter().map(|v| v.as_f64().unwrap() as f32).collect(); + let (on, away) = (egui::pos2((r[0] + r[2]) / 2.0, (r[1] + r[3]) / 2.0), egui::pos2(r[0] - 40.0, r[1] - 40.0)); + while u.time < start + 0.3 { + u.frame(vec![egui::Event::PointerMoved(away)]); + } + let fired: Vec<(NodeId, usize)> = crate::tabs::shown_or_paused(&u.app).map(|p| p.delays_fired.iter().copied().collect()).unwrap_or_default(); + assert!(!fired.is_empty(), "A's delay ran before the hover"); + u.frame(vec![egui::Event::PointerMoved(on)]); + u.frame(Vec::new()); + assert_eq!(shown(&u, inst, &variants), Some(1), "while hovering: the hover variant"); + u.frame(vec![egui::Event::PointerMoved(away)]); + u.frame(Vec::new()); + assert_eq!(shown(&u, inst, &variants), Some(0), "the hover left: A again"); + u.frames(5, Vec::new); + let p = crate::tabs::shown_or_paused(&u.app).expect("presenting"); + assert!(fired.iter().all(|k| p.delays_fired.contains(k)), "the delay that ran before the hover is still marked as run: {:?}", p.delays_fired); + let runs = p.design.as_ref().and_then(|d| d.variables.resolve(&count, &Default::default())); + assert_eq!(runs, Some(figcraft_doc::VariableValue::Number(1.0)), "A's delay ran once, not again after the hover"); +} + +/// An edit in the editor while presenting brings back the editor's variants, so the swap times of +/// the variants changed at run time no longer delay them. +#[test] +fn an_edit_while_presenting_drops_the_swap_times() { + let (mut u, inst, variants) = chain(500); + let start = u.time; + let mut guard = 0; + while shown(&u, inst, &variants) != Some(1) { + u.frame(Vec::new()); + guard += 1; + assert!(guard < 120, "A never changed to B"); + } + // An edit: the presenter shows the editor's document again, the instance on A. + u.run("page.rename", json!({"name": "Renamed"})); + u.frame(Vec::new()); + let edited = u.time; + // A's 500 ms delay counts from the screen's arrival, long past: it fires at once, not 500 ms + // after the run-time change to B. + while u.time < edited + 0.2 { + u.frame(Vec::new()); + } + assert!(edited < start + 0.9, "the edit came before B's own delay was due"); + assert_eq!(shown(&u, inst, &variants), Some(1), "A changed to B again right after the edit"); +} + #[test] fn a_single_after_delay_still_fires_once() { let mut s = Session::new(); diff --git a/crates/ui-egui/src/present/navigate.rs b/crates/ui-egui/src/present/navigate.rs index b76ac8844..de4439c24 100644 --- a/crates/ui-egui/src/present/navigate.rs +++ b/crates/ui-egui/src/present/navigate.rs @@ -42,6 +42,8 @@ impl Presenter { self.closing.retain(|(_, o)| doc.contains(o.id)); // These hold states of the document before the edit. self.temp = None; + // The edit's variants replace the runtime ones: their swap times no longer apply. + self.swapped.clear(); self.anim = None; self.scroll_anim = None; self.stop_scrolling(); @@ -151,6 +153,7 @@ impl Presenter { history: self.history.clone(), scroll: self.scroll.clone(), swapped: self.swapped.clone(), + delays_fired: self.delays_fired.clone(), }) } @@ -230,6 +233,8 @@ impl Presenter { self.history = t.snap.history; self.scroll = t.snap.scroll; self.swapped = t.snap.swapped; + // Put back the delays the temporary variant change cleared; keep the ones that fired since. + self.delays_fired.extend(t.snap.delays_fired); self.stop_scrolling(); self.hovered.clear(); self.rev += 1; From 3a8313aba72992e0527eb8815d8e7dea69d897b9 Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Sat, 14 Nov 2026 03:40:07 -0500 Subject: [PATCH 4/5] M5: a delay fired on a hover variant doesn't block the restored variant's delay (#2352) A fired-delay key is (node, index), and inside an instance the index belongs to the variant shown. On revert, keys that fired during the temporary change inside an instance it changed the variant of are dropped (they name the hover variant's interactions); the snapshot's keys come back and the other new ones stay. Test a_delay_fired_on_a_hover_variant_does_not_block_the_restored_variants_delay (red before: A's runs left 0, right 1); present 105 passed, clippy clean (CodeRabbit on #2362). Co-Authored-By: Claude Opus 5.5 --- .../src/present/afterdelay_chain_tests.rs | 64 +++++++++++++++++++ crates/ui-egui/src/present/navigate.rs | 11 +++- 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/present/afterdelay_chain_tests.rs b/crates/ui-egui/src/present/afterdelay_chain_tests.rs index e407e7322..e07976f28 100644 --- a/crates/ui-egui/src/present/afterdelay_chain_tests.rs +++ b/crates/ui-egui/src/present/afterdelay_chain_tests.rs @@ -190,6 +190,70 @@ fn a_hover_variant_that_reverts_does_not_rerun_a_fired_delay() { assert_eq!(runs, Some(figcraft_doc::VariableValue::Number(1.0)), "A's delay ran once, not again after the hover"); } +/// A delay that fires on the hover variant names that variant's interaction: after the revert it +/// doesn't count as A's delay at the same index, which still runs on its own deadline. +#[test] +fn a_delay_fired_on_a_hover_variant_does_not_block_the_restored_variants_delay() { + let mut s = Session::new(); + ex(&mut s, "variable.createCollection", json!({"name": "Run", "modes": ["Default"]})); + ex(&mut s, "variable.create", json!({"collection": "Run", "name": "a_runs", "type": "number", "value": 0})); + ex(&mut s, "variable.create", json!({"collection": "Run", "name": "hover_runs", "type": "number", "value": 0})); + let var = |s: &Session, name: &str| s.doc().unwrap().variables.get(name).map(|v| v.id.clone()).unwrap(); + let (a_runs, hover_runs) = (var(&s, "a_runs"), var(&s, "hover_runs")); + let inc = |v: &str| json!({"op": {"op": "add", "args": [{"value": {"type": "alias", "value": v}}, {"value": {"type": "number", "value": 1}}]}}); + let variants: Vec = ["A", "Hover"] + .iter() + .enumerate() + .map(|(k, state)| { + let f = id(&ex( + &mut s, + "frame.create", + json!({"x": k as f64 * 150.0, "y": 600, "width": 100, "height": 60, "name": format!("State={state}")}), + )); + id(&ex(&mut s, "component.create", json!({"ids": [f]}))) + }) + .collect(); + ex(&mut s, "component.combineVariants", json!({"ids": variants.clone()})); + let instant = json!({"type": "instant"}); + // A: index 0 a 1,000 ms delay counting its runs, index 1 the hover. Hover: index 0 a delay due at once. + let a_delay = json!({"id": variants[0], "trigger": {"type": "afterDelay", "ms": 1000}, "action": {"type": "setVariable", "variable": a_runs, "value": null, "expr": inc(&a_runs)}, "transition": instant, "durationMs": 0}); + ex(&mut s, "proto.interaction.add", a_delay); + let hover = json!({"id": variants[0], "trigger": {"type": "onHover"}, "action": {"type": "changeTo", "dest": variants[1]}, "transition": instant, "durationMs": 0}); + ex(&mut s, "proto.interaction.add", hover); + let h_delay = json!({"id": variants[1], "trigger": {"type": "afterDelay", "ms": 0}, "action": {"type": "setVariable", "variable": hover_runs, "value": null, "expr": inc(&hover_runs)}, "transition": instant, "durationMs": 0}); + ex(&mut s, "proto.interaction.add", h_delay); + let set = s.doc().unwrap().node(variants[0]).unwrap().parent.unwrap(); + let screen = id(&ex(&mut s, "frame.create", json!({"x": 0, "y": 0, "width": 400, "height": 300, "name": "Screen"}))); + let inst = id(&ex(&mut s, "instance.create", json!({"component": set, "x": 20, "y": 20, "parent": screen}))); + ex(&mut s, "instance.swap", json!({"ids": [inst], "component": variants[0]})); + let mut u = Harness::new(s); + u.run("proto.present", json!({"start": screen})); + u.frame(Vec::new()); + let start = u.time; + let hot = u.app.inspect()["presenter"]["hotspots"].as_array().cloned().unwrap_or_default(); + let h = hot.iter().find(|h| h["id"] == json!(inst)).unwrap_or_else(|| panic!("no hotspot on the instance: {hot:?}")); + let r: Vec = h["rect"].as_array().unwrap().iter().map(|v| v.as_f64().unwrap() as f32).collect(); + let (on, away) = (egui::pos2((r[0] + r[2]) / 2.0, (r[1] + r[3]) / 2.0), egui::pos2(r[0] - 40.0, r[1] - 40.0)); + let value = |u: &Harness, v: &str| { + crate::tabs::shown_or_paused(&u.app).and_then(|p| p.design.as_ref().and_then(|d| d.variables.resolve(v, &Default::default()))) + }; + while u.time < start + 0.2 { + u.frame(vec![egui::Event::PointerMoved(away)]); + } + while u.time < start + 0.4 { + u.frame(vec![egui::Event::PointerMoved(on)]); + } + assert_eq!(shown(&u, inst, &variants), Some(1), "while hovering: the hover variant"); + assert_eq!(value(&u, &hover_runs), Some(figcraft_doc::VariableValue::Number(1.0)), "the hover variant's delay ran"); + u.frame(vec![egui::Event::PointerMoved(away)]); + u.frame(Vec::new()); + assert_eq!(shown(&u, inst, &variants), Some(0), "the hover left: A again"); + while u.time < start + 1.3 { + u.frame(vec![egui::Event::PointerMoved(away)]); + } + assert_eq!(value(&u, &a_runs), Some(figcraft_doc::VariableValue::Number(1.0)), "A's own delay at index 0 ran on its deadline"); +} + /// An edit in the editor while presenting brings back the editor's variants, so the swap times of /// the variants changed at run time no longer delay them. #[test] diff --git a/crates/ui-egui/src/present/navigate.rs b/crates/ui-egui/src/present/navigate.rs index de4439c24..dbfd32506 100644 --- a/crates/ui-egui/src/present/navigate.rs +++ b/crates/ui-egui/src/present/navigate.rs @@ -225,6 +225,14 @@ impl Presenter { pub(super) fn revert(&mut self, now: f64) { let Some(t) = self.temp.take() else { return }; + // The delays fired during the temporary change: inside an instance it changed the variant + // of, a key names the temporary variant's interaction, not the restored one's, so drop it; + // keep the others, and put back the ones the change cleared. + let changed: Vec = self.swapped.iter().filter(|(i, at)| t.snap.swapped.get(*i) != Some(*at)).map(|(i, _)| *i).collect(); + let restored = &t.snap.doc; + let during: Vec<(NodeId, usize)> = self.delays_fired.difference(&t.snap.delays_fired).copied().collect(); + let mut fired = t.snap.delays_fired.clone(); + fired.extend(during.into_iter().filter(|(id, _)| !changed.iter().any(|i| i == id || restored.is_ancestor(*i, *id)))); self.begin(t.transition.reversed(), t.dur, t.easing, now); self.current = t.snap.current; self.design = Some(t.snap.doc); @@ -233,8 +241,7 @@ impl Presenter { self.history = t.snap.history; self.scroll = t.snap.scroll; self.swapped = t.snap.swapped; - // Put back the delays the temporary variant change cleared; keep the ones that fired since. - self.delays_fired.extend(t.snap.delays_fired); + self.delays_fired = fired; self.stop_scrolling(); self.hovered.clear(); self.rev += 1; From 55239a44f1a74ac01b42baa79dc91d70ce14be89 Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Sat, 14 Nov 2026 04:40:10 -0500 Subject: [PATCH 5/5] M5: a reverted hover leaves no fired key for a layer that is gone (#2352) On revert, fired-delay keys are kept only for layers the restored document has: a layer only the hover variant had is gone again, and its id may be issued anew by the next hover. Test a_hover_variants_own_layer_leaves_no_fired_key_after_the_revert (red before: a key for the missing NodeId(8) stayed); present 106 passed, clippy clean (CodeRabbit on #2362). Co-Authored-By: Claude Opus 5.5 --- .../src/present/afterdelay_chain_tests.rs | 52 +++++++++++++++++++ crates/ui-egui/src/present/navigate.rs | 2 + 2 files changed, 54 insertions(+) diff --git a/crates/ui-egui/src/present/afterdelay_chain_tests.rs b/crates/ui-egui/src/present/afterdelay_chain_tests.rs index e07976f28..af67d1f1a 100644 --- a/crates/ui-egui/src/present/afterdelay_chain_tests.rs +++ b/crates/ui-egui/src/present/afterdelay_chain_tests.rs @@ -254,6 +254,58 @@ fn a_delay_fired_on_a_hover_variant_does_not_block_the_restored_variants_delay() assert_eq!(value(&u, &a_runs), Some(figcraft_doc::VariableValue::Number(1.0)), "A's own delay at index 0 ran on its deadline"); } +/// A layer that only the hover variant has, with its own After delay that ran during the hover, +/// leaves no fired key behind after the revert (its id may be issued again by the next hover). +#[test] +fn a_hover_variants_own_layer_leaves_no_fired_key_after_the_revert() { + let mut s = Session::new(); + ex(&mut s, "variable.createCollection", json!({"name": "Run", "modes": ["Default"]})); + ex(&mut s, "variable.create", json!({"collection": "Run", "name": "runs", "type": "number", "value": 0})); + let runs = s.doc().unwrap().variables.get("runs").map(|v| v.id.clone()).unwrap(); + let inc = json!({"op": {"op": "add", "args": [{"value": {"type": "alias", "value": runs}}, {"value": {"type": "number", "value": 1}}]}}); + let frames: Vec = ["A", "Hover"] + .iter() + .enumerate() + .map(|(k, state)| { + id(&ex(&mut s, "frame.create", json!({"x": k as f64 * 150.0, "y": 600, "width": 100, "height": 60, "name": format!("State={state}")}))) + }) + .collect(); + // Only the hover state has this layer, with a delay due at once. + let dot = id(&ex(&mut s, "shape.rectangle", json!({"x": 160, "y": 610, "width": 10, "height": 10, "parent": frames[1]}))); + let instant = json!({"type": "instant"}); + let dot_delay = json!({"id": dot, "trigger": {"type": "afterDelay", "ms": 0}, "action": {"type": "setVariable", "variable": runs, "value": null, "expr": inc}, "transition": instant, "durationMs": 0}); + ex(&mut s, "proto.interaction.add", dot_delay); + let variants: Vec = frames.iter().map(|f| id(&ex(&mut s, "component.create", json!({"ids": [f]})))).collect(); + ex(&mut s, "component.combineVariants", json!({"ids": variants.clone()})); + let hover = json!({"id": variants[0], "trigger": {"type": "onHover"}, "action": {"type": "changeTo", "dest": variants[1]}, "transition": instant, "durationMs": 0}); + ex(&mut s, "proto.interaction.add", hover); + let set = s.doc().unwrap().node(variants[0]).unwrap().parent.unwrap(); + let screen = id(&ex(&mut s, "frame.create", json!({"x": 0, "y": 0, "width": 400, "height": 300, "name": "Screen"}))); + let inst = id(&ex(&mut s, "instance.create", json!({"component": set, "x": 20, "y": 20, "parent": screen}))); + ex(&mut s, "instance.swap", json!({"ids": [inst], "component": variants[0]})); + let mut u = Harness::new(s); + u.run("proto.present", json!({"start": screen})); + u.frame(Vec::new()); + let hot = u.app.inspect()["presenter"]["hotspots"].as_array().cloned().unwrap_or_default(); + let h = hot.iter().find(|h| h["id"] == json!(inst)).unwrap_or_else(|| panic!("no hotspot on the instance: {hot:?}")); + let r: Vec = h["rect"].as_array().unwrap().iter().map(|v| v.as_f64().unwrap() as f32).collect(); + let (on, away) = (egui::pos2((r[0] + r[2]) / 2.0, (r[1] + r[3]) / 2.0), egui::pos2(r[0] - 40.0, r[1] - 40.0)); + for _ in 0..6 { + u.frame(vec![egui::Event::PointerMoved(on)]); + } + assert_eq!(shown(&u, inst, &variants), Some(1), "while hovering: the hover variant"); + let p = crate::tabs::shown_or_paused(&u.app).expect("presenting"); + let ran = p.design.as_ref().and_then(|d| d.variables.resolve(&runs, &Default::default())); + assert_eq!(ran, Some(figcraft_doc::VariableValue::Number(1.0)), "the hover variant's own layer ran its delay"); + u.frame(vec![egui::Event::PointerMoved(away)]); + u.frame(Vec::new()); + assert_eq!(shown(&u, inst, &variants), Some(0), "the hover left: A again"); + let p = crate::tabs::shown_or_paused(&u.app).expect("presenting"); + let d = p.design.as_ref().expect("a document"); + let stale: Vec<_> = p.delays_fired.iter().filter(|(id, _)| !d.contains(*id)).collect(); + assert!(stale.is_empty(), "no fired key names a layer the restored document lacks: {stale:?}"); +} + /// An edit in the editor while presenting brings back the editor's variants, so the swap times of /// the variants changed at run time no longer delay them. #[test] diff --git a/crates/ui-egui/src/present/navigate.rs b/crates/ui-egui/src/present/navigate.rs index dbfd32506..a83e3940f 100644 --- a/crates/ui-egui/src/present/navigate.rs +++ b/crates/ui-egui/src/present/navigate.rs @@ -233,6 +233,8 @@ impl Presenter { let during: Vec<(NodeId, usize)> = self.delays_fired.difference(&t.snap.delays_fired).copied().collect(); let mut fired = t.snap.delays_fired.clone(); fired.extend(during.into_iter().filter(|(id, _)| !changed.iter().any(|i| i == id || restored.is_ancestor(*i, *id)))); + // A layer the temporary change added is gone again (and its id may be issued anew). + fired.retain(|(id, _)| restored.contains(*id)); self.begin(t.transition.reversed(), t.dur, t.easing, now); self.current = t.snap.current; self.design = Some(t.snap.doc);