use super::range::Range;
use crate::core::Pixels;
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Length {
Fixed(f32),
Fill,
FillRange(Range),
FillPortion(u16),
FillPortionRange(u16, Range),
Shrink,
ShrinkRange(Range),
}
impl Length {
pub fn fill_factor(&self) -> u16 {
match self {
Length::Fill | Length::FillRange(_) => 1,
Length::FillPortion(factor) | Length::FillPortionRange(factor, _) => *factor,
Length::Shrink | Length::ShrinkRange(_) | Length::Fixed(_) => 0,
}
}
pub fn is_fill(&self) -> bool {
self.fill_factor() != 0
}
pub fn fluid(&self) -> Self {
match self {
Length::Fill
| Length::FillRange(_)
| Length::FillPortion(_)
| Length::FillPortionRange(_, _) => Length::Fill,
Length::Shrink | Length::ShrinkRange(_) | Length::Fixed(_) => Length::Shrink,
}
}
pub fn enclose(self, other: Length) -> Self {
match (self, other) {
(
Length::Shrink | Length::ShrinkRange(_),
Length::Fill
| Length::FillRange(_)
| Length::FillPortion(_)
| Length::FillPortionRange(_, _),
) => other,
_ => self,
}
}
}
impl From<Pixels> for Length {
fn from(amount: Pixels) -> Self {
Length::Fixed(f32::from(amount))
}
}
impl From<f32> for Length {
fn from(amount: f32) -> Self {
Length::Fixed(amount)
}
}
impl From<u16> for Length {
fn from(units: u16) -> Self {
Length::Fixed(f32::from(units))
}
}
impl From<crate::core::Length> for Length {
fn from(length: crate::core::Length) -> Self {
match length {
crate::core::Length::Shrink => Length::Shrink,
crate::core::Length::Fixed(value) => Length::Fixed(value),
crate::core::Length::Fill => Length::Fill,
crate::core::Length::FillPortion(value) => Length::FillPortion(value),
}
}
}
impl From<Length> for crate::core::Length {
fn from(length: Length) -> Self {
match length {
Length::Shrink | Length::ShrinkRange(_) => crate::core::Length::Shrink,
Length::Fixed(value) => crate::core::Length::Fixed(value),
Length::Fill | Length::FillRange(_) => crate::core::Length::Fill,
Length::FillPortion(value) | Length::FillPortionRange(value, _) => {
crate::core::Length::FillPortion(value)
}
}
}
}