Skip to content

Commit 08c5a64

Browse files
authored
Run a frame per queued event in egui_kittest (emilk#5704)
This should fix the remaining problems with the modifiers * [x] I have followed the instructions in the PR template
1 parent 982b258 commit 08c5a64

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed

crates/egui_kittest/src/event.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ pub(crate) struct EventState {
88
}
99

1010
impl EventState {
11-
/// Map the kittest events to egui events, add them to the input and update the modifiers.
11+
/// Map the kittest event to an egui event, add it to the input and update the modifiers.
1212
/// This function accesses `egui::RawInput::modifiers`. Make sure it is not reset after each
1313
/// frame (Since we use [`egui::RawInput::take`], this should be fine).
14-
pub fn update(&mut self, events: Vec<kittest::Event>, input: &mut egui::RawInput) {
15-
for event in events {
16-
if let Some(event) = self.kittest_event_to_egui(&mut input.modifiers, event) {
17-
input.events.push(event);
18-
}
14+
pub fn update(&mut self, event: kittest::Event, input: &mut egui::RawInput) {
15+
if let Some(event) = self.kittest_event_to_egui(&mut input.modifiers, event) {
16+
input.events.push(event);
1917
}
2018
}
2119

crates/egui_kittest/src/lib.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,17 +219,22 @@ impl<'a, State> Harness<'a, State> {
219219
self
220220
}
221221

222-
/// Run a frame.
223-
/// This will call the app closure with the queued events and current context and
222+
/// Run a frame for each queued event (or a single frame if there are no events).
223+
/// This will call the app closure with each queued event and
224224
/// update the Harness.
225225
pub fn step(&mut self) {
226-
self._step(false);
226+
let events = self.kittest.take_events();
227+
if events.is_empty() {
228+
self._step(false);
229+
}
230+
for event in events {
231+
self.event_state.update(event, &mut self.input);
232+
self._step(false);
233+
}
227234
}
228235

236+
/// Run a single step. This will not process any events.
229237
fn _step(&mut self, sizing_pass: bool) {
230-
self.event_state
231-
.update(self.kittest.take_events(), &mut self.input);
232-
233238
self.input.predicted_dt = self.step_dt;
234239

235240
let mut output = self.ctx.run(self.input.take(), |ctx| {

crates/egui_kittest/tests/tests.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ fn test_modifiers() {
2222
struct State {
2323
cmd_clicked: bool,
2424
cmd_z_pressed: bool,
25+
cmd_y_pressed: bool,
2526
}
2627
let mut harness = Harness::new_ui_state(
2728
|ui, state| {
@@ -31,6 +32,9 @@ fn test_modifiers() {
3132
if ui.input(|i| i.modifiers.command && i.key_pressed(egui::Key::Z)) {
3233
state.cmd_z_pressed = true;
3334
}
35+
if ui.input(|i| i.modifiers.command && i.key_pressed(egui::Key::Y)) {
36+
state.cmd_y_pressed = true;
37+
}
3438
},
3539
State::default(),
3640
);
@@ -39,18 +43,17 @@ fn test_modifiers() {
3943
// This run isn't necessary, but allows us to test whether modifiers are remembered between frames
4044
harness.run();
4145
harness.get_by_label("Click me").click();
42-
// TODO(lucasmerlin): Right now the key_up needs to happen on a separate frame or it won't register.
43-
// This should be more intuitive
44-
harness.run();
4546
harness.get_by_label("Click me").key_up(Key::Command);
46-
4747
harness.run();
4848

4949
harness.press_key_modifiers(Modifiers::COMMAND, egui::Key::Z);
50-
// TODO(lucasmerlin): This should also work (Same problem as above)
51-
// harness.node().key_combination(&[Key::Command, Key::Z]);
50+
harness.run();
51+
52+
harness.node().key_combination(&[Key::Command, Key::Y]);
53+
harness.run();
5254

5355
let state = harness.state();
5456
assert!(state.cmd_clicked, "The button wasn't command-clicked");
5557
assert!(state.cmd_z_pressed, "Cmd+Z wasn't pressed");
58+
assert!(state.cmd_y_pressed, "Cmd+Y wasn't pressed");
5659
}

0 commit comments

Comments
 (0)