1use taffy::style_helpers::{TaffyGridLine, TaffyGridSpan};
4
5use crate::units::{Length, ResolveContext, Resolved};
6use crate::widget::TextAlign;
7
8#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
9pub enum Display {
10 #[default]
11 Flex,
12 Grid,
13 Block,
14 None,
16}
17
18#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
19pub enum FlexDirection {
20 Row,
21 #[default]
23 Column,
24 RowReverse,
25 ColumnReverse,
26}
27
28#[derive(Clone, Copy, Debug, PartialEq, Eq)]
29pub enum Align {
30 Start,
31 End,
32 Center,
33 Stretch,
34 Baseline,
35}
36
37#[derive(Clone, Copy, Debug, PartialEq, Eq)]
38pub enum Justify {
39 Start,
40 End,
41 Center,
42 Stretch,
43 SpaceBetween,
44 SpaceAround,
45 SpaceEvenly,
46}
47
48#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
49pub enum Position {
50 #[default]
51 Relative,
52 Absolute,
53}
54
55#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
58pub enum TextDirection {
59 #[default]
60 Inherit,
61 Ltr,
62 Rtl,
63}
64
65#[derive(Clone, Copy, Debug, Default, PartialEq)]
67pub struct Edges {
68 pub start: Length,
69 pub end: Length,
70 pub top: Length,
71 pub bottom: Length,
72}
73
74impl Edges {
75 pub fn all(v: Length) -> Edges {
76 Edges { start: v, end: v, top: v, bottom: v }
77 }
78
79 pub fn zero() -> Edges {
80 Edges::all(Length::Px(0.0))
81 }
82}
83
84#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
86pub struct GridPlacement {
87 pub start: Option<i16>,
89 pub span: Option<u16>,
91}
92
93impl GridPlacement {
94 pub fn at(line: i16) -> GridPlacement {
95 GridPlacement { start: Some(line), span: None }
96 }
97
98 pub fn span(tracks: u16) -> GridPlacement {
99 GridPlacement { start: None, span: Some(tracks) }
100 }
101
102 pub fn at_span(line: i16, tracks: u16) -> GridPlacement {
103 GridPlacement { start: Some(line), span: Some(tracks) }
104 }
105}
106
107#[derive(Clone, Copy, Debug, PartialEq)]
109pub enum Track {
110 Size(Length),
111 MinMax(Length, Length),
112 MinContent,
113 MaxContent,
114}
115
116impl From<Length> for Track {
117 fn from(l: Length) -> Track {
118 Track::Size(l)
119 }
120}
121
122pub fn repeat(count: usize, track: impl Into<Track>) -> Vec<Track> {
124 vec![track.into(); count]
125}
126
127#[derive(Clone, Debug, PartialEq)]
128pub struct Style {
129 pub display: Display,
130 pub hidden: bool,
132 pub position: Position,
133 pub direction: TextDirection,
134 pub text_align: Option<TextAlign>,
136 pub scroll_x: bool,
139 pub scroll_y: bool,
140 pub inset: Edges,
141
142 pub width: Length,
143 pub height: Length,
144 pub min_width: Length,
145 pub min_height: Length,
146 pub max_width: Length,
147 pub max_height: Length,
148 pub aspect_ratio: Option<f32>,
149
150 pub margin: Edges,
151 pub padding: Edges,
152
153 pub flex_direction: FlexDirection,
154 pub flex_wrap: bool,
155 pub flex_grow: f32,
156 pub flex_shrink: f32,
157 pub flex_basis: Length,
158
159 pub align_items: Option<Align>,
160 pub align_self: Option<Align>,
161 pub justify_self: Option<Align>,
163 pub align_content: Option<Justify>,
164 pub justify_content: Option<Justify>,
165 pub row_gap: Length,
166 pub column_gap: Length,
167
168 pub grid_template_columns: Vec<Track>,
169 pub grid_template_rows: Vec<Track>,
170 pub grid_column: GridPlacement,
171 pub grid_row: GridPlacement,
172}
173
174impl Default for Style {
175 fn default() -> Style {
176 Style {
177 display: Display::Flex,
178 hidden: false,
179 position: Position::Relative,
180 direction: TextDirection::Inherit,
181 text_align: None,
182 scroll_x: false,
183 scroll_y: false,
184 inset: Edges::default(),
185 width: Length::Auto,
186 height: Length::Auto,
187 min_width: Length::Auto,
188 min_height: Length::Auto,
189 max_width: Length::Auto,
190 max_height: Length::Auto,
191 aspect_ratio: None,
192 margin: Edges::zero(),
193 padding: Edges::zero(),
194 flex_direction: FlexDirection::Column,
195 flex_wrap: false,
196 flex_grow: 0.0,
197 flex_shrink: 1.0,
198 flex_basis: Length::Auto,
199 align_items: None,
200 align_self: None,
201 justify_self: None,
202 align_content: None,
203 justify_content: None,
204 row_gap: Length::Px(0.0),
205 column_gap: Length::Px(0.0),
206 grid_template_columns: Vec::new(),
207 grid_template_rows: Vec::new(),
208 grid_column: GridPlacement::default(),
209 grid_row: GridPlacement::default(),
210 }
211 }
212}
213
214fn dimension(l: Length, cx: &ResolveContext<'_>) -> taffy::Dimension {
217 match l.resolve(cx) {
218 Resolved::Length(v) => taffy::Dimension::length(v),
219 Resolved::Percent(v) => taffy::Dimension::percent(v),
220 Resolved::Fr(_) | Resolved::Auto => taffy::Dimension::auto(),
221 }
222}
223
224fn lpa(l: Length, cx: &ResolveContext<'_>) -> taffy::LengthPercentageAuto {
225 match l.resolve(cx) {
226 Resolved::Length(v) => taffy::LengthPercentageAuto::length(v),
227 Resolved::Percent(v) => taffy::LengthPercentageAuto::percent(v),
228 Resolved::Fr(_) | Resolved::Auto => taffy::LengthPercentageAuto::auto(),
229 }
230}
231
232fn lp(l: Length, cx: &ResolveContext<'_>) -> taffy::LengthPercentage {
233 match l.resolve(cx) {
234 Resolved::Length(v) => taffy::LengthPercentage::length(v),
235 Resolved::Percent(v) => taffy::LengthPercentage::percent(v),
236 Resolved::Fr(_) | Resolved::Auto => taffy::LengthPercentage::length(0.0),
237 }
238}
239
240fn min_track(l: Length, cx: &ResolveContext<'_>) -> taffy::MinTrackSizingFunction {
241 match l.resolve(cx) {
242 Resolved::Length(v) => taffy::MinTrackSizingFunction::length(v),
243 Resolved::Percent(v) => taffy::MinTrackSizingFunction::percent(v),
244 Resolved::Fr(_) | Resolved::Auto => taffy::MinTrackSizingFunction::auto(),
245 }
246}
247
248fn max_track(l: Length, cx: &ResolveContext<'_>) -> taffy::MaxTrackSizingFunction {
249 match l.resolve(cx) {
250 Resolved::Length(v) => taffy::MaxTrackSizingFunction::length(v),
251 Resolved::Percent(v) => taffy::MaxTrackSizingFunction::percent(v),
252 Resolved::Fr(v) => taffy::MaxTrackSizingFunction::fr(v),
253 Resolved::Auto => taffy::MaxTrackSizingFunction::auto(),
254 }
255}
256
257fn track(t: Track, cx: &ResolveContext<'_>) -> taffy::TrackSizingFunction {
258 match t {
259 Track::Size(l) => taffy::MinMax { min: min_track(l, cx), max: max_track(l, cx) },
261 Track::MinMax(min, max) => taffy::MinMax { min: min_track(min, cx), max: max_track(max, cx) },
262 Track::MinContent => taffy::MinMax {
263 min: taffy::MinTrackSizingFunction::min_content(),
264 max: taffy::MaxTrackSizingFunction::min_content(),
265 },
266 Track::MaxContent => taffy::MinMax {
267 min: taffy::MinTrackSizingFunction::max_content(),
268 max: taffy::MaxTrackSizingFunction::max_content(),
269 },
270 }
271}
272
273fn placement(p: GridPlacement) -> taffy::Line<taffy::GridPlacement> {
274 let start = match p.start {
275 Some(line) => taffy::GridPlacement::from_line_index(line),
276 None => taffy::GridPlacement::Auto,
277 };
278 let end = match p.span {
279 Some(n) => taffy::GridPlacement::from_span(n),
280 None => taffy::GridPlacement::Auto,
281 };
282 taffy::Line { start, end }
283}
284
285fn align_items(a: Align) -> taffy::AlignItems {
286 match a {
287 Align::Start => taffy::AlignItems::START,
288 Align::End => taffy::AlignItems::END,
289 Align::Center => taffy::AlignItems::CENTER,
290 Align::Stretch => taffy::AlignItems::STRETCH,
291 Align::Baseline => taffy::AlignItems::BASELINE,
292 }
293}
294
295fn justify(j: Justify) -> taffy::JustifyContent {
296 match j {
297 Justify::Start => taffy::JustifyContent::START,
298 Justify::End => taffy::JustifyContent::END,
299 Justify::Center => taffy::JustifyContent::CENTER,
300 Justify::Stretch => taffy::JustifyContent::STRETCH,
301 Justify::SpaceBetween => taffy::JustifyContent::SPACE_BETWEEN,
302 Justify::SpaceAround => taffy::JustifyContent::SPACE_AROUND,
303 Justify::SpaceEvenly => taffy::JustifyContent::SPACE_EVENLY,
304 }
305}
306
307impl Style {
308 pub(crate) fn to_taffy(&self, cx: &ResolveContext<'_>, rtl: bool) -> taffy::Style {
310 let edges_lpa = |e: &Edges| {
311 let (left, right) = if rtl { (e.end, e.start) } else { (e.start, e.end) };
312 taffy::Rect { left: lpa(left, cx), right: lpa(right, cx), top: lpa(e.top, cx), bottom: lpa(e.bottom, cx) }
313 };
314 let edges_lp = |e: &Edges| {
315 let (left, right) = if rtl { (e.end, e.start) } else { (e.start, e.end) };
316 taffy::Rect { left: lp(left, cx), right: lp(right, cx), top: lp(e.top, cx), bottom: lp(e.bottom, cx) }
317 };
318 taffy::Style {
319 display: match if self.hidden { Display::None } else { self.display } {
320 Display::Flex => taffy::Display::Flex,
321 Display::Grid => taffy::Display::Grid,
322 Display::Block => taffy::Display::Block,
323 Display::None => taffy::Display::None,
324 },
325 direction: if rtl { taffy::Direction::Rtl } else { taffy::Direction::Ltr },
326 position: match self.position {
327 Position::Relative => taffy::Position::Relative,
328 Position::Absolute => taffy::Position::Absolute,
329 },
330 inset: edges_lpa(&self.inset),
331 overflow: taffy::Point {
332 x: if self.scroll_x { taffy::Overflow::Scroll } else { taffy::Overflow::Visible },
333 y: if self.scroll_y { taffy::Overflow::Scroll } else { taffy::Overflow::Visible },
334 },
335 scrollbar_width: 0.0,
336 size: taffy::Size { width: dimension(self.width, cx), height: dimension(self.height, cx) },
337 min_size: taffy::Size { width: lpa(self.min_width, cx), height: lpa(self.min_height, cx) },
338 max_size: taffy::Size { width: lpa(self.max_width, cx), height: lpa(self.max_height, cx) },
339 aspect_ratio: self.aspect_ratio,
340 margin: edges_lpa(&self.margin),
341 padding: edges_lp(&self.padding),
342 flex_direction: match self.flex_direction {
343 FlexDirection::Row => taffy::FlexDirection::Row,
344 FlexDirection::Column => taffy::FlexDirection::Column,
345 FlexDirection::RowReverse => taffy::FlexDirection::RowReverse,
346 FlexDirection::ColumnReverse => taffy::FlexDirection::ColumnReverse,
347 },
348 flex_wrap: if self.flex_wrap { taffy::FlexWrap::Wrap } else { taffy::FlexWrap::NoWrap },
349 flex_grow: self.flex_grow,
350 flex_shrink: self.flex_shrink,
351 flex_basis: dimension(self.flex_basis, cx),
352 align_items: self.align_items.map(align_items),
353 align_self: self.align_self.map(align_items),
354 justify_self: self.justify_self.map(align_items),
355 align_content: self.align_content.map(justify),
356 justify_content: self.justify_content.map(justify),
357 gap: taffy::Size { width: lp(self.column_gap, cx), height: lp(self.row_gap, cx) },
358 grid_template_columns: self
359 .grid_template_columns
360 .iter()
361 .map(|t| taffy::GridTemplateComponent::Single(track(*t, cx)))
362 .collect(),
363 grid_template_rows: self
364 .grid_template_rows
365 .iter()
366 .map(|t| taffy::GridTemplateComponent::Single(track(*t, cx)))
367 .collect(),
368 grid_column: placement(self.grid_column),
369 grid_row: placement(self.grid_row),
370 ..taffy::Style::default()
371 }
372 }
373}
374
375impl Style {
376 pub fn is_hidden(&self) -> bool {
378 self.hidden || self.display == Display::None
379 }
380}
381
382macro_rules! static_value {
383 ($($t:ty),*) => {$(
384 impl mitsuami_reactive::IntoValue<$t> for $t {
385 fn into_value(self) -> mitsuami_reactive::Value<$t> {
386 mitsuami_reactive::Value::Static(self)
387 }
388 }
389 )*};
390}
391
392static_value!(Display, FlexDirection, Align, Justify, Position, TextDirection, GridPlacement);