Skip to content

Commit

Permalink
Update event loop
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoqsh committed May 24, 2024
1 parent 689710d commit 57fa677
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -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 --"
30 changes: 28 additions & 2 deletions dunge/src/el.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ pub type SmolStr = keyboard::SmolStr;
pub type MouseButton = event::MouseButton;

pub fn run<U>(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<U>(cx: Context, ws: WindowState, upd: U) -> Result<(), LoopError>
where
U: Update,
{
Expand All @@ -41,11 +57,21 @@ where
out.or(handler.out)
}

pub(crate) fn spawn<U>(cx: Context, ws: WindowState, upd: U)
#[cfg(target_arch = "wasm32")]
fn spawn<U>(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]
Expand Down
5 changes: 4 additions & 1 deletion dunge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
4 changes: 2 additions & 2 deletions dunge/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ use {
/// }
/// # }
/// ```
#[deprecated]
#[cfg(all(feature = "winit", not(target_arch = "wasm32")))]
#[deprecated]
pub fn window<V>() -> WindowBuilder<V> {
WindowBuilder::new(Element(()))
}

/// 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<V>(id: &str) -> WindowBuilder<V> {
use web_sys::Window;

Expand Down

0 comments on commit 57fa677

Please sign in to comment.