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.
This commit is contained in:
David Mann
2026-11-18 00:01:07 +01:00
parent 427c816db0
commit 8197531378
4 changed files with 90 additions and 4 deletions
+4 -3
View File
@@ -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();
}
}
+65
View File
@@ -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<RefCell<craft_automation::control::ControlChannel>>,
reply: Rc<std::sync::mpsc::Receiver<craft_automation::control::ControlResponse>>,
discard: impl Fn(usize) -> bool + 'static,
) -> (Fixture, Rc<RefCell<Vec<(bool, Option<Pos2>)>>>) {
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<bool> = 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() {
@@ -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<ViewportId>) -> bool {
let viewports: Vec<ViewportId> = Service::<Injected>::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::<Injected>::run(ctx, |injected| {
injected.settle(&passes);
injected.lanes.is_empty()
@@ -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)