`Harness`: Add `remove_cursor`, `event` and `event_modifiers` (#7607)
* Closes https://github.com/emilk/egui/issues/7591
This commit is contained in:
parent
4d4f90eb31
commit
718a82b013
|
|
@ -460,11 +460,15 @@ impl<'a, State> Harness<'a, State> {
|
||||||
&mut self.state
|
&mut self.state
|
||||||
}
|
}
|
||||||
|
|
||||||
fn event(&self, event: egui::Event) {
|
/// Queue an event to be processed in the next frame.
|
||||||
|
pub fn event(&self, event: egui::Event) {
|
||||||
self.queued_events.lock().push(EventType::Event(event));
|
self.queued_events.lock().push(EventType::Event(event));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn event_modifiers(&self, event: egui::Event, modifiers: Modifiers) {
|
/// Queue an event with modifiers.
|
||||||
|
///
|
||||||
|
/// Queues the modifiers to be pressed, then the event, then the modifiers to be released.
|
||||||
|
pub fn event_modifiers(&self, event: egui::Event, modifiers: Modifiers) {
|
||||||
let mut queue = self.queued_events.lock();
|
let mut queue = self.queued_events.lock();
|
||||||
queue.push(EventType::Modifiers(modifiers));
|
queue.push(EventType::Modifiers(modifiers));
|
||||||
queue.push(EventType::Event(event));
|
queue.push(EventType::Event(event));
|
||||||
|
|
@ -584,6 +588,16 @@ impl<'a, State> Harness<'a, State> {
|
||||||
self.key_combination_modifiers(modifiers, &[key]);
|
self.key_combination_modifiers(modifiers, &[key]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Remove the cursor from the screen.
|
||||||
|
///
|
||||||
|
/// Will fire a [`egui::Event::PointerGone`] event.
|
||||||
|
///
|
||||||
|
/// If you click a button and then take a snapshot, the button will be shown as hovered.
|
||||||
|
/// If you don't want that, you can call this method after clicking.
|
||||||
|
pub fn remove_cursor(&self) {
|
||||||
|
self.event(egui::Event::PointerGone);
|
||||||
|
}
|
||||||
|
|
||||||
/// Mask something. Useful for snapshot tests.
|
/// Mask something. Useful for snapshot tests.
|
||||||
///
|
///
|
||||||
/// Call this _after_ [`Self::run`] and before [`Self::snapshot`].
|
/// Call this _after_ [`Self::run`] and before [`Self::snapshot`].
|
||||||
|
|
|
||||||
|
|
@ -181,3 +181,36 @@ fn test_masking() {
|
||||||
|
|
||||||
harness.snapshot("test_masking");
|
harness.snapshot("test_masking");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_remove_cursor() {
|
||||||
|
let hovered = false;
|
||||||
|
let mut harness = Harness::new_ui_state(
|
||||||
|
|ui, state| {
|
||||||
|
let response = ui.button("Click me");
|
||||||
|
*state = response.hovered();
|
||||||
|
},
|
||||||
|
hovered,
|
||||||
|
);
|
||||||
|
|
||||||
|
harness.fit_contents();
|
||||||
|
|
||||||
|
harness.get_by_label("Click me").click();
|
||||||
|
harness.run();
|
||||||
|
|
||||||
|
assert!(harness.state(), "The button should be hovered");
|
||||||
|
let hovered_button_snapshot = harness.render().expect("Failed to render");
|
||||||
|
|
||||||
|
harness.remove_cursor();
|
||||||
|
harness.run();
|
||||||
|
assert!(
|
||||||
|
!harness.state(),
|
||||||
|
"The button should not be hovered after removing cursor"
|
||||||
|
);
|
||||||
|
|
||||||
|
let non_hovered_button_snapshot = harness.render().expect("Failed to render");
|
||||||
|
assert_ne!(
|
||||||
|
hovered_button_snapshot, non_hovered_button_snapshot,
|
||||||
|
"The button appearance should change"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue