Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wasm32 deployments not displaying anything #2574

Merged
merged 4 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading