From 3b0815d618374510f8a16390990d29107ed947ae Mon Sep 17 00:00:00 2001 From: Nico Burns Date: Thu, 24 Aug 2023 01:13:52 +0100 Subject: [PATCH] Implement TaffyView --- src/compute/mod.rs | 6 ++-- src/compute/taffy_tree.rs | 63 ++++++++++++++++++------------------- src/tree/measure_func.rs | 15 +++++++++ src/tree/mod.rs | 2 +- src/tree/taffy_tree/mod.rs | 2 +- src/tree/taffy_tree/tree.rs | 54 ++++++++++++++++--------------- src/util/debug.rs | 17 +++++----- 7 files changed, 88 insertions(+), 71 deletions(-) diff --git a/src/compute/mod.rs b/src/compute/mod.rs index 8f61d8140..3537647af 100644 --- a/src/compute/mod.rs +++ b/src/compute/mod.rs @@ -110,12 +110,12 @@ mod tests { use super::perform_hidden_layout; use crate::geometry::{Point, Size}; use crate::style::{Display, Style}; - use crate::tree::MeasureFunc; + use crate::tree::TaffyView; use crate::Taffy; #[test] fn hidden_layout_should_hide_recursively() { - let mut taffy: Taffy> = Taffy::new(); + let mut taffy: Taffy<()> = Taffy::new(); let style: Style = Style { display: Display::Flex, size: Size::from_lengths(50.0, 50.0), ..Default::default() }; @@ -133,7 +133,7 @@ mod tests { ) .unwrap(); - perform_hidden_layout(&mut taffy, root.into()); + perform_hidden_layout(&mut TaffyView { taffy: &mut taffy, context: () }, root.into()); // Whatever size and display-mode the nodes had previously, // all layouts should resolve to ZERO due to the root's DISPLAY::NONE diff --git a/src/compute/taffy_tree.rs b/src/compute/taffy_tree.rs index 77f56d099..a5e4905ea 100644 --- a/src/compute/taffy_tree.rs +++ b/src/compute/taffy_tree.rs @@ -4,7 +4,7 @@ use crate::compute::{leaf, LayoutAlgorithm}; use crate::geometry::{Line, Point, Size}; use crate::style::{AvailableSpace, Display}; use crate::tree::{ - Layout, LayoutTree, Measurable, NodeId, RunMode, SizeBaselinesAndMargins, SizingMode, Taffy, TaffyError, + Layout, LayoutTree, Measurable, NodeId, RunMode, SizeBaselinesAndMargins, SizingMode, Taffy, TaffyView, TaffyError, }; use crate::util::sys::round; @@ -37,15 +37,14 @@ fn debug_log_node( /// Updates the stored layout of the provided `node` and its children pub(crate) fn compute_layout( - taffy: &mut Taffy, + taffy_view: &mut TaffyView, root: NodeId, available_space: Size, ) -> Result<(), TaffyError> { - taffy.is_layouting = true; // Recursively compute node layout let size_and_baselines = perform_node_layout( - taffy, + taffy_view, root, Size::NONE, available_space.into_options(), @@ -55,21 +54,19 @@ pub(crate) fn compute_layout( ); let layout = Layout { order: 0, size: size_and_baselines.size, location: Point::ZERO }; - *taffy.layout_mut(root) = layout; + *taffy_view.layout_mut(root) = layout; // If rounding is enabled, recursively round the layout's of this node and all children - if taffy.config.use_rounding { - round_layout(taffy, root, 0.0, 0.0); + if taffy_view.taffy.config.use_rounding { + round_layout(taffy_view.taffy, root, 0.0, 0.0); } - taffy.is_layouting = false; - Ok(()) } /// Perform full layout on a node. Chooses which algorithm to use based on the `display` property. pub(crate) fn perform_node_layout( - tree: &mut Taffy, + taffy_view: &mut TaffyView, node: NodeId, known_dimensions: Size>, parent_size: Size>, @@ -78,7 +75,7 @@ pub(crate) fn perform_node_layout( vertical_margins_are_collapsible: Line, ) -> SizeBaselinesAndMargins { compute_node_layout( - tree, + taffy_view, node, known_dimensions, parent_size, @@ -91,7 +88,7 @@ pub(crate) fn perform_node_layout( /// Measure a node's size. Chooses which algorithm to use based on the `display` property. pub(crate) fn measure_node_size( - tree: &mut Taffy, + taffy_view: &mut TaffyView, node: NodeId, known_dimensions: Size>, parent_size: Size>, @@ -100,7 +97,7 @@ pub(crate) fn measure_node_size( vertical_margins_are_collapsible: Line, ) -> Size { compute_node_layout( - tree, + taffy_view, node, known_dimensions, parent_size, @@ -115,7 +112,7 @@ pub(crate) fn measure_node_size( /// Updates the stored layout of the provided `node` and its children #[allow(clippy::too_many_arguments)] fn compute_node_layout( - tree: &mut Taffy, + taffy_view: &mut TaffyView, node: NodeId, known_dimensions: Size>, parent_size: Size>, @@ -130,12 +127,12 @@ fn compute_node_layout( println!(); let node_key = node.into(); - let has_children = !tree.children[node_key].is_empty(); + let has_children = !taffy_view.taffy.children[node_key].is_empty(); // First we check if we have a cached result for the given input let cache_run_mode = if !has_children { RunMode::PerformLayout } else { run_mode }; if let Some(cached_size_and_baselines) = - tree.nodes[node_key].cache.get(known_dimensions, available_space, cache_run_mode) + taffy_view.taffy.nodes[node_key].cache.get(known_dimensions, available_space, cache_run_mode) { #[cfg(feature = "debug")] NODE_LOGGER.labelled_debug_log("CACHE", cached_size_and_baselines.size); @@ -188,15 +185,15 @@ fn compute_node_layout( } } - let display_mode = tree.nodes[node_key].style.display; + let display_mode = taffy_view.taffy.nodes[node_key].style.display; let computed_size_and_baselines = match (display_mode, has_children) { (Display::None, _) => { - perform_taffy_tree_hidden_layout(tree, node); + perform_taffy_tree_hidden_layout(taffy_view.taffy, node); SizeBaselinesAndMargins::HIDDEN } #[cfg(feature = "block_layout")] (Display::Block, true) => perform_computations::( - tree, + taffy_view, node, known_dimensions, parent_size, @@ -207,7 +204,7 @@ fn compute_node_layout( ), #[cfg(feature = "flexbox")] (Display::Flex, true) => perform_computations::( - tree, + taffy_view, node, known_dimensions, parent_size, @@ -218,7 +215,7 @@ fn compute_node_layout( ), #[cfg(feature = "grid")] (Display::Grid, true) => perform_computations::( - tree, + taffy_view, node, known_dimensions, parent_size, @@ -229,29 +226,29 @@ fn compute_node_layout( ), (_, false) => match run_mode { RunMode::PerformLayout => leaf::perform_layout( - &tree.nodes[node_key].style, - tree.nodes[node_key].needs_measure.then(|| &mut tree.measure_funcs[node_key]), + &taffy_view.taffy.nodes[node_key].style, + taffy_view.taffy.nodes[node_key].needs_measure.then(|| &mut taffy_view.taffy.measure_funcs[node_key]), known_dimensions, parent_size, available_space, sizing_mode, - tree.context.as_mut().unwrap(), + &mut taffy_view.context, ), RunMode::ComputeSize => leaf::measure_size( - &tree.nodes[node_key].style, - tree.nodes[node_key].needs_measure.then(|| &mut tree.measure_funcs[node_key]), + &taffy_view.taffy.nodes[node_key].style, + taffy_view.taffy.nodes[node_key].needs_measure.then(|| &mut taffy_view.taffy.measure_funcs[node_key]), known_dimensions, parent_size, available_space, sizing_mode, - tree.context.as_mut().unwrap(), + &mut taffy_view.context, ) .into(), }, }; // Cache result - tree.nodes[node_key].cache.store(known_dimensions, available_space, cache_run_mode, computed_size_and_baselines); + taffy_view.taffy.nodes[node_key].cache.store(known_dimensions, available_space, cache_run_mode, computed_size_and_baselines); #[cfg(feature = "debug")] NODE_LOGGER.labelled_debug_log("RESULT", computed_size_and_baselines.size); @@ -267,15 +264,17 @@ fn perform_taffy_tree_hidden_layout(tree: &mut Taffy