Skip to content

Commit 5fb1e45

Browse files
committed
Introduce DocumentConfig struct
Signed-off-by: Nico Burns <[email protected]>
1 parent 251e62e commit 5fb1e45

File tree

12 files changed

+126
-112
lines changed

12 files changed

+126
-112
lines changed

apps/readme/src/main.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use anyrender_vello::VelloWindowRenderer as WindowRenderer;
2020
#[cfg(feature = "cpu")]
2121
use anyrender_vello_cpu::VelloCpuWindowRenderer as WindowRenderer;
2222

23+
use blitz_dom::DocumentConfig;
2324
use blitz_dom::net::Resource;
2425
use blitz_html::HtmlDocument;
2526
use blitz_net::Provider;
@@ -100,11 +101,13 @@ fn main() {
100101

101102
let doc = HtmlDocument::from_html(
102103
&html,
103-
Some(base_url),
104-
stylesheets,
105-
net_provider.clone(),
106-
None,
107-
navigation_provider.clone(),
104+
DocumentConfig {
105+
base_url: Some(base_url),
106+
ua_stylesheets: Some(stylesheets),
107+
net_provider: Some(net_provider.clone()),
108+
navigation_provider: Some(navigation_provider.clone()),
109+
..Default::default()
110+
},
108111
);
109112
let renderer = WindowRenderer::new();
110113
let attrs = WindowAttributes::default().with_title(title);

apps/readme/src/readme_application.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::sync::Arc;
22

33
use crate::WindowRenderer;
4+
use blitz_dom::DocumentConfig;
45
use blitz_dom::net::Resource;
56
use blitz_html::HtmlDocument;
67
use blitz_net::Provider;
@@ -110,11 +111,13 @@ impl ReadmeApplication {
110111

111112
let doc = HtmlDocument::from_html(
112113
&html,
113-
Some(url),
114-
stylesheets,
115-
self.net_provider.clone(),
116-
None,
117-
self.navigation_provider.clone(),
114+
DocumentConfig {
115+
base_url: Some(url),
116+
ua_stylesheets: Some(stylesheets),
117+
net_provider: Some(self.net_provider.clone()),
118+
navigation_provider: Some(self.navigation_provider.clone()),
119+
..Default::default()
120+
},
118121
);
119122
self.window_mut()
120123
.replace_document(Box::new(doc) as _, retain_scroll_position);

examples/wgpu_texture/src/html.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
use std::sync::Arc;
2-
31
use anyrender_vello::VelloWindowRenderer;
4-
use blitz_dom::{local_name, ns, QualName};
2+
use blitz_dom::{local_name, ns, DocumentConfig, QualName};
53
use blitz_html::HtmlDocument;
64
use blitz_shell::{create_default_event_loop, BlitzApplication, BlitzShellEvent, WindowConfig};
7-
use blitz_traits::{navigation::DummyNavigationProvider, net::DummyNetProvider};
85

96
use crate::{limits, DemoPaintSource, FEATURES, STYLES};
107

@@ -19,14 +16,7 @@ pub fn launch_html() {
1916

2017
// Parse the HTML into a Blitz document
2118
let html = HTML.replace("{{STYLES_PLACEHOLDER}}", STYLES);
22-
let mut doc = HtmlDocument::from_html(
23-
&html,
24-
None,
25-
Vec::new(),
26-
Arc::new(DummyNetProvider),
27-
None,
28-
Arc::new(DummyNavigationProvider),
29-
);
19+
let mut doc = HtmlDocument::from_html(&html, DocumentConfig::default());
3020

3121
// Set the "src" attribute on the `<canvas>` element to the paint source's id
3222
// (`<canvas src=".." />` is proprietary blitz extension to HTML)

packages/blitz-dom/src/config.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use crate::net::Resource;
2+
use blitz_traits::{
3+
navigation::NavigationProvider,
4+
net::NetProvider,
5+
shell::{ShellProvider, Viewport},
6+
};
7+
use parley::FontContext;
8+
use std::sync::Arc;
9+
10+
/// Options used when constructing a [`BaseDocument`](crate::BaseDocument)
11+
#[derive(Default)]
12+
pub struct DocumentConfig {
13+
/// The initial `Viewport`
14+
pub viewport: Option<Viewport>,
15+
/// The base url which relative URLs are resolved against
16+
pub base_url: Option<String>,
17+
/// User Agent stylesheets
18+
pub ua_stylesheets: Option<Vec<String>>,
19+
/// Net provider to handle network requests for resources
20+
pub net_provider: Option<Arc<dyn NetProvider<Resource>>>,
21+
/// Navigation provider to handle link clicks and form submissions
22+
pub navigation_provider: Option<Arc<dyn NavigationProvider>>,
23+
/// Shell provider to redraw requests, clipboard, etc
24+
pub shell_provider: Option<Arc<dyn ShellProvider>>,
25+
/// Parley `FontContext`
26+
pub font_ctx: Option<FontContext>,
27+
}

packages/blitz-dom/src/document.rs

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ use crate::node::{ImageData, NodeFlags, RasterImageData, SpecialElementData, Sta
66
use crate::stylo_to_cursor_icon::stylo_to_cursor_icon;
77
use crate::traversal::TreeTraverser;
88
use crate::util::{ImageType, resolve_url};
9-
use crate::{DocumentMutator, ElementData, Node, NodeData, TextNodeData};
9+
use crate::{
10+
DEFAULT_CSS, DocumentConfig, DocumentMutator, ElementData, Node, NodeData, TextNodeData,
11+
};
1012
use app_units::Au;
1113
use blitz_traits::devtools::DevtoolSettings;
1214
use blitz_traits::events::{DomEvent, HitResult, UiEvent};
@@ -187,14 +189,11 @@ impl BaseDocument {
187189
}
188190

189191
impl BaseDocument {
190-
pub fn new(viewport: Viewport) -> Self {
191-
Self::with_font_ctx(viewport, parley::FontContext::default())
192-
}
193-
194-
pub fn with_font_ctx(viewport: Viewport, mut font_ctx: FontContext) -> Self {
192+
pub fn new(config: DocumentConfig) -> Self {
195193
static ID_GENERATOR: AtomicUsize = AtomicUsize::new(1);
196194

197195
let id = ID_GENERATOR.fetch_add(1, Ordering::SeqCst);
196+
let viewport = config.viewport.unwrap_or_default();
198197
let device = make_device(&viewport);
199198
let stylist = Stylist::new(device, QuirksMode::NoQuirks);
200199
let snapshots = SnapshotMap::new();
@@ -209,9 +208,23 @@ impl BaseDocument {
209208
style_config::set_bool("layout.unimplemented", true);
210209
style_config::set_bool("layout.columns.enabled", true);
211210

212-
font_ctx
213-
.collection
214-
.register_fonts(Blob::new(Arc::new(crate::BULLET_FONT) as _), None);
211+
let font_ctx = config.font_ctx.unwrap_or_else(|| {
212+
let mut font_ctx = FontContext::default();
213+
font_ctx
214+
.collection
215+
.register_fonts(Blob::new(Arc::new(crate::BULLET_FONT) as _), None);
216+
font_ctx
217+
});
218+
219+
let net_provider = config
220+
.net_provider
221+
.unwrap_or_else(|| Arc::new(DummyNetProvider));
222+
let navigation_provider = config
223+
.navigation_provider
224+
.unwrap_or_else(|| Arc::new(DummyNavigationProvider));
225+
let shell_provider = config
226+
.shell_provider
227+
.unwrap_or_else(|| Arc::new(DummyShellProvider));
215228

216229
let mut doc = Self {
217230
id,
@@ -236,15 +249,24 @@ impl BaseDocument {
236249
is_animating: false,
237250
changed_nodes: HashSet::new(),
238251
controls_to_form: HashMap::new(),
239-
net_provider: Arc::new(DummyNetProvider),
240-
navigation_provider: Arc::new(DummyNavigationProvider),
241-
shell_provider: Arc::new(DummyShellProvider),
252+
net_provider,
253+
navigation_provider,
254+
shell_provider,
242255
};
243256

244257
// Initialise document with root Document node
245258
doc.create_node(NodeData::Document);
246259
doc.root_node_mut().flags.insert(NodeFlags::IS_IN_DOCUMENT);
247260

261+
match config.ua_stylesheets {
262+
Some(stylesheets) => {
263+
for ss in &stylesheets {
264+
doc.add_user_agent_stylesheet(ss);
265+
}
266+
}
267+
None => doc.add_user_agent_stylesheet(DEFAULT_CSS),
268+
}
269+
248270
// Stylo data on the root node container is needed to render the node
249271
let stylo_element_data = StyloElementData {
250272
styles: ElementStyles {

packages/blitz-dom/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ mod document;
3434
/// The nodes themsleves, and their data.
3535
pub mod node;
3636

37+
mod config;
3738
mod debug;
3839
mod events;
3940
mod form;
@@ -53,6 +54,7 @@ pub mod util;
5354
#[cfg(feature = "accessibility")]
5455
mod accessibility;
5556

57+
pub use config::DocumentConfig;
5658
pub use document::{BaseDocument, Document};
5759
pub use markup5ever::{
5860
LocalName, Namespace, NamespaceStaticSet, Prefix, PrefixStaticSet, QualName, local_name,
Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
1-
use std::{
2-
ops::{Deref, DerefMut},
3-
sync::Arc,
4-
};
1+
use std::ops::{Deref, DerefMut};
52

63
use crate::DocumentHtmlParser;
74

85
use blitz_dom::{
9-
BaseDocument, DEFAULT_CSS, Document, EventDriver, FontContext, NoopEventHandler, net::Resource,
10-
};
11-
use blitz_traits::{
12-
events::UiEvent,
13-
navigation::NavigationProvider,
14-
net::SharedProvider,
15-
shell::{ColorScheme, Viewport},
6+
BaseDocument, DEFAULT_CSS, Document, DocumentConfig, EventDriver, NoopEventHandler,
167
};
8+
use blitz_traits::events::UiEvent;
179

1810
pub struct HtmlDocument {
1911
inner: BaseDocument,
@@ -52,41 +44,14 @@ impl Document for HtmlDocument {
5244
}
5345

5446
impl HtmlDocument {
55-
pub fn from_html(
56-
html: &str,
57-
base_url: Option<String>,
58-
stylesheets: Vec<String>,
59-
net_provider: SharedProvider<Resource>,
60-
font_ctx: Option<FontContext>,
61-
navigation_provider: Arc<dyn NavigationProvider>,
62-
) -> Self {
63-
// Spin up the virtualdom and include the default stylesheet
64-
let viewport = Viewport::new(0, 0, 1.0, ColorScheme::Light);
65-
let mut doc = match font_ctx {
66-
Some(font_ctx) => BaseDocument::with_font_ctx(viewport, font_ctx),
67-
None => BaseDocument::new(viewport),
68-
};
69-
70-
// Set base url if configured
71-
if let Some(url) = &base_url {
72-
doc.set_base_url(url);
47+
pub fn from_html(html: &str, mut config: DocumentConfig) -> Self {
48+
if let Some(ss) = &mut config.ua_stylesheets {
49+
if !ss.iter().any(|s| s == DEFAULT_CSS) {
50+
ss.push(String::from(DEFAULT_CSS));
51+
}
7352
}
74-
75-
// Set the net provider
76-
doc.set_net_provider(net_provider.clone());
77-
78-
// Set the navigation provider
79-
doc.set_navigation_provider(navigation_provider.clone());
80-
81-
// Include default and user-specified stylesheets
82-
doc.add_user_agent_stylesheet(DEFAULT_CSS);
83-
for ss in &stylesheets {
84-
doc.add_user_agent_stylesheet(ss);
85-
}
86-
87-
// Parse HTML string into document
53+
let mut doc = BaseDocument::new(config);
8854
DocumentHtmlParser::parse_into_doc(&mut doc, html);
89-
9055
HtmlDocument { inner: doc }
9156
}
9257
}

packages/blitz-html/src/html_sink.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,10 @@ impl<'b> TreeSink for DocumentHtmlParser<'b> {
237237

238238
#[test]
239239
fn parses_some_html() {
240-
use blitz_traits::shell::{ColorScheme, Viewport};
240+
use blitz_dom::DocumentConfig;
241241

242242
let html = "<!DOCTYPE html><html><body><h1>hello world</h1></body></html>";
243-
let viewport = Viewport::new(800, 600, 1.0, ColorScheme::Light);
244-
let mut doc = BaseDocument::new(viewport);
243+
let mut doc = BaseDocument::new(DocumentConfig::default());
245244
let sink = DocumentHtmlParser::new(&mut doc);
246245

247246
html5ever::parse_document(sink, Default::default())

packages/blitz-traits/src/shell.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,23 @@ pub enum ColorScheme {
3636
Dark,
3737
}
3838

39-
#[derive(Default, Debug, Clone)]
39+
#[derive(Debug, Clone)]
4040
pub struct Viewport {
41+
pub color_scheme: ColorScheme,
4142
pub window_size: (u32, u32),
43+
pub hidpi_scale: f32,
44+
pub zoom: f32,
45+
}
4246

43-
hidpi_scale: f32,
44-
45-
zoom: f32,
46-
47-
pub font_size: f32,
48-
49-
pub color_scheme: ColorScheme,
47+
impl Default for Viewport {
48+
fn default() -> Self {
49+
Self {
50+
window_size: (0, 0),
51+
hidpi_scale: 1.0,
52+
zoom: 1.0,
53+
color_scheme: ColorScheme::Light,
54+
}
55+
}
5056
}
5157

5258
impl Viewport {
@@ -60,7 +66,6 @@ impl Viewport {
6066
window_size: (physical_width, physical_height),
6167
hidpi_scale: scale_factor,
6268
zoom: 1.0,
63-
font_size: 16.0,
6469
color_scheme,
6570
}
6671
}

packages/blitz/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
use std::sync::Arc;
1515

1616
use anyrender_vello::VelloWindowRenderer as WindowRenderer;
17+
use blitz_dom::DocumentConfig;
1718
use blitz_dom::net::Resource;
1819
use blitz_html::HtmlDocument;
1920
use blitz_shell::{
2021
BlitzApplication, BlitzShellEvent, BlitzShellNetCallback, Config, EventLoop, WindowConfig,
2122
create_default_event_loop,
2223
};
23-
use blitz_traits::navigation::DummyNavigationProvider;
2424
use blitz_traits::net::{NetProvider, Request};
2525

2626
#[doc(inline)]
@@ -101,14 +101,14 @@ fn launch_internal(
101101
event_loop: EventLoop<BlitzShellEvent>,
102102
net_provider: Arc<dyn NetProvider<Resource>>,
103103
) {
104-
let navigation_provider = Arc::new(DummyNavigationProvider);
105104
let doc = HtmlDocument::from_html(
106105
html,
107-
cfg.base_url,
108-
cfg.stylesheets,
109-
net_provider,
110-
None,
111-
navigation_provider,
106+
DocumentConfig {
107+
base_url: cfg.base_url,
108+
ua_stylesheets: Some(cfg.stylesheets),
109+
net_provider: Some(net_provider),
110+
..Default::default()
111+
},
112112
);
113113
let renderer = WindowRenderer::new();
114114
let window = WindowConfig::new(Box::new(doc) as _, renderer);

0 commit comments

Comments
 (0)