use embedded_graphics::{
prelude::{Point, Size},
primitives::Rectangle,
};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct Constraints {
min: Size,
max: Size,
}
impl Constraints {
pub(crate) const fn exact(size: Size) -> Self {
Self { min: size, max: size }
}
pub(crate) const fn max(size: Size) -> Self {
Self { min: Size::zero(), max: size }
}
pub(crate) fn constrain(self, desired: Size) -> Size {
Size::new(
desired.width.clamp(self.min.width, self.max.width),
desired.height.clamp(self.min.height, self.max.height),
)
}
pub(crate) const fn loosen(self) -> Self {
Self { min: Size::zero(), max: self.max }
}
pub(crate) const fn deflate(self, by: Size) -> Self {
Self { min: self.min.saturating_sub(by), max: self.max.saturating_sub(by) }
}
}
#[derive(PartialEq, Eq)]
pub(crate) struct Layout {
pub(crate) offset: Point,
pub(crate) border_offset: Point,
pub(crate) content_offset: Point,
pub(crate) outer_size: Size,
pub(crate) border_size: Size,
pub(crate) content_size: Size,
}
impl Layout {
pub(crate) const fn empty() -> Self {
Self {
offset: Point::new(0, 0),
border_offset: Point::new(0, 0),
content_offset: Point::new(0, 0),
outer_size: Size::zero(),
border_size: Size::zero(),
content_size: Size::zero(),
}
}
pub(crate) const fn set_offset(&mut self, offset: Point) {
self.offset = offset;
}
pub(crate) fn resolve(&self, parent_origin: Point) -> BoxLayout {
let outer_origin = parent_origin + self.offset;
let border_origin = outer_origin + self.border_offset;
let content_origin = outer_origin + self.content_offset;
BoxLayout {
border: Rectangle::new(border_origin, self.border_size),
content: Rectangle::new(content_origin, self.content_size),
}
}
}
pub(crate) struct BoxLayout {
pub(crate) border: Rectangle,
pub(crate) content: Rectangle,
}