From 6ff39d88bf766da674fc3c6da632085c09d0cbb9 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 13 Dec 2020 00:30:54 +0100 Subject: [PATCH] Demo App: Add ability to close the Backend window --- egui/benches/benchmark.rs | 6 +++--- egui/src/demos/app.rs | 34 +++++++++++++++++++++++++++++----- egui/src/demos/demo_windows.rs | 4 ++++ egui/src/lib.rs | 24 ++++++++++++------------ 4 files changed, 48 insertions(+), 20 deletions(-) diff --git a/egui/benches/benchmark.rs b/egui/benches/benchmark.rs index aa49a73a..0a90a614 100644 --- a/egui/benches/benchmark.rs +++ b/egui/benches/benchmark.rs @@ -13,7 +13,7 @@ pub fn criterion_benchmark(c: &mut Criterion) { c.bench_function("demo_windows_minimal", |b| { b.iter(|| { ctx.begin_frame(raw_input.clone()); - demo_windows.ui(&ctx, &Default::default(), &mut None); + demo_windows.ui(&ctx, &Default::default(), &mut None, |_ui| {}); ctx.end_frame() }) }); @@ -27,7 +27,7 @@ pub fn criterion_benchmark(c: &mut Criterion) { c.bench_function("demo_windows_full", |b| { b.iter(|| { ctx.begin_frame(raw_input.clone()); - demo_windows.ui(&ctx, &Default::default(), &mut None); + demo_windows.ui(&ctx, &Default::default(), &mut None, |_ui| {}); ctx.end_frame() }) }); @@ -38,7 +38,7 @@ pub fn criterion_benchmark(c: &mut Criterion) { ctx.memory().all_collpasing_are_open = true; // expand the demo window with everything let mut demo_windows = egui::demos::DemoWindows::default(); ctx.begin_frame(raw_input.clone()); - demo_windows.ui(&ctx, &Default::default(), &mut None); + demo_windows.ui(&ctx, &Default::default(), &mut None, |_ui| {}); let (_, paint_commands) = ctx.end_frame(); c.bench_function("tesselate", |b| { diff --git a/egui/src/demos/app.rs b/egui/src/demos/app.rs index 1f8481ac..956fb09d 100644 --- a/egui/src/demos/app.rs +++ b/egui/src/demos/app.rs @@ -69,6 +69,10 @@ impl FrameHistory { self.frame_times.add(now, previus_frame_time); // projected } + fn mean_frame_time(&self) -> f32 { + self.frame_times.average().unwrap_or_default() + } + fn fps(&self) -> f32 { 1.0 / self.frame_times.mean_time_interval().unwrap_or_default() } @@ -81,7 +85,7 @@ impl FrameHistory { ui.label(format!( "Mean CPU usage per frame: {:.2} ms / frame", - 1e3 * self.frame_times.average().unwrap_or_default() + 1e3 * self.mean_frame_time() )) .on_hover_text( "Includes Egui layout and tesselation time.\n\ @@ -174,6 +178,8 @@ impl FrameHistory { pub struct DemoApp { demo_windows: demos::DemoWindows, + backend_window_open: bool, + #[cfg_attr(feature = "serde", serde(skip))] // go back to `Reactive` mode each time we start run_mode: RunMode, @@ -186,9 +192,6 @@ pub struct DemoApp { impl DemoApp { fn backend_ui(&mut self, ui: &mut Ui, integration_context: &mut app::IntegrationContext<'_>) { - self.frame_history - .on_new_frame(ui.input().time, integration_context.info.cpu_usage); - let is_web = integration_context.info.web_info.is_some(); if is_web { @@ -280,6 +283,9 @@ impl app::App for DemoApp { ctx: &Arc, integration_context: &mut crate::app::IntegrationContext<'_>, ) { + self.frame_history + .on_new_frame(ctx.input().time, integration_context.info.cpu_usage); + let web_location_hash = integration_context .info .web_info @@ -298,18 +304,36 @@ impl app::App for DemoApp { link, }; - self.demo_windows.ui( + let mean_frame_time = self.frame_history.mean_frame_time(); + + let Self { + demo_windows, + backend_window_open, + .. + } = self; + + demo_windows.ui( ctx, &demo_environment, &mut integration_context.tex_allocator, + |ui| { + ui.separator(); + ui.checkbox(backend_window_open, "💻 Backend"); + + ui.label(format!("{:.2} ms / frame", 1e3 * mean_frame_time)) + .on_hover_text("CPU usage."); + }, ); + let mut backend_window_open = self.backend_window_open; crate::Window::new("💻 Backend") .min_width(360.0) .scroll(false) + .open(&mut backend_window_open) .show(ctx, |ui| { self.backend_ui(ui, integration_context); }); + self.backend_window_open = backend_window_open; if self.run_mode == RunMode::Continuous { // Tell the backend to repaint as soon as possible diff --git a/egui/src/demos/demo_windows.rs b/egui/src/demos/demo_windows.rs index a871ad31..98c89f26 100644 --- a/egui/src/demos/demo_windows.rs +++ b/egui/src/demos/demo_windows.rs @@ -84,11 +84,13 @@ pub struct DemoWindows { impl DemoWindows { /// Show the app ui (menu bar and windows). + /// `sidebar_ui` can be used to optionally show some things in the sidebar pub fn ui( &mut self, ctx: &Arc, env: &DemoEnvironment, tex_allocator: &mut Option<&mut dyn app::TextureAllocator>, + sidebar_ui: impl FnOnce(&mut Ui), ) { if self.previous_link != env.link { match env.link { @@ -125,6 +127,8 @@ impl DemoWindows { self.open_windows.checkboxes(ui); self.demos.checkboxes(ui); }); + + sidebar_ui(ui); }); crate::TopPanel::top(Id::new("menu_bar")).show(ctx, |ui| { diff --git a/egui/src/lib.rs b/egui/src/lib.rs index 64c37492..8dd65c48 100644 --- a/egui/src/lib.rs +++ b/egui/src/lib.rs @@ -116,8 +116,18 @@ pub use { widgets::*, }; +#[cfg(debug_assertions)] +pub(crate) fn has_debug_assertions() -> bool { + true +} + +#[cfg(not(debug_assertions))] +pub(crate) fn has_debug_assertions() -> bool { + false +} + #[test] -pub fn text_egui_e2e() { +fn test_egui_e2e() { let mut demo_windows = crate::demos::DemoWindows::default(); let mut ctx = crate::Context::new(); let raw_input = crate::RawInput { @@ -128,19 +138,9 @@ pub fn text_egui_e2e() { const NUM_FRAMES: usize = 5; for _ in 0..NUM_FRAMES { ctx.begin_frame(raw_input.clone()); - demo_windows.ui(&ctx, &Default::default(), &mut None); + demo_windows.ui(&ctx, &Default::default(), &mut None, |_ui| {}); let (_output, paint_commands) = ctx.end_frame(); let paint_jobs = ctx.tesselate(paint_commands); assert!(!paint_jobs.is_empty()); } } - -#[cfg(debug_assertions)] -pub(crate) fn has_debug_assertions() -> bool { - true -} - -#[cfg(not(debug_assertions))] -pub(crate) fn has_debug_assertions() -> bool { - false -}