Skip to main content

dioxus_native/
config.rs

1use blitz_dom::FontContext;
2use dioxus_core::LaunchConfig;
3use winit::window::WindowAttributes;
4
5/// Launch-time configuration for a dioxus-native application.
6pub struct Config {
7    pub(crate) window_attributes: WindowAttributes,
8    pub(crate) font_ctx: Option<FontContext>,
9}
10
11impl LaunchConfig for Config {}
12
13impl Default for Config {
14    fn default() -> Self {
15        let window_attributes = WindowAttributes::default()
16            .with_title(dioxus_cli_config::app_title().unwrap_or_else(|| "Dioxus App".to_string()));
17
18        // On WASM, append the canvas to the document body. Surface size is
19        // intentionally not seeded: winit-web's with_surface_size writes inline
20        // canvas.style.width/height that overrides host CSS. View::init reads
21        // the canvas's CSS layout box when winit reports a 0×0 initial size.
22        // To target an existing `<canvas>`, replace these attributes via
23        // [`Config::with_window_attributes`].
24        #[cfg(target_arch = "wasm32")]
25        let window_attributes = {
26            use winit::platform::web::WindowAttributesWeb;
27            window_attributes.with_platform_attributes(Box::new(
28                WindowAttributesWeb::default().with_append(true),
29            ))
30        };
31
32        Self {
33            window_attributes,
34            font_ctx: None,
35        }
36    }
37}
38
39impl Config {
40    pub fn new() -> Self {
41        Self::default()
42    }
43
44    /// Set the configuration for the window.
45    pub fn with_window_attributes(mut self, attrs: WindowAttributes) -> Self {
46        self.window_attributes = attrs;
47        self
48    }
49
50    /// Set a custom [`FontContext`] for the document.
51    ///
52    /// On WASM, browsers don't expose system fonts, so a `FontContext` with
53    /// bundled fonts must be provided. Use [`crate::build_single_font_ctx`] for
54    /// the common one-font case.
55    pub fn with_font_ctx(mut self, font_ctx: FontContext) -> Self {
56        self.font_ctx = Some(font_ctx);
57        self
58    }
59}