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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use crate::{color::Color, unit::*, Style, StyleUpdater};
use derive_rich::Rich;

/// ```
/// use css_style::{Style, Color, unit::em};
///
/// Style::default()
///     .and_background(|conf| {
///         conf.image("/bg/fullpage.png")
///             .color(Color::White)
///             .scroll()
///     });
/// ```
#[derive(Rich, Clone, Debug, PartialEq, Default)]
pub struct Background {
    #[rich(write)]
    pub color: Option<Color>,
    // TODO: support multiple images
    #[rich(write, value_fns = { empty = Image::None })]
    pub image: Option<Image>,
    #[rich(value_fns = {
        repeat_x = Repeat::RepeatX,
        repeat_y = Repeat::RepeatY,
        repeat = Repeat::Repeat,
        repeat_with_space = Repeat::Space,
        repeat_round = Repeat::Round,
        no_repeat = Repeat::NoRepeat,
        initial_repeat = Repeat::Initial,
        inherit_repeat = Repeat::Inherit,
    })]
    pub repeat: Option<Repeat>,
    #[rich(write(rename = attachment), value_fns = {
        scroll = Attachment::Scroll,
        fixed = Attachment::Fixed,
        local = Attachment::Local,
        initial_attachment = Attachment::Initial,
        inherit_attachment = Attachment::Inherit,
    })]
    // #[derive(Clone, Debug, PartialEq, Display, From)]
    // pub enum Position {
    //     #[display(fmt = "{} {}", _0, _1)]
    //     #[from]
    //     Percent(Percent, Percent),
    //     #[display(fmt = "{} {}", _0, _1)]
    //     #[from]
    //     Placement(Horizontal, Vertical),
    // }
    pub attachment: Option<Attachment>,
    #[rich(write(rename = position), value_fns = {
        center_top = (Horizontal::Center, Vertical::Top(None)),
        center = (Horizontal::Center, Vertical::Center),
        center_bottom = (Horizontal::Center, Vertical::Bottom(None)),
        left_top = (Horizontal::Left(None), Vertical::Top(None)),
        left_center = (Horizontal::Left(None), Vertical::Center),
        left_bottom = (Horizontal::Left(None), Vertical::Bottom(None)),
        right_top = (Horizontal::Right(None), Vertical::Top(None)),
        right_center = (Horizontal::Right(None), Vertical::Center),
        right_bottom = (Horizontal::Right(None), Vertical::Bottom(None)),
    })]
    pub position: Option<Position>,
    #[rich(write(rename = clip), value_fns = {
        fill_under_border = Clip::BorderBox,
        fill_inside_border = Clip::PaddingBox,
        fill_under_content = Clip::ContentBox,
    })]
    pub clip: Option<Clip>,
    #[rich(write(rename = origin), value_fns = {
        image_fill_under_border = Origin::BorderBox,
        image_inside_border = Origin::PaddingBox,
        image_under_content = Origin::ContentBox,
    })]
    pub origin: Option<Origin>,
    #[rich(write(rename = size), value_fns = {
        full = (1.0, 1.0),
        half = (0.5, 0.5),
        quarter = (0.25, 0.25),
        auto_size = Size::Auto,
        cover = Size::Cover,
        contain = Size::Contain,
    })]
    pub size: Option<Size>,
}

impl<T: Into<Color>> From<T> for Background {
    fn from(source: T) -> Self {
        Background::default().color(source.into())
    }
}

impl StyleUpdater for Background {
    fn update_style(self, style: Style) -> Style {
        style
            .try_insert("background-color", self.color)
            .try_insert("background-image", self.image.as_ref())
            .try_insert("background-repeat", self.repeat)
            .try_insert("background-attachment", self.attachment)
            .try_insert("background-position", self.position)
    }
}

#[derive(Clone, Debug, PartialEq, Display, From)]
pub enum Image {
    #[display(fmt = "none")]
    None,
    #[display(fmt = "url({})", _0)]
    #[from(forward)]
    Url(String),
}

#[derive(Clone, Copy, Debug, PartialEq, Display, From)]
pub enum Repeat {
    #[display(fmt = "repeat-x")]
    RepeatX,
    #[display(fmt = "repeat-y")]
    RepeatY,
    #[display(fmt = "repeat")]
    Repeat,
    #[display(fmt = "space")]
    Space,
    #[display(fmt = "round")]
    Round,
    #[display(fmt = "no-repeat")]
    NoRepeat,
    #[display(fmt = "initial")]
    Initial,
    #[display(fmt = "inherit")]
    Inherit,
}

#[derive(Clone, Copy, Debug, PartialEq, Display, From)]
pub enum Attachment {
    #[display(fmt = "scroll")]
    Scroll,
    #[display(fmt = "fixed")]
    Fixed,
    #[display(fmt = "local")]
    Local,
    #[display(fmt = "initial")]
    Initial,
    #[display(fmt = "inherit")]
    Inherit,
}

fn display_helper(v: &Option<impl ToString>) -> String {
    v.as_ref()
        .map_or("".into(), |s| format!(" {}", s.to_string()))
}

#[derive(Clone, Debug, PartialEq, Display)]
pub enum Horizontal {
    #[display(fmt = "center")]
    Center,
    #[display(fmt = "left{}", "display_helper(_0)")]
    Left(Option<Length>),
    #[display(fmt = "right{}", "display_helper(_0)")]
    Right(Option<Length>),
}

#[derive(Clone, Debug, PartialEq, Display)]
pub enum Vertical {
    #[display(fmt = "center")]
    Center,
    #[display(fmt = "top{}", "display_helper(_0)")]
    Top(Option<Length>),
    #[display(fmt = "bottom{}", "display_helper(_0)")]
    Bottom(Option<Length>),
}

#[derive(Clone, Debug, PartialEq, Display, From)]
pub enum Position {
    #[display(fmt = "{} {}", _0, _1)]
    #[from]
    Percent(Percent, Percent),
    #[display(fmt = "{} {}", _0, _1)]
    #[from]
    Placement(Horizontal, Vertical),
}

impl From<(f32, f32)> for Position {
    fn from((hor, ver): (f32, f32)) -> Self {
        Position::Percent(hor.into(), ver.into())
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Display, From)]
pub enum Box {
    #[display(fmt = "border-box")]
    BorderBox,
    #[display(fmt = "padding-box")]
    PaddingBox,
    #[display(fmt = "content-box")]
    ContentBox,
    #[display(fmt = "initial")]
    Initial,
    #[display(fmt = "inherit")]
    Inherit,
}

pub type Clip = Box;
pub type Origin = Box;

#[derive(Clone, Debug, PartialEq, Display, From)]
pub enum Size {
    #[display(fmt = "{} {}", _0, _1)]
    WidthHeight(LengthPercent, LengthPercent),
    #[display(fmt = "auto")]
    Auto,
    #[display(fmt = "cover")]
    Cover,
    #[display(fmt = "contain")]
    Contain,
    #[display(fmt = "initial")]
    Initial,
}

impl From<(f32, f32)> for Size {
    fn from((width, height): (f32, f32)) -> Self {
        Self::WidthHeight(width.into(), height.into())
    }
}