diff --git a/Cargo.toml b/Cargo.toml index d990357d..9f708a42 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -55,6 +55,7 @@ incremental = false # mozbuild = "0.1.0" blitz = { path = "./packages/blitz" } blitz-dom = { path = "./packages/dom" } +blitz-net = { path = "./packages/net", features = ["blocking"]} comrak = { version = "0.21.0", default-features = false, features = ["syntect"] } png = { version = "0.17" } dioxus-blitz = { path = "./packages/dioxus-blitz" } diff --git a/examples/screenshot.rs b/examples/screenshot.rs index b0a62fcc..784125bb 100644 --- a/examples/screenshot.rs +++ b/examples/screenshot.rs @@ -2,6 +2,7 @@ use blitz::render_to_buffer; use blitz_dom::{HtmlDocument, Viewport}; +use blitz_net::SyncProvider; use reqwest::Url; use std::{ fs::File, @@ -46,8 +47,15 @@ async fn main() { .and_then(|arg| arg.parse().ok()) .unwrap_or(1200); + let net = SyncProvider::new(); + // Create HtmlDocument - let mut document = HtmlDocument::from_html(&html, Some(url.clone()), Vec::new()); + let mut document = HtmlDocument::from_html(&html, Some(url.clone()), Vec::new(), &net); + + for (node_id, resource) in net.0.into_inner().drain(..) { + document.as_mut().load_resource(node_id, resource) + } + document .as_mut() .set_viewport(Viewport::new(width * scale, height * scale, scale as f32)); diff --git a/packages/dioxus-blitz/src/window.rs b/packages/dioxus-blitz/src/window.rs index 3b7d66f5..76a08bd7 100644 --- a/packages/dioxus-blitz/src/window.rs +++ b/packages/dioxus-blitz/src/window.rs @@ -11,7 +11,6 @@ use wgpu::rwh::HasWindowHandle; #[cfg(all(feature = "menu", not(any(target_os = "android", target_os = "ios"))))] use crate::menu::init_menu; -use blitz_dom::node::{ImageData, NodeSpecificData}; use blitz_dom::util::Resource; use blitz_net::AsyncProvider; use std::sync::Arc; @@ -259,22 +258,7 @@ impl View { self.poll(); } BlitzWindowEvent::ResourceLoad { node_id, resource } => { - match resource { - Resource::Css(css) => { - let css = html_escape::decode_html_entities(&css); - self.dom.as_mut().add_stylesheet(&css); - } - Resource::Image(image) => { - let node = self.dom.as_mut().get_node_mut(node_id).unwrap(); - node.element_data_mut().unwrap().node_specific_data = - NodeSpecificData::Image(ImageData::new(Arc::new(image))) - } - Resource::Svg(tree) => { - let node = self.dom.as_mut().get_node_mut(node_id).unwrap(); - node.element_data_mut().unwrap().node_specific_data = - NodeSpecificData::Svg(tree) - } - } + self.dom.as_mut().load_resource(node_id, resource); self.request_redraw(); } #[cfg(feature = "accessibility")] diff --git a/packages/dom/src/document.rs b/packages/dom/src/document.rs index 8e2d3aaa..72abf767 100644 --- a/packages/dom/src/document.rs +++ b/packages/dom/src/document.rs @@ -1,14 +1,16 @@ use crate::events::{EventData, HitResult, RendererEvent}; -use crate::node::TextBrush; +use crate::node::{ImageData, NodeSpecificData, TextBrush}; use crate::{Node, NodeData, TextNodeData, Viewport}; use app_units::Au; use peniko::kurbo; // use quadtree_rs::Quadtree; +use crate::util::Resource; use parley::editor::{PointerButton, TextEvent}; use selectors::{matching::QuirksMode, Element}; use slab::Slab; use std::any::Any; use std::collections::{HashMap, HashSet, VecDeque}; +use std::sync::Arc; use style::selector_parser::ServoElementSnapshot; use style::servo::media_queries::FontMetricsProvider; use style::servo_arc::Arc as ServoArc; @@ -466,6 +468,24 @@ impl Document { .force_stylesheet_origins_dirty(Origin::Author.into()); } + pub fn load_resource(&mut self, node_id: usize, resource: Resource) { + match resource { + Resource::Css(css) => { + let css = html_escape::decode_html_entities(&css); + self.add_stylesheet(&css); + } + Resource::Image(image) => { + let node = self.get_node_mut(node_id).unwrap(); + node.element_data_mut().unwrap().node_specific_data = + NodeSpecificData::Image(ImageData::new(Arc::new(image))) + } + Resource::Svg(tree) => { + let node = self.get_node_mut(node_id).unwrap(); + node.element_data_mut().unwrap().node_specific_data = NodeSpecificData::Svg(tree) + } + } + } + pub fn snapshot_node(&mut self, node_id: usize) { let node = &mut self.nodes[node_id]; let opaque_node_id = TNode::opaque(&&*node); diff --git a/packages/net/src/lib.rs b/packages/net/src/lib.rs index e5c2b4cf..396ec969 100644 --- a/packages/net/src/lib.rs +++ b/packages/net/src/lib.rs @@ -1,3 +1,4 @@ +use std::rc::Rc; use std::sync::Arc; use url::Url; @@ -27,6 +28,22 @@ impl> NetProvider for Arc

{ self.as_ref().fetch(url, i, handler) } } +impl> NetProvider for Rc

{ + fn fetch(&self, url: Url, i: I, handler: F) + where + F: Fn(&[u8]) -> T + Send + Sync + 'static, + { + self.as_ref().fetch(url, i, handler) + } +} +impl> NetProvider for &P { + fn fetch(&self, url: Url, i: I, handler: F) + where + F: Fn(&[u8]) -> T + Send + Sync + 'static, + { + NetProvider::fetch(*self, url, i, handler); + } +} pub struct DummyProvider; impl NetProvider for DummyProvider {