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
use crate::css::{unit::*, values as val, St, StyleValues, UpdateStyleValues};
use derive_rich::Rich;

/// ```
/// use savory_html::css::{Style, unit::px};
///
/// Style::default()
///     .and_position(|conf| {
///         conf.absolute().top(px(28)).left(px(40))
///     });
/// ```
#[derive(Rich, Clone, Copy, Debug, PartialEq, Default)]
pub struct Position {
    #[rich(write(rename = position), write(option, rename = try_position), value_fns = {
        static_ = val::Static,
        absolute = val::Absolute,
        fixed = val::Fixed,
        relative = val::Relative,
        sticky = val::Sticky,
        initial = val::Initial,
        inherit = val::Inherit,
    })]
    pub position: Option<PositionType>,
    #[rich(write, write(option))]
    pub left: Option<PostionLength>,
    #[rich(write, write(option))]
    pub top: Option<PostionLength>,
    #[rich(write, write(option))]
    pub right: Option<PostionLength>,
    #[rich(write, write(option))]
    pub bottom: Option<PostionLength>,
    #[rich(write, write(option))]
    pub z_index: Option<i32>,
    #[rich(write, write(option))]
    pub clip: Option<Clip>,
}

impl Position {
    pub fn move_top(self) -> Self {
        self.top(px(0))
    }

    pub fn move_right(self) -> Self {
        self.right(px(0))
    }

    pub fn move_bottom(self) -> Self {
        self.bottom(px(0))
    }

    pub fn move_left(self) -> Self {
        self.left(px(0))
    }

    pub fn move_top_stretch(self) -> Self {
        self.move_top().move_left().move_right()
    }

    pub fn move_right_stretch(self) -> Self {
        self.move_right().move_top().move_bottom()
    }

    pub fn move_bottom_stretch(self) -> Self {
        self.move_bottom().move_left().move_right()
    }

    pub fn move_left_stretch(self) -> Self {
        self.move_left().move_top().move_bottom()
    }

    pub fn move_top_right(self) -> Self {
        self.move_top().move_right()
    }

    pub fn move_top_left(self) -> Self {
        self.move_top().move_left()
    }

    pub fn move_bottom_right(self) -> Self {
        self.move_bottom().move_right()
    }

    pub fn move_bottom_left(self) -> Self {
        self.move_bottom().move_left()
    }

    pub fn cover(self) -> Self {
        self.move_top_right().move_bottom_left()
    }
}

impl UpdateStyleValues for Position {
    fn update_style_values(self, values: StyleValues) -> StyleValues {
        values
            .try_add(St::Position, self.position.as_ref())
            .try_add(St::Left, self.left.as_ref())
            .try_add(St::Top, self.top.as_ref())
            .try_add(St::Right, self.right.as_ref())
            .try_add(St::Bottom, self.bottom.as_ref())
            .try_add(St::ZIndex, self.z_index.as_ref())
            .try_add(St::Clip, self.clip.as_ref())
    }
}

#[derive(Clone, Debug, Copy, PartialEq, Display, From)]
pub enum PostionLength {
    #[from]
    Initial(val::Initial),
    #[from]
    Inherit(val::Inherit),
    #[from]
    Auto(val::Auto),
    #[from]
    Length(Length),
    #[from(forward)]
    Percent(Percent),
}

#[derive(Clone, Copy, Debug, PartialEq, Display, From)]
pub enum Clip {
    #[from]
    Auto(val::Auto),
    #[display(fmt = "rect({}, {}, {}, {})", top, right, bottom, left)]
    ShapeRect {
        top: ClipRectLength,
        right: ClipRectLength,
        bottom: ClipRectLength,
        left: ClipRectLength,
    },
    #[from]
    Initial(val::Initial),
    #[from]
    Inherit(val::Inherit),
}

impl Clip {
    pub fn rect(
        top: impl Into<ClipRectLength>,
        right: impl Into<ClipRectLength>,
        bottom: impl Into<ClipRectLength>,
        left: impl Into<ClipRectLength>,
    ) -> Self {
        Self::ShapeRect {
            top: top.into(),
            right: right.into(),
            bottom: bottom.into(),
            left: left.into(),
        }
    }
}

#[derive(Clone, Debug, Copy, PartialEq, Display, From)]
pub enum ClipRectLength {
    #[from]
    Initial(val::Initial),
    #[from]
    Inherit(val::Inherit),
    #[from]
    Auto(val::Auto),
    #[from]
    Length(Length),
    #[from(forward)]
    Percent(Percent),
}

#[derive(Clone, Copy, Debug, PartialEq, Display, From)]
pub enum PositionType {
    Static(val::Static),
    Absolute(val::Absolute),
    Fixed(val::Fixed),
    Relative(val::Relative),
    Sticky(val::Sticky),
    Initial(val::Initial),
    Inherit(val::Inherit),
}