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

Fix the web backend too

This commit is contained in:
Emil Ernerfeldt
2023-04-20 10:14:17 +02:00
parent a27182472f
commit 6bac3d702f
5 changed files with 37 additions and 6 deletions

View File

@@ -63,4 +63,6 @@ env_logger = "0.10"
# web:
[target.'cfg(target_arch = "wasm32")'.dependencies]
console_error_panic_hook = "0.1.6"
wasm-bindgen = "=0.2.84"
wasm-bindgen-futures = "0.4"
web-sys = "0.3"

View File

@@ -265,8 +265,7 @@ impl BackendPanel {
{
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));
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));
});
@@ -385,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
}