From 8197531378b60b2242020f5d70bd535f6aa41f1f Mon Sep 17 00:00:00 2001 From: David Mann Date: Wed, 18 Nov 2026 00:01:07 +0100 Subject: [PATCH 1/2] Control channel: answer injected input with the pass that ran it, not one pass later (photocraft#15493) An application polls ControlChannel::poll_input at the end of its UI, inside the pass that ran the injected step. There the running pass number still equals the step's pass, so the step counted as unrun and the reply waited for the next pass: one more frame per ui.move, ui.click or ui.key round trip. WindowHost::injected_idle_at_pass_end counts the running pass as run, except a pass egui runs again (request_discard), whose rerun answers. poll_input uses it; injected_idle and every caller between passes keep their meaning. --- crates/automation/src/control.rs | 7 +- crates/automation/tests/it/synthetic.rs | 65 +++++++++++++++++++ .../ui-contract/src/runtime/egui/injected.rs | 16 ++++- .../ui-contract/src/runtime/windows/host.rs | 6 ++ 4 files changed, 90 insertions(+), 4 deletions(-) diff --git a/crates/automation/src/control.rs b/crates/automation/src/control.rs index 22a058119..c0f594251 100644 --- a/crates/automation/src/control.rs +++ b/crates/automation/src/control.rs @@ -226,12 +226,13 @@ impl ControlChannel { /// Answers the [`Reply::AfterInput`] waiters from the window host's input state: with /// `input cancelled` when the host withdrew queued input ([`TARGET_CHANGED`]), with success - /// once nothing is queued or in flight. Call it once per pass, after the pass that consumed the - /// input. + /// once nothing is queued or in flight. Call it once per pass, at the end of the pass (after its + /// UI ran): the step that pass consumed counts as run, so the reply goes out with that pass + /// ([`WindowHost::injected_idle_at_pass_end`]). Called between passes, it waits for the pass. pub fn poll_input(&mut self, host: &WindowHost) { if host.take_injected_withdrawn() { self.input_cancelled(TARGET_CHANGED); - } else if host.injected_idle() { + } else if host.injected_idle_at_pass_end() { self.input_processed(); } } diff --git a/crates/automation/tests/it/synthetic.rs b/crates/automation/tests/it/synthetic.rs index a8e99ee5e..62b1175e8 100644 --- a/crates/automation/tests/it/synthetic.rs +++ b/crates/automation/tests/it/synthetic.rs @@ -179,6 +179,71 @@ fn injected_caps_lock_holds_until_cleared() { assert_eq!(rig.host().injected_caps_lock(), None); } +/// A fixture whose UI polls `channel` at the end of each pass, as an application does after its UI, with `discard` +/// asking egui to run the pass again on the passes it is true for; `answered` records, per pass, whether the waiter +/// had its reply by the pass's end and the pointer position the pass saw. +fn polling_fixture( + channel: Rc>, + reply: Rc>, + discard: impl Fn(usize) -> bool + 'static, +) -> (Fixture, Rc)>>>) { + let answered = Rc::new(RefCell::new(Vec::new())); + let seen = answered.clone(); + let fx = Fixture::new(move |ui| { + let pass = seen.borrow().len(); + if discard(pass) { + ui.ctx().request_discard("the test runs this pass again"); + } + channel.borrow_mut().poll_input(&WindowHost::of(ui.ctx())); + let pos = ui.input(|i| i.pointer.latest_pos()); + seen.borrow_mut().push((reply.try_recv().is_ok(), pos)); + }); + (fx, answered) +} + +/// A waiter on a move is answered at the end of the pass that ran the move, not a pass later: the application polls +/// after its UI, where the running pass has not ended yet. +#[trial] +fn a_waiter_is_answered_by_the_pass_that_ran_its_step() { + use craft_automation::control::{ControlChannel, ControlRequest, Reply}; + + let (_transport, requests) = std::sync::mpsc::channel(); + let channel = Rc::new(RefCell::new(ControlChannel::new(requests))); + let (req, reply) = ControlRequest::new("ui.move", json!({"x": 30, "y": 20})); + let (mut fx, answered) = polling_fixture(channel.clone(), Rc::new(reply), |_| false); + fx.idle(2); + answered.borrow_mut().clear(); + let host = WindowHost::of(fx.ctx()); + input_method("ui.move", &json!({"x": 30, "y": 20}), &host, ViewportId::ROOT, &map).expect("a method").expect("valid"); + channel.borrow_mut().settle(req, Reply::AfterInput, None, 0.0); + fx.frame(Vec::new()); + let passes = answered.borrow().clone(); + assert_eq!(passes, vec![(true, Some(pos2(30.0, 20.0)))], "the pass that saw the move answered it"); +} + +/// A pass egui runs again (`request_discard`) does not answer: the pass that runs again, and draws what the step +/// did, does. +#[trial] +fn a_pass_run_again_leaves_the_answer_to_its_rerun() { + use craft_automation::control::{ControlChannel, ControlRequest, Reply}; + + let (_transport, requests) = std::sync::mpsc::channel(); + let channel = Rc::new(RefCell::new(ControlChannel::new(requests))); + let (req, reply) = ControlRequest::new("ui.move", json!({"x": 30, "y": 20})); + let start = Rc::new(Cell::new(usize::MAX)); + let first = start.clone(); + let (mut fx, answered) = polling_fixture(channel.clone(), Rc::new(reply), move |pass| pass == first.get()); + fx.idle(2); + start.set(answered.borrow().len()); + let host = WindowHost::of(fx.ctx()); + input_method("ui.move", &json!({"x": 30, "y": 20}), &host, ViewportId::ROOT, &map).expect("a method").expect("valid"); + channel.borrow_mut().settle(req, Reply::AfterInput, None, 0.0); + fx.frame(Vec::new()); + assert_eq!(fx.passes(), 2, "egui ran the discarded pass again"); + let passes: Vec = answered.borrow()[start.get()..].iter().map(|(done, _)| *done).collect(); + assert_eq!(passes, vec![false, true], "the rerun answers, the discarded pass does not"); +} + /// A move with `rest` counts as processed only once the pointer has rested that long. #[trial] fn a_hover_rest_holds_the_reply_until_it_has_passed() { diff --git a/crates/ui-contract/src/runtime/egui/injected.rs b/crates/ui-contract/src/runtime/egui/injected.rs index f89ab3419..45befc044 100644 --- a/crates/ui-contract/src/runtime/egui/injected.rs +++ b/crates/ui-contract/src/runtime/egui/injected.rs @@ -729,8 +729,22 @@ pub(crate) fn end(ctx: &Context) { /// Whether nothing is queued and no step awaits its pass or its rest: the application may answer its waiters. pub(crate) fn idle(ctx: &Context) -> bool { + settled(ctx, None) +} + +/// [`idle`] asked at the end of the running pass, after its UI: the step that pass carried counts as run, so the +/// reply goes out with the pass that ran it and not one pass later. A pass egui will run again (`request_discard`) +/// does not count: the pass that runs again draws what the step did. Outside a pass it is [`idle`]. +pub(crate) fn idle_at_pass_end(ctx: &Context) -> bool { + let ending = crate::runtime::frame::running(ctx).filter(|_| !ctx.will_discard()).map(|facts| facts.viewport); + settled(ctx, ending) +} + +/// Settles the steps whose pass has run, `ending`'s running pass included, and says whether nothing is left. +fn settled(ctx: &Context, ending: Option) -> bool { let viewports: Vec = Service::::run(ctx, |injected| injected.lanes.keys().copied().collect()); - let passes: Vec<(ViewportId, u64)> = viewports.into_iter().map(|viewport| (viewport, pass::pass_nr(ctx, viewport))).collect(); + let passes: Vec<(ViewportId, u64)> = + viewports.into_iter().map(|viewport| (viewport, pass::pass_nr(ctx, viewport).saturating_add(u64::from(ending == Some(viewport))))).collect(); Service::::run(ctx, |injected| { injected.settle(&passes); injected.lanes.is_empty() diff --git a/crates/ui-contract/src/runtime/windows/host.rs b/crates/ui-contract/src/runtime/windows/host.rs index 5ce81ebda..f45738fa6 100644 --- a/crates/ui-contract/src/runtime/windows/host.rs +++ b/crates/ui-contract/src/runtime/windows/host.rs @@ -240,6 +240,12 @@ impl WindowHost { injected::idle(&self.ctx) } + /// [`injected_idle`](Self::injected_idle) asked at the end of a pass, after its UI ran: the step the running pass + /// carried counts as run, unless egui runs the pass again. Outside a pass it is `injected_idle`. + pub fn injected_idle_at_pass_end(&self) -> bool { + injected::idle_at_pass_end(&self.ctx) + } + /// Whether an injected step was withdrawn since the last call. pub fn take_injected_withdrawn(&self) -> bool { injected::take_withdrawn(&self.ctx) From 367d2a3e6511d55a476aaae5fe7ccda6efd38a98 Mon Sep 17 00:00:00 2001 From: David Mann Date: Wed, 18 Nov 2026 18:38:41 +0100 Subject: [PATCH 2/2] automation tests: name the polling fixture's pass log (clippy type_complexity) Co-Authored-By: Claude Opus 5.5 --- crates/automation/tests/it/synthetic.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/automation/tests/it/synthetic.rs b/crates/automation/tests/it/synthetic.rs index 62b1175e8..9452879ae 100644 --- a/crates/automation/tests/it/synthetic.rs +++ b/crates/automation/tests/it/synthetic.rs @@ -179,6 +179,9 @@ fn injected_caps_lock_holds_until_cleared() { assert_eq!(rig.host().injected_caps_lock(), None); } +/// Per pass: whether the waiter had its reply by the pass's end, and the pointer position the pass saw. +type PassLog = Rc)>>>; + /// A fixture whose UI polls `channel` at the end of each pass, as an application does after its UI, with `discard` /// asking egui to run the pass again on the passes it is true for; `answered` records, per pass, whether the waiter /// had its reply by the pass's end and the pointer position the pass saw. @@ -186,7 +189,7 @@ fn polling_fixture( channel: Rc>, reply: Rc>, discard: impl Fn(usize) -> bool + 'static, -) -> (Fixture, Rc)>>>) { +) -> (Fixture, PassLog) { let answered = Rc::new(RefCell::new(Vec::new())); let seen = answered.clone(); let fx = Fixture::new(move |ui| {