WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 718a82b

Browse files
authored
Harness: Add remove_cursor, event and event_modifiers (#7607)
* Closes #7591
1 parent 4d4f90e commit 718a82b

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

crates/egui_kittest/src/lib.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,11 +460,15 @@ impl<'a, State> Harness<'a, State> {
460460
&mut self.state
461461
}
462462

463-
fn event(&self, event: egui::Event) {
463+
/// Queue an event to be processed in the next frame.
464+
pub fn event(&self, event: egui::Event) {
464465
self.queued_events.lock().push(EventType::Event(event));
465466
}
466467

467-
fn event_modifiers(&self, event: egui::Event, modifiers: Modifiers) {
468+
/// Queue an event with modifiers.
469+
///
470+
/// Queues the modifiers to be pressed, then the event, then the modifiers to be released.
471+
pub fn event_modifiers(&self, event: egui::Event, modifiers: Modifiers) {
468472
let mut queue = self.queued_events.lock();
469473
queue.push(EventType::Modifiers(modifiers));
470474
queue.push(EventType::Event(event));
@@ -584,6 +588,16 @@ impl<'a, State> Harness<'a, State> {
584588
self.key_combination_modifiers(modifiers, &[key]);
585589
}
586590

591+
/// Remove the cursor from the screen.
592+
///
593+
/// Will fire a [`egui::Event::PointerGone`] event.
594+
///
595+
/// If you click a button and then take a snapshot, the button will be shown as hovered.
596+
/// If you don't want that, you can call this method after clicking.
597+
pub fn remove_cursor(&self) {
598+
self.event(egui::Event::PointerGone);
599+
}
600+
587601
/// Mask something. Useful for snapshot tests.
588602
///
589603
/// Call this _after_ [`Self::run`] and before [`Self::snapshot`].

crates/egui_kittest/tests/tests.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,36 @@ fn test_masking() {
181181

182182
harness.snapshot("test_masking");
183183
}
184+
185+
#[test]
186+
fn test_remove_cursor() {
187+
let hovered = false;
188+
let mut harness = Harness::new_ui_state(
189+
|ui, state| {
190+
let response = ui.button("Click me");
191+
*state = response.hovered();
192+
},
193+
hovered,
194+
);
195+
196+
harness.fit_contents();
197+
198+
harness.get_by_label("Click me").click();
199+
harness.run();
200+
201+
assert!(harness.state(), "The button should be hovered");
202+
let hovered_button_snapshot = harness.render().expect("Failed to render");
203+
204+
harness.remove_cursor();
205+
harness.run();
206+
assert!(
207+
!harness.state(),
208+
"The button should not be hovered after removing cursor"
209+
);
210+
211+
let non_hovered_button_snapshot = harness.render().expect("Failed to render");
212+
assert_ne!(
213+
hovered_button_snapshot, non_hovered_button_snapshot,
214+
"The button appearance should change"
215+
);
216+
}

0 commit comments

Comments
 (0)