use crate::kurbo::Size;
use log;
#[derive(Clone, Copy, Debug)]
pub struct BoxConstraints {
min: Size,
max: Size,
}
impl BoxConstraints {
pub fn new(min: Size, max: Size) -> BoxConstraints {
BoxConstraints {
min: min.expand(),
max: max.expand(),
}
}
pub fn tight(size: Size) -> BoxConstraints {
let size = size.expand();
BoxConstraints {
min: size,
max: size,
}
}
pub fn loosen(&self) -> BoxConstraints {
BoxConstraints {
min: Size::ZERO,
max: self.max,
}
}
pub fn constrain(&self, size: impl Into<Size>) -> Size {
size.into().expand().clamp(self.min, self.max)
}
pub fn max(&self) -> Size {
self.max
}
pub fn min(&self) -> Size {
self.min
}
pub fn is_width_bounded(&self) -> bool {
self.max.width.is_finite()
}
pub fn is_height_bounded(&self) -> bool {
self.max.height.is_finite()
}
pub fn debug_check(&self, name: &str) {
if !(0.0 <= self.min.width
&& self.min.width <= self.max.width
&& 0.0 <= self.min.height
&& self.min.height <= self.max.height
&& self.min.expand() == self.min
&& self.max.expand() == self.max)
{
log::warn!("Bad BoxConstraints passed to {}:", name);
log::warn!("{:?}", self);
}
if self.min.width.is_infinite() {
log::warn!("Infinite minimum width constraint passed to {}:", name);
}
if self.min.height.is_infinite() {
log::warn!("Infinite minimum height constraint passed to {}:", name);
}
}
pub fn shrink(&self, diff: impl Into<Size>) -> BoxConstraints {
let diff = diff.into().expand();
let min = Size::new(
(self.min().width - diff.width).max(0.),
(self.min().height - diff.height).max(0.),
);
let max = Size::new(
(self.max().width - diff.width).max(0.),
(self.max().height - diff.height).max(0.),
);
BoxConstraints::new(min, max)
}
}