#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
pub enum LayoutType {
Row,
#[default]
Column,
Grid,
}
impl LayoutType {
pub(crate) fn select<T: Default, S>(
&self,
s: S,
first: impl FnOnce(S) -> Option<T>,
second: impl FnOnce(S) -> Option<T>,
) -> Option<T> {
match self {
LayoutType::Row => first(s),
LayoutType::Column | LayoutType::Grid => second(s),
}
}
pub(crate) fn select_unwrap<T: Default, S>(
&self,
s: S,
first: impl FnOnce(S) -> Option<T>,
second: impl FnOnce(S) -> Option<T>,
) -> T {
match self {
LayoutType::Row => first(s).unwrap_or_default(),
LayoutType::Column | LayoutType::Grid => second(s).unwrap_or_default(),
}
}
pub(crate) fn select_unwrap_default<T, S>(
&self,
s: S,
first: impl FnOnce(S) -> Option<T>,
second: impl FnOnce(S) -> Option<T>,
default: T,
) -> T {
match self {
LayoutType::Row => first(s).unwrap_or(default),
LayoutType::Column | LayoutType::Grid => second(s).unwrap_or(default),
}
}
}
impl std::fmt::Display for LayoutType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LayoutType::Column => write!(f, "column"),
LayoutType::Row => write!(f, "row"),
LayoutType::Grid => write!(f, "grid"),
}
}
}
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
pub enum PositionType {
Absolute,
#[default]
Relative,
}
impl std::fmt::Display for PositionType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
PositionType::Absolute => write!(f, "absolute"),
PositionType::Relative => write!(f, "relative"),
}
}
}
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
pub enum Direction {
#[default]
LeftToRight,
RightToLeft,
}
impl std::fmt::Display for Direction {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Direction::LeftToRight => write!(f, "ltr"),
Direction::RightToLeft => write!(f, "rtl"),
}
}
}
#[derive(Default, Debug, Clone, Copy, PartialEq)]
pub enum Units {
Pixels(f32),
Percentage(f32),
Stretch(f32),
#[default]
Auto,
}
impl Units {
pub fn to_px(&self, parent_value: f32, default: f32) -> f32 {
match self {
Units::Pixels(pixels) => *pixels,
Units::Percentage(percentage) => (percentage / 100.0) * parent_value,
Units::Stretch(_) => default,
Units::Auto => default,
}
}
pub fn to_px_clamped(&self, parent_value: f32, default: f32, min: Units, max: Units) -> f32 {
let min = min.to_px(parent_value, f32::MIN);
let max = max.to_px(parent_value, f32::MAX);
match self {
Units::Pixels(pixels) => pixels.min(max).max(min),
Units::Percentage(percentage) => ((percentage / 100.0) * parent_value).min(max).max(min),
Units::Stretch(_) => default.min(max).max(min),
Units::Auto => default.min(max).max(min),
}
}
pub fn clamp(&self, min: Units, max: Units) -> Self {
match (self, min, max) {
(Units::Pixels(val), Units::Pixels(min), Units::Pixels(max)) => Units::Pixels(val.min(max).max(min)),
(Units::Percentage(val), Units::Percentage(min), Units::Percentage(max)) => {
Units::Percentage(val.min(max).max(min))
}
(Units::Stretch(val), Units::Stretch(min), Units::Stretch(max)) => Units::Stretch(val.min(max).max(min)),
_ => *self,
}
}
pub fn is_pixels(&self) -> bool {
matches!(self, Units::Pixels(_))
}
pub fn is_percentage(&self) -> bool {
matches!(self, Units::Percentage(_))
}
pub fn is_stretch(&self) -> bool {
matches!(self, Units::Stretch(_))
}
pub fn is_auto(&self) -> bool {
self == &Units::Auto
}
}
impl std::fmt::Display for Units {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Units::Auto => write!(f, "auto"),
Units::Pixels(p) => write!(f, "{}px", p),
Units::Percentage(p) => write!(f, "{}%", p),
Units::Stretch(s) => write!(f, "{}s", s),
}
}
}
#[derive(Default, Debug, Copy, Clone, PartialEq)]
pub enum Alignment {
#[default]
TopLeft,
TopCenter,
TopRight,
Left,
Center,
Right,
BottomLeft,
BottomCenter,
BottomRight,
}
impl std::fmt::Display for Alignment {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Alignment::BottomCenter => write!(f, "bottom-center"),
Alignment::TopLeft => write!(f, "top-left"),
Alignment::TopCenter => write!(f, "top-center"),
Alignment::TopRight => write!(f, "top-right"),
Alignment::Left => write!(f, "left"),
Alignment::Center => write!(f, "center"),
Alignment::Right => write!(f, "right"),
Alignment::BottomLeft => write!(f, "bottom-left"),
Alignment::BottomRight => write!(f, "bottom-right"),
}
}
}
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
pub enum LayoutWrap {
#[default]
NoWrap,
Wrap,
}
impl std::fmt::Display for LayoutWrap {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LayoutWrap::NoWrap => write!(f, "no-wrap"),
LayoutWrap::Wrap => write!(f, "wrap"),
}
}
}
#[derive(Default, Debug, Copy, Clone, PartialEq)]
pub struct Size {
pub main: f32,
pub cross: f32,
}