1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use crate::{
    calc::Calc,
    css,
    unit::{self, *},
    Style, StyleUpdater,
};
use derive_rich::Rich;

/// ```
/// use css_style::{Style, unit::em};
///
/// Style::default()
///     .and_size(|conf| {
///         conf.width(em(2.))
///             .height(em(1.5))
///             .min_width(em(1.5))
///             .min_height(em(1.))
///             .max_width(em(4.))
///             .max_height(em(3.))
///     });
/// ```
#[derive(Rich, Clone, Debug, PartialEq, Default)]
pub struct Size {
    #[rich(write, write(option))]
    pub width: Option<Length>,
    #[rich(write, write(option))]
    pub min_width: Option<Length>,
    #[rich(write, write(option))]
    pub max_width: Option<Length>,
    #[rich(write, write(option))]
    pub height: Option<Length>,
    #[rich(write, write(option))]
    pub min_height: Option<Length>,
    #[rich(write, write(option))]
    pub max_height: Option<Length>,
}

impl StyleUpdater for Size {
    fn update_style(self, style: Style) -> Style {
        style
            .try_insert("width", self.width)
            .try_insert("min-width", self.min_width)
            .try_insert("max-width", self.max_width)
            .try_insert("height", self.height)
            .try_insert("min-height", self.min_height)
            .try_insert("max-height", self.max_height)
    }
}

impl From<Length> for Size {
    fn from(source: Length) -> Self {
        Self::default().all(source)
    }
}

impl From<unit::Length> for Size {
    fn from(source: unit::Length) -> Self {
        Self::default().all(source)
    }
}

impl From<Percent> for Size {
    fn from(source: Percent) -> Self {
        Self::default().all(source)
    }
}

impl From<f32> for Size {
    fn from(source: f32) -> Self {
        Self::default().all(source)
    }
}

impl Size {
    pub fn full(self) -> Self {
        self.width(1.0).height(1.0)
    }

    pub fn half(self) -> Self {
        self.width(0.5).height(0.5)
    }

    pub fn min_content(self) -> Self {
        self.width(css::MinContent).height(css::MinContent)
    }

    pub fn max_content(self) -> Self {
        self.width(css::MaxContent).height(css::MaxContent)
    }

    pub fn auto(self) -> Self {
        self.width(css::Auto).height(css::Auto)
    }

    pub fn resize(self, width: impl Into<Length>, height: impl Into<Length>) -> Self {
        self.width(width).height(height)
    }

    pub fn all(self, val: impl Into<Length>) -> Self {
        let val = val.into();
        self.all_widths(val.clone()).all_heights(val)
    }

    pub fn all_widths(self, width: impl Into<Length>) -> Self {
        let width = width.into();
        self.width(width.clone())
            .min_width(width.clone())
            .max_width(width)
    }

    pub fn all_heights(self, val: impl Into<Length>) -> Self {
        let val = val.into();
        self.height(val.clone())
            .min_height(val.clone())
            .max_height(val)
    }
}

// https://www.w3.org/TR/css-values-4/#lengths
#[derive(Clone, Debug, PartialEq, Display, From)]
pub enum Length {
    #[from]
    Auto(css::Auto),
    #[from]
    MinContent(css::MinContent),
    #[from]
    MaxContent(css::MaxContent),
    #[from]
    Length(unit::Length),
    #[from(forward)]
    Percent(Percent),
    Calc(Box<Calc<Self>>),
}

impl From<Calc<Self>> for Length {
    fn from(source: Calc<Self>) -> Self {
        Self::Calc(Box::new(source))
    }
}