mirror of
https://github.com/emilk/egui.git
synced 2026-08-29 04:40:03 -04:00
Create custom egui_kittest::Node (#7138)
This adds a custom Node struct with proper support for egui types (`Key`, `Modifiers`, `egui::Event`, `Rect`) instead of needing to use the kittest / accesskit types. I also changed the `click` function to do a proper mouse move / mouse down instead of the accesskit click. Also added `accesskit_click` to trigger the accesskit event. This resulted in some changed snapshots, since the elements are now hovered. Also renamed `press_key` to `key_press` for consistency with `key_down/key_up`. Also removed the Deref to the AccessKit Node, to make it clearer when to expect egui and when to expect accesskit types. * Closes #5705 * [x] I have followed the instructions in the PR template
This commit is contained in:
@@ -4,7 +4,6 @@
|
||||
#![cfg_attr(feature = "document-features", doc = document_features::document_features!())]
|
||||
|
||||
mod builder;
|
||||
mod event;
|
||||
#[cfg(feature = "snapshot")]
|
||||
mod snapshot;
|
||||
|
||||
@@ -14,6 +13,7 @@ use std::fmt::{Debug, Display, Formatter};
|
||||
use std::time::Duration;
|
||||
|
||||
mod app_kind;
|
||||
mod node;
|
||||
mod renderer;
|
||||
#[cfg(feature = "wgpu")]
|
||||
mod texture_to_image;
|
||||
@@ -23,13 +23,13 @@ pub mod wgpu;
|
||||
pub use kittest;
|
||||
|
||||
use crate::app_kind::AppKind;
|
||||
use crate::event::EventState;
|
||||
|
||||
pub use builder::*;
|
||||
pub use node::*;
|
||||
pub use renderer::*;
|
||||
|
||||
use egui::{Modifiers, Pos2, Rect, RepaintCause, Vec2, ViewportId};
|
||||
use kittest::{Node, Queryable};
|
||||
use egui::{Key, Modifiers, Pos2, Rect, RepaintCause, Vec2, ViewportId};
|
||||
use kittest::Queryable;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ExceededMaxStepsError {
|
||||
@@ -61,13 +61,13 @@ pub struct Harness<'a, State = ()> {
|
||||
kittest: kittest::State,
|
||||
output: egui::FullOutput,
|
||||
app: AppKind<'a, State>,
|
||||
event_state: EventState,
|
||||
response: Option<egui::Response>,
|
||||
state: State,
|
||||
renderer: Box<dyn TestRenderer>,
|
||||
max_steps: u64,
|
||||
step_dt: f32,
|
||||
wait_for_pending_images: bool,
|
||||
queued_events: EventQueue,
|
||||
}
|
||||
|
||||
impl<State> Debug for Harness<'_, State> {
|
||||
@@ -126,12 +126,12 @@ impl<'a, State> Harness<'a, State> {
|
||||
),
|
||||
output,
|
||||
response,
|
||||
event_state: EventState::default(),
|
||||
state,
|
||||
renderer,
|
||||
max_steps,
|
||||
step_dt,
|
||||
wait_for_pending_images,
|
||||
queued_events: Default::default(),
|
||||
};
|
||||
// Run the harness until it is stable, ensuring that all Areas are shown and animations are done
|
||||
harness.run_ok();
|
||||
@@ -227,12 +227,19 @@ impl<'a, State> Harness<'a, State> {
|
||||
/// This will call the app closure with each queued event and
|
||||
/// update the Harness.
|
||||
pub fn step(&mut self) {
|
||||
let events = self.kittest.take_events();
|
||||
let events = std::mem::take(&mut *self.queued_events.lock());
|
||||
if events.is_empty() {
|
||||
self._step(false);
|
||||
}
|
||||
for event in events {
|
||||
self.event_state.update(event, &mut self.input);
|
||||
match event {
|
||||
EventType::Event(event) => {
|
||||
self.input.events.push(event);
|
||||
}
|
||||
EventType::Modifiers(modifiers) => {
|
||||
self.input.modifiers = modifiers;
|
||||
}
|
||||
}
|
||||
self._step(false);
|
||||
}
|
||||
}
|
||||
@@ -414,52 +421,128 @@ impl<'a, State> Harness<'a, State> {
|
||||
&mut self.state
|
||||
}
|
||||
|
||||
/// Press a key.
|
||||
/// This will create a key down event and a key up event.
|
||||
pub fn press_key(&mut self, key: egui::Key) {
|
||||
self.input.events.push(egui::Event::Key {
|
||||
fn event(&self, event: egui::Event) {
|
||||
self.queued_events.lock().push(EventType::Event(event));
|
||||
}
|
||||
|
||||
fn event_modifiers(&self, event: egui::Event, modifiers: Modifiers) {
|
||||
let mut queue = self.queued_events.lock();
|
||||
queue.push(EventType::Modifiers(modifiers));
|
||||
queue.push(EventType::Event(event));
|
||||
queue.push(EventType::Modifiers(Modifiers::default()));
|
||||
}
|
||||
|
||||
fn modifiers(&self, modifiers: Modifiers) {
|
||||
self.queued_events
|
||||
.lock()
|
||||
.push(EventType::Modifiers(modifiers));
|
||||
}
|
||||
|
||||
pub fn key_down(&self, key: egui::Key) {
|
||||
self.event(egui::Event::Key {
|
||||
key,
|
||||
pressed: true,
|
||||
modifiers: self.input.modifiers,
|
||||
repeat: false,
|
||||
physical_key: None,
|
||||
});
|
||||
self.input.events.push(egui::Event::Key {
|
||||
key,
|
||||
pressed: false,
|
||||
modifiers: self.input.modifiers,
|
||||
modifiers: Modifiers::default(),
|
||||
repeat: false,
|
||||
physical_key: None,
|
||||
});
|
||||
}
|
||||
|
||||
/// Press a key with modifiers.
|
||||
/// This will create a key-down event, a key-up event, and update the modifiers.
|
||||
///
|
||||
/// NOTE: In contrast to the event fns on [`Node`], this will call [`Harness::step`], in
|
||||
/// order to properly update modifiers.
|
||||
pub fn press_key_modifiers(&mut self, modifiers: Modifiers, key: egui::Key) {
|
||||
// Combine the modifiers with the current modifiers
|
||||
let previous_modifiers = self.input.modifiers;
|
||||
self.input.modifiers |= modifiers;
|
||||
|
||||
self.input.events.push(egui::Event::Key {
|
||||
key,
|
||||
pressed: true,
|
||||
pub fn key_down_modifiers(&self, modifiers: Modifiers, key: egui::Key) {
|
||||
self.event_modifiers(
|
||||
egui::Event::Key {
|
||||
key,
|
||||
pressed: true,
|
||||
modifiers,
|
||||
repeat: false,
|
||||
physical_key: None,
|
||||
},
|
||||
modifiers,
|
||||
repeat: false,
|
||||
physical_key: None,
|
||||
});
|
||||
self.step();
|
||||
self.input.events.push(egui::Event::Key {
|
||||
);
|
||||
}
|
||||
|
||||
pub fn key_up(&self, key: egui::Key) {
|
||||
self.event(egui::Event::Key {
|
||||
key,
|
||||
pressed: false,
|
||||
modifiers,
|
||||
modifiers: Modifiers::default(),
|
||||
repeat: false,
|
||||
physical_key: None,
|
||||
});
|
||||
}
|
||||
|
||||
self.input.modifiers = previous_modifiers;
|
||||
pub fn key_up_modifiers(&self, modifiers: Modifiers, key: egui::Key) {
|
||||
self.event_modifiers(
|
||||
egui::Event::Key {
|
||||
key,
|
||||
pressed: false,
|
||||
modifiers,
|
||||
repeat: false,
|
||||
physical_key: None,
|
||||
},
|
||||
modifiers,
|
||||
);
|
||||
}
|
||||
|
||||
/// Press the given keys in combination.
|
||||
///
|
||||
/// For e.g. [`Key::A`] + [`Key::B`] this would generate:
|
||||
/// - Press [`Key::A`]
|
||||
/// - Press [`Key::B`]
|
||||
/// - Release [`Key::B`]
|
||||
/// - Release [`Key::A`]
|
||||
pub fn key_combination(&self, keys: &[Key]) {
|
||||
for key in keys {
|
||||
self.key_down(*key);
|
||||
}
|
||||
for key in keys.iter().rev() {
|
||||
self.key_up(*key);
|
||||
}
|
||||
}
|
||||
|
||||
/// Press the given keys in combination, with modifiers.
|
||||
///
|
||||
/// For e.g. [`Modifiers::COMMAND`] + [`Key::A`] + [`Key::B`] this would generate:
|
||||
/// - Press [`Modifiers::COMMAND`]
|
||||
/// - Press [`Key::A`]
|
||||
/// - Press [`Key::B`]
|
||||
/// - Release [`Key::B`]
|
||||
/// - Release [`Key::A`]
|
||||
/// - Release [`Modifiers::COMMAND`]
|
||||
pub fn key_combination_modifiers(&self, modifiers: Modifiers, keys: &[Key]) {
|
||||
self.modifiers(modifiers);
|
||||
|
||||
for pressed in [true, false] {
|
||||
for key in keys {
|
||||
self.event(egui::Event::Key {
|
||||
key: *key,
|
||||
pressed,
|
||||
modifiers,
|
||||
repeat: false,
|
||||
physical_key: None,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
self.modifiers(Modifiers::default());
|
||||
}
|
||||
|
||||
/// Press a key.
|
||||
///
|
||||
/// This will create a key down event and a key up event.
|
||||
pub fn key_press(&self, key: egui::Key) {
|
||||
self.key_combination(&[key]);
|
||||
}
|
||||
|
||||
/// Press a key with modifiers.
|
||||
///
|
||||
/// This will
|
||||
/// - set the modifiers
|
||||
/// - create a key down event
|
||||
/// - create a key up event
|
||||
/// - reset the modifiers
|
||||
pub fn key_press_modifiers(&self, modifiers: Modifiers, key: egui::Key) {
|
||||
self.key_combination_modifiers(modifiers, &[key]);
|
||||
}
|
||||
|
||||
/// Render the last output to an image.
|
||||
@@ -478,6 +561,18 @@ impl<'a, State> Harness<'a, State> {
|
||||
.get(&ViewportId::ROOT)
|
||||
.expect("Missing root viewport")
|
||||
}
|
||||
|
||||
fn root(&self) -> Node<'_> {
|
||||
Node {
|
||||
accesskit_node: self.kittest.root(),
|
||||
queue: &self.queued_events,
|
||||
}
|
||||
}
|
||||
|
||||
#[deprecated = "Use `Harness::root` instead."]
|
||||
pub fn node(&self) -> Node<'_> {
|
||||
self.root()
|
||||
}
|
||||
}
|
||||
|
||||
/// Utilities for stateless harnesses.
|
||||
@@ -526,11 +621,11 @@ impl<'a> Harness<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'t, 'n, State> Queryable<'t, 'n> for Harness<'_, State>
|
||||
impl<'tree, 'node, State> Queryable<'tree, 'node, Node<'tree>> for Harness<'_, State>
|
||||
where
|
||||
'n: 't,
|
||||
'node: 'tree,
|
||||
{
|
||||
fn node(&'n self) -> Node<'t> {
|
||||
self.kittest_state().node()
|
||||
fn queryable_node(&'node self) -> Node<'tree> {
|
||||
self.root()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user