Skip to content

Commit

Permalink
Output computed margins in Layout
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed Jul 3, 2024
1 parent d275cbe commit 424b30e
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/compute/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ fn perform_final_layout_on_in_flow_children(
location,
padding: item.padding,
border: item.border,
margin: resolved_margin,
},
);

Expand Down Expand Up @@ -674,6 +675,7 @@ fn perform_absolute_layout_on_absolute_children(
location,
padding,
border,
margin: resolved_margin,
},
);

Expand Down
2 changes: 2 additions & 0 deletions src/compute/flexbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1835,6 +1835,7 @@ fn calculate_flex_item(
location,
padding: item.padding,
border: item.border,
margin: item.margin,
},
);

Expand Down Expand Up @@ -2168,6 +2169,7 @@ fn perform_absolute_layout_on_absolute_children(
location,
padding,
border,
margin: resolved_margin,
},
);

Expand Down
11 changes: 7 additions & 4 deletions src/compute/grid/alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ pub(super) fn align_and_position_item(
// Resolve final size
let Size { width, height } = Size { width, height }.unwrap_or(layout_output.size).maybe_clamp(min_size, max_size);

let x = align_item_within_area(
let (x, x_margin) = align_item_within_area(
Line { start: grid_area.left, end: grid_area.right },
justify_self.unwrap_or(alignment_styles.horizontal),
width,
Expand All @@ -199,7 +199,7 @@ pub(super) fn align_and_position_item(
margin.horizontal_components(),
0.0,
);
let y = align_item_within_area(
let (y, y_margin) = align_item_within_area(
Line { start: grid_area.top, end: grid_area.bottom },
align_self.unwrap_or(alignment_styles.vertical),
height,
Expand All @@ -214,6 +214,8 @@ pub(super) fn align_and_position_item(
height: if overflow.x == Overflow::Scroll { scrollbar_width } else { 0.0 },
};

let resolved_margin = Rect { left: x_margin.start, right: x_margin.end, top: y_margin.start, bottom: y_margin.end };

tree.set_unrounded_layout(
node,
&Layout {
Expand All @@ -225,6 +227,7 @@ pub(super) fn align_and_position_item(
scrollbar_size,
padding,
border,
margin: resolved_margin,
},
);

Expand All @@ -246,7 +249,7 @@ pub(super) fn align_item_within_area(
inset: Line<Option<f32>>,
margin: Line<Option<f32>>,
baseline_shim: f32,
) -> f32 {
) -> (f32, Line<f32>) {
// Calculate grid area dimension in the axis
let non_auto_margin = Line { start: margin.start.unwrap_or(0.0) + baseline_shim, end: margin.end.unwrap_or(0.0) };
let grid_area_size = f32_max(grid_area.end - grid_area.start, 0.0);
Expand Down Expand Up @@ -287,5 +290,5 @@ pub(super) fn align_item_within_area(
start += inset.start.or(inset.end.map(|pos| -pos)).unwrap_or(0.0);
}

start
(start, resolved_margin)
}
2 changes: 2 additions & 0 deletions src/compute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ pub fn compute_root_layout(tree: &mut impl LayoutPartialTree, root: NodeId, avai
scrollbar_size,
padding,
border,
// TODO: support auto margins for root node?
margin,
},
);
}
Expand Down
4 changes: 4 additions & 0 deletions src/tree/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ pub struct Layout {
pub border: Rect<f32>,
/// The size of the padding of the node
pub padding: Rect<f32>,
/// The size of the margin of the node
pub margin: Rect<f32>,
}

impl Default for Layout {
Expand All @@ -268,6 +270,7 @@ impl Layout {
scrollbar_size: Size::zero(),
border: Rect::zero(),
padding: Rect::zero(),
margin: Rect::zero(),
}
}

Expand All @@ -286,6 +289,7 @@ impl Layout {
scrollbar_size: Size::zero(),
border: Rect::zero(),
padding: Rect::zero(),
margin: Rect::zero(),
}
}
}
Expand Down

0 comments on commit 424b30e

Please sign in to comment.