Compute size of the axes of flex containers

Compute the available main and cross size of flex containers, and add a
helper for min/main constraints
This commit is contained in:
Daniel Robertson 2016-03-31 22:15:04 -04:00
parent 436f7316d9
commit 7946ebb36a
2 changed files with 156 additions and 30 deletions

View file

@ -478,3 +478,67 @@ impl ToGfxMatrix for ComputedMatrix {
}
}
}
// https://drafts.csswg.org/css2/visudet.html#min-max-widths
// https://drafts.csswg.org/css2/visudet.html#min-max-heights
/// A min or max constraint
///
/// A `max` of `None` is equivalent to no limmit for the size in the given
/// dimension. The `min` is >= 0, as negative values are illegal and by
/// default `min` is 0.
#[derive(Debug)]
pub struct MinMaxConstraint {
min: Au,
max: Option<Au>
}
impl MinMaxConstraint {
/// Create a `MinMaxConstraint` for a dimension given the min, max, and content box size for
/// an axis
pub fn new(content_size: Option<Au>, min: LengthOrPercentage,
max: LengthOrPercentageOrNone) -> MinMaxConstraint {
let min = match min {
LengthOrPercentage::Length(length) => length,
LengthOrPercentage::Percentage(percent) => {
match content_size {
Some(size) => size.scale_by(percent),
None => Au(0),
}
},
LengthOrPercentage::Calc(calc) => {
match content_size {
Some(size) => size.scale_by(calc.percentage()),
None => Au(0),
}
}
};
let max = match max {
LengthOrPercentageOrNone::Length(length) => Some(length),
LengthOrPercentageOrNone::Percentage(percent) => {
content_size.map(|size| size.scale_by(percent))
},
LengthOrPercentageOrNone::Calc(calc) => {
content_size.map(|size| size.scale_by(calc.percentage()))
}
LengthOrPercentageOrNone::None => None,
};
MinMaxConstraint {
min: min,
max: max
}
}
/// Clamp the given size by the given `min` and `max` constraints.
pub fn clamp(&self, other: Au) -> Au {
if other < self.min {
self.min
} else {
match self.max {
Some(max) if max < other => max,
_ => other
}
}
}
}