use kurbo::Axis;
use crate::{
core::{Property, UsesProperty, Widget},
layout::{Dim, Length},
};
impl<W: Widget> UsesProperty<Dimensions> for W {}
#[derive(Copy, Clone, Debug, Default, PartialEq)]
pub struct Dimensions {
width: Dim,
height: Dim,
}
impl Property for Dimensions {
fn static_default() -> &'static Self {
static DEFAULT: Dimensions = Dimensions::AUTO;
&DEFAULT
}
}
impl From<Dim> for Dimensions {
fn from(dim: Dim) -> Self {
Self::new(dim, dim)
}
}
impl From<(Dim, Dim)> for Dimensions {
fn from(dims: (Dim, Dim)) -> Self {
Self::new(dims.0, dims.1)
}
}
impl From<Length> for Dimensions {
fn from(value: Length) -> Self {
Self::fixed(value, value)
}
}
impl From<(Length, Length)> for Dimensions {
fn from(value: (Length, Length)) -> Self {
Self::fixed(value.0, value.1)
}
}
impl From<(Length, Dim)> for Dimensions {
fn from(value: (Length, Dim)) -> Self {
Self::new(Dim::Fixed(value.0), value.1)
}
}
impl From<(Dim, Length)> for Dimensions {
fn from(value: (Dim, Length)) -> Self {
Self::new(value.0, Dim::Fixed(value.1))
}
}
impl Dimensions {
pub const AUTO: Self = Self::new(Dim::Auto, Dim::Auto);
pub const STRETCH: Self = Self::new(Dim::Stretch, Dim::Stretch);
pub const MIN: Self = Self::new(Dim::MinContent, Dim::MinContent);
pub const MAX: Self = Self::new(Dim::MaxContent, Dim::MaxContent);
pub const FIT: Self = Self::new(Dim::FitContent, Dim::FitContent);
pub const fn new(width: Dim, height: Dim) -> Self {
Self { width, height }
}
pub const fn fixed(width: Length, height: Length) -> Self {
Self {
width: Dim::Fixed(width),
height: Dim::Fixed(height),
}
}
pub const fn ratio(width_ratio: f64, height_ratio: f64) -> Self {
Self {
width: Dim::Ratio(width_ratio),
height: Dim::Ratio(height_ratio),
}
}
pub fn width(dim: impl Into<Dim>) -> Self {
Self {
width: dim.into(),
height: Dim::Auto,
}
}
pub fn height(dim: impl Into<Dim>) -> Self {
Self {
width: Dim::Auto,
height: dim.into(),
}
}
pub fn with(self, axis: Axis, dim: impl Into<Dim>) -> Self {
match axis {
Axis::Horizontal => self.with_width(dim),
Axis::Vertical => self.with_height(dim),
}
}
pub fn with_width(mut self, dim: impl Into<Dim>) -> Self {
self.width = dim.into();
self
}
pub fn with_height(mut self, dim: impl Into<Dim>) -> Self {
self.height = dim.into();
self
}
pub const fn dim(&self, axis: Axis) -> Dim {
match axis {
Axis::Horizontal => self.width,
Axis::Vertical => self.height,
}
}
}