Skip to content

Commit

Permalink
Make Viewport::zoom public to replace methods and add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Oct 26, 2024
1 parent ad9ae8d commit 32583b3
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 26 deletions.
7 changes: 6 additions & 1 deletion packages/blitz-dom/src/image.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
use taffy::{MaybeMath, MaybeResolve};

/// Layout context for an image.
#[derive(Debug, Clone, Copy)]
pub struct ImageContext {
/// Inherent size of the image.
pub inherent_size: taffy::Size<f32>,

/// Specified size of the image.
pub attr_size: taffy::Size<Option<f32>>,
}

pub fn image_measure_function(
/// Measure an image.
pub fn measure_image(
known_dimensions: taffy::Size<Option<f32>>,
parent_size: taffy::Size<Option<f32>>,
image_context: &ImageContext,
Expand Down
4 changes: 2 additions & 2 deletions packages/blitz-dom/src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use crate::node::{NodeData, NodeKind, NodeSpecificData};
use crate::{
document::Document,
image::{image_measure_function, ImageContext},
image::{measure_image, ImageContext},
node::Node,
};
use html5ever::local_name;
Expand Down Expand Up @@ -245,7 +245,7 @@ impl LayoutPartialTree for Document {
inputs,
&node.style,
|known_dimensions, _available_space| {
image_measure_function(
measure_image(
known_dimensions,
inputs.parent_size,
&image_context,
Expand Down
6 changes: 6 additions & 0 deletions packages/blitz-dom/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
/// This is the primary entry point for this crate.
pub mod document;

/// HTML document data structure.
pub mod html_document;
/// An implementation for Html5ever's sink trait, allowing us to parse HTML into a DOM.
pub mod htmlsink;
Expand All @@ -37,14 +38,19 @@ pub mod stylo_to_parley;
/// Conversions from Stylo types to Taffy and Parley types
pub mod stylo_to_taffy;

/// Image layout.
pub mod image;

/// Utility functions.
pub mod util;

/// Debugging.
pub mod debug;

/// Events.
pub mod events;

/// Window viewport.
pub mod viewport;

pub use document::{Document, DocumentLike};
Expand Down
3 changes: 2 additions & 1 deletion packages/blitz-dom/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ impl ElementNodeData {
}
}

pub fn flush_is_focussable(&mut self) {
pub(crate) fn flush_is_focussable(&mut self) {
let disabled: bool = self.attr_parsed(local_name!("disabled")).unwrap_or(false);
let tabindex: Option<i32> = self.attr_parsed(local_name!("tabindex"));

Expand Down Expand Up @@ -513,6 +513,7 @@ impl ElementNodeData {
});
}

/// Take the current cached inline text layout.
pub fn take_inline_layout(&mut self) -> Option<Box<TextLayout>> {
std::mem::take(&mut self.inline_layout_data)
}
Expand Down
29 changes: 12 additions & 17 deletions packages/blitz-dom/src/viewport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@ use style::{
properties::{style_structs::Font, ComputedValues},
};

/// Window viewport.
#[derive(Default, Debug, Clone)]
pub struct Viewport {
/// Size of the window.
pub window_size: (u32, u32),

hidpi_scale: f32,
/// Font size.
pub font_size: f32,

zoom: f32,
/// Zoom level.
pub zoom: f32,

pub font_size: f32,
hidpi_scale: f32,
}

impl Viewport {
/// Create a new viewport from a window's physical size and scale factor.
pub fn new(physical_width: u32, physical_height: u32, scale_factor: f32) -> Self {
Self {
window_size: (physical_width, physical_height),
Expand All @@ -25,31 +30,21 @@ impl Viewport {
}
}

// Total scaling, the product of the zoom and hdpi scale
/// Total scaling, the product of the zoom and hdpi scale.
pub fn scale(&self) -> f32 {
self.hidpi_scale * self.zoom
}
// Total scaling, the product of the zoom and hdpi scale

/// Total scaling, the product of the zoom and hdpi scale (as an `f64`).
pub fn scale_f64(&self) -> f64 {
self.scale() as f64
}

/// Set the hidi scale.
pub fn set_hidpi_scale(&mut self, scale: f32) {
self.hidpi_scale = scale;
}

pub fn zoom(&self) -> f32 {
self.zoom
}

pub fn set_zoom(&mut self, zoom: f32) {
self.zoom = zoom;
}

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();
Expand Down
10 changes: 5 additions & 5 deletions packages/dioxus-native/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ impl<Doc: DocumentLike> View<Doc> {

pub fn mouse_move(&mut self, x: f32, y: f32) -> bool {
let viewport_scroll = self.dom.as_ref().viewport_scroll();
let dom_x = x + viewport_scroll.x as f32 / self.viewport.zoom();
let dom_y = y + viewport_scroll.y as f32 / self.viewport.zoom();
let dom_x = x + viewport_scroll.x as f32 / self.viewport.zoom;
let dom_y = y + viewport_scroll.y as f32 / self.viewport.zoom;

// println!("Mouse move: ({}, {})", x, y);
// println!("Unscaled: ({}, {})",);
Expand Down Expand Up @@ -338,15 +338,15 @@ impl<Doc: DocumentLike> View<Doc> {
if ctrl | meta {
match key_code {
KeyCode::Equal => {
*self.viewport.zoom_mut() += 0.1;
self.viewport.zoom += 0.1;
self.kick_dom_viewport();
}
KeyCode::Minus => {
*self.viewport.zoom_mut() -= 0.1;
self.viewport.zoom -= 0.1;
self.kick_dom_viewport();
}
KeyCode::Digit0 => {
*self.viewport.zoom_mut() = 1.0;
self.viewport.zoom = 1.0;
self.kick_dom_viewport();
}
_ => {}
Expand Down

0 comments on commit 32583b3

Please sign in to comment.