Skip to content

Commit

Permalink
Move Viewport to blitz_traits
Browse files Browse the repository at this point in the history
Signed-off-by: Nico Burns <[email protected]>
  • Loading branch information
nicoburns committed Dec 17, 2024
1 parent e889e9b commit cb5dab8
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 52 deletions.
2 changes: 1 addition & 1 deletion apps/wpt/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use blitz_dom::net::Resource;
use blitz_dom::{ColorScheme, Viewport};
use blitz_renderer_vello::VelloImageRenderer;
use blitz_traits::{ColorScheme, Viewport};
use parley::FontContext;
use thread_local::ThreadLocal;
use url::Url;
Expand Down
2 changes: 1 addition & 1 deletion examples/screenshot.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! Load first CLI argument as a url. Fallback to google.com if no CLI argument is provided.
use blitz_dom::net::Resource;
use blitz_dom::{ColorScheme, Viewport};
use blitz_html::HtmlDocument;
use blitz_net::{MpscCallback, Provider};
use blitz_renderer_vello::render_to_buffer;
use blitz_traits::net::SharedProvider;
use blitz_traits::{ColorScheme, Viewport};
use reqwest::Url;
use std::sync::Arc;
use std::{
Expand Down
29 changes: 26 additions & 3 deletions packages/blitz-dom/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use crate::events::{apply_keypress_event, EventData, HitResult, RendererEvent};
use crate::layout::construct::collect_layout_children;
use crate::node::{ImageData, NodeSpecificData, Status, TextBrush};
use crate::util::ImageType;
use crate::{ElementNodeData, Node, NodeData, TextNodeData, Viewport};
use crate::{ElementNodeData, Node, NodeData, TextNodeData};
use app_units::Au;
use blitz_traits::net::{DummyNetProvider, SharedProvider};
use blitz_traits::{ColorScheme, Viewport};
use markup5ever::local_name;
use parley::FontContext;
use peniko::kurbo;
Expand All @@ -24,6 +25,8 @@ use std::any::Any;
use std::collections::{BTreeMap, Bound, HashMap, HashSet, VecDeque};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use style::media_queries::MediaType;
use style::queries::values::PrefersColorScheme;
use style::selector_parser::ServoElementSnapshot;
use style::servo::media_queries::FontMetricsProvider;
use style::servo_arc::Arc as ServoArc;
Expand Down Expand Up @@ -143,6 +146,26 @@ pub struct Document {
pub net_provider: SharedProvider<Resource>,
}

fn make_device(viewport: &Viewport) -> Device {
let width = viewport.window_size.0 as f32 / viewport.scale();
let height = viewport.window_size.1 as f32 / viewport.scale();
let viewport_size = euclid::Size2D::new(width, height);
let device_pixel_ratio = euclid::Scale::new(viewport.scale());

Device::new(
MediaType::screen(),
selectors::matching::QuirksMode::NoQuirks,
viewport_size,
device_pixel_ratio,
Box::new(DummyFontMetricsProvider),
ComputedValues::initial_values_with_font_override(Font::initial_values()),
match viewport.color_scheme {
ColorScheme::Light => PrefersColorScheme::Light,
ColorScheme::Dark => PrefersColorScheme::Dark,
},
)
}

impl DocumentLike for Document {
fn handle_event(&mut self, event: RendererEvent) {
let target_node_id = event.target;
Expand Down Expand Up @@ -270,7 +293,7 @@ impl Document {
static ID_GENERATOR: AtomicUsize = AtomicUsize::new(1);

let id = ID_GENERATOR.fetch_add(1, Ordering::SeqCst);
let device = viewport.make_device();
let device = make_device(&viewport);
let stylist = Stylist::new(device, QuirksMode::NoQuirks);
let snapshots = SnapshotMap::new();
let nodes = Box::new(Slab::new());
Expand Down Expand Up @@ -970,7 +993,7 @@ impl Document {

pub fn set_viewport(&mut self, viewport: Viewport) {
self.viewport = viewport;
self.set_stylist_device(self.viewport.make_device());
self.set_stylist_device(make_device(&self.viewport));
}

pub fn get_viewport(&self) -> Viewport {
Expand Down
3 changes: 0 additions & 3 deletions packages/blitz-dom/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ pub mod events;

pub mod net;

pub mod viewport;

pub use document::{Document, DocumentLike};
pub use markup5ever::{
local_name, namespace_prefix, namespace_url, ns, Namespace, NamespaceStaticSet, Prefix,
Expand All @@ -52,4 +50,3 @@ pub use node::{ElementNodeData, Node, NodeData, TextNodeData};
pub use parley::FontContext;
pub use string_cache::Atom;
pub use style::invalidation::element::restyle_hints::RestyleHint;
pub use viewport::{ColorScheme, Viewport};
5 changes: 2 additions & 3 deletions packages/blitz-html/src/html_document.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::DocumentHtmlParser;

use blitz_dom::{
events::RendererEvent, net::Resource, ColorScheme, Document, DocumentLike, FontContext,
Viewport, DEFAULT_CSS,
events::RendererEvent, net::Resource, Document, DocumentLike, FontContext, DEFAULT_CSS,
};
use blitz_traits::net::SharedProvider;
use blitz_traits::{net::SharedProvider, ColorScheme, Viewport};

pub struct HtmlDocument {
inner: Document,
Expand Down
4 changes: 2 additions & 2 deletions packages/blitz-renderer-vello/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ mod multicolor_rounded_rect;
mod render;

use crate::renderer::render::generate_vello_scene;
use blitz_dom::{Document, Viewport};
use blitz_traits::Devtools;
use blitz_dom::Document;
use blitz_traits::{Devtools, Viewport};
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
use std::num::NonZeroUsize;
use std::sync::Arc;
Expand Down
2 changes: 1 addition & 1 deletion packages/blitz-shell/src/stylo_to_winit.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use blitz_dom::ColorScheme;
use blitz_traits::ColorScheme;
use style::values::computed::ui::CursorKind as StyloCursorKind;
use winit::window::{CursorIcon as WinitCursor, Theme};

Expand Down
4 changes: 2 additions & 2 deletions packages/blitz-shell/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use crate::accessibility::AccessibilityState;
use crate::event::{create_waker, BlitzEvent};
use crate::stylo_to_winit::{self, color_scheme_to_theme, theme_to_color_scheme};
use blitz_dom::events::{EventData, RendererEvent};
use blitz_dom::{DocumentLike, Viewport};
use blitz_dom::DocumentLike;
use blitz_renderer_vello::Renderer;
use blitz_traits::Devtools;
use blitz_traits::{Devtools, Viewport};
use winit::keyboard::PhysicalKey;

use std::sync::Arc;
Expand Down
3 changes: 3 additions & 0 deletions packages/blitz-traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ pub mod net;

mod devtools;
pub use devtools::Devtools;

mod viewport;
pub use viewport::{ColorScheme, Viewport};
Original file line number Diff line number Diff line change
@@ -1,26 +1,10 @@
use crate::document::DummyFontMetricsProvider;
use style::{
media_queries::{Device, MediaType},
properties::{style_structs::Font, ComputedValues},
queries::values::PrefersColorScheme,
};

#[derive(Default, Debug, Clone, Copy)]
pub enum ColorScheme {
#[default]
Light,
Dark,
}

impl From<ColorScheme> for PrefersColorScheme {
fn from(value: ColorScheme) -> Self {
match value {
ColorScheme::Light => PrefersColorScheme::Light,
ColorScheme::Dark => PrefersColorScheme::Dark,
}
}
}

#[derive(Default, Debug, Clone)]
pub struct Viewport {
pub window_size: (u32, u32),
Expand Down Expand Up @@ -74,21 +58,4 @@ impl Viewport {
pub fn zoom_mut(&mut self) -> &mut f32 {
&mut self.zoom
}

pub(crate) fn make_device(&self) -> Device {
let width = self.window_size.0 as f32 / self.scale();
let height = self.window_size.1 as f32 / self.scale();
let viewport_size = euclid::Size2D::new(width, height);
let device_pixel_ratio = euclid::Scale::new(self.scale());

Device::new(
MediaType::screen(),
selectors::matching::QuirksMode::NoQuirks,
viewport_size,
device_pixel_ratio,
Box::new(DummyFontMetricsProvider),
ComputedValues::initial_values_with_font_override(Font::initial_values()),
self.color_scheme.into(),
)
}
}
5 changes: 2 additions & 3 deletions packages/dioxus-native/src/dioxus_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ use blitz_dom::{
local_name, namespace_url,
net::Resource,
node::NodeSpecificData,
ns, Atom, ColorScheme, Document, DocumentLike, ElementNodeData, Node, NodeData, QualName,
Viewport, DEFAULT_CSS,
ns, Atom, Document, DocumentLike, ElementNodeData, Node, NodeData, QualName, DEFAULT_CSS,
};

use blitz_traits::net::NetProvider;
use blitz_traits::{net::NetProvider, ColorScheme, Viewport};
use dioxus_core::{ElementId, Event, VirtualDom};
use dioxus_html::{set_event_converter, FormValue, PlatformEventData};
use futures_util::{pin_mut, FutureExt};
Expand Down

0 comments on commit cb5dab8

Please sign in to comment.