Skip to main content

dioxus_native/
config.rs

1use anyrender::CompositeAlphaMode;
2use blitz_dom::FontContext;
3use dioxus_core::LaunchConfig;
4use peniko::Color;
5use winit::window::WindowAttributes;
6
7/// Launch-time configuration for a dioxus-native application.
8pub struct Config {
9    pub(crate) window_attributes: WindowAttributes,
10    pub(crate) font_ctx: Option<FontContext>,
11    pub(crate) alpha_mode: Option<CompositeAlphaMode>,
12    pub(crate) base_color: Option<Color>,
13}
14
15impl LaunchConfig for Config {}
16
17impl Default for Config {
18    fn default() -> Self {
19        let window_attributes = WindowAttributes::default()
20            .with_title(dioxus_cli_config::app_title().unwrap_or_else(|| "Dioxus App".to_string()));
21
22        // On WASM, append the canvas to the document body. Surface size is
23        // intentionally not seeded: winit-web's with_surface_size writes inline
24        // canvas.style.width/height that overrides host CSS. View::init reads
25        // the canvas's CSS layout box when winit reports a 0×0 initial size.
26        // To target an existing `<canvas>`, replace these attributes via
27        // [`Config::with_window_attributes`].
28        #[cfg(target_arch = "wasm32")]
29        let window_attributes = {
30            use winit::platform::web::WindowAttributesWeb;
31            window_attributes.with_platform_attributes(Box::new(
32                WindowAttributesWeb::default().with_append(true),
33            ))
34        };
35
36        Self {
37            window_attributes,
38            font_ctx: None,
39            alpha_mode: None,
40            base_color: None,
41        }
42    }
43}
44
45impl Config {
46    pub fn new() -> Self {
47        Self::default()
48    }
49
50    /// Set the configuration for the window.
51    pub fn with_window_attributes(mut self, attrs: WindowAttributes) -> Self {
52        self.window_attributes = attrs;
53        self
54    }
55
56    /// Set a custom [`FontContext`] for the document.
57    ///
58    /// On WASM, browsers don't expose system fonts, so a `FontContext` with
59    /// bundled fonts must be provided. Use [`crate::build_single_font_ctx`] for
60    /// the common one-font case.
61    pub fn with_font_ctx(mut self, font_ctx: FontContext) -> Self {
62        self.font_ctx = Some(font_ctx);
63        self
64    }
65
66    /// Set the alpha mode used when compositing the window surface.
67    ///
68    /// This controls how the rendered content is blended with the background,
69    /// e.g. to enable transparent windows. Not all renderers support every
70    /// mode; unsupported modes are ignored by the active renderer.
71    pub fn with_alpha_mode(mut self, alpha_mode: CompositeAlphaMode) -> Self {
72        self.alpha_mode = Some(alpha_mode);
73        self
74    }
75
76    /// Set the base (background) color used to clear each frame.
77    pub fn with_base_color(mut self, base_color: Color) -> Self {
78        self.base_color = Some(base_color);
79        self
80    }
81}