Skip to main content

blitz_dom/
config.rs

1use crate::HtmlParserProvider;
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/// Blitz permits at most one parallel traversal at a time through a
14/// process-wide guard. Concurrent `Parallel` resolves of independent
15/// documents fall back to sequential traversal when the guard is unavailable.
16/// This avoids overlapping parallel traversals borrowing Stylo's worker-local
17/// caches: <https://github.com/DioxusLabs/blitz/issues/430>.
18///
19/// Styling must not reenter Stylo on a thread with an active style context.
20/// Other users of Stylo's global pool must coordinate separately.
21#[derive(Default, Clone, Copy, PartialEq, Eq, Debug)]
22pub enum StyleThreading {
23    /// Use Stylo's parallel traversal via its global rayon thread pool when
24    /// the traversal guard is available. Otherwise run sequentially on the
25    /// calling thread without waiting for the guard.
26    Parallel,
27    /// Run style traversal sequentially on the calling thread, bypassing
28    /// the global pool. This is the default.
29    #[default]
30    Sequential,
31}
32
33/// Options used when constructing a [`BaseDocument`](crate::BaseDocument)
34#[derive(Default)]
35pub struct DocumentConfig {
36    /// The initial `Viewport`
37    pub viewport: Option<Viewport>,
38    /// The base url which relative URLs are resolved against
39    pub base_url: Option<String>,
40    /// User Agent stylesheets
41    pub ua_stylesheets: Option<Vec<String>>,
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    /// Defer system-font discovery until this document first accesses its font context.
53    ///
54    /// The default is `false`, preserving eager system-font loading behavior.
55    /// When no custom font context is supplied, deferred documents share a
56    /// process-local system-font template while keeping document registrations isolated.
57    pub defer_system_fonts: bool,
58    /// The CSS media type used to evaluate `@media` rules.
59    /// Defaults to [`MediaType::screen`].
60    pub media_type: Option<MediaType>,
61    /// Strategy for Stylo's style traversal.
62    /// Defaults to [`StyleThreading::Sequential`].
63    pub style_threading: StyleThreading,
64    /// Whether incremental layout is enabled. Defaults to `true`.
65    pub incremental: Option<bool>,
66    /// If set, every sub-resource `Request` blitz-dom creates for this
67    /// document will carry this signal. Aborting it cancels every in-flight
68    /// fetch tied to this document.
69    pub abort_signal: Option<AbortSignal>,
70    /// Host receipt established before parsing or resource initiation.
71    pub teardown: Option<Arc<crate::release::Teardown>>,
72    /// How deeply this document is nested within other documents
73    /// (0 for a root document). Used to limit `<iframe>` nesting depth.
74    pub subdocument_depth: usize,
75    /// Maps a CSS family name to a family the embedder registered in `font_ctx`, tried
76    /// after the author name. Process-wide: the first document that sets it wins. See
77    /// [`font_fallback`](crate::font_fallback).
78    pub family_fallback: Option<crate::font_fallback::FamilyFallback>,
79}