Skip to content

Commit

Permalink
Fixing screenshot example.
Browse files Browse the repository at this point in the history
  • Loading branch information
kokoISnoTarget committed Aug 20, 2024
1 parent d8106dd commit 6799c3b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 19 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
10 changes: 9 additions & 1 deletion examples/screenshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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));
Expand Down
18 changes: 1 addition & 17 deletions packages/dioxus-blitz/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -259,22 +258,7 @@ impl<Doc: DocumentLike> View<Doc> {
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")]
Expand Down
23 changes: 22 additions & 1 deletion packages/dom/src/document.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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;
Expand All @@ -9,6 +9,7 @@ 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;
Expand All @@ -23,6 +24,7 @@ use style::{
};
use taffy::AvailableSpace;
use url::Url;
use crate::util::Resource;

// TODO: implement a proper font metrics provider
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -466,6 +468,25 @@ 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);
Expand Down
17 changes: 17 additions & 0 deletions packages/net/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::rc::Rc;
use std::sync::Arc;
use url::Url;

Expand Down Expand Up @@ -27,6 +28,22 @@ impl<I, T, P: NetProvider<I, T>> NetProvider<I, T> for Arc<P> {
self.as_ref().fetch(url, i, handler)
}
}
impl<I, T, P: NetProvider<I, T>> NetProvider<I, T> for Rc<P> {
fn fetch<F>(&self, url: Url, i: I, handler: F)
where
F: Fn(&[u8]) -> T + Send + Sync + 'static,
{
self.as_ref().fetch(url, i, handler)
}
}
impl<I, T, P: NetProvider<I, T>> NetProvider<I, T> for &P {
fn fetch<F>(&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<I, T> NetProvider<I, T> for DummyProvider {
Expand Down

0 comments on commit 6799c3b

Please sign in to comment.