Skip to content

Commit

Permalink
Move clamp_* functions to methods of Length
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin committed Dec 10, 2019
1 parent 8a7de32 commit a2c2b29
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
32 changes: 11 additions & 21 deletions components/layout_2020/flow/mod.rs
Expand Up @@ -411,7 +411,7 @@ fn layout_in_flow_non_replaced_block_level<'a>(
// https://drafts.csswg.org/css2/visudet.html#min-max-heights
let mut block_size = box_size.block;
if let LengthOrAuto::LengthPercentage(ref mut block_size) = block_size {
*block_size = clamp_between_extremums(*block_size, min_box_size.block, max_box_size.block);
*block_size = block_size.clamp_between_extremums(min_box_size.block, max_box_size.block);
}

let containing_block_for_children = ContainingBlock {
Expand Down Expand Up @@ -475,11 +475,9 @@ fn layout_in_flow_non_replaced_block_level<'a>(
.collapsed_through;
let relative_adjustement = relative_adjustement(style, inline_size, block_size);
let block_size = block_size.auto_is(|| {
clamp_between_extremums(
flow_layout.content_block_size,
min_box_size.block,
max_box_size.block,
)
flow_layout
.content_block_size
.clamp_between_extremums(min_box_size.block, max_box_size.block)
});
let content_rect = Rect {
start_corner: Vec2 {
Expand Down Expand Up @@ -536,10 +534,10 @@ fn layout_in_flow_replaced_block_level<'a>(
percent_resolved_box_size(style.min_box_size(), containing_block).auto_is(Length::zero);
let max_box_size = percent_resolved_max_box_size(style.max_box_size(), containing_block);

let clamp = |inline_size, block_size| {
let clamp = |inline_size: Length, block_size: Length| {
(
clamp_between_extremums(inline_size, min_box_size.inline, max_box_size.inline),
clamp_between_extremums(block_size, min_box_size.block, max_box_size.block),
inline_size.clamp_between_extremums(min_box_size.inline, max_box_size.inline),
block_size.clamp_between_extremums(min_box_size.block, max_box_size.block),
)
};
// https://drafts.csswg.org/css2/visudet.html#min-max-widths
Expand Down Expand Up @@ -590,7 +588,7 @@ fn layout_in_flow_replaced_block_level<'a>(
// Row 3.
(Violation::Below(min_inline_size), Violation::None) => {
let block_size =
clamp_below_max(min_inline_size / intrinsic_ratio, max_box_size.block);
(min_inline_size / intrinsic_ratio).clamp_below_max(max_box_size.block);
(min_inline_size, block_size)
},
// Row 4.
Expand All @@ -601,7 +599,7 @@ fn layout_in_flow_replaced_block_level<'a>(
// Row 5.
(Violation::None, Violation::Below(min_block_size)) => {
let inline_size =
clamp_below_max(min_block_size * intrinsic_ratio, max_box_size.inline);
(min_block_size * intrinsic_ratio).clamp_below_max(max_box_size.inline);
(inline_size, min_block_size)
},
// Rows 6-7.
Expand All @@ -627,12 +625,12 @@ fn layout_in_flow_replaced_block_level<'a>(
{
// Row 8.
let inline_size =
clamp_below_max(min_block_size * intrinsic_ratio, max_box_size.inline);
(min_block_size * intrinsic_ratio).clamp_below_max(max_box_size.inline);
(inline_size, min_block_size)
} else {
// Row 9.
let block_size =
clamp_below_max(min_inline_size / intrinsic_ratio, max_box_size.block);
(min_inline_size / intrinsic_ratio).clamp_below_max(max_box_size.block);
(min_inline_size, block_size)
}
},
Expand Down Expand Up @@ -704,14 +702,6 @@ fn solve_inline_margins_for_in_flow_block_level(
}
}

fn clamp_between_extremums(size: Length, min_size: Length, max_size: Option<Length>) -> Length {
clamp_below_max(size, max_size).max(min_size)
}

fn clamp_below_max(size: Length, max_size: Option<Length>) -> Length {
max_size.map_or(size, |max_size| size.min(max_size))
}

fn percent_resolved_box_size(
box_size: Vec2<LengthPercentageOrAuto>,
containing_block: &ContainingBlock,
Expand Down
26 changes: 23 additions & 3 deletions components/style/values/computed/length.rs
Expand Up @@ -657,14 +657,14 @@ impl CSSPixelLength {

/// Return the containing pixel value.
#[inline]
pub fn px(&self) -> CSSFloat {
pub fn px(self) -> CSSFloat {
self.0
}

/// Return the length with app_unit i32 type.
#[inline]
pub fn to_i32_au(&self) -> i32 {
Au::from(*self).0
pub fn to_i32_au(self) -> i32 {
Au::from(self).0
}

/// Return the absolute value of this length.
Expand Down Expand Up @@ -692,9 +692,29 @@ impl CSSPixelLength {
}

/// Sets `self` to the maximum between `self` and `other`.
#[inline]
pub fn max_assign(&mut self, other: Self) {
*self = self.max(other);
}

/// Clamp the value to a lower bound and an optional upper bound.
///
/// Can be used for example with `min-width` and `max-width`.
#[inline]
pub fn clamp_between_extremums(self, min_size: Self, max_size: Option<Self>) -> Self {
self.clamp_below_max(max_size).max(min_size)
}

/// Clamp the value to an optional upper bound.
///
/// Can be used for example with `max-width`.
#[inline]
pub fn clamp_below_max(self, max_size: Option<Self>) -> Self {
match max_size {
None => self,
Some(max_size) => self.min(max_size),
}
}
}

impl Zero for CSSPixelLength {
Expand Down

0 comments on commit a2c2b29

Please sign in to comment.