use crate::{
environment::LayoutEnvironment,
primitives::{Dimensions, ProposedDimension, ProposedDimensions},
};
#[derive(Clone, Copy, Debug, PartialEq, Default)]
pub enum LayoutDirection {
#[default]
Horizontal,
Vertical,
}
#[derive(Clone, Copy, Debug, PartialEq, Default)]
pub struct Alignment {
pub horizontal: HorizontalAlignment,
pub vertical: VerticalAlignment,
}
#[derive(Clone, Copy, Debug, PartialEq, Default)]
pub enum HorizontalAlignment {
Leading,
#[default]
Center,
Trailing,
}
impl HorizontalAlignment {
#[must_use]
pub fn align(&self, available: i16, content: i16) -> i16 {
match self {
HorizontalAlignment::Leading => 0,
HorizontalAlignment::Center => (available - content) / 2,
HorizontalAlignment::Trailing => available - content,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Default)]
pub enum VerticalAlignment {
Top,
#[default]
Center,
Bottom,
}
impl VerticalAlignment {
#[must_use]
pub fn align(&self, available: i16, content: i16) -> i16 {
match self {
VerticalAlignment::Top => 0,
VerticalAlignment::Center => (available - content) / 2,
VerticalAlignment::Bottom => available - content,
}
}
}
#[derive(Clone, PartialEq)]
pub struct ResolvedLayout<C: Clone + PartialEq> {
pub sublayouts: C,
pub resolved_size: Dimensions,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Axis {
FixedWidth(u16),
FixedHeight(u16),
}
impl Axis {
#[must_use]
pub fn into_min_proposal(self) -> ProposedDimensions {
match self {
Axis::FixedWidth(w) => ProposedDimensions {
width: ProposedDimension::Exact(w),
height: ProposedDimension::Exact(0),
},
Axis::FixedHeight(h) => ProposedDimensions {
width: ProposedDimension::Exact(0),
height: ProposedDimension::Exact(h),
},
}
}
#[must_use]
pub fn into_max_proposal(self) -> ProposedDimensions {
match self {
Axis::FixedWidth(w) => ProposedDimensions {
width: ProposedDimension::Exact(w),
height: ProposedDimension::Infinite,
},
Axis::FixedHeight(h) => ProposedDimensions {
width: ProposedDimension::Infinite,
height: ProposedDimension::Exact(h),
},
}
}
}
pub trait Layout: Sized {
type Sublayout: Clone + PartialEq;
fn layout(
&self,
offer: &ProposedDimensions,
env: &impl LayoutEnvironment,
) -> ResolvedLayout<Self::Sublayout>;
fn priority(&self) -> i8 {
0
}
fn is_empty(&self) -> bool {
false
}
}