mirror of
https://github.com/emilk/egui.git
synced 2026-09-02 14:50:03 -04:00
Add Harness::new_eframe and TestRenderer trait (#5539)
Co-authored-by: Andreas Reich <r_andreas2@web.de>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
use crate::app_kind::AppKind;
|
||||
use crate::Harness;
|
||||
use crate::wgpu::WgpuTestRenderer;
|
||||
use crate::{Harness, LazyRenderer, TestRenderer};
|
||||
use egui::{Pos2, Rect, Vec2};
|
||||
use std::marker::PhantomData;
|
||||
|
||||
@@ -8,6 +9,7 @@ pub struct HarnessBuilder<State = ()> {
|
||||
pub(crate) screen_rect: Rect,
|
||||
pub(crate) pixels_per_point: f32,
|
||||
pub(crate) state: PhantomData<State>,
|
||||
pub(crate) renderer: Box<dyn TestRenderer>,
|
||||
}
|
||||
|
||||
impl<State> Default for HarnessBuilder<State> {
|
||||
@@ -16,6 +18,7 @@ impl<State> Default for HarnessBuilder<State> {
|
||||
screen_rect: Rect::from_min_size(Pos2::ZERO, Vec2::new(800.0, 600.0)),
|
||||
pixels_per_point: 1.0,
|
||||
state: PhantomData,
|
||||
renderer: Box::new(LazyRenderer::default()),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -37,6 +40,29 @@ impl<State> HarnessBuilder<State> {
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the [`TestRenderer`] to use for rendering.
|
||||
///
|
||||
/// By default, a [`LazyRenderer`] is used.
|
||||
#[inline]
|
||||
pub fn renderer(mut self, renderer: impl TestRenderer + 'static) -> Self {
|
||||
self.renderer = Box::new(renderer);
|
||||
self
|
||||
}
|
||||
|
||||
/// Enable wgpu rendering with a default setup suitable for testing.
|
||||
///
|
||||
/// This sets up a [`WgpuTestRenderer`] with the default setup.
|
||||
#[cfg(feature = "wgpu")]
|
||||
pub fn wgpu(self) -> Self {
|
||||
self.renderer(WgpuTestRenderer::default())
|
||||
}
|
||||
|
||||
/// Enable wgpu rendering with the given setup.
|
||||
#[cfg(feature = "wgpu")]
|
||||
pub fn wgpu_setup(self, setup: egui_wgpu::WgpuSetup) -> Self {
|
||||
self.renderer(WgpuTestRenderer::from_setup(setup))
|
||||
}
|
||||
|
||||
/// Create a new Harness with the given app closure and a state.
|
||||
///
|
||||
/// The app closure will immediately be called once to create the initial ui.
|
||||
@@ -66,7 +92,7 @@ impl<State> HarnessBuilder<State> {
|
||||
app: impl FnMut(&egui::Context, &mut State) + 'a,
|
||||
state: State,
|
||||
) -> Harness<'a, State> {
|
||||
Harness::from_builder(&self, AppKind::ContextState(Box::new(app)), state)
|
||||
Harness::from_builder(self, AppKind::ContextState(Box::new(app)), state, None)
|
||||
}
|
||||
|
||||
/// Create a new Harness with the given ui closure and a state.
|
||||
@@ -95,7 +121,30 @@ impl<State> HarnessBuilder<State> {
|
||||
app: impl FnMut(&mut egui::Ui, &mut State) + 'a,
|
||||
state: State,
|
||||
) -> Harness<'a, State> {
|
||||
Harness::from_builder(&self, AppKind::UiState(Box::new(app)), state)
|
||||
Harness::from_builder(self, AppKind::UiState(Box::new(app)), state, None)
|
||||
}
|
||||
|
||||
/// Create a new [Harness] from the given eframe creation closure.
|
||||
/// The app can be accessed via the [`Harness::state`] / [`Harness::state_mut`] methods.
|
||||
#[cfg(feature = "eframe")]
|
||||
pub fn build_eframe<'a>(
|
||||
self,
|
||||
build: impl FnOnce(&mut eframe::CreationContext<'a>) -> State,
|
||||
) -> Harness<'a, State>
|
||||
where
|
||||
State: eframe::App,
|
||||
{
|
||||
let ctx = egui::Context::default();
|
||||
|
||||
let mut cc = eframe::CreationContext::_new_kittest(ctx.clone());
|
||||
let mut frame = eframe::Frame::_new_kittest();
|
||||
|
||||
self.renderer.setup_eframe(&mut cc, &mut frame);
|
||||
|
||||
let app = build(&mut cc);
|
||||
|
||||
let kind = AppKind::Eframe((|state| state, frame));
|
||||
Harness::from_builder(self, kind, app, Some(ctx))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,7 +168,7 @@ impl HarnessBuilder {
|
||||
/// });
|
||||
/// ```
|
||||
pub fn build<'a>(self, app: impl FnMut(&egui::Context) + 'a) -> Harness<'a> {
|
||||
Harness::from_builder(&self, AppKind::Context(Box::new(app)), ())
|
||||
Harness::from_builder(self, AppKind::Context(Box::new(app)), (), None)
|
||||
}
|
||||
|
||||
/// Create a new Harness with the given ui closure.
|
||||
@@ -138,6 +187,6 @@ impl HarnessBuilder {
|
||||
/// });
|
||||
/// ```
|
||||
pub fn build_ui<'a>(self, app: impl FnMut(&mut egui::Ui) + 'a) -> Harness<'a> {
|
||||
Harness::from_builder(&self, AppKind::Ui(Box::new(app)), ())
|
||||
Harness::from_builder(self, AppKind::Ui(Box::new(app)), (), None)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user