From 57fa6771dbc947e00df423433251f9f364fbff72 Mon Sep 17 00:00:00 2001 From: nanoqsh Date: Fri, 24 May 2024 14:14:26 +0500 Subject: [PATCH] Update event loop --- .cargo/config.toml | 2 ++ dunge/src/el.rs | 30 ++++++++++++++++++++++++++++-- dunge/src/lib.rs | 5 ++++- dunge/src/window.rs | 4 ++-- 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index 049a52d..8da6d54 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,6 +1,8 @@ [build] +# target = "wasm32-unknown-unknown" rustflags = ["--cfg=web_sys_unstable_apis"] rustdocflags = ["--cfg=web_sys_unstable_apis"] [alias] xtask = "run --package xtask --" +x = "run --package xtask --" diff --git a/dunge/src/el.rs b/dunge/src/el.rs index 5de93ce..c0ec9c1 100644 --- a/dunge/src/el.rs +++ b/dunge/src/el.rs @@ -28,6 +28,22 @@ pub type SmolStr = keyboard::SmolStr; pub type MouseButton = event::MouseButton; pub fn run(cx: Context, ws: WindowState, upd: U) -> Result<(), LoopError> +where + U: Update + 'static, +{ + #[cfg(not(target_arch = "wasm32"))] + { + run_local(cx, ws, upd) + } + + #[cfg(target_arch = "wasm32")] + { + spawn(cx, ws, upd) + } +} + +#[cfg(not(target_arch = "wasm32"))] +pub fn run_local(cx: Context, ws: WindowState, upd: U) -> Result<(), LoopError> where U: Update, { @@ -41,11 +57,21 @@ where out.or(handler.out) } -pub(crate) fn spawn(cx: Context, ws: WindowState, upd: U) +#[cfg(target_arch = "wasm32")] +fn spawn(cx: Context, ws: WindowState, upd: U) -> Result<(), LoopError> where U: Update + 'static, { - todo!() + use winit::platform::web::EventLoopExtWebSys; + + let lu = EventLoop::with_user_event() + .build() + .map_err(LoopError::EventLoop)?; + + let view = View::new(ws); + let handler = Handler::new(cx, view, upd); + lu.spawn_app(handler); + Ok(()) } #[deprecated] diff --git a/dunge/src/lib.rs b/dunge/src/lib.rs index 4f397c6..2b7b419 100644 --- a/dunge/src/lib.rs +++ b/dunge/src/lib.rs @@ -49,7 +49,10 @@ pub use { }; #[cfg(all(feature = "winit", not(target_arch = "wasm32")))] -pub use crate::window::{window, window_state}; +pub use crate::{ + el::run_local, + window::{window, window_state}, +}; #[cfg(all(feature = "winit", target_arch = "wasm32"))] pub use crate::window::{from_element, window_state_from_element}; diff --git a/dunge/src/window.rs b/dunge/src/window.rs index 2ed8614..8a2338f 100644 --- a/dunge/src/window.rs +++ b/dunge/src/window.rs @@ -41,8 +41,8 @@ use { /// } /// # } /// ``` -#[deprecated] #[cfg(all(feature = "winit", not(target_arch = "wasm32")))] +#[deprecated] pub fn window() -> WindowBuilder { WindowBuilder::new(Element(())) } @@ -50,8 +50,8 @@ pub fn window() -> WindowBuilder { /// Creates the [window builder](WindowBuilder) to /// construct the [window](Window) /// in the given html element. -#[deprecated] #[cfg(all(feature = "winit", target_arch = "wasm32"))] +#[deprecated] pub fn from_element(id: &str) -> WindowBuilder { use web_sys::Window;