From 591969d14a1acebe461cc221b44b4dfa6a527279 Mon Sep 17 00:00:00 2001 From: Lucas Meurer Date: Mon, 10 Feb 2025 10:13:32 +0100 Subject: [PATCH] Run a frame per queued event in egui_kittest --- crates/egui_kittest/src/event.rs | 10 ++++------ crates/egui_kittest/src/lib.rs | 17 +++++++++++------ crates/egui_kittest/tests/tests.rs | 15 +++++++++------ 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/crates/egui_kittest/src/event.rs b/crates/egui_kittest/src/event.rs index c3045d623e8..e756d4dc9b3 100644 --- a/crates/egui_kittest/src/event.rs +++ b/crates/egui_kittest/src/event.rs @@ -8,14 +8,12 @@ pub(crate) struct EventState { } impl EventState { - /// Map the kittest events to egui events, add them to the input and update the modifiers. + /// Map the kittest event to an egui event, add it to the input and update the modifiers. /// This function accesses `egui::RawInput::modifiers`. Make sure it is not reset after each /// frame (Since we use [`egui::RawInput::take`], this should be fine). - pub fn update(&mut self, events: Vec, input: &mut egui::RawInput) { - for event in events { - if let Some(event) = self.kittest_event_to_egui(&mut input.modifiers, event) { - input.events.push(event); - } + pub fn update(&mut self, event: kittest::Event, input: &mut egui::RawInput) { + if let Some(event) = self.kittest_event_to_egui(&mut input.modifiers, event) { + input.events.push(event); } } diff --git a/crates/egui_kittest/src/lib.rs b/crates/egui_kittest/src/lib.rs index 9bccd263e43..59bd5c056ac 100644 --- a/crates/egui_kittest/src/lib.rs +++ b/crates/egui_kittest/src/lib.rs @@ -219,17 +219,22 @@ impl<'a, State> Harness<'a, State> { self } - /// Run a frame. - /// This will call the app closure with the queued events and current context and + /// Run a frame for each queued event (or a single frame if there are no events). + /// This will call the app closure with each queued event and /// update the Harness. pub fn step(&mut self) { - self._step(false); + let events = self.kittest.take_events(); + if events.is_empty() { + self._step(false); + } + for event in events { + self.event_state.update(event, &mut self.input); + self._step(false); + } } + /// Run a single step. This will not process any events. fn _step(&mut self, sizing_pass: bool) { - self.event_state - .update(self.kittest.take_events(), &mut self.input); - self.input.predicted_dt = self.step_dt; let mut output = self.ctx.run(self.input.take(), |ctx| { diff --git a/crates/egui_kittest/tests/tests.rs b/crates/egui_kittest/tests/tests.rs index 4fa1239a7c6..52f455c7bdc 100644 --- a/crates/egui_kittest/tests/tests.rs +++ b/crates/egui_kittest/tests/tests.rs @@ -22,6 +22,7 @@ fn test_modifiers() { struct State { cmd_clicked: bool, cmd_z_pressed: bool, + cmd_y_pressed: bool, } let mut harness = Harness::new_ui_state( |ui, state| { @@ -31,6 +32,9 @@ fn test_modifiers() { if ui.input(|i| i.modifiers.command && i.key_pressed(egui::Key::Z)) { state.cmd_z_pressed = true; } + if ui.input(|i| i.modifiers.command && i.key_pressed(egui::Key::Y)) { + state.cmd_y_pressed = true; + } }, State::default(), ); @@ -39,18 +43,17 @@ fn test_modifiers() { // This run isn't necessary, but allows us to test whether modifiers are remembered between frames harness.run(); harness.get_by_label("Click me").click(); - // TODO(lucasmerlin): Right now the key_up needs to happen on a separate frame or it won't register. - // This should be more intuitive - harness.run(); harness.get_by_label("Click me").key_up(Key::Command); - harness.run(); harness.press_key_modifiers(Modifiers::COMMAND, egui::Key::Z); - // TODO(lucasmerlin): This should also work (Same problem as above) - // harness.node().key_combination(&[Key::Command, Key::Z]); + harness.run(); + + harness.node().key_combination(&[Key::Command, Key::Y]); + harness.run(); let state = harness.state(); assert!(state.cmd_clicked, "The button wasn't command-clicked"); assert!(state.cmd_z_pressed, "Cmd+Z wasn't pressed"); + assert!(state.cmd_y_pressed, "Cmd+Y wasn't pressed"); }