Skip to content
Open
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
11 changes: 9 additions & 2 deletions benches/benches/mixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,15 @@ fn mixed_benchmark(c: &mut Criterion) {
.compute_layout_with_measure(
root,
Size::MAX_CONTENT,
|known_dimensions, available_space, _node_id, node_context, _style| {
measure_function(known_dimensions, available_space, node_context)
|inputs, _node_id, node_context, style| {
taffy::compute_leaf_layout(
inputs,
style,
|_, _| 0.0,
|known_dimensions, available_space| {
measure_function(known_dimensions, available_space, node_context)
},
)
},
)
.unwrap();
Expand Down
6 changes: 4 additions & 2 deletions examples/cosmic_text/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,10 @@ fn main() -> Result<(), taffy::TaffyError> {
Size::MAX_CONTENT,
// Note: this closure is a FnMut closure and can be used to borrow external context for the duration of layout
// For example, you may wish to borrow a global font registry and pass it into your text measuring function
|known_dimensions, available_space, _node_id, node_context, _style| {
measure_function(known_dimensions, available_space, node_context, &mut font_system)
|inputs, _node_id, node_context, style| {
taffy::compute_leaf_layout(inputs, style, |_, _| 0.0, |known_dimensions, available_space| {
measure_function(known_dimensions, available_space, node_context, &mut font_system)
})
},
)?;
taffy.print_tree(root);
Expand Down
11 changes: 9 additions & 2 deletions examples/measure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,15 @@ fn main() -> Result<(), taffy::TaffyError> {
Size::MAX_CONTENT,
// Note: this closure is a FnMut closure and can be used to borrow external context for the duration of layout
// For example, you may wish to borrow a global font registry and pass it into your text measuring function
|known_dimensions, available_space, _node_id, node_context, _style| {
measure_function(known_dimensions, available_space, node_context, &font_metrics)
|inputs, _node_id, node_context, style| {
taffy::compute_leaf_layout(
inputs,
style,
|_, _| 0.0,
|known_dimensions, available_space| {
measure_function(known_dimensions, available_space, node_context, &font_metrics)
},
)
},
)?;
taffy.print_tree(root);
Expand Down
63 changes: 30 additions & 33 deletions src/tree/taffy_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,7 @@ impl<NodeContext> PrintTree for TaffyTree<NodeContext> {
/// which makes the lifetimes of the context much more flexible.
pub(crate) struct TaffyView<'t, NodeContext, MeasureFunction>
where
MeasureFunction:
FnMut(Size<Option<f32>>, Size<AvailableSpace>, NodeId, Option<&mut NodeContext>, &Style) -> Size<f32>,
MeasureFunction: FnMut(LayoutInput, NodeId, Option<&mut NodeContext>, &Style) -> LayoutOutput,
{
/// A reference to the TaffyTree
pub(crate) taffy: &'t mut TaffyTree<NodeContext>,
Expand All @@ -275,8 +274,7 @@ where

impl<NodeContext, MeasureFunction> TaffyView<'_, NodeContext, MeasureFunction>
where
MeasureFunction:
FnMut(Size<Option<f32>>, Size<AvailableSpace>, NodeId, Option<&mut NodeContext>, &Style) -> Size<f32>,
MeasureFunction: FnMut(LayoutInput, NodeId, Option<&mut NodeContext>, &Style) -> LayoutOutput,
{
#[inline(always)]
/// Unified implementation that both `LayoutPartialTree::compute_child_layout`
Expand Down Expand Up @@ -320,10 +318,7 @@ where
let style = &tree.taffy.nodes[node_key].style;
let has_context = tree.taffy.nodes[node_key].has_context;
let node_context = has_context.then(|| tree.taffy.node_context_data.get_mut(node_key)).flatten();
let measure_function = |known_dimensions, available_space| {
(tree.measure_function)(known_dimensions, available_space, node_id, node_context, style)
};
compute_leaf_layout(inputs, style, |_, _| 0.0, measure_function)
(tree.measure_function)(inputs, node_id, node_context, style)
}
}
})
Expand All @@ -333,8 +328,7 @@ where
// TraversePartialTree impl for TaffyView
impl<NodeContext, MeasureFunction> TraversePartialTree for TaffyView<'_, NodeContext, MeasureFunction>
where
MeasureFunction:
FnMut(Size<Option<f32>>, Size<AvailableSpace>, NodeId, Option<&mut NodeContext>, &Style) -> Size<f32>,
MeasureFunction: FnMut(LayoutInput, NodeId, Option<&mut NodeContext>, &Style) -> LayoutOutput,
{
type ChildIter<'a>
= TaffyTreeChildIter<'a>
Expand All @@ -359,16 +353,14 @@ where

// TraverseTree impl for TaffyView
impl<NodeContext, MeasureFunction> TraverseTree for TaffyView<'_, NodeContext, MeasureFunction> where
MeasureFunction:
FnMut(Size<Option<f32>>, Size<AvailableSpace>, NodeId, Option<&mut NodeContext>, &Style) -> Size<f32>
MeasureFunction: FnMut(LayoutInput, NodeId, Option<&mut NodeContext>, &Style) -> LayoutOutput
{
}

// LayoutPartialTree impl for TaffyView
impl<NodeContext, MeasureFunction> LayoutPartialTree for TaffyView<'_, NodeContext, MeasureFunction>
where
MeasureFunction:
FnMut(Size<Option<f32>>, Size<AvailableSpace>, NodeId, Option<&mut NodeContext>, &Style) -> Size<f32>,
MeasureFunction: FnMut(LayoutInput, NodeId, Option<&mut NodeContext>, &Style) -> LayoutOutput,
{
type CoreContainerStyle<'a>
= &'a Style
Expand Down Expand Up @@ -405,8 +397,7 @@ where

impl<NodeContext, MeasureFunction> CacheTree for TaffyView<'_, NodeContext, MeasureFunction>
where
MeasureFunction:
FnMut(Size<Option<f32>>, Size<AvailableSpace>, NodeId, Option<&mut NodeContext>, &Style) -> Size<f32>,
MeasureFunction: FnMut(LayoutInput, NodeId, Option<&mut NodeContext>, &Style) -> LayoutOutput,
{
fn cache_get(&self, node_id: NodeId, input: &LayoutInput) -> Option<LayoutOutput> {
self.taffy.nodes[node_id.into()].cache.get(input)
Expand All @@ -424,8 +415,7 @@ where
#[cfg(feature = "block_layout")]
impl<NodeContext, MeasureFunction> LayoutBlockContainer for TaffyView<'_, NodeContext, MeasureFunction>
where
MeasureFunction:
FnMut(Size<Option<f32>>, Size<AvailableSpace>, NodeId, Option<&mut NodeContext>, &Style) -> Size<f32>,
MeasureFunction: FnMut(LayoutInput, NodeId, Option<&mut NodeContext>, &Style) -> LayoutOutput,
{
type BlockContainerStyle<'a>
= &'a Style
Expand Down Expand Up @@ -460,8 +450,7 @@ where
#[cfg(feature = "flexbox")]
impl<NodeContext, MeasureFunction> LayoutFlexboxContainer for TaffyView<'_, NodeContext, MeasureFunction>
where
MeasureFunction:
FnMut(Size<Option<f32>>, Size<AvailableSpace>, NodeId, Option<&mut NodeContext>, &Style) -> Size<f32>,
MeasureFunction: FnMut(LayoutInput, NodeId, Option<&mut NodeContext>, &Style) -> LayoutOutput,
{
type FlexboxContainerStyle<'a>
= &'a Style
Expand All @@ -486,8 +475,7 @@ where
#[cfg(feature = "grid")]
impl<NodeContext, MeasureFunction> LayoutGridContainer for TaffyView<'_, NodeContext, MeasureFunction>
where
MeasureFunction:
FnMut(Size<Option<f32>>, Size<AvailableSpace>, NodeId, Option<&mut NodeContext>, &Style) -> Size<f32>,
MeasureFunction: FnMut(LayoutInput, NodeId, Option<&mut NodeContext>, &Style) -> LayoutOutput,
{
type GridContainerStyle<'a>
= &'a Style
Expand Down Expand Up @@ -518,8 +506,7 @@ where
// RoundTree impl for TaffyView
impl<NodeContext, MeasureFunction> RoundTree for TaffyView<'_, NodeContext, MeasureFunction>
where
MeasureFunction:
FnMut(Size<Option<f32>>, Size<AvailableSpace>, NodeId, Option<&mut NodeContext>, &Style) -> Size<f32>,
MeasureFunction: FnMut(LayoutInput, NodeId, Option<&mut NodeContext>, &Style) -> LayoutOutput,
{
#[inline(always)]
fn get_unrounded_layout(&self, node: NodeId) -> Layout {
Expand Down Expand Up @@ -909,8 +896,7 @@ impl<NodeContext> TaffyTree<NodeContext> {
measure_function: MeasureFunction,
) -> Result<(), TaffyError>
where
MeasureFunction:
FnMut(Size<Option<f32>>, Size<AvailableSpace>, NodeId, Option<&mut NodeContext>, &Style) -> Size<f32>,
MeasureFunction: FnMut(LayoutInput, NodeId, Option<&mut NodeContext>, &Style) -> LayoutOutput,
{
let use_rounding = self.config.use_rounding;
let mut taffy_view = TaffyView { taffy: self, measure_function };
Expand All @@ -923,7 +909,9 @@ impl<NodeContext> TaffyTree<NodeContext> {

/// Updates the stored layout of the provided `node` and its children
pub fn compute_layout(&mut self, node: NodeId, available_space: Size<AvailableSpace>) -> Result<(), TaffyError> {
self.compute_layout_with_measure(node, available_space, |_, _, _, _, _| Size::ZERO)
self.compute_layout_with_measure(node, available_space, |inputs, _, _, style| {
compute_leaf_layout(inputs, style, |_, _| 0.0, |_, _| Size::ZERO)
})
}

/// Prints a debug representation of the tree's layout
Expand All @@ -935,7 +923,10 @@ impl<NodeContext> TaffyTree<NodeContext> {
/// Returns an instance of LayoutTree representing the TaffyTree
#[cfg(test)]
pub(crate) fn as_layout_tree(&mut self) -> impl LayoutPartialTree + CacheTree + '_ {
TaffyView { taffy: self, measure_function: |_, _, _, _, _| Size::ZERO }
TaffyView {
taffy: self,
measure_function: |inputs, _, _, style| compute_leaf_layout(inputs, style, |_, _| 0.0, |_, _| Size::ZERO),
}
}
}

Expand All @@ -948,13 +939,19 @@ mod tests {
use crate::util::sys;

fn size_measure_function(
known_dimensions: Size<Option<f32>>,
_available_space: Size<AvailableSpace>,
inputs: LayoutInput,
_node_id: NodeId,
node_context: Option<&mut Size<f32>>,
_style: &Style,
) -> Size<f32> {
known_dimensions.unwrap_or(node_context.cloned().unwrap_or(Size::ZERO))
style: &Style,
) -> LayoutOutput {
compute_leaf_layout(
inputs,
style,
|_, _| 0.0,
|known_dimensions, _available_space| {
known_dimensions.unwrap_or(node_context.cloned().unwrap_or(Size::ZERO))
},
)
}

#[test]
Expand Down
48 changes: 27 additions & 21 deletions tests/common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use taffy::{AvailableSpace, NodeId, Size, Style, TaffyTree};
use taffy::{compute_leaf_layout, LayoutInput, LayoutOutput, NodeId, Size, Style, TaffyTree};

/// Creates a `TaffyTree` that uses `TestNodeContext`. The purpose of this function is
/// to allow `TaffyTree` to be monomophised once in this crate rather than separately for
Expand Down Expand Up @@ -61,32 +61,38 @@ pub enum TestMeasureData {

/// A measure function for tests that works with `TestNodeContext`
pub fn test_measure_function(
known_dimensions: Size<Option<f32>>,
available_space: Size<AvailableSpace>,
inputs: LayoutInput,
_node_id: NodeId,
context: Option<&mut TestNodeContext>,
_style: &Style,
) -> Size<f32> {
if let Size { width: Some(width), height: Some(height) } = known_dimensions {
return Size { width, height };
}
style: &Style,
) -> LayoutOutput {
compute_leaf_layout(
inputs,
style,
|_, _| 0.0,
|known_dimensions, available_space| {
if let Size { width: Some(width), height: Some(height) } = known_dimensions {
return Size { width, height };
}

let Some(context) = context else { return known_dimensions.map(|d| d.unwrap_or(0.0)) };
let Some(context) = context else { return known_dimensions.map(|d| d.unwrap_or(0.0)) };

// Increment count
context.count += 1;
// Increment count
context.count += 1;

let compute_size = match &context.measure_data {
TestMeasureData::Zero => Size::ZERO,
TestMeasureData::Fixed(size) => *size,
TestMeasureData::AspectRatio(data) => data.measure(known_dimensions),
TestMeasureData::AhemText(data) => data.measure(known_dimensions, available_space),
};
let compute_size = match &context.measure_data {
TestMeasureData::Zero => Size::ZERO,
TestMeasureData::Fixed(size) => *size,
TestMeasureData::AspectRatio(data) => data.measure(known_dimensions),
TestMeasureData::AhemText(data) => data.measure(known_dimensions, available_space),
};

Size {
width: known_dimensions.width.unwrap_or(compute_size.width),
height: known_dimensions.height.unwrap_or(compute_size.height),
}
Size {
width: known_dimensions.width.unwrap_or(compute_size.width),
height: known_dimensions.height.unwrap_or(compute_size.height),
}
},
)
}

/// Measure data for nodes that returns results based on an intrinsic aspect ratio
Expand Down
1 change: 1 addition & 0 deletions tests/hand_written.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod hand_written {
mod baseline;
mod border_and_padding;
mod caching;
mod measure;
Expand Down
Loading
Loading