Skip to content

Commit

Permalink
Run a frame per queued event in egui_kittest (emilk#5704)
Browse files Browse the repository at this point in the history
This should fix the remaining problems with the modifiers
* [x] I have followed the instructions in the PR template
  • Loading branch information
lucasmerlin authored Feb 12, 2025
1 parent 982b258 commit 08c5a64
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
10 changes: 4 additions & 6 deletions crates/egui_kittest/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<kittest::Event>, 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);
}
}

Expand Down
17 changes: 11 additions & 6 deletions crates/egui_kittest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
15 changes: 9 additions & 6 deletions crates/egui_kittest/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand All @@ -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(),
);
Expand All @@ -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");
}

0 comments on commit 08c5a64

Please sign in to comment.