Skip to content

Commit

Permalink
Add early return to grid algorithm for RunMode::ComputeSize case
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed Jan 27, 2023
1 parent adef609 commit a26f6ad
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/compute/flexbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ fn compute_preliminary(
// We have the container size.
// If our caller does not care about performing layout we are done now.
if run_mode == RunMode::ComputeSize {
return SizeAndBaselines { size: constants.container_size, first_baselines: Point::NONE };
return constants.container_size.into();
}

// 16. Align all flex lines per align-content.
Expand Down
37 changes: 21 additions & 16 deletions src/compute/grid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub fn compute(
known_dimensions: Size<Option<f32>>,
parent_size: Size<Option<f32>>,
available_space: Size<AvailableSpace>,
_run_mode: RunMode,
run_mode: RunMode,
) -> SizeAndBaselines {
let get_child_styles_iter = |node| tree.children(node).map(|child_node: &Node| tree.style(*child_node));
let style = tree.style(node).clone();
Expand Down Expand Up @@ -211,6 +211,26 @@ pub fn compute(
let initial_row_sum = rows.iter().map(|track| track.base_size).sum::<f32>();
inner_node_size.height = inner_node_size.height.or_else(|| initial_row_sum.into());

// 6. Compute container size
let resolved_style_size = known_dimensions.or(style.size.maybe_resolve(parent_size));
let container_border_box = Size {
width: resolved_style_size
.get(AbstractAxis::Inline)
.unwrap_or_else(|| initial_column_sum + padding.horizontal_axis_sum() + border.horizontal_axis_sum()),
height: resolved_style_size
.get(AbstractAxis::Block)
.unwrap_or_else(|| initial_row_sum + padding.vertical_axis_sum() + border.vertical_axis_sum()),
};
let container_content_box = Size {
width: container_border_box.width - padding.horizontal_axis_sum() - border.horizontal_axis_sum(),
height: container_border_box.height - padding.vertical_axis_sum() - border.vertical_axis_sum(),
};

// If only the container's size has been requested
if run_mode == RunMode::ComputeSize {
return container_border_box.into();
}

// Re-run track sizing algorithm for Inline axis
track_sizing_algorithm(
tree,
Expand Down Expand Up @@ -241,21 +261,6 @@ pub fn compute(
|track: &GridTrack, _| Some(track.base_size),
);

// 6. Compute container size
let resolved_style_size = known_dimensions.or(style.size.maybe_resolve(parent_size));
let container_border_box = Size {
width: resolved_style_size
.get(AbstractAxis::Inline)
.unwrap_or_else(|| initial_column_sum + padding.horizontal_axis_sum() + border.horizontal_axis_sum()),
height: resolved_style_size
.get(AbstractAxis::Block)
.unwrap_or_else(|| initial_row_sum + padding.vertical_axis_sum() + border.vertical_axis_sum()),
};
let container_content_box = Size {
width: container_border_box.width - padding.horizontal_axis_sum() - border.horizontal_axis_sum(),
height: container_border_box.height - padding.vertical_axis_sum() - border.vertical_axis_sum(),
};

// 7. Resolve percentage track base sizes
// In the case of an indefinitely sized container these resolve to zero during the "Initialise Tracks" step
// and therefore need to be re-resolved here based on the content-sized content box of the container
Expand Down
6 changes: 6 additions & 0 deletions src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ pub struct SizeAndBaselines {
pub first_baselines: Point<Option<f32>>,
}

impl From<Size<f32>> for SizeAndBaselines {
fn from(size: Size<f32>) -> Self {
Self { size, first_baselines: Point::NONE }
}
}

/// The final result of a layout algorithm for a single [`Node`](crate::node::Node).
#[derive(Debug, Copy, Clone)]
pub struct Layout {
Expand Down

0 comments on commit a26f6ad

Please sign in to comment.