Skip to content

Commit 1632826

Browse files
authored
Merge pull request #1443 from traxys/div_canvas
Allow to replace an element instead of append to body for web rendering
2 parents d222b5c + d8d57a8 commit 1632826

5 files changed

Lines changed: 38 additions & 7 deletions

File tree

winit/src/application.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ where
137137
runtime.enter(|| A::new(flags))
138138
};
139139

140+
#[cfg(target_arch = "wasm32")]
141+
let target = settings.window.platform_specific.target.clone();
142+
140143
let builder = settings.window.into_builder(
141144
&application.title(),
142145
event_loop.primary_monitor(),
@@ -159,9 +162,20 @@ where
159162
let document = window.document().unwrap();
160163
let body = document.body().unwrap();
161164

162-
let _ = body
163-
.append_child(&canvas)
164-
.expect("Append canvas to HTML body");
165+
let target = target.and_then(|target| {
166+
body.query_selector(&format!("#{}", target))
167+
.ok()
168+
.unwrap_or(None)
169+
});
170+
171+
let _ = match target {
172+
Some(node) => node
173+
.replace_child(&canvas, &node)
174+
.expect(&format!("Could not replace #{}", node.id())),
175+
None => body
176+
.append_child(&canvas)
177+
.expect("Append canvas to HTML body"),
178+
};
165179
}
166180

167181
let (compositor, renderer) = C::new(compositor_settings, Some(&window))?;

winit/src/settings.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@ mod platform;
77
#[path = "settings/macos.rs"]
88
mod platform;
99

10-
#[cfg(not(any(target_os = "windows", target_os = "macos")))]
10+
#[cfg(target_arch = "wasm32")]
11+
#[path = "settings/wasm.rs"]
12+
mod platform;
13+
14+
#[cfg(not(any(
15+
target_os = "windows",
16+
target_os = "macos",
17+
target_arch = "wasm32"
18+
)))]
1119
#[path = "settings/other.rs"]
1220
mod platform;
1321

@@ -27,7 +35,7 @@ pub struct Settings<Flags> {
2735
/// communicate with it through the windowing system.
2836
pub id: Option<String>,
2937

30-
/// The [`Window`] settings
38+
/// The [`Window`] settings.
3139
pub window: Window,
3240

3341
/// The data needed to initialize an [`Application`].

winit/src/settings/macos.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![cfg(target_os = "macos")]
21
//! Platform specific settings for macOS.
32
43
/// The platform specific window settings of an application.

winit/src/settings/wasm.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//! Platform specific settings for WebAssembly.
2+
3+
/// The platform specific window settings of an application.
4+
#[derive(Debug, Clone, PartialEq, Eq, Default)]
5+
pub struct PlatformSpecific {
6+
/// The identifier of a DOM element that will be replaced with the
7+
/// application.
8+
///
9+
/// If set to `None`, the application will be appended to the HTML body.
10+
pub target: Option<String>,
11+
}

winit/src/settings/windows.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![cfg(target_os = "windows")]
21
//! Platform specific settings for Windows.
32
43
/// The platform specific window settings of an application.

0 commit comments

Comments
 (0)