use embedded_graphics::prelude::*;
pub const UNBOUNDED: u32 = u16::MAX as u32;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Constraints {
pub min: Size,
pub max: Size,
}
impl Constraints {
#[must_use]
pub const fn loose(max: Size) -> Self {
Self {
min: Size::zero(),
max,
}
}
#[must_use]
pub const fn tight(size: Size) -> Self {
Self {
min: size,
max: size,
}
}
#[must_use]
pub const fn new(min: Size, max: Size) -> Self {
Self { min, max }
}
#[must_use]
pub const fn unbounded() -> Self {
Self {
min: Size::zero(),
max: Size::new(UNBOUNDED, UNBOUNDED),
}
}
#[must_use]
pub fn clamp(&self, size: Size) -> Size {
Size::new(
size.width.clamp(self.min.width, self.max.width),
size.height.clamp(self.min.height, self.max.height),
)
}
#[must_use]
pub fn with_width(self, w: u32) -> Self {
Self {
min: Size::new(self.min.width.min(w), self.min.height),
max: Size::new(w, self.max.height),
}
}
#[must_use]
pub fn with_height(self, h: u32) -> Self {
Self {
min: Size::new(self.min.width, self.min.height.min(h)),
max: Size::new(self.max.width, h),
}
}
#[must_use]
pub fn shrink(self, dx: u32, dy: u32) -> Self {
Self {
min: Size::new(
self.min.width.saturating_sub(dx),
self.min.height.saturating_sub(dy),
),
max: Size::new(
self.max.width.saturating_sub(dx),
self.max.height.saturating_sub(dy),
),
}
}
}
impl Default for Constraints {
fn default() -> Self {
Self::unbounded()
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Length {
Fixed(u32),
Shrink,
Fill,
FillPortion(u32),
}
impl Length {
#[must_use]
pub fn fixed(self) -> Option<u32> {
match self {
Length::Fixed(n) => Some(n),
_ => None,
}
}
#[must_use]
pub fn portion(self) -> u32 {
match self {
Length::Fill => 1,
Length::FillPortion(p) => p.max(1),
_ => 0,
}
}
#[must_use]
pub fn resolve(self, intrinsic: u32, max: u32) -> u32 {
match self {
Length::Fixed(n) => n.min(max),
Length::Shrink => intrinsic.min(max),
Length::Fill | Length::FillPortion(_) => max,
}
}
}
impl From<u32> for Length {
fn from(n: u32) -> Self {
Length::Fixed(n)
}
}
impl From<u16> for Length {
fn from(n: u16) -> Self {
Length::Fixed(n as u32)
}
}
impl From<i32> for Length {
fn from(n: i32) -> Self {
Length::Fixed(n.max(0) as u32)
}
}
impl From<usize> for Length {
fn from(n: usize) -> Self {
Length::Fixed(n.min(u32::MAX as usize) as u32)
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Horizontal {
Left,
Center,
Right,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Vertical {
Top,
Center,
Bottom,
}