Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run a frame per queued event in egui_kittest #5704

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
}
Loading