From 30c79485191f48d0c82ddf7971f1e300a784c8a3 Mon Sep 17 00:00:00 2001 From: Nico Burns Date: Sun, 23 Jul 2023 12:10:29 +0100 Subject: [PATCH] Rename abs_x/abs_y to cumulative_x/cumulative_y --- src/compute/taffy_tree.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/compute/taffy_tree.rs b/src/compute/taffy_tree.rs index a76e82ee9..3d2efae5e 100644 --- a/src/compute/taffy_tree.rs +++ b/src/compute/taffy_tree.rs @@ -278,28 +278,32 @@ fn perform_taffy_tree_hidden_layout(tree: &mut Taffy, node: NodeId) { } /// Rounds the calculated [`Layout`] to exact pixel values +/// /// In order to ensure that no gaps in the layout are introduced we: -/// - Always round based on the absolute coordinates rather than parent-relative coordinates +/// - Always round based on the cumulative x/y coordinates (relative to the viewport) rather than +/// parent-relative coordinates /// - Compute width/height by first rounding the top/bottom/left/right and then computing the difference /// rather than rounding the width/height directly -/// /// See for more context -fn round_layout(tree: &mut Taffy, node_id: NodeId, abs_x: f32, abs_y: f32) { +/// +/// In order to prevent innacuracies caused by rounding already-rounded values, we read from `unrounded_layout` +/// and write to `final_layout`. +fn round_layout(tree: &mut Taffy, node_id: NodeId, cumulative_x: f32, cumulative_y: f32) { let node = &mut tree.nodes[node_id.into()]; let unrounded_layout = node.unrounded_layout; let layout = &mut node.final_layout; - let abs_x = abs_x + unrounded_layout.location.x; - let abs_y = abs_y + unrounded_layout.location.y; + let cumulative_x = cumulative_x + unrounded_layout.location.x; + let cumulative_y = cumulative_y + unrounded_layout.location.y; layout.location.x = round(unrounded_layout.location.x); layout.location.y = round(unrounded_layout.location.y); - layout.size.width = round(abs_x + unrounded_layout.size.width) - round(abs_x); - layout.size.height = round(abs_y + unrounded_layout.size.height) - round(abs_y); + layout.size.width = round(cumulative_x + unrounded_layout.size.width) - round(cumulative_x); + layout.size.height = round(cumulative_y + unrounded_layout.size.height) - round(cumulative_y); let child_count = tree.child_count(node_id).unwrap(); for index in 0..child_count { let child = tree.child(node_id, index); - round_layout(tree, child, abs_x, abs_y); + round_layout(tree, child, cumulative_x, cumulative_y); } }