ui-contract: an injected step whose events the frame it entered held back (behind a real press, H0) stays in flight into the pass that delivers them, which reports injected_pass

This commit is contained in:
bflatastic
2026-11-23 17:28:09 +07:00
parent 77b2d98f66
commit 2f068b43a0
4 changed files with 44 additions and 1 deletions
@@ -330,6 +330,7 @@ pub(crate) fn input_hook(ctx: &Context, input: &mut egui::RawInput) -> bool {
let (pass_nr, focused) = (pass::pass_nr(ctx, viewport), crate::runtime::focus::record(ctx, viewport).focused);
let frame = pass::frame_nr(ctx, viewport);
let blurred = input.events.iter().any(|event| matches!(event, Event::WindowFocused(false)));
let held = crate::runtime::frame::held_for(ctx, viewport, frame);
let mut wake = None;
let follow = Service::<Injected>::run(ctx, |injected| {
if blurred {
@@ -358,7 +359,7 @@ pub(crate) fn input_hook(ctx: &Context, input: &mut egui::RawInput) -> bool {
return false;
}
}
if let Some(flight) = &lane.flight {
if let Some(flight) = &mut lane.flight {
if flight.pass < pass_nr {
if let (Some(until), Some(now)) = (flight.until, input.time)
&& now < until
@@ -366,6 +367,12 @@ pub(crate) fn input_hook(ctx: &Context, input: &mut egui::RawInput) -> bool {
wake = Some(until - now);
return false;
}
// Events of the step that the frame it entered held back (behind a real press there, H0) arrive in this
// pass: the step's pass is this one too, so it acts for the client, and the next step waits for it.
if flight.step.events.iter().any(|event| held.contains(event)) {
flight.pass = pass_nr;
return !lane.queue.is_empty();
}
lane.flight = None;
} else if flight.focus != focused {
withdraw_flight(injected, viewport, &mut input.events);
@@ -63,6 +63,11 @@ impl HeldInput {
pub(crate) fn holds(&self) -> bool {
!self.events.is_empty()
}
/// The held events the frame `frame`'s input gets: none once that frame took them.
pub(crate) fn delivered_in(&self, frame: u64) -> Vec<Event> {
if self.frame == Some(frame) { Vec::new() } else { self.events.iter().map(|arrived| arrived.event.clone()).collect() }
}
}
/// H0: this pass carries at most one pointer press, as its last pointer event. Events after the first press
+6
View File
@@ -644,6 +644,12 @@ pub(crate) fn holds_input(ctx: &Context, viewport: ViewportId) -> bool {
PerViewport::<ViewportRecord>::with(ctx, viewport, |record| record.held.holds())
}
/// The events the one-press rule (H0) or the key stage held that the frame `frame` of `viewport` gets, read in its
/// input hook before H0 hands them on.
pub(crate) fn held_for(ctx: &Context, viewport: ViewportId, frame: u64) -> Vec<egui::Event> {
PerViewport::<ViewportRecord>::with(ctx, viewport, |record| record.held.delivered_in(frame))
}
/// The input hook: egui runs it before it pushes the viewport, so every step reads only `RawInput` and
/// records keyed by `input.viewport_id`.
pub(crate) fn input_hook(ctx: &Context, input: &mut RawInput) {
@@ -356,6 +356,31 @@ fn a_rerun_pass_delivers_none_of_the_frames_input_again() {
assert!(log[0].stirred && log[0].injected && !log[1].stirred && !log[1].injected, "the first pass carries the input");
}
/// An injected step that enters a frame behind a real pointer press waits for the next frame (one press per pass), and
/// the pass that carries it acts for the automation client. Guards against a step whose flight ended with the frame it
/// entered, so the pass its text arrived in read as the user's (`injected_pass` false).
#[trial]
fn an_injected_step_held_behind_a_press_arrives_in_an_injected_pass() {
let log: Rc<RefCell<Vec<(bool, bool)>>> = Rc::default();
let sink = log.clone();
let mut show = move |ui: &mut Ui| {
let ctx = ui.ctx().clone();
let typed = ctx.input(|input| input.events.iter().any(|event| matches!(event, egui::Event::Text(text) if text == "x")));
sink.borrow_mut().push((typed, craft_ui::runtime::windows::WindowHost::of(&ctx).injected_pass()));
};
let mut frames = Frames::new();
frames.idle(2, &mut show);
craft_ui::runtime::windows::WindowHost::of(&frames.ctx).inject_text(egui::ViewportId::ROOT, "x");
log.borrow_mut().clear();
let at = Pos2::new(20.0, 20.0);
frames.frame(vec![pointer::moved(at), pointer::press(at)], &mut show);
frames.frame(vec![pointer::release(at)], &mut show);
frames.idle(2, &mut show);
let log = log.borrow();
let carrying: Vec<bool> = log.iter().filter(|(typed, _)| *typed).map(|(_, injected)| *injected).collect();
assert_eq!(carrying, [true], "one pass carries the text, and it is the client's: {log:?}");
}
fn downs(phases: &[PressPhase]) -> usize {
phases.iter().filter(|phase| matches!(phase, PressPhase::Down { .. })).count()
}