mirror of
https://github.com/emilk/egui.git
synced 2026-09-02 06:40:06 -04:00
Add Harness::new_ui, Harness::fit_contents (#5301)
This adds a `Harness::new_ui`, which accepts a Ui closure and shows the ui in a central panel. One big benefit is that this allows us to add a fit_contents method that can run the ui closure with a sizing pass and resize the "screen" based on the content size. I also used this to add a snapshot test for the rendering_test at different scales.
This commit is contained in:
@@ -290,7 +290,7 @@ fn doc_link_label_with_crate<'a>(
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::View;
|
||||
use egui::{CentralPanel, Context, Vec2};
|
||||
use egui::Vec2;
|
||||
use egui_kittest::Harness;
|
||||
|
||||
#[test]
|
||||
@@ -300,15 +300,12 @@ mod tests {
|
||||
date: Some(chrono::NaiveDate::from_ymd_opt(2024, 1, 1).unwrap()),
|
||||
..Default::default()
|
||||
};
|
||||
let app = |ctx: &Context| {
|
||||
CentralPanel::default().show(ctx, |ui| {
|
||||
demo.ui(ui);
|
||||
});
|
||||
};
|
||||
let harness = Harness::builder()
|
||||
let mut harness = Harness::builder()
|
||||
.with_pixels_per_point(2.0)
|
||||
.with_size(Vec2::new(380.0, 550.0))
|
||||
.with_dpi(2.0)
|
||||
.build(app);
|
||||
.build_ui(|ui| demo.ui(ui));
|
||||
|
||||
harness.fit_contents();
|
||||
|
||||
harness.wgpu_snapshot("widget_gallery");
|
||||
}
|
||||
|
||||
@@ -435,10 +435,7 @@ fn pixel_test_strokes(ui: &mut Ui) {
|
||||
let thickness_pixels = thickness_pixels as f32;
|
||||
let thickness_points = thickness_pixels / pixels_per_point;
|
||||
let num_squares = (pixels_per_point * 10.0).round().max(10.0) as u32;
|
||||
let size_pixels = vec2(
|
||||
ui.available_width(),
|
||||
num_squares as f32 + thickness_pixels * 2.0,
|
||||
);
|
||||
let size_pixels = vec2(ui.min_size().x, num_squares as f32 + thickness_pixels * 2.0);
|
||||
let size_points = size_pixels / pixels_per_point + Vec2::splat(2.0);
|
||||
let (response, painter) = ui.allocate_painter(size_points, Sense::hover());
|
||||
|
||||
@@ -680,3 +677,34 @@ fn mul_color_gamma(left: Color32, right: Color32) -> Color32 {
|
||||
(left.a() as f32 * right.a() as f32 / 255.0).round() as u8,
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::ColorTest;
|
||||
use egui::vec2;
|
||||
|
||||
#[test]
|
||||
pub fn rendering_test() {
|
||||
let mut errors = vec![];
|
||||
for dpi in [1.0, 1.25, 1.5, 1.75, 1.6666667, 2.0] {
|
||||
let mut color_test = ColorTest::default();
|
||||
let mut harness = egui_kittest::Harness::builder()
|
||||
.with_size(vec2(2000.0, 2000.0))
|
||||
.with_pixels_per_point(dpi)
|
||||
.build_ui(|ui| {
|
||||
color_test.ui(ui);
|
||||
});
|
||||
|
||||
//harness.set_size(harness.ctx.used_size());
|
||||
|
||||
harness.fit_contents();
|
||||
|
||||
let result = harness.try_wgpu_snapshot(&format!("rendering_test/dpi_{dpi:.2}"));
|
||||
if let Err(err) = result {
|
||||
errors.push(err);
|
||||
}
|
||||
}
|
||||
|
||||
assert!(errors.is_empty(), "Errors: {errors:#?}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:023eaa363b42ec24ae845dc2ca9ff271a0bd47217e625785d3716044ecfa7a64
|
||||
size 278444
|
||||
@@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:d81f618e54176b1c43b710121f249e13ce29827fbea3451827ab62229006677e
|
||||
size 378603
|
||||
@@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:2d8eca6d5555ef779233175615b877fb91318b4a09a37e5cfbe71973d56f4caf
|
||||
size 465907
|
||||
@@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:4768804f57dfc54c5f6b84a2686038b8d630a28c7e928ae044d5b2ce8377e2cd
|
||||
size 538775
|
||||
@@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:fcee0e0302f33d681348d62bee3b548beb494c6dd1fa3454586986e0b699e162
|
||||
size 572403
|
||||
@@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:254a8dff0b1d4b74971fd3bd4044c4ec0ce49412a95e98419a14dc55b32a4fc9
|
||||
size 663272
|
||||
@@ -1,3 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:5dc632962f8894c4f20a48c9b9e57d60470f3f83ef7f19d05854dba718610a2f
|
||||
size 161820
|
||||
oid sha256:c069ef4f86beeeafd8686f30fc914bedd7e7e7ec38fd96e9a46ac6b31308c43f
|
||||
size 160883
|
||||
|
||||
Reference in New Issue
Block a user