Skip to main content

blitz_dom/
config.rs

1use crate::{HtmlParserProvider, UserAgentStylesheet};
2use blitz_traits::{
3    navigation::NavigationProvider,
4    net::{AbortSignal, NetProvider},
5    shell::{ShellProvider, Viewport},
6};
7use parley::FontContext;
8use std::sync::Arc;
9use style::media_queries::MediaType;
10
11/// Strategy for Stylo's style traversal during `resolve`.
12///
13/// Two `Document`s resolving on [`StyleThreading::Parallel`] concurrently
14/// share Stylo's global thread pool and can panic with
15/// `already mutably borrowed` — see
16/// <https://github.com/DioxusLabs/blitz/issues/430>. Set
17/// [`StyleThreading::Sequential`] on documents that may resolve from a
18/// user thread while another `Parallel` resolve is in flight.
19#[derive(Default, Clone, Copy, PartialEq, Eq, Debug)]
20pub enum StyleThreading {
21    /// Use Stylo's parallel traversal via its global rayon thread pool.
22    /// Fastest for a single document; panics if another `Parallel` resolve
23    /// is in flight on a different thread.
24    Parallel,
25    /// Run style traversal sequentially on the calling thread, bypassing
26    /// the global pool. Safe to use from many user threads concurrently.
27    #[default]
28    Sequential,
29}
30
31/// Options used when constructing a [`BaseDocument`](crate::BaseDocument)
32pub struct DocumentConfig {
33    /// The initial `Viewport`
34    pub viewport: Option<Viewport>,
35    /// The base url which relative URLs are resolved against
36    pub base_url: Option<String>,
37    /// User Agent stylesheets, in cascade order. Defaults to `vec![UserAgentStylesheet::default_sheet()]`.
38    ///
39    /// The list is used as given, so replacing it removes the default sheet unless it is
40    /// included, e.g. `vec![UserAgentStylesheet::default_sheet(), custom]`.
41    pub ua_stylesheets: Vec<UserAgentStylesheet>,
42    /// Net provider to handle network requests for resources
43    pub net_provider: Option<Arc<dyn NetProvider>>,
44    /// Navigation provider to handle link clicks and form submissions
45    pub navigation_provider: Option<Arc<dyn NavigationProvider>>,
46    /// Shell provider to redraw requests, clipboard, etc
47    pub shell_provider: Option<Arc<dyn ShellProvider>>,
48    /// HTML parser provider. Used to parse HTML for setInnerHTML
49    pub html_parser_provider: Option<Arc<dyn HtmlParserProvider>>,
50    /// Parley `FontContext`
51    pub font_ctx: Option<FontContext>,
52    /// The CSS media type used to evaluate `@media` rules.
53    /// Defaults to [`MediaType::screen`].
54    pub media_type: Option<MediaType>,
55    /// Strategy for Stylo's style traversal.
56    /// Defaults to [`StyleThreading::Parallel`].
57    pub style_threading: StyleThreading,
58    /// Whether incremental layout is enabled. Defaults to `true`.
59    ///
60    /// When disabled, every node is treated as damaged on each `resolve` and the
61    /// whole tree is reconstructed and laid out from scratch through the same
62    /// damage-driven pipeline used by incremental mode.
63    pub incremental: Option<bool>,
64    /// If set, every sub-resource `Request` blitz-dom creates for this
65    /// document will carry this signal. Aborting it cancels every in-flight
66    /// fetch tied to this document.
67    pub abort_signal: Option<AbortSignal>,
68    /// How deeply this document is nested within other documents
69    /// (0 for a root document). Used to limit `<iframe>` nesting depth.
70    pub subdocument_depth: usize,
71}
72
73impl Default for DocumentConfig {
74    fn default() -> Self {
75        Self {
76            viewport: None,
77            base_url: None,
78            ua_stylesheets: vec![UserAgentStylesheet::default_sheet()],
79            net_provider: None,
80            navigation_provider: None,
81            shell_provider: None,
82            html_parser_provider: None,
83            font_ctx: None,
84            media_type: None,
85            style_threading: StyleThreading::default(),
86            incremental: None,
87            abort_signal: None,
88            subdocument_depth: 0,
89        }
90    }
91}