diff --git a/crates/thing-core/src/frame.rs b/crates/thing-core/src/frame.rs index f8c6d0b..51f671e 100644 --- a/crates/thing-core/src/frame.rs +++ b/crates/thing-core/src/frame.rs @@ -124,13 +124,17 @@ impl Core { pub fn frame(&mut self, input: FrameInput, ports: Ports<'_>) -> FrameOutput { let FrameInput { now, viewport, scale, events } = input; let port = ports.state; + let mut diagnostics = std::mem::take(&mut self.notes); + // Input is judged entirely against the frame the user saw: its geometry and the values bound + // into it. Nothing of this frame (the surface it switches to, the provider updates it drains, + // the tree it binds) is in place yet, so no event reads a world the user did not look at. + diagnostics.extend(self.route(&events, scale, now, ports.ids, port)); let mut changes = Vec::new(); if self.wanted != self.shown { self.switch(port, &mut changes); } changes.extend(port.drain()); - let mut diagnostics = std::mem::take(&mut self.notes); let mut effects = Vec::new(); let mut rebound = false; for change in &changes { @@ -167,9 +171,6 @@ impl Core { None => self.held.push(resume), } } - // Pointer events act on what the user saw: the last good frame, before this frame's state is - // laid out, each in the order the host saw it. Keys, text and focus change nothing yet. - diagnostics.extend(self.route(&events, scale, now, ports.ids, port)); // Results of this frame are applied above, before the deadline is judged: a late result that was // already queued wins over the timeout. diagnostics.extend(self.commands.tick(now, port, &mut self.ledger)); diff --git a/crates/thing-core/tests/input.rs b/crates/thing-core/tests/input.rs index 6c23a10..0a82970 100644 --- a/crates/thing-core/tests/input.rs +++ b/crates/thing-core/tests/input.rs @@ -21,7 +21,7 @@ use thing_testkit::fixture::Fixture; use thing_testkit::ids::SeqIds; use thing_testkit::measure::FixedMeasure; use thing_testkit::mem_state::MemState; -use thing_testkit::state::{self, Item}; +use thing_testkit::state::{self, Item, Settings}; const PROVIDER: &str = "provider.pointer"; const FEED: &str = "provider.feed"; @@ -837,6 +837,25 @@ fn events_in_one_frame_act_on_the_frame_the_user_saw_and_the_next_frame_draws_th } } +#[test] +fn a_click_is_judged_against_the_values_the_user_saw_not_the_ones_the_provider_wrote_in_the_same_frame() { + for mode in MODES { + let fixture = Fixture::F04Form; + let mut state = MemState::from_fixture(fixture); + state::script(fixture, &mut state); + let document = fixture.load().surfaces.remove(0); + let mut seen = MemState::from_fixture(fixture); + state::script(fixture, &mut seen); + let toggle = inside(Rig::new(seen, &document, mode).rect("enabled")); + let mut host = Host::new(state, &document, mode); + // The user saw `enabled` checked. In the frame they click it the provider writes it unchecked. + host.state.put_state(&ProviderId::new("provider.settings"), "current", &Settings { enabled: false, volume: 40.0, theme: "dark".into() }); + let output = host.frame(vec![down(toggle), up(toggle)]); + assert_eq!(output.outbound.len(), 1, "{mode:?}"); + assert_eq!(output.outbound[0].captured[0].value, InputValue::Bool(BoolValue { value: false }), "{mode:?}: they unchecked what they saw checked"); + } +} + #[test] fn a_surface_left_mid_press_drops_the_press_and_a_stray_up_does_nothing() { let (one, mut two) = (controls_surface(), controls_surface());