diff options
Diffstat (limited to 'winit')
-rw-r--r-- | winit/Cargo.toml | 4 | ||||
-rw-r--r-- | winit/README.md | 2 | ||||
-rw-r--r-- | winit/src/application.rs | 64 | ||||
-rw-r--r-- | winit/src/settings.rs | 6 |
4 files changed, 69 insertions, 7 deletions
diff --git a/winit/Cargo.toml b/winit/Cargo.toml index bfcfacbc..46f0cdb1 100644 --- a/winit/Cargo.toml +++ b/winit/Cargo.toml @@ -37,3 +37,7 @@ path = "../futures" [target.'cfg(target_os = "windows")'.dependencies.winapi] version = "0.3.6" + +[target.'cfg(target_arch = "wasm32")'.dependencies.web-sys] +version = "0.3" +features = ["Document", "Window"] diff --git a/winit/README.md b/winit/README.md index 58c782c6..082ae6a8 100644 --- a/winit/README.md +++ b/winit/README.md @@ -2,7 +2,7 @@ [][documentation] [](https://crates.io/crates/iced_winit) [](https://github.com/hecrj/iced/blob/master/LICENSE) -[](https://iced.zulipchat.com) +[](https://discord.gg/3xZJ65GAhd) `iced_winit` offers some convenient abstractions on top of [`iced_native`] to quickstart development when using [`winit`]. diff --git a/winit/src/application.rs b/winit/src/application.rs index e9212b5e..ed077507 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -115,12 +115,11 @@ where use futures::task; use futures::Future; use winit::event_loop::EventLoop; - use winit::platform::run_return::EventLoopExtRunReturn; let mut debug = Debug::new(); debug.startup_started(); - let mut event_loop = EventLoop::with_user_event(); + let event_loop = EventLoop::with_user_event(); let mut proxy = event_loop.create_proxy(); let mut runtime = { @@ -149,6 +148,21 @@ where .build(&event_loop) .map_err(Error::WindowCreationFailed)?; + #[cfg(target_arch = "wasm32")] + { + use winit::platform::web::WindowExtWebSys; + + let canvas = window.canvas(); + + let window = web_sys::window().unwrap(); + let document = window.document().unwrap(); + let body = document.body().unwrap(); + + let _ = body + .append_child(&canvas) + .expect("Append canvas to HTML body"); + } + let mut clipboard = Clipboard::connect(&window); run_command( @@ -179,7 +193,7 @@ where let mut context = task::Context::from_waker(task::noop_waker_ref()); - event_loop.run_return(move |event, _, control_flow| { + platform::run(event_loop, move |event, _, control_flow| { use winit::event_loop::ControlFlow; if let ControlFlow::Exit = control_flow { @@ -211,9 +225,7 @@ where task::Poll::Ready(_) => ControlFlow::Exit, }; } - }); - - Ok(()) + }) } async fn run_instance<A, E, C>( @@ -562,3 +574,43 @@ pub fn run_command<Message: 'static + std::fmt::Debug + Send, E: Executor>( } } } + +#[cfg(not(target_arch = "wasm32"))] +mod platform { + pub fn run<T, F>( + mut event_loop: winit::event_loop::EventLoop<T>, + event_handler: F, + ) -> Result<(), super::Error> + where + F: 'static + + FnMut( + winit::event::Event<'_, T>, + &winit::event_loop::EventLoopWindowTarget<T>, + &mut winit::event_loop::ControlFlow, + ), + { + use winit::platform::run_return::EventLoopExtRunReturn; + + let _ = event_loop.run_return(event_handler); + + Ok(()) + } +} + +#[cfg(target_arch = "wasm32")] +mod platform { + pub fn run<T, F>( + event_loop: winit::event_loop::EventLoop<T>, + event_handler: F, + ) -> ! + where + F: 'static + + FnMut( + winit::event::Event<'_, T>, + &winit::event_loop::EventLoopWindowTarget<T>, + &mut winit::event_loop::ControlFlow, + ), + { + event_loop.run(event_handler) + } +} diff --git a/winit/src/settings.rs b/winit/src/settings.rs index 045cb156..9a93824a 100644 --- a/winit/src/settings.rs +++ b/winit/src/settings.rs @@ -38,6 +38,12 @@ pub struct Settings<Flags> { /// Whether the [`Application`] should exit when the user requests the /// window to close (e.g. the user presses the close button). pub exit_on_close_request: bool, + + /// Whether the [`Application`] should try to build the context + /// using OpenGL ES first then OpenGL. + /// + /// NOTE: Only works for the `glow` backend. + pub try_opengles_first: bool, } /// The window settings of an application. |