Skip to content

Commit

Permalink
layout: Use Au in ResolvedMargins and CollapsedMargin (#31848)
Browse files Browse the repository at this point in the history
* change ResolvedMargins to use Au instead of length

* made suggested changes

removed whitespace changes

made suggested changes

* Update components/layout_2020/flexbox/layout.rs

Co-authored-by: Oriol Brufau <obrufau@igalia.com>

* Update components/layout_2020/flow/mod.rs

Co-authored-by: Oriol Brufau <obrufau@igalia.com>

* fixed errors

fixed errors

fixed formatting

fixed errors

* modified changes

made suggested changes

* Update components/layout_2020/fragment_tree/fragment.rs

Co-authored-by: Oriol Brufau <obrufau@igalia.com>

updated components/layout_2020/fragment_tree/fragment.rs

* Update components/layout_2020/flow/mod.rs

Co-authored-by: Oriol Brufau <obrufau@igalia.com>

* Update components/layout_2020/flow/mod.rs

Co-authored-by: Oriol Brufau <obrufau@igalia.com>

* updated changes

updated changes

* unified all the suggested changes here

* fixed formatting

* reverted unnecessary changes

* fixed unnecessary warnings

---------

Co-authored-by: Oriol Brufau <obrufau@igalia.com>
  • Loading branch information
ektuu and Loirooriol committed Mar 28, 2024
1 parent 66ad795 commit 5d518ca
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 70 deletions.
4 changes: 2 additions & 2 deletions components/layout_2020/flexbox/layout.rs
Expand Up @@ -847,7 +847,7 @@ impl FlexLine<'_> {
.zip(&item_margins)
.map(|(((item, item_result), content_rect), margin)| {
let content_rect = flex_context.rect_to_flow_relative(line_size, content_rect);
let margin = flex_context.sides_to_flow_relative(*margin);
let margin = flex_context.sides_to_flow_relative(*margin).into();
let collapsed_margin = CollapsedBlockMargins::from_margin(&margin);
(
// TODO: We should likely propagate baselines from `display: flex`.
Expand All @@ -858,7 +858,7 @@ impl FlexLine<'_> {
content_rect,
flex_context.sides_to_flow_relative(item.padding),
flex_context.sides_to_flow_relative(item.border),
margin.into(),
margin,
None, /* clearance */
collapsed_margin,
),
Expand Down
18 changes: 5 additions & 13 deletions components/layout_2020/flow/float.rs
Expand Up @@ -1062,7 +1062,7 @@ impl SequentialLayoutState {
/// Return the current block position in the float containing block formatting
/// context and any uncollapsed block margins.
pub(crate) fn current_block_position_including_margins(&self) -> Au {
self.bfc_relative_block_position + self.current_margin.solve().into()
self.bfc_relative_block_position + self.current_margin.solve()
}

/// Collapses margins, moving the block position down by the collapsed value of `current_margin`
Expand All @@ -1071,29 +1071,23 @@ impl SequentialLayoutState {
/// Call this method before laying out children when it is known that the start margin of the
/// current fragment can't collapse with the margins of any of its children.
pub(crate) fn collapse_margins(&mut self) {
self.advance_block_position(self.current_margin.solve().into());
self.advance_block_position(self.current_margin.solve());
self.current_margin = CollapsedMargin::zero();
}

/// Computes the position of the block-start border edge of an element
/// with the provided `block_start_margin`, assuming no clearance.
pub(crate) fn position_without_clearance(&self, block_start_margin: &CollapsedMargin) -> Au {
// Adjoin `current_margin` and `block_start_margin` since there is no clearance.
self.bfc_relative_block_position +
self.current_margin
.adjoin(block_start_margin)
.solve()
.into()
self.bfc_relative_block_position + self.current_margin.adjoin(block_start_margin).solve()
}

/// Computes the position of the block-start border edge of an element
/// with the provided `block_start_margin`, assuming a clearance of 0px.
pub(crate) fn position_with_zero_clearance(&self, block_start_margin: &CollapsedMargin) -> Au {
// Clearance prevents `current_margin` and `block_start_margin` from being
// adjoining, so we need to solve them separately and then sum.
self.bfc_relative_block_position +
self.current_margin.solve().into() +
block_start_margin.solve().into()
self.bfc_relative_block_position + self.current_margin.solve() + block_start_margin.solve()
}

/// Returns the block-end outer edge of the lowest float that is to be cleared (if any)
Expand Down Expand Up @@ -1191,7 +1185,6 @@ impl SequentialLayoutState {
.containing_block_info
.block_start_margins_not_collapsed
.solve()
.into()
}

/// This function places a Fragment that has been created for a FloatBox.
Expand All @@ -1206,8 +1199,7 @@ impl SequentialLayoutState {
.containing_block_info
.block_start_margins_not_collapsed
.adjoin(&margins_collapsing_with_parent_containing_block)
.solve()
.into();
.solve();

self.floats.set_ceiling_from_non_floats(
block_start_of_containing_block_in_bfc + block_offset_from_containing_block_top,
Expand Down
91 changes: 45 additions & 46 deletions components/layout_2020/flow/mod.rs
Expand Up @@ -117,7 +117,7 @@ impl BlockLevelBox {

let pbm = style.padding_border_margin(containing_block);
let start_margin = pbm.margin.block_start.auto_is(Au::zero);
collected_margin.adjoin_assign(&CollapsedMargin::new(start_margin.into()));
collected_margin.adjoin_assign(&CollapsedMargin::new(start_margin));

let child_boxes = match self {
BlockLevelBox::SameFormattingContextBlock { ref contents, .. } => match contents {
Expand Down Expand Up @@ -176,7 +176,7 @@ impl BlockLevelBox {
}

let end_margin = pbm.margin.block_end.auto_is(Au::zero);
collected_margin.adjoin_assign(&CollapsedMargin::new(end_margin.into()));
collected_margin.adjoin_assign(&CollapsedMargin::new(end_margin));

true
}
Expand Down Expand Up @@ -242,10 +242,9 @@ impl BlockFormattingContext {

IndependentLayout {
fragments: flow_layout.fragments,
content_block_size: (flow_layout.content_block_size +
content_block_size: Au::from(flow_layout.content_block_size) +
flow_layout.collapsible_margins_in_children.end.solve() +
clearance.unwrap_or_else(Au::zero).into())
.into(),
clearance.unwrap_or_default(),
content_inline_size_for_table: None,
baselines: flow_layout.baselines,
}
Expand Down Expand Up @@ -638,7 +637,7 @@ fn layout_in_flow_non_replaced_block_level_same_formatting_context(
} = solve_margins(
containing_block,
&pbm,
containing_block_for_children.inline_size.into(),
containing_block_for_children.inline_size,
);

let computed_block_size = style.content_block_size();
Expand Down Expand Up @@ -710,7 +709,7 @@ fn layout_in_flow_non_replaced_block_level_same_formatting_context(
.inline_start +
pbm.padding.inline_start +
pbm.border.inline_start +
effective_margin_inline_start.into();
effective_margin_inline_start;
let new_cb_offsets = ContainingBlockPositionInfo {
block_start: sequential_layout_state.bfc_relative_block_position,
block_start_margins_not_collapsed: sequential_layout_state.current_margin,
Expand Down Expand Up @@ -753,7 +752,7 @@ fn layout_in_flow_non_replaced_block_level_same_formatting_context(
.end
.adjoin_assign(&collapsible_margins_in_children.end);
} else {
content_block_size += collapsible_margins_in_children.end.solve();
content_block_size += collapsible_margins_in_children.end.solve().into();
}

let computed_min_block_size = style.min_block_size();
Expand Down Expand Up @@ -801,7 +800,7 @@ fn layout_in_flow_non_replaced_block_level_same_formatting_context(
clearance.unwrap_or_else(Au::zero)),
inline: pbm.padding.inline_start +
pbm.border.inline_start +
effective_margin_inline_start.into(),
effective_margin_inline_start,
},
size: LogicalVec2 {
block: block_size,
Expand All @@ -816,7 +815,7 @@ fn layout_in_flow_non_replaced_block_level_same_formatting_context(
content_rect.into(),
pbm.padding,
pbm.border,
margin.into(),
margin,
clearance,
block_margins_collapsed_with_children,
)
Expand Down Expand Up @@ -878,14 +877,14 @@ impl NonReplacedFormattingContext {
let ResolvedMargins {
margin,
effective_margin_inline_start,
} = solve_margins(containing_block, &pbm, inline_size.into());
} = solve_margins(containing_block, &pbm, inline_size);

let content_rect = LogicalRect {
start_corner: LogicalVec2 {
block: pbm.padding.block_start + pbm.border.block_start,
inline: pbm.padding.inline_start +
pbm.border.inline_start +
effective_margin_inline_start.into(),
effective_margin_inline_start,
},
size: LogicalVec2 {
block: block_size,
Expand All @@ -902,7 +901,7 @@ impl NonReplacedFormattingContext {
content_rect.into(),
pbm.padding,
pbm.border,
margin.into(),
margin,
None, /* clearance */
block_margins_collapsed_with_children,
)
Expand Down Expand Up @@ -935,7 +934,7 @@ impl NonReplacedFormattingContext {
let effective_margin_inline_start;
let (margin_block_start, margin_block_end) =
solve_block_margins_for_in_flow_block_level(&pbm);
let collapsed_margin_block_start = CollapsedMargin::new(margin_block_start.into());
let collapsed_margin_block_start = CollapsedMargin::new(margin_block_start);

// From https://drafts.csswg.org/css2/#floats:
// "The border box of a table, a block-level replaced element, or an element in
Expand Down Expand Up @@ -1112,10 +1111,10 @@ impl NonReplacedFormattingContext {
}

let margin = LogicalSides {
inline_start: margin_inline_start.into(),
inline_end: margin_inline_end.into(),
block_start: margin_block_start.into(),
block_end: margin_block_end.into(),
inline_start: margin_inline_start,
inline_end: margin_inline_end,
block_start: margin_block_start,
block_end: margin_block_end,
};

// Clearance prevents margin collapse between this block and previous ones,
Expand Down Expand Up @@ -1154,7 +1153,7 @@ impl NonReplacedFormattingContext {
content_rect.into(),
pbm.padding,
pbm.border,
margin.into(),
margin,
clearance,
block_margins_collapsed_with_children,
)
Expand Down Expand Up @@ -1193,7 +1192,7 @@ fn layout_in_flow_replaced_block_level(
// sufficient space. They may even make the border box of said element narrower
// than defined by section 10.3.3. CSS 2 does not define when a UA may put said
// element next to the float or by how much said element may become narrower."
let collapsed_margin_block_start = CollapsedMargin::new(margin_block_start.into());
let collapsed_margin_block_start = CollapsedMargin::new(margin_block_start);
let size = &content_size + &pbm.padding_border_sums.clone();
(
clearance,
Expand All @@ -1219,7 +1218,7 @@ fn layout_in_flow_replaced_block_level(
sequential_layout_state.collapse_margins();
sequential_layout_state
.advance_block_position(size.block + clearance.unwrap_or_else(Au::zero));
sequential_layout_state.adjoin_assign(&CollapsedMargin::new(margin_block_end.into()));
sequential_layout_state.adjoin_assign(&CollapsedMargin::new(margin_block_end));
} else {
clearance = None;
(
Expand All @@ -1228,15 +1227,15 @@ fn layout_in_flow_replaced_block_level(
) = solve_inline_margins_for_in_flow_block_level(
containing_block,
&pbm,
content_size.inline.into(),
content_size.inline,
);
};

let margin = LogicalSides {
inline_start: margin_inline_start.into(),
inline_end: margin_inline_end.into(),
block_start: margin_block_start.into(),
block_end: margin_block_end.into(),
inline_start: margin_inline_start,
inline_end: margin_inline_end,
block_start: margin_block_start,
block_end: margin_block_end,
};

let start_corner = LogicalVec2 {
Expand All @@ -1259,7 +1258,7 @@ fn layout_in_flow_replaced_block_level(
content_rect.into(),
pbm.padding,
pbm.border,
margin.into(),
margin,
clearance,
block_margins_collapsed_with_children,
)
Expand All @@ -1274,15 +1273,15 @@ struct ContainingBlockPaddingAndBorder<'a> {

struct ResolvedMargins {
/// Used value for the margin properties, as exposed in getComputedStyle().
margin: LogicalSides<Length>,
margin: LogicalSides<Au>,

/// Distance between the border box and the containing block on the inline-start side.
/// This is typically the same as the inline-start margin, but can be greater when
/// the box is justified within the free space in the containing block.
/// The reason we aren't just adjusting the used margin-inline-start is that
/// this shouldn't be observable via getComputedStyle().
/// <https://drafts.csswg.org/css-align/#justify-self-property>
effective_margin_inline_start: Length,
effective_margin_inline_start: Au,
}

/// Given the style for an in-flow box and its containing block, determine the containing
Expand Down Expand Up @@ -1348,19 +1347,19 @@ fn solve_containing_block_padding_and_border_for_in_flow_box<'a>(
fn solve_margins(
containing_block: &ContainingBlock<'_>,
pbm: &PaddingBorderMargin,
inline_size: Length,
inline_size: Au,
) -> ResolvedMargins {
let (inline_margins, effective_margin_inline_start) =
solve_inline_margins_for_in_flow_block_level(containing_block, pbm, inline_size);
let block_margins = solve_block_margins_for_in_flow_block_level(pbm);
ResolvedMargins {
margin: LogicalSides {
inline_start: inline_margins.0.into(),
inline_end: inline_margins.1.into(),
block_start: block_margins.0.into(),
block_end: block_margins.1.into(),
inline_start: inline_margins.0,
inline_end: inline_margins.1,
block_start: block_margins.0,
block_end: block_margins.1,
},
effective_margin_inline_start: effective_margin_inline_start.into(),
effective_margin_inline_start,
}
}

Expand Down Expand Up @@ -1409,10 +1408,9 @@ fn justify_self_alignment(containing_block: &ContainingBlock, free_space: Au) ->
fn solve_inline_margins_for_in_flow_block_level(
containing_block: &ContainingBlock,
pbm: &PaddingBorderMargin,
inline_size: Length,
inline_size: Au,
) -> ((Au, Au), Au) {
let free_space =
containing_block.inline_size - pbm.padding_border_sums.inline - inline_size.into();
let free_space = containing_block.inline_size - pbm.padding_border_sums.inline - inline_size;
let mut justification = Au::zero();
let inline_margins = match (pbm.margin.inline_start, pbm.margin.inline_end) {
(AuOrAuto::Auto, AuOrAuto::Auto) => {
Expand Down Expand Up @@ -1598,7 +1596,7 @@ impl PlacementState {
// Setting `next_in_flow_margin_collapses_with_parent_start_margin` to false
// prevents collapsing with the start margin of the parent, and will set
// `collapsed_through` to false, preventing the parent from collapsing through.
self.current_block_direction_position += self.current_margin.solve();
self.current_block_direction_position += self.current_margin.solve().into();
self.current_margin = CollapsedMargin::zero();
self.next_in_flow_margin_collapses_with_parent_start_margin = false;
if fragment_block_margins.collapsed_through {
Expand All @@ -1609,7 +1607,7 @@ impl PlacementState {
}

if self.next_in_flow_margin_collapses_with_parent_start_margin {
debug_assert_eq!(self.current_margin.solve(), Length::zero());
debug_assert!(self.current_margin.solve().is_zero());
self.start_margin
.adjoin_assign(&fragment_block_margins.start);
if fragment_block_margins.collapsed_through {
Expand All @@ -1621,9 +1619,9 @@ impl PlacementState {
self.current_margin
.adjoin_assign(&fragment_block_margins.start);
}

fragment.content_rect.start_corner.block +=
self.current_margin.solve() + self.current_block_direction_position;
Length::from(self.current_margin.solve()) +
self.current_block_direction_position;

if fragment_block_margins.collapsed_through {
// `fragment_block_size` is typically zero when collapsing through,
Expand All @@ -1633,13 +1631,14 @@ impl PlacementState {
.adjoin_assign(&fragment_block_margins.end);
} else {
self.current_block_direction_position +=
self.current_margin.solve() + fragment_block_size.into();
Length::from(self.current_margin.solve()) + fragment_block_size.into();
self.current_margin = fragment_block_margins.end;
}
},
Fragment::AbsoluteOrFixedPositioned(fragment) => {
let offset = LogicalVec2 {
block: self.current_margin.solve() + self.current_block_direction_position,
block: Length::from(self.current_margin.solve()) +
self.current_block_direction_position,
inline: Length::new(0.),
};
fragment.borrow_mut().adjust_offsets(offset);
Expand All @@ -1648,7 +1647,7 @@ impl PlacementState {
let sequential_layout_state = sequential_layout_state
.expect("Found float fragment without SequentialLayoutState");
let block_offset_from_containing_block_top =
self.current_block_direction_position + self.current_margin.solve();
self.current_block_direction_position + self.current_margin.solve().into();
sequential_layout_state.place_float_fragment(
box_fragment,
self.start_margin,
Expand All @@ -1662,7 +1661,7 @@ impl PlacementState {

fn finish(mut self) -> (Length, CollapsedBlockMargins, Baselines) {
if !self.last_in_flow_margin_collapses_with_parent_end_margin {
self.current_block_direction_position += self.current_margin.solve();
self.current_block_direction_position += self.current_margin.solve().into();
self.current_margin = CollapsedMargin::zero();
}
(
Expand Down

0 comments on commit 5d518ca

Please sign in to comment.