1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-31 22:00:03 -04:00

Add test to egui_demo_app

This commit is contained in:
Emil Ernerfeldt
2023-04-20 09:46:34 +02:00
parent 4851c51357
commit f7bba5fba2
2 changed files with 23 additions and 38 deletions

View File

@@ -1,5 +1,3 @@
use egui::Widget;
/// How often we repaint the demo app by default /// How often we repaint the demo app by default
#[derive(Clone, Copy, Debug, Eq, PartialEq)] #[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum RunMode { enum RunMode {
@@ -43,6 +41,7 @@ impl Default for RunMode {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
#[derive(Default)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(default))] #[cfg_attr(feature = "serde", serde(default))]
pub struct BackendPanel { pub struct BackendPanel {
@@ -52,9 +51,6 @@ pub struct BackendPanel {
// go back to [`RunMode::Reactive`] mode each time we start // go back to [`RunMode::Reactive`] mode each time we start
run_mode: RunMode, run_mode: RunMode,
#[cfg_attr(feature = "serde", serde(skip))]
repaint_after_seconds: f32,
/// current slider value for current gui scale /// current slider value for current gui scale
#[cfg_attr(feature = "serde", serde(skip))] #[cfg_attr(feature = "serde", serde(skip))]
pixels_per_point: Option<f32>, pixels_per_point: Option<f32>,
@@ -65,19 +61,6 @@ pub struct BackendPanel {
egui_windows: EguiWindows, egui_windows: EguiWindows,
} }
impl Default for BackendPanel {
fn default() -> Self {
Self {
open: false,
run_mode: Default::default(),
repaint_after_seconds: 1.0,
pixels_per_point: None,
frame_history: Default::default(),
egui_windows: Default::default(),
}
}
}
impl BackendPanel { impl BackendPanel {
pub fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) { pub fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
self.frame_history self.frame_history
@@ -90,9 +73,6 @@ impl BackendPanel {
} }
RunMode::Reactive => { RunMode::Reactive => {
// let the computer rest for a bit // let the computer rest for a bit
ctx.request_repaint_after(std::time::Duration::from_secs_f32(
self.repaint_after_seconds,
));
} }
} }
} }
@@ -271,17 +251,28 @@ impl BackendPanel {
} else { } else {
ui.label("Only running UI code when there are animations or input."); ui.label("Only running UI code when there are animations or input.");
ui.horizontal(|ui| { // Add a test for `request_repaint_after`, but onl in debug
ui.spacing_mut().item_spacing.x = 0.0; // builds to keep the noise down in the official demo.
ui.label("(but at least every "); if cfg!(debug_assertions) {
egui::DragValue::new(&mut self.repaint_after_seconds) ui.collapsing("More…", |ui| {
.clamp_range(0.1..=10.0) ui.horizontal(|ui| {
.speed(0.1) ui.label("Frame number:");
.suffix(" s") ui.monospace(ui.ctx().frame_nr().to_string());
.ui(ui) });
.on_hover_text("Repaint this often, even if there is no input."); if ui
ui.label(")"); .button("Wait 2s, then request repaint after another 3s")
}); .clicked()
{
log::info!("Waiting 2s before requesting repaint...");
let ctx = ui.ctx().clone();
std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_secs(2));
log::info!("Request a repaint in 3s...");
ctx.request_repaint_after(std::time::Duration::from_secs(3));
});
}
});
}
} }
} }
} }

View File

@@ -33,12 +33,6 @@ impl FrameHistory {
} }
pub fn ui(&mut self, ui: &mut egui::Ui) { pub fn ui(&mut self, ui: &mut egui::Ui) {
ui.label(format!(
"Total frames painted: {}",
self.frame_times.total_count()
))
.on_hover_text("Includes this frame.");
ui.label(format!( ui.label(format!(
"Mean CPU usage: {:.2} ms / frame", "Mean CPU usage: {:.2} ms / frame",
1e3 * self.mean_frame_time() 1e3 * self.mean_frame_time()