Skip to main content

inset_embedder_default/
lib.rs

1//! The stock host: winit on native, the canvas WebGPU host on wasm.
2//!
3//! An application depends on this crate instead of picking an embedder and
4//! repeating target-cfg dependencies. `DefaultEmbedder::run` is the same
5//! closure on both.
6
7use inset_embedder::EmbedderClient;
8
9#[cfg(not(target_arch = "wasm32"))]
10pub use inset_embedder_winit::ImplicitViewConfig;
11
12/// Runs the application on this target's host.
13#[derive(Default)]
14pub struct DefaultEmbedder {
15    #[cfg(not(target_arch = "wasm32"))]
16    inner: inset_embedder_winit::WinitEmbedder,
17    #[cfg(target_arch = "wasm32")]
18    inner: inset_embedder_web::WebEmbedder,
19}
20
21impl DefaultEmbedder {
22    /// Native implicit window. Ignored on wasm, where the page supplies the canvas.
23    #[cfg(not(target_arch = "wasm32"))]
24    pub fn implicit_view(mut self, config: Option<ImplicitViewConfig>) -> DefaultEmbedder {
25        self.inner.implicit_view = config;
26        self
27    }
28
29    /// Canvas element id. Ignored on native.
30    #[cfg(target_arch = "wasm32")]
31    pub fn canvas_id(mut self, id: impl Into<String>) -> DefaultEmbedder {
32        self.inner = self.inner.canvas_id(id);
33        self
34    }
35
36    /// Starts the host loop. `start` runs once the implicit view exists (native)
37    /// or the canvas GPU is ready (wasm).
38    pub fn run<C: EmbedderClient + 'static>(
39        self,
40        start: impl FnOnce(inset_embedder::PlatformRef) -> C + 'static,
41    ) {
42        self.inner.run(start);
43    }
44}