Skip to content

Commit

Permalink
Add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmerlin committed Oct 5, 2024
1 parent e1a8196 commit a5994d8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 44 deletions.
21 changes: 21 additions & 0 deletions crates/egui_kittest/src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::Harness;
use egui::{Pos2, Rect, Vec2};

/// Builder for [`Harness`].
pub struct HarnessBuilder {
pub(crate) screen_rect: Rect,
pub(crate) dpi: f32,
Expand All @@ -16,17 +17,37 @@ impl Default for HarnessBuilder {
}

impl HarnessBuilder {
/// Set the size of the window.
#[inline]
pub fn with_size(mut self, size: Vec2) -> Self {
self.screen_rect.set_width(size.x);
self.screen_rect.set_height(size.y);
self
}

/// Set the DPI of the window.
#[inline]
pub fn with_dpi(mut self, dpi: f32) -> Self {
self.dpi = dpi;
self
}

/// Create a new Harness with the given app closure.
///
/// The ui closure will immediately be called once to create the initial ui.
///
/// # Example
/// ```rust
/// # use egui::CentralPanel;
/// # use egui_kittest::Harness;
/// let mut harness = Harness::builder()
/// .with_size(egui::Vec2::new(300.0, 200.0))
/// .build(|ctx| {
/// CentralPanel::default().show(ctx, |ui| {
/// ui.label("Hello, world!");
/// });
/// });
/// ```
pub fn build<'a>(self, app: impl FnMut(&egui::Context) + 'a) -> Harness<'a> {
Harness::from_builder(&self, app)
}
Expand Down
72 changes: 28 additions & 44 deletions crates/egui_kittest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ use std::mem;
use crate::event::{kittest_key_to_egui, pointer_button_to_egui};
pub use accesskit_consumer;
pub use builder::*;
use egui::accesskit::NodeId;
use egui::{Event, Modifiers, Pos2, Rect, TexturesDelta, Vec2, ViewportId};
use kittest::{ElementState, Node, Queryable, SimulatedEvent, State};
use kittest::{ElementState, Node, Queryable, SimulatedEvent};

/// The test Harness. This contains everything needed to run the test.
/// Create a new Harness using [`Harness::new`] or [`Harness::builder`].
pub struct Harness<'a> {
pub ctx: egui::Context,
input: egui::RawInput,
kittest: State,
kittest: kittest::State,
output: egui::FullOutput,
texture_deltas: Vec<TexturesDelta>,
update_fn: Box<dyn FnMut(&egui::Context) + 'a>,
Expand Down Expand Up @@ -52,7 +53,7 @@ impl<'a> Harness<'a> {
update_fn: Box::new(app),
ctx,
input,
kittest: State::new(
kittest: kittest::State::new(
output
.platform_output
.accesskit_update
Expand All @@ -71,6 +72,22 @@ impl<'a> Harness<'a> {
HarnessBuilder::default()
}

/// Create a new Harness with the given app closure.
///
/// The ui closure will immediately be called once to create the initial ui.
///
/// If you e.g. want to customize the size of the window, you can use [`Harness::builder`].
///
/// # Example
/// ```rust
/// # use egui::CentralPanel;
/// # use egui_kittest::Harness;
/// let mut harness = Harness::new(|ctx| {
/// CentralPanel::default().show(ctx, |ui| {
/// ui.label("Hello, world!");
/// });
/// });
/// ```
pub fn new(app: impl FnMut(&egui::Context) + 'a) -> Self {
Self::builder().build(app)
}
Expand All @@ -93,6 +110,8 @@ impl<'a> Harness<'a> {
self
}

/// Run a frame.
/// This will call the app closure with the current context and update the Harness.
pub fn run(&mut self) {
for event in self.kittest.take_events() {
match event {
Expand Down Expand Up @@ -164,58 +183,23 @@ impl<'a> Harness<'a> {
self.output = output;
}

pub fn click(&mut self, id: NodeId) {
let action = egui::accesskit::ActionRequest {
target: id,
action: egui::accesskit::Action::Default,
data: None,
};
self.input
.events
.push(egui::Event::AccessKitActionRequest(action));
}

pub fn focus(&mut self, id: NodeId) {
let action = egui::accesskit::ActionRequest {
target: id,
action: egui::accesskit::Action::Focus,
data: None,
};
self.input
.events
.push(Event::AccessKitActionRequest(action));
}

// TODO(lucasmerlin): SetValue is currently not supported by egui
// pub fn set_text(&mut self, id: NodeId, text: &str) {
// let action = egui::accesskit::ActionRequest {
// target: id,
// action: egui::accesskit::Action::SetValue,
// data: Some(ActionData::Value(Box::from(text))),
// };
// self.input
// .events
// .push(egui::Event::AccessKitActionRequest(action));
// }

pub fn type_text(&mut self, id: NodeId, text: &str) {
self.focus(id);
self.input.events.push(egui::Event::Text(text.to_owned()));
}

/// Access the [`egui::RawInput`] for the next frame.
pub fn input(&self) -> &egui::RawInput {
&self.input
}

/// Access the [`egui::RawInput`] for the next frame mutably.
pub fn input_mut(&mut self) -> &mut egui::RawInput {
&mut self.input
}

/// Access the [`egui::FullOutput`] for the last frame.
pub fn output(&self) -> &egui::FullOutput {
&self.output
}

pub fn kittest_state(&self) -> &State {
/// Access the [`kittest::State`].
pub fn kittest_state(&self) -> &kittest::State {
&self.kittest
}
}
Expand Down

0 comments on commit a5994d8

Please sign in to comment.