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
use crate::css::{
unit::{self, *},
values as val, St, StyleValues, UpdateStyleValues,
};
use derive_rich::Rich;
#[derive(Rich, Clone, Debug, Copy, PartialEq, From, Default)]
pub struct Margin {
#[rich(write, write(option))]
pub top: Option<Length>,
#[rich(write, write(option))]
pub right: Option<Length>,
#[rich(write, write(option))]
pub bottom: Option<Length>,
#[rich(write, write(option))]
pub left: Option<Length>,
}
impl From<Length> for Margin {
fn from(source: Length) -> Self {
Self::default().all(source)
}
}
impl From<unit::Length> for Margin {
fn from(source: unit::Length) -> Self {
Self::default().all(source)
}
}
impl From<Percent> for Margin {
fn from(source: Percent) -> Self {
Self::default().all(source)
}
}
impl UpdateStyleValues for Margin {
fn update_style_values(self, values: StyleValues) -> StyleValues {
values
.try_add(St::MarginTop, self.top)
.try_add(St::MarginRight, self.right)
.try_add(St::MarginBottom, self.bottom)
.try_add(St::MarginLeft, self.left)
}
}
impl Margin {
pub fn all(self, value: impl Into<Length>) -> Self {
let value = value.into();
self.right(value).top(value).left(value).bottom(value)
}
pub fn zero(self) -> Self {
self.all(px(0.))
}
pub fn x(self, value: impl Into<Length>) -> Self {
let value = value.into();
self.left(value).right(value)
}
pub fn y(self, value: impl Into<Length>) -> Self {
let value = value.into();
self.top(value).bottom(value)
}
pub fn horizontal(self, value: impl Into<Length>) -> Self {
self.y(value)
}
pub fn vertical(self, value: impl Into<Length>) -> Self {
self.x(value)
}
pub fn auto(self) -> Self {
self.all(val::Auto)
}
pub fn full(self) -> Self {
self.all(1.)
}
pub fn half(self) -> Self {
self.all(0.5)
}
}
#[derive(Clone, Debug, Copy, PartialEq, Display, From)]
pub enum Length {
#[from]
Auto(val::Auto),
#[from]
Inherit(val::Inherit),
#[from]
Initial(val::Initial),
#[from]
Length(unit::Length),
#[from(forward)]
Percent(Percent),
}