1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-02 06:40:06 -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:
Lucas Meurer
2025-06-17 12:17:38 +02:00
committed by GitHub
parent 8c2df4802c
commit 0152a87519
20 changed files with 359 additions and 305 deletions

View File

@@ -1,194 +0,0 @@
use egui::Event::PointerButton;
use egui::{Event, Modifiers, Pos2};
use kittest::{ElementState, MouseButton, SimulatedEvent};
#[derive(Default)]
pub(crate) struct EventState {
last_mouse_pos: Pos2,
}
impl EventState {
/// 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, event: kittest::Event, input: &mut egui::RawInput) {
if let Some(event) = self.kittest_event_to_egui(&mut input.modifiers, event) {
input.events.push(event);
}
}
fn kittest_event_to_egui(
&mut self,
modifiers: &mut Modifiers,
event: kittest::Event,
) -> Option<egui::Event> {
match event {
kittest::Event::ActionRequest(e) => Some(Event::AccessKitActionRequest(e)),
kittest::Event::Simulated(e) => match e {
SimulatedEvent::CursorMoved { position } => {
self.last_mouse_pos = Pos2::new(position.x as f32, position.y as f32);
Some(Event::PointerMoved(Pos2::new(
position.x as f32,
position.y as f32,
)))
}
SimulatedEvent::MouseInput { state, button } => {
pointer_button_to_egui(button).map(|button| PointerButton {
button,
modifiers: *modifiers,
pos: self.last_mouse_pos,
pressed: matches!(state, ElementState::Pressed),
})
}
SimulatedEvent::Ime(text) => Some(Event::Text(text)),
SimulatedEvent::KeyInput { state, key } => {
match key {
kittest::Key::Alt => {
modifiers.alt = matches!(state, ElementState::Pressed);
}
kittest::Key::Command => {
modifiers.command = matches!(state, ElementState::Pressed);
}
kittest::Key::Control => {
modifiers.ctrl = matches!(state, ElementState::Pressed);
}
kittest::Key::Shift => {
modifiers.shift = matches!(state, ElementState::Pressed);
}
_ => {}
}
kittest_key_to_egui(key).map(|key| Event::Key {
key,
modifiers: *modifiers,
pressed: matches!(state, ElementState::Pressed),
repeat: false,
physical_key: None,
})
}
},
}
}
}
fn kittest_key_to_egui(value: kittest::Key) -> Option<egui::Key> {
use egui::Key as EKey;
use kittest::Key;
match value {
Key::ArrowDown => Some(EKey::ArrowDown),
Key::ArrowLeft => Some(EKey::ArrowLeft),
Key::ArrowRight => Some(EKey::ArrowRight),
Key::ArrowUp => Some(EKey::ArrowUp),
Key::Escape => Some(EKey::Escape),
Key::Tab => Some(EKey::Tab),
Key::Backspace => Some(EKey::Backspace),
Key::Enter => Some(EKey::Enter),
Key::Space => Some(EKey::Space),
Key::Insert => Some(EKey::Insert),
Key::Delete => Some(EKey::Delete),
Key::Home => Some(EKey::Home),
Key::End => Some(EKey::End),
Key::PageUp => Some(EKey::PageUp),
Key::PageDown => Some(EKey::PageDown),
Key::Copy => Some(EKey::Copy),
Key::Cut => Some(EKey::Cut),
Key::Paste => Some(EKey::Paste),
Key::Colon => Some(EKey::Colon),
Key::Comma => Some(EKey::Comma),
Key::Backslash => Some(EKey::Backslash),
Key::Slash => Some(EKey::Slash),
Key::Pipe => Some(EKey::Pipe),
Key::Questionmark => Some(EKey::Questionmark),
Key::OpenBracket => Some(EKey::OpenBracket),
Key::CloseBracket => Some(EKey::CloseBracket),
Key::Backtick => Some(EKey::Backtick),
Key::Minus => Some(EKey::Minus),
Key::Period => Some(EKey::Period),
Key::Plus => Some(EKey::Plus),
Key::Equals => Some(EKey::Equals),
Key::Semicolon => Some(EKey::Semicolon),
Key::Quote => Some(EKey::Quote),
Key::Num0 => Some(EKey::Num0),
Key::Num1 => Some(EKey::Num1),
Key::Num2 => Some(EKey::Num2),
Key::Num3 => Some(EKey::Num3),
Key::Num4 => Some(EKey::Num4),
Key::Num5 => Some(EKey::Num5),
Key::Num6 => Some(EKey::Num6),
Key::Num7 => Some(EKey::Num7),
Key::Num8 => Some(EKey::Num8),
Key::Num9 => Some(EKey::Num9),
Key::A => Some(EKey::A),
Key::B => Some(EKey::B),
Key::C => Some(EKey::C),
Key::D => Some(EKey::D),
Key::E => Some(EKey::E),
Key::F => Some(EKey::F),
Key::G => Some(EKey::G),
Key::H => Some(EKey::H),
Key::I => Some(EKey::I),
Key::J => Some(EKey::J),
Key::K => Some(EKey::K),
Key::L => Some(EKey::L),
Key::M => Some(EKey::M),
Key::N => Some(EKey::N),
Key::O => Some(EKey::O),
Key::P => Some(EKey::P),
Key::Q => Some(EKey::Q),
Key::R => Some(EKey::R),
Key::S => Some(EKey::S),
Key::T => Some(EKey::T),
Key::U => Some(EKey::U),
Key::V => Some(EKey::V),
Key::W => Some(EKey::W),
Key::X => Some(EKey::X),
Key::Y => Some(EKey::Y),
Key::Z => Some(EKey::Z),
Key::F1 => Some(EKey::F1),
Key::F2 => Some(EKey::F2),
Key::F3 => Some(EKey::F3),
Key::F4 => Some(EKey::F4),
Key::F5 => Some(EKey::F5),
Key::F6 => Some(EKey::F6),
Key::F7 => Some(EKey::F7),
Key::F8 => Some(EKey::F8),
Key::F9 => Some(EKey::F9),
Key::F10 => Some(EKey::F10),
Key::F11 => Some(EKey::F11),
Key::F12 => Some(EKey::F12),
Key::F13 => Some(EKey::F13),
Key::F14 => Some(EKey::F14),
Key::F15 => Some(EKey::F15),
Key::F16 => Some(EKey::F16),
Key::F17 => Some(EKey::F17),
Key::F18 => Some(EKey::F18),
Key::F19 => Some(EKey::F19),
Key::F20 => Some(EKey::F20),
Key::F21 => Some(EKey::F21),
Key::F22 => Some(EKey::F22),
Key::F23 => Some(EKey::F23),
Key::F24 => Some(EKey::F24),
Key::F25 => Some(EKey::F25),
Key::F26 => Some(EKey::F26),
Key::F27 => Some(EKey::F27),
Key::F28 => Some(EKey::F28),
Key::F29 => Some(EKey::F29),
Key::F30 => Some(EKey::F30),
Key::F31 => Some(EKey::F31),
Key::F32 => Some(EKey::F32),
Key::F33 => Some(EKey::F33),
Key::F34 => Some(EKey::F34),
Key::F35 => Some(EKey::F35),
_ => None,
}
}
fn pointer_button_to_egui(value: MouseButton) -> Option<egui::PointerButton> {
match value {
MouseButton::Left => Some(egui::PointerButton::Primary),
MouseButton::Right => Some(egui::PointerButton::Secondary),
MouseButton::Middle => Some(egui::PointerButton::Middle),
MouseButton::Back => Some(egui::PointerButton::Extra1),
MouseButton::Forward => Some(egui::PointerButton::Extra2),
MouseButton::Other(_) => None,
}
}

View File

@@ -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()
}
}

View File

@@ -0,0 +1,162 @@
use egui::accesskit::ActionRequest;
use egui::mutex::Mutex;
use egui::{accesskit, Modifiers, PointerButton, Pos2};
use kittest::{debug_fmt_node, AccessKitNode, NodeT};
use std::fmt::{Debug, Formatter};
pub(crate) enum EventType {
Event(egui::Event),
Modifiers(Modifiers),
}
pub(crate) type EventQueue = Mutex<Vec<EventType>>;
#[derive(Clone, Copy)]
pub struct Node<'tree> {
pub(crate) accesskit_node: AccessKitNode<'tree>,
pub(crate) queue: &'tree EventQueue,
}
impl Debug for Node<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
debug_fmt_node(self, f)
}
}
impl<'tree> NodeT<'tree> for Node<'tree> {
fn accesskit_node(&self) -> AccessKitNode<'tree> {
self.accesskit_node
}
fn new_related(&self, child_node: AccessKitNode<'tree>) -> Self {
Self {
queue: self.queue,
accesskit_node: child_node,
}
}
}
impl Node<'_> {
fn event(&self, event: egui::Event) {
self.queue.lock().push(EventType::Event(event));
}
fn modifiers(&self, modifiers: Modifiers) {
self.queue.lock().push(EventType::Modifiers(modifiers));
}
pub fn hover(&self) {
self.event(egui::Event::PointerMoved(self.rect().center()));
}
/// Click at the node center with the primary button.
pub fn click(&self) {
self.click_button(PointerButton::Primary);
}
#[deprecated = "Use `click()` instead."]
pub fn simulate_click(&self) {
self.click();
}
pub fn click_secondary(&self) {
self.click_button(PointerButton::Secondary);
}
pub fn click_button(&self, button: PointerButton) {
self.hover();
for pressed in [true, false] {
self.event(egui::Event::PointerButton {
pos: self.rect().center(),
button,
pressed,
modifiers: Modifiers::default(),
});
}
}
pub fn click_modifiers(&self, modifiers: Modifiers) {
self.click_button_modifiers(PointerButton::Primary, modifiers);
}
pub fn click_button_modifiers(&self, button: PointerButton, modifiers: Modifiers) {
self.hover();
self.modifiers(modifiers);
for pressed in [true, false] {
self.event(egui::Event::PointerButton {
pos: self.rect().center(),
button,
pressed,
modifiers,
});
}
self.modifiers(Modifiers::default());
}
/// Click the node via accesskit.
///
/// This will trigger a [`accesskit::Action::Click`] action.
/// In contrast to `click()`, this can also click widgets that are not currently visible.
pub fn click_accesskit(&self) {
self.event(egui::Event::AccessKitActionRequest(
accesskit::ActionRequest {
target: self.accesskit_node.id(),
action: accesskit::Action::Click,
data: None,
},
));
}
pub fn rect(&self) -> egui::Rect {
let rect = self
.accesskit_node
.bounding_box()
.expect("Every egui node should have a rect");
egui::Rect {
min: Pos2::new(rect.x0 as f32, rect.y0 as f32),
max: Pos2::new(rect.x1 as f32, rect.y1 as f32),
}
}
pub fn focus(&self) {
self.event(egui::Event::AccessKitActionRequest(ActionRequest {
action: accesskit::Action::Focus,
target: self.accesskit_node.id(),
data: None,
}));
}
#[deprecated = "Use `Harness::key_down` instead."]
pub fn key_down(&self, key: egui::Key) {
self.event(egui::Event::Key {
key,
pressed: true,
modifiers: Modifiers::default(),
repeat: false,
physical_key: None,
});
}
#[deprecated = "Use `Harness::key_up` instead."]
pub fn key_up(&self, key: egui::Key) {
self.event(egui::Event::Key {
key,
pressed: false,
modifiers: Modifiers::default(),
repeat: false,
physical_key: None,
});
}
pub fn type_text(&self, text: &str) {
self.event(egui::Event::Text(text.to_owned()));
}
pub fn value(&self) -> Option<String> {
self.accesskit_node.value()
}
pub fn is_focused(&self) -> bool {
self.accesskit_node.is_focused()
}
}