Skip to content

Commit

Permalink
Remove style clone in grid algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed Jul 7, 2024
1 parent ddefab9 commit b03ec1b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
33 changes: 20 additions & 13 deletions src/compute/grid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
//! <https://www.w3.org/TR/css-grid-1>
use crate::geometry::{AbsoluteAxis, AbstractAxis, InBothAbsAxis};
use crate::geometry::{Line, Point, Rect, Size};
use crate::style::{AlignContent, AlignItems, AlignSelf, AvailableSpace, Overflow, Position};
use crate::style::{AlignItems, AlignSelf, AvailableSpace, Overflow, Position};
use crate::tree::{Layout, LayoutInput, LayoutOutput, LayoutPartialTreeExt, NodeId, RunMode, SizingMode};
use crate::util::debug::debug_log;
use crate::util::sys::{f32_max, GridTrackVec, Vec};
use crate::util::MaybeMath;
use crate::util::{MaybeResolve, ResolveOrZero};
use crate::{
style_helpers::*, BoxGenerationMode, BoxSizing, CoreStyle, GridContainerStyle, GridItemStyle, LayoutGridContainer,
style_helpers::*, AlignContent, BoxGenerationMode, BoxSizing, CoreStyle, GridContainerStyle, GridItemStyle,
JustifyContent, LayoutGridContainer,
};
use alignment::{align_and_position_item, align_tracks};
use explicit_grid::{compute_explicit_grid_size_in_axis, initialize_grid_tracks};
Expand Down Expand Up @@ -39,7 +40,7 @@ mod util;
pub fn compute_grid_layout(tree: &mut impl LayoutGridContainer, node: NodeId, inputs: LayoutInput) -> LayoutOutput {
let LayoutInput { known_dimensions, parent_size, available_space, run_mode, .. } = inputs;

let style = tree.get_grid_container_style(node).clone();
let style = tree.get_grid_container_style(node);

// 1. Compute "available grid space"
// https://www.w3.org/TR/css-grid-1/#available-grid-space
Expand Down Expand Up @@ -83,6 +84,13 @@ pub fn compute_grid_layout(tree: &mut impl LayoutGridContainer, node: NodeId, in
content_box_inset.right += scrollbar_gutter.x;
content_box_inset.bottom += scrollbar_gutter.y;

let align_content = style.align_content().unwrap_or(AlignContent::Stretch);
let justify_content = style.justify_content().unwrap_or(JustifyContent::Stretch);
let align_items = style.align_items();
let justify_items = style.justify_items();

drop(style);

let constrained_available_space = known_dimensions
.or(preferred_size)
.map(|size| size.map(AvailableSpace::Definite))
Expand Down Expand Up @@ -159,8 +167,8 @@ pub fn compute_grid_layout(tree: &mut impl LayoutGridContainer, node: NodeId, in
&mut items,
in_flow_children_iter,
style.grid_auto_flow(),
style.align_items().unwrap_or(AlignItems::Stretch),
style.justify_items().unwrap_or(AlignItems::Stretch),
align_items.unwrap_or(AlignItems::Stretch),
justify_items.unwrap_or(AlignItems::Stretch),
);

// Extract track counts from previous step (auto-placement can expand the number of tracks)
Expand Down Expand Up @@ -209,7 +217,7 @@ pub fn compute_grid_layout(tree: &mut impl LayoutGridContainer, node: NodeId, in
AbstractAxis::Inline,
min_size.get(AbstractAxis::Inline),
max_size.get(AbstractAxis::Inline),
style.grid_align_content(AbstractAxis::Block),
align_content,
available_grid_space,
inner_node_size,
&mut columns,
Expand All @@ -229,7 +237,7 @@ pub fn compute_grid_layout(tree: &mut impl LayoutGridContainer, node: NodeId, in
AbstractAxis::Block,
min_size.get(AbstractAxis::Block),
max_size.get(AbstractAxis::Block),
style.grid_align_content(AbstractAxis::Inline),
justify_content,
available_grid_space,
inner_node_size,
&mut rows,
Expand Down Expand Up @@ -342,7 +350,7 @@ pub fn compute_grid_layout(tree: &mut impl LayoutGridContainer, node: NodeId, in
AbstractAxis::Inline,
min_size.get(AbstractAxis::Inline),
max_size.get(AbstractAxis::Inline),
style.grid_align_content(AbstractAxis::Block),
align_content,
available_grid_space,
inner_node_size,
&mut columns,
Expand Down Expand Up @@ -404,7 +412,7 @@ pub fn compute_grid_layout(tree: &mut impl LayoutGridContainer, node: NodeId, in
AbstractAxis::Block,
min_size.get(AbstractAxis::Block),
max_size.get(AbstractAxis::Block),
style.grid_align_content(AbstractAxis::Inline),
justify_content,
available_grid_space,
inner_node_size,
&mut rows,
Expand All @@ -424,15 +432,15 @@ pub fn compute_grid_layout(tree: &mut impl LayoutGridContainer, node: NodeId, in
Line { start: padding.left, end: padding.right },
Line { start: border.left, end: border.right },
&mut columns,
style.justify_content().unwrap_or(AlignContent::Stretch),
justify_content,
);
// Align rows
align_tracks(
container_content_box.get(AbstractAxis::Block),
Line { start: padding.top, end: padding.bottom },
Line { start: border.top, end: border.bottom },
&mut rows,
style.align_content().unwrap_or(AlignContent::Stretch),
align_content,
);

// 9. Size, Align, and Position Grid Items
Expand All @@ -443,8 +451,7 @@ pub fn compute_grid_layout(tree: &mut impl LayoutGridContainer, node: NodeId, in
// Sort items back into original order to allow them to be matched up with styles
items.sort_by_key(|item| item.source_order);

let container_alignment_styles = InBothAbsAxis { horizontal: style.justify_items(), vertical: style.align_items() };
drop(style);
let container_alignment_styles = InBothAbsAxis { horizontal: justify_items, vertical: align_items };

// Position in-flow children (stored in items vector)
for (index, item) in items.iter_mut().enumerate() {
Expand Down
4 changes: 2 additions & 2 deletions src/tree/taffy_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,11 +380,11 @@ where
MeasureFunction:
FnMut(Size<Option<f32>>, Size<AvailableSpace>, NodeId, Option<&mut NodeContext>, &Style) -> Size<f32>,
{
type ContainerStyle = Style;
type ContainerStyle<'a> = &'a Style where Self: 'a;
type ItemStyle<'a> = &'a Style where Self: 'a;

#[inline(always)]
fn get_grid_container_style(&self, node_id: NodeId) -> &Self::ContainerStyle {
fn get_grid_container_style(&self, node_id: NodeId) -> Self::ContainerStyle<'_> {
&self.taffy.nodes[node_id.into()].style
}

Expand Down
6 changes: 4 additions & 2 deletions src/tree/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,17 @@ pub trait LayoutFlexboxContainer: LayoutPartialTree {
/// Extends [`LayoutPartialTree`] with getters for the styles required for CSS Grid layout
pub trait LayoutGridContainer: LayoutPartialTree {
/// The style type representing the CSS Grid container's styles
type ContainerStyle: GridContainerStyle + Clone;
type ContainerStyle<'a>: GridContainerStyle
where
Self: 'a;

/// The style type representing each CSS Grid item's styles
type ItemStyle<'a>: GridItemStyle
where
Self: 'a;

/// Get the container's styles
fn get_grid_container_style(&self, node_id: NodeId) -> &Self::ContainerStyle;
fn get_grid_container_style(&self, node_id: NodeId) -> Self::ContainerStyle<'_>;

/// Get the child's styles
fn get_grid_child_style(&self, child_node_id: NodeId) -> Self::ItemStyle<'_>;
Expand Down

0 comments on commit b03ec1b

Please sign in to comment.