1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-02 06:40:06 -04:00

Fix: request_repaint_after works even when called from background thread (#2939)

* Refactor repaint logic

* request_repaint_after also fires the request_repaint callback

* Bug fixes

* Add test to egui_demo_app

* build_demo_web: build debug unless --release is specified

* Fix the web backend too

* Run special clippy for wasm, forbidding some types/methods

* Remove wasm_bindgen_check.sh

* Fix typos

* Revert "Remove wasm_bindgen_check.sh"

This reverts commit 92dde25344.

* Only run cranky/clippy once
This commit is contained in:
Emil Ernerfeldt
2023-04-20 10:56:52 +02:00
committed by GitHub
parent d46cf067ea
commit 834e2e9f50
14 changed files with 370 additions and 188 deletions

View File

@@ -1,5 +1,3 @@
use egui::Widget;
/// How often we repaint the demo app by default
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
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", serde(default))]
pub struct BackendPanel {
@@ -52,9 +51,6 @@ pub struct BackendPanel {
// go back to [`RunMode::Reactive`] mode each time we start
run_mode: RunMode,
#[cfg_attr(feature = "serde", serde(skip))]
repaint_after_seconds: f32,
/// current slider value for current gui scale
#[cfg_attr(feature = "serde", serde(skip))]
pixels_per_point: Option<f32>,
@@ -65,19 +61,6 @@ pub struct BackendPanel {
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 {
pub fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
self.frame_history
@@ -90,9 +73,6 @@ impl BackendPanel {
}
RunMode::Reactive => {
// let the computer rest for a bit
ctx.request_repaint_after(std::time::Duration::from_secs_f32(
self.repaint_after_seconds,
));
}
}
}
@@ -271,17 +251,27 @@ impl BackendPanel {
} else {
ui.label("Only running UI code when there are animations or input.");
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = 0.0;
ui.label("(but at least every ");
egui::DragValue::new(&mut self.repaint_after_seconds)
.clamp_range(0.1..=10.0)
.speed(0.1)
.suffix(" s")
.ui(ui)
.on_hover_text("Repaint this often, even if there is no input.");
ui.label(")");
});
// Add a test for `request_repaint_after`, but only in debug
// builds to keep the noise down in the official demo.
if cfg!(debug_assertions) {
ui.collapsing("More…", |ui| {
ui.horizontal(|ui| {
ui.label("Frame number:");
ui.monospace(ui.ctx().frame_nr().to_string());
});
if ui
.button("Wait 2s, then request repaint after another 3s")
.clicked()
{
log::info!("Waiting 2s before requesting repaint...");
let ctx = ui.ctx().clone();
call_after_delay(std::time::Duration::from_secs(2), move || {
log::info!("Request a repaint in 3s...");
ctx.request_repaint_after(std::time::Duration::from_secs(3));
});
}
});
}
}
}
}
@@ -394,3 +384,28 @@ impl EguiWindows {
});
}
}
// ----------------------------------------------------------------------------
#[cfg(not(target_arch = "wasm32"))]
fn call_after_delay(delay: std::time::Duration, f: impl FnOnce() + Send + 'static) {
std::thread::spawn(move || {
std::thread::sleep(delay);
f();
});
}
#[cfg(target_arch = "wasm32")]
fn call_after_delay(delay: std::time::Duration, f: impl FnOnce() + Send + 'static) {
use wasm_bindgen::prelude::*;
let window = web_sys::window().unwrap();
let closure = Closure::once(f);
let delay_ms = delay.as_millis() as _;
window
.set_timeout_with_callback_and_timeout_and_arguments_0(
closure.as_ref().unchecked_ref(),
delay_ms,
)
.unwrap();
closure.forget(); // We must forget it, or else the callback is canceled on drop
}

View File

@@ -33,12 +33,6 @@ impl FrameHistory {
}
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!(
"Mean CPU usage: {:.2} ms / frame",
1e3 * self.mean_frame_time()