Skip to content

Commit

Permalink
Fix modifiers not working in kittest
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmerlin committed Feb 6, 2025
1 parent 5c372a7 commit bc214ce
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
4 changes: 3 additions & 1 deletion RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,15 @@ I usually do this all on the `master` branch, but doing it in a release branch i
(cd crates/egui && cargo publish --quiet) && echo "✅ egui"
(cd crates/egui-winit && cargo publish --quiet) && echo "✅ egui-winit"
(cd crates/egui-wgpu && cargo publish --quiet) && echo "✅ egui-wgpu"
(cd crates/eframe && cargo publish --quiet) && echo "✅ eframe"
(cd crates/egui_kittest && cargo publish --quiet) && echo "✅ egui_kittest"
(cd crates/egui_extras && cargo publish --quiet) && echo "✅ egui_extras"
(cd crates/egui_demo_lib && cargo publish --quiet) && echo "✅ egui_demo_lib"
(cd crates/egui_glow && cargo publish --quiet) && echo "✅ egui_glow"
(cd crates/eframe && cargo publish --quiet) && echo "✅ eframe"
```

\<continue with the checklist above\>

## Announcements
* [ ] [Bluesky](https://bsky.app/profile/ernerfeldt.bsky.social)
* [ ] egui discord
Expand Down
16 changes: 13 additions & 3 deletions crates/egui_kittest/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,17 @@ pub(crate) struct EventState {
}

impl EventState {
pub fn kittest_event_to_egui(&mut self, event: kittest::Event) -> Option<egui::Event> {
/// Map the kittest events to egui events, add them to the input and update the modifiers.
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(event) {
input.events.push(event);
}
}
input.modifiers = self.modifiers;
}

fn kittest_event_to_egui(&mut self, event: kittest::Event) -> Option<egui::Event> {
match event {
kittest::Event::ActionRequest(e) => Some(Event::AccessKitActionRequest(e)),
kittest::Event::Simulated(e) => match e {
Expand Down Expand Up @@ -58,7 +68,7 @@ impl EventState {
}
}

pub fn kittest_key_to_egui(value: kittest::Key) -> Option<egui::Key> {
fn kittest_key_to_egui(value: kittest::Key) -> Option<egui::Key> {
use egui::Key as EKey;
use kittest::Key;
match value {
Expand Down Expand Up @@ -170,7 +180,7 @@ pub fn kittest_key_to_egui(value: kittest::Key) -> Option<egui::Key> {
}
}

pub fn pointer_button_to_egui(value: MouseButton) -> Option<egui::PointerButton> {
fn pointer_button_to_egui(value: MouseButton) -> Option<egui::PointerButton> {
match value {
MouseButton::Left => Some(egui::PointerButton::Primary),
MouseButton::Right => Some(egui::PointerButton::Secondary),
Expand Down
7 changes: 2 additions & 5 deletions crates/egui_kittest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,8 @@ impl<'a, State> Harness<'a, State> {
}

fn _step(&mut self, sizing_pass: bool) {
for event in self.kittest.take_events() {
if let Some(event) = self.event_state.kittest_event_to_egui(event) {
self.input.events.push(event);
}
}
self.event_state
.update(self.kittest.take_events(), &mut self.input);

self.input.predicted_dt = self.step_dt;

Expand Down
30 changes: 26 additions & 4 deletions crates/egui_kittest/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use egui_kittest::{Harness, SnapshotResults};
use egui_kittest::Harness;
use kittest::{Key, Queryable};

#[test]
fn test_shrink() {
Expand All @@ -10,8 +11,29 @@ fn test_shrink() {

harness.fit_contents();

let mut results = SnapshotResults::new();

#[cfg(all(feature = "snapshot", feature = "wgpu"))]
results.add(harness.try_snapshot("test_shrink"));
harness.snapshot("test_shrink");
}

#[test]
fn test_modifiers() {
let mut harness = Harness::new_ui_state(
|ui, cmd_clicked| {
if ui.button("Click me").clicked() && ui.input(|i| i.modifiers.command) {
*cmd_clicked = true;
}
},
false,
);

harness.get_by_label("Click me").key_down(Key::Command);
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();

assert!(harness.state(), "The button wasn't command-clicked");
}

0 comments on commit bc214ce

Please sign in to comment.