Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DPI scale factor to the render root state #872

Merged
merged 5 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions masonry/src/app/render_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,6 @@ pub struct RenderRoot {
/// Current size of the window.
pub(crate) size: PhysicalSize<u32>,

/// DPI scale factor.
///
/// Kurbo coordinates are assumed to be in logical pixels
pub(crate) scale_factor: f64,

/// Is `Some` if the most recently displayed frame was an animation frame.
pub(crate) last_anim: Option<Instant>,

Expand Down Expand Up @@ -147,6 +142,11 @@ pub(crate) struct RenderRootState {
/// Pass tracing configuration, used to skip tracing to limit overhead.
pub(crate) trace: PassTracing,
pub(crate) inspector_state: InspectorState,

/// DPI scale factor.
///
/// Kurbo coordinates are assumed to be in logical pixels
pub(crate) scale_factor: f64,
}

pub(crate) struct MutateCallback {
Expand Down Expand Up @@ -258,7 +258,6 @@ impl RenderRoot {
root: WidgetPod::new(root_widget).erased(),
size_policy,
size: PhysicalSize::new(0, 0),
scale_factor,
last_anim: None,
last_mouse_pos: None,
global_state: RenderRootState {
Expand Down Expand Up @@ -290,6 +289,7 @@ impl RenderRoot {
is_picking_widget: false,
hovered_widget: None,
},
scale_factor,
},
widget_arena: WidgetArena {
widgets: TreeArena::new(),
Expand Down Expand Up @@ -338,7 +338,7 @@ impl RenderRoot {
pub fn handle_window_event(&mut self, event: WindowEvent) -> Handled {
match event {
WindowEvent::Rescale(scale_factor) => {
self.scale_factor = scale_factor;
self.global_state.scale_factor = scale_factor;
self.request_render_all();
Handled::Yes
}
Expand Down Expand Up @@ -443,7 +443,7 @@ impl RenderRoot {

// TODO - Handle invalidation regions
let scene = run_paint_pass(self);
let tree_update = run_accessibility_pass(self, self.scale_factor);
let tree_update = run_accessibility_pass(self, self.global_state.scale_factor);
(scene, tree_update)
}

Expand Down Expand Up @@ -539,7 +539,7 @@ impl RenderRoot {
}

pub(crate) fn get_kurbo_size(&self) -> kurbo::Size {
let size = self.size.to_logical(self.scale_factor);
let size = self.size.to_logical(self.global_state.scale_factor);
kurbo::Size::new(size.width, size.height)
}

Expand Down
16 changes: 16 additions & 0 deletions masonry/src/core/contexts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,22 @@ impl_context_method!(
}
);

impl_context_method!(PaintCtx<'_>, AccessCtx<'_>, {
/// Get DPI scaling factor.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Get DPI scaling factor.
/// Get DPI scaling factor.
///
/// This is not required for most widgets, and should be used only for precise
/// rendering, such as rendering single pixel lines or selecting image variants.
/// This is currently only provided in the render stages, as these are the only passes which
/// are re-run when the scale factor changes.
///
/// Note that accessibility nodes and paint results will automatically be scaled by Masonry.
/// This also doesn't account for the widget's current transform, which cannot currently be
/// accessed by widgets directly.

///
/// This is not required for most widgets, and should be used only for precise
/// rendering, such as rendering single pixel lines or selecting image variants.
/// This is currently only provided in the render stages, as these are the only passes which
/// are re-run when the scale factor changes.
///
/// Note that accessibility nodes and paint results will automatically be scaled by Masonry.
/// This also doesn't account for the widget's current transform, which cannot currently be
/// accessed by widgets directly.
pub fn get_scale_factor(&self) -> f64 {
self.global_state.scale_factor
}
});

// Methods on all context types
// Access status information (hovered/pointer captured/disabled/etc).
impl_context_method!(
Expand Down
3 changes: 2 additions & 1 deletion masonry/src/passes/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ pub(crate) fn run_layout_pass(root: &mut RenderRoot) {
ctx.place_child(&mut root.root, Point::ORIGIN);

if let WindowSizePolicy::Content = root.size_policy {
let new_size = LogicalSize::new(size.width, size.height).to_physical(root.scale_factor);
let new_size =
LogicalSize::new(size.width, size.height).to_physical(root.global_state.scale_factor);
if root.size != new_size {
root.size = new_size;
root.global_state
Expand Down
Loading