Run a frame per queued event in egui_kittest (#5704)
This should fix the remaining problems with the modifiers * [x] I have followed the instructions in the PR template
This commit is contained in:
parent
982b2580f4
commit
08c5a641a1
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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| {
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue