From b0f858b567a06de2ec91e0043afed8a25378b3cf Mon Sep 17 00:00:00 2001 From: aiongg Date: Fri, 13 Nov 2026 17:44:43 +0000 Subject: [PATCH] Injected input: a document withdraw takes effect per step at the next input hook and removes a handed-over step --- crates/automation/tests/it/synthetic.rs | 15 ++++-- .../ui-contract/src/runtime/egui/injected.rs | 51 ++++++++++++++----- .../ui-contract/src/runtime/windows/host.rs | 6 ++- 3 files changed, 54 insertions(+), 18 deletions(-) diff --git a/crates/automation/tests/it/synthetic.rs b/crates/automation/tests/it/synthetic.rs index a129e63bb..b9b8794cb 100644 --- a/crates/automation/tests/it/synthetic.rs +++ b/crates/automation/tests/it/synthetic.rs @@ -18,6 +18,7 @@ struct Seen { alt: bool, shift: bool, space_down: bool, + any_key_down: bool, events: Vec, injected: bool, } @@ -44,6 +45,7 @@ impl Rig { alt: i.modifiers.alt, shift: i.modifiers.shift, space_down: i.keys_down.contains(&Key::Space), + any_key_down: !i.keys_down.is_empty(), events: i.events.clone(), injected, })); @@ -199,9 +201,9 @@ fn document_points_need_a_canvas() { assert_eq!(screen, Some(Ok(()))); } -/// A step handed to the input whose pass has not run is withdrawn when the focus moved: the integration's preserved -/// copy leaves the input, and the waiters fail with `TARGET_CHANGED`. The application's document withdraw does the -/// same for what is queued. +/// A step handed to the input whose pass has not run is withdrawn when the focus moved, and when the application +/// withdraws the input because its document changed: the integration's preserved copy leaves the input, no key +/// stays down, and the waiters fail with `TARGET_CHANGED`. #[trial] fn queued_input_is_withdrawn_when_its_target_changes() { use craft_automation::control::{ControlChannel, ControlRequest, Reply}; @@ -232,7 +234,14 @@ fn queued_input_is_withdrawn_when_its_target_changes() { input_method("ui.key", &json!({"key": "A"}), &host, ViewportId::ROOT, &map).expect("a method").expect("valid"); let reply = waiting(&mut channel); + let handed = hook_without_pass(&ctx, ViewportId::ROOT); + assert!(!handed.is_empty(), "the press was handed to the input"); host.withdraw_injected(ViewportId::ROOT); + rig.take(); + rig.fx.frame(handed); + let passes = rig.take(); + assert!(passes.iter().flat_map(|pass| &pass.events).all(|event| !matches!(event, Event::Key { .. })), "the preserved press is removed"); + assert!(passes.iter().all(|pass| !pass.any_key_down), "no key stays down"); channel.poll_input(&host); let answer = reply.try_recv().expect("the waiter is answered"); assert!(answer["error"].as_str().is_some_and(|error| error.contains(TARGET_CHANGED)), "{answer}"); diff --git a/crates/ui-contract/src/runtime/egui/injected.rs b/crates/ui-contract/src/runtime/egui/injected.rs index 8bc019010..0dc5ef637 100644 --- a/crates/ui-contract/src/runtime/egui/injected.rs +++ b/crates/ui-contract/src/runtime/egui/injected.rs @@ -4,8 +4,8 @@ //! A request appends frames of events to its viewport's queue. Each pass's input hook ([`input_hook`]) appends //! the next frame, so a move, a press, a release and a key's down and up land on separate passes as real input //! does, and pass through the same input stages. A step stays in flight until a pass of its viewport has run. A -//! step whose pass has not run is withdrawn when the focus changed since it was injected, and the application -//! withdraws a viewport's queue when its document changed ([`withdraw`]). Screen points are egui points. +//! step whose pass has not run is withdrawn when the focus changed since it was injected, and when the application +//! withdraws the viewport's input because its document changed ([`withdraw`]). Screen points are egui points. use std::collections::{HashMap, VecDeque}; @@ -228,6 +228,8 @@ struct Flight { struct Lane { queue: VecDeque, flight: Option, + /// The application withdrew the lane's input ([`withdraw`]); the next input hook carries it out. + withdrawn: bool, } /// The queues of injected input, one per viewport that has any, and what injected input holds in each viewport. @@ -269,6 +271,17 @@ impl Injected { } } +/// Withdraws the step in flight of `viewport`, whose pass has not run, and the queue behind it: the integration's +/// preserved copy of the step leaves `events`, and what was held before the step is held again. +fn withdraw_flight(injected: &mut Injected, viewport: ViewportId, events: &mut Vec) { + let Some(lane) = injected.lanes.remove(&viewport) else { return }; + if let Some(flight) = lane.flight { + remove_event_run(events, &flight.step.events); + injected.held.insert(viewport, flight.before); + } + injected.withdrawn = true; +} + /// Removes one contiguous run equal to `step` from `events` (the integration preserves events of a pass it /// skipped). A no-op when the run is not present. fn remove_event_run(events: &mut Vec, step: &[Event]) { @@ -289,6 +302,21 @@ pub(crate) fn input_hook(ctx: &Context, input: &mut egui::RawInput) -> bool { let mut wake = None; let follow = Service::::run(ctx, |injected| { let Some(lane) = injected.lanes.get_mut(&viewport) else { return false }; + if lane.withdrawn { + if lane.flight.as_ref().is_some_and(|flight| flight.pass >= pass_nr) { + withdraw_flight(injected, viewport, &mut input.events); + return false; + } + lane.withdrawn = false; + if !lane.queue.is_empty() { + lane.queue.clear(); + injected.withdrawn = true; + } + if lane.flight.is_none() { + injected.lanes.remove(&viewport); + return false; + } + } if let Some(flight) = &lane.flight { if flight.pass < pass_nr { if let (Some(until), Some(now)) = (flight.until, input.time) @@ -299,11 +327,7 @@ pub(crate) fn input_hook(ctx: &Context, input: &mut egui::RawInput) -> bool { } lane.flight = None; } else if flight.focus != focused { - remove_event_run(&mut input.events, &flight.step.events); - let before = flight.before.clone(); - injected.held.insert(viewport, before); - injected.withdrawn = true; - injected.lanes.remove(&viewport); + withdraw_flight(injected, viewport, &mut input.events); return false; } else { return !lane.queue.is_empty(); @@ -454,14 +478,15 @@ pub(crate) fn in_pass(ctx: &Context) -> bool { }) } -/// Drops the queue of `viewport`: the application's document changed. A pending queue counts as withdrawn -/// ([`take_withdrawn`]). +/// Withdraws the injected input of `viewport`: the application's document changed. The viewport's next input hook +/// carries it out per step: a step handed to the input whose pass has not run leaves the input with the queue behind +/// it, and what was held before it is held again; after a step whose pass has run, only the queue is dropped. +/// Anything dropped counts as withdrawn ([`take_withdrawn`]). The application withdraws from its raw input hook, +/// which runs right before the framework's, so every request queued before that hook is dropped with the rest. pub(crate) fn withdraw(ctx: &Context, viewport: ViewportId) { Service::::run(ctx, |injected| { - if let Some(lane) = injected.lanes.remove(&viewport) - && (lane.flight.is_some() || !lane.queue.is_empty()) - { - injected.withdrawn = true; + if let Some(lane) = injected.lanes.get_mut(&viewport) { + lane.withdrawn = true; } }); } diff --git a/crates/ui-contract/src/runtime/windows/host.rs b/crates/ui-contract/src/runtime/windows/host.rs index 7dc41652a..f39aff936 100644 --- a/crates/ui-contract/src/runtime/windows/host.rs +++ b/crates/ui-contract/src/runtime/windows/host.rs @@ -170,8 +170,10 @@ impl WindowHost { injected::inject_text(&self.ctx, viewport, text); } - /// Drops the injected queue of `viewport` (the document changed); a pending queue counts as withdrawn - /// ([`take_injected_withdrawn`](Self::take_injected_withdrawn)). + /// Withdraws the injected input of `viewport` (the document changed) at its next input hook: a step whose pass + /// has not run and the queue behind it; what is dropped counts as withdrawn + /// ([`take_injected_withdrawn`](Self::take_injected_withdrawn)). Called from the raw input hook, so every + /// request queued before the framework's input hook is dropped with the rest. pub fn withdraw_injected(&self, viewport: ViewportId) { injected::withdraw(&self.ctx, viewport); }