1use crate::geometry::Size;
4
5#[derive(Clone, Copy, Debug, Default, PartialEq)]
10pub enum Length {
11 Px(f32),
13 Em(f32),
15 Rem(f32),
17 Percent(f32),
19 Vw(f32),
21 Vh(f32),
23 Vmin(f32),
24 Vmax(f32),
25 Fr(f32),
27 Token(Spacing),
29 #[default]
30 Auto,
31 MaxContent,
36}
37
38#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
41pub enum Spacing {
42 None,
43 Xs,
44 Sm,
45 Md,
46 Lg,
47 Xl,
48 Indent,
51}
52
53impl From<Spacing> for Length {
54 fn from(token: Spacing) -> Length {
55 Length::Token(token)
56 }
57}
58
59pub trait LengthExt {
61 fn px(self) -> Length;
62 fn em(self) -> Length;
63 fn rem(self) -> Length;
64 fn pct(self) -> Length;
65 fn vw(self) -> Length;
66 fn vh(self) -> Length;
67 fn vmin(self) -> Length;
68 fn vmax(self) -> Length;
69 fn fr(self) -> Length;
70}
71
72macro_rules! length_ext {
73 ($($t:ty),*) => {$(
74 impl LengthExt for $t {
75 fn px(self) -> Length { Length::Px(self as f32) }
76 fn em(self) -> Length { Length::Em(self as f32) }
77 fn rem(self) -> Length { Length::Rem(self as f32) }
78 fn pct(self) -> Length { Length::Percent(self as f32) }
79 fn vw(self) -> Length { Length::Vw(self as f32) }
80 fn vh(self) -> Length { Length::Vh(self as f32) }
81 fn vmin(self) -> Length { Length::Vmin(self as f32) }
82 fn vmax(self) -> Length { Length::Vmax(self as f32) }
83 fn fr(self) -> Length { Length::Fr(self as f32) }
84 }
85 )*};
86}
87
88length_ext!(i32, u32, f32, f64);
89
90#[derive(Clone, Copy, Debug)]
92pub struct ResolveContext<'a> {
93 pub font_size: f32,
94 pub root_font_size: f32,
95 pub viewport: Size,
96 pub spacing: &'a SpacingScale,
97}
98
99#[derive(Clone, Copy, Debug, PartialEq)]
101pub struct SpacingScale {
102 pub xs: f32,
103 pub sm: f32,
104 pub md: f32,
105 pub lg: f32,
106 pub xl: f32,
107 pub indent: f32,
109}
110
111impl SpacingScale {
112 pub fn get(&self, token: Spacing) -> f32 {
113 match token {
114 Spacing::None => 0.0,
115 Spacing::Xs => self.xs,
116 Spacing::Sm => self.sm,
117 Spacing::Md => self.md,
118 Spacing::Lg => self.lg,
119 Spacing::Xl => self.xl,
120 Spacing::Indent => self.indent,
121 }
122 }
123}
124
125#[derive(Clone, Copy, Debug, PartialEq)]
127pub enum Resolved {
128 Length(f32),
129 Percent(f32),
131 Fr(f32),
132 Auto,
133}
134
135impl Length {
136 pub fn resolve(&self, cx: &ResolveContext<'_>) -> Resolved {
137 let vw = cx.viewport.width / 100.0;
138 let vh = cx.viewport.height / 100.0;
139 match *self {
140 Length::Px(v) => Resolved::Length(v),
141 Length::Em(v) => Resolved::Length(v * cx.font_size),
142 Length::Rem(v) => Resolved::Length(v * cx.root_font_size),
143 Length::Percent(v) => Resolved::Percent(v / 100.0),
144 Length::Vw(v) => Resolved::Length(v * vw),
145 Length::Vh(v) => Resolved::Length(v * vh),
146 Length::Vmin(v) => Resolved::Length(v * vw.min(vh)),
147 Length::Vmax(v) => Resolved::Length(v * vw.max(vh)),
148 Length::Fr(v) => Resolved::Fr(v),
149 Length::Token(t) => Resolved::Length(cx.spacing.get(t)),
150 Length::Auto | Length::MaxContent => Resolved::Auto,
152 }
153 }
154}
155
156impl From<i32> for Length {
157 fn from(v: i32) -> Length {
158 Length::Px(v as f32)
159 }
160}
161
162impl From<f32> for Length {
163 fn from(v: f32) -> Length {
164 Length::Px(v)
165 }
166}
167
168macro_rules! into_length_value {
170 ($($t:ty => $e:expr),*) => {$(
171 impl mitsuami_reactive::IntoValue<Length> for $t {
172 fn into_value(self) -> mitsuami_reactive::Value<Length> {
173 let f: fn($t) -> Length = $e;
174 mitsuami_reactive::Value::Static(f(self))
175 }
176 }
177 )*};
178}
179
180into_length_value!(
181 Length => |l| l,
182 Spacing => Length::Token,
183 i32 => |v| Length::Px(v as f32),
184 f32 => Length::Px
185);