Skip to content

Commit

Permalink
Fix wasm32 deployments not displaying anything (#2574)
Browse files Browse the repository at this point in the history
* reuse `canvas` element generated by dummy window

* fix formatting

* set `control_flow` to `Poll` in `resumed`

this is mostly a fix for Chrome

* Avoid blowing up memory when booting up on Wasm

---------

Co-authored-by: Héctor Ramón Jiménez <hector@hecrj.dev>
  • Loading branch information
derezzedex and hecrj authored Sep 13, 2024
1 parent f3b51e4 commit d46f6f9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 27 deletions.
2 changes: 1 addition & 1 deletion winit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ winapi.workspace = true

[target.'cfg(target_arch = "wasm32")'.dependencies]
web-sys.workspace = true
web-sys.features = ["Document", "Window"]
web-sys.features = ["Document", "Window", "HtmlCanvasElement"]
wasm-bindgen-futures.workspace = true
67 changes: 41 additions & 26 deletions winit/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ where
#[cfg(target_arch = "wasm32")]
is_booted: std::rc::Rc<std::cell::RefCell<bool>>,
#[cfg(target_arch = "wasm32")]
queued_events: Vec<Event<Action<Message>>>,
canvas: Option<web_sys::HtmlCanvasElement>,
}

struct BootConfig<C> {
Expand All @@ -276,7 +276,7 @@ where
#[cfg(target_arch = "wasm32")]
is_booted: std::rc::Rc::new(std::cell::RefCell::new(false)),
#[cfg(target_arch = "wasm32")]
queued_events: Vec::new(),
canvas: None,
};

impl<Message, F, C> winit::application::ApplicationHandler<Action<Message>>
Expand Down Expand Up @@ -307,6 +307,12 @@ where
}
};

#[cfg(target_arch = "wasm32")]
{
use winit::platform::web::WindowExtWebSys;
self.canvas = window.canvas();
}

let finish_boot = async move {
let mut compositor =
C::new(graphics_settings, window.clone()).await?;
Expand Down Expand Up @@ -340,6 +346,9 @@ where

*is_booted.borrow_mut() = true;
});

event_loop
.set_control_flow(winit::event_loop::ControlFlow::Poll);
}
}

Expand All @@ -352,6 +361,11 @@ where
return;
}

#[cfg(target_arch = "wasm32")]
if !*self.is_booted.borrow() {
return;
}

self.process_event(
event_loop,
Event::EventLoopAwakened(winit::event::Event::NewEvents(cause)),
Expand Down Expand Up @@ -430,6 +444,11 @@ where
&mut self,
event_loop: &winit::event_loop::ActiveEventLoop,
) {
#[cfg(target_arch = "wasm32")]
if !*self.is_booted.borrow() {
return;
}

self.process_event(
event_loop,
Event::EventLoopAwakened(winit::event::Event::AboutToWait),
Expand All @@ -447,19 +466,6 @@ where
event_loop: &winit::event_loop::ActiveEventLoop,
event: Event<Action<Message>>,
) {
#[cfg(target_arch = "wasm32")]
if !*self.is_booted.borrow() {
self.queued_events.push(event);
return;
} else if !self.queued_events.is_empty() {
let queued_events = std::mem::take(&mut self.queued_events);

// This won't infinitely recurse, since we `mem::take`
for event in queued_events {
self.process_event(event_loop, event);
}
}

if event_loop.exiting() {
return;
}
Expand Down Expand Up @@ -505,18 +511,27 @@ where
let target =
settings.platform_specific.target.clone();

let window = event_loop
.create_window(
conversion::window_attributes(
settings,
&title,
monitor
.or(event_loop
.primary_monitor()),
self.id.clone(),
)
.with_visible(false),
let window_attributes =
conversion::window_attributes(
settings,
&title,
monitor
.or(event_loop.primary_monitor()),
self.id.clone(),
)
.with_visible(false);

#[cfg(target_arch = "wasm32")]
let window_attributes = {
use winit::platform::web::WindowAttributesExtWebSys;
window_attributes
.with_canvas(self.canvas.take())
};

log::info!("Window attributes for id `{id:#?}`: {window_attributes:#?}");

let window = event_loop
.create_window(window_attributes)
.expect("Create window");

#[cfg(target_arch = "wasm32")]
Expand Down

0 comments on commit d46f6f9

Please sign in to comment.