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 if l == Length::MaxContent {
242 return taffy::MinTrackSizingFunction::max_content();
243 }
244 match l.resolve(cx) {
245 Resolved::Length(v) => taffy::MinTrackSizingFunction::length(v),
246 Resolved::Percent(v) => taffy::MinTrackSizingFunction::percent(v),
247 Resolved::Fr(_) | Resolved::Auto => taffy::MinTrackSizingFunction::auto(),
248 }
249}
250
251fn max_track(l: Length, cx: &ResolveContext<'_>) -> taffy::MaxTrackSizingFunction {
252 if l == Length::MaxContent {
253 return taffy::MaxTrackSizingFunction::max_content();
254 }
255 match l.resolve(cx) {
256 Resolved::Length(v) => taffy::MaxTrackSizingFunction::length(v),
257 Resolved::Percent(v) => taffy::MaxTrackSizingFunction::percent(v),
258 Resolved::Fr(v) => taffy::MaxTrackSizingFunction::fr(v),
259 Resolved::Auto => taffy::MaxTrackSizingFunction::auto(),
260 }
261}
262
263fn track(t: Track, cx: &ResolveContext<'_>) -> taffy::TrackSizingFunction {
264 match t {
265 Track::Size(l) => taffy::MinMax { min: min_track(l, cx), max: max_track(l, cx) },
267 Track::MinMax(min, max) => taffy::MinMax { min: min_track(min, cx), max: max_track(max, cx) },
268 Track::MinContent => taffy::MinMax {
269 min: taffy::MinTrackSizingFunction::min_content(),
270 max: taffy::MaxTrackSizingFunction::min_content(),
271 },
272 Track::MaxContent => taffy::MinMax {
273 min: taffy::MinTrackSizingFunction::max_content(),
274 max: taffy::MaxTrackSizingFunction::max_content(),
275 },
276 }
277}
278
279fn placement(p: GridPlacement) -> taffy::Line<taffy::GridPlacement> {
280 let start = match p.start {
281 Some(line) => taffy::GridPlacement::from_line_index(line),
282 None => taffy::GridPlacement::Auto,
283 };
284 let end = match p.span {
285 Some(n) => taffy::GridPlacement::from_span(n),
286 None => taffy::GridPlacement::Auto,
287 };
288 taffy::Line { start, end }
289}
290
291fn align_items(a: Align) -> taffy::AlignItems {
292 match a {
293 Align::Start => taffy::AlignItems::START,
294 Align::End => taffy::AlignItems::END,
295 Align::Center => taffy::AlignItems::CENTER,
296 Align::Stretch => taffy::AlignItems::STRETCH,
297 Align::Baseline => taffy::AlignItems::BASELINE,
298 }
299}
300
301fn justify(j: Justify) -> taffy::JustifyContent {
302 match j {
303 Justify::Start => taffy::JustifyContent::START,
304 Justify::End => taffy::JustifyContent::END,
305 Justify::Center => taffy::JustifyContent::CENTER,
306 Justify::Stretch => taffy::JustifyContent::STRETCH,
307 Justify::SpaceBetween => taffy::JustifyContent::SPACE_BETWEEN,
308 Justify::SpaceAround => taffy::JustifyContent::SPACE_AROUND,
309 Justify::SpaceEvenly => taffy::JustifyContent::SPACE_EVENLY,
310 }
311}
312
313impl Style {
314 pub(crate) fn to_taffy(&self, cx: &ResolveContext<'_>, rtl: bool) -> taffy::Style {
316 let edges_lpa = |e: &Edges| {
317 let (left, right) = if rtl { (e.end, e.start) } else { (e.start, e.end) };
318 taffy::Rect { left: lpa(left, cx), right: lpa(right, cx), top: lpa(e.top, cx), bottom: lpa(e.bottom, cx) }
319 };
320 let edges_lp = |e: &Edges| {
321 let (left, right) = if rtl { (e.end, e.start) } else { (e.start, e.end) };
322 taffy::Rect { left: lp(left, cx), right: lp(right, cx), top: lp(e.top, cx), bottom: lp(e.bottom, cx) }
323 };
324 taffy::Style {
325 display: match if self.hidden { Display::None } else { self.display } {
326 Display::Flex => taffy::Display::Flex,
327 Display::Grid => taffy::Display::Grid,
328 Display::Block => taffy::Display::Block,
329 Display::None => taffy::Display::None,
330 },
331 direction: if rtl { taffy::Direction::Rtl } else { taffy::Direction::Ltr },
332 position: match self.position {
333 Position::Relative => taffy::Position::Relative,
334 Position::Absolute => taffy::Position::Absolute,
335 },
336 inset: edges_lpa(&self.inset),
337 overflow: taffy::Point {
338 x: if self.scroll_x { taffy::Overflow::Scroll } else { taffy::Overflow::Visible },
339 y: if self.scroll_y { taffy::Overflow::Scroll } else { taffy::Overflow::Visible },
340 },
341 scrollbar_width: 0.0,
342 size: taffy::Size { width: dimension(self.width, cx), height: dimension(self.height, cx) },
343 min_size: taffy::Size { width: lpa(self.min_width, cx), height: lpa(self.min_height, cx) },
344 max_size: taffy::Size { width: lpa(self.max_width, cx), height: lpa(self.max_height, cx) },
345 aspect_ratio: self.aspect_ratio,
346 margin: edges_lpa(&self.margin),
347 padding: edges_lp(&self.padding),
348 flex_direction: match self.flex_direction {
349 FlexDirection::Row => taffy::FlexDirection::Row,
350 FlexDirection::Column => taffy::FlexDirection::Column,
351 FlexDirection::RowReverse => taffy::FlexDirection::RowReverse,
352 FlexDirection::ColumnReverse => taffy::FlexDirection::ColumnReverse,
353 },
354 flex_wrap: if self.flex_wrap { taffy::FlexWrap::Wrap } else { taffy::FlexWrap::NoWrap },
355 flex_grow: self.flex_grow,
356 flex_shrink: self.flex_shrink,
357 flex_basis: dimension(self.flex_basis, cx),
358 align_items: self.align_items.map(align_items),
359 align_self: self.align_self.map(align_items),
360 justify_self: self.justify_self.map(align_items),
361 align_content: self.align_content.map(justify),
362 justify_content: self.justify_content.map(justify),
363 gap: taffy::Size { width: lp(self.column_gap, cx), height: lp(self.row_gap, cx) },
364 grid_template_columns: self
365 .grid_template_columns
366 .iter()
367 .map(|t| taffy::GridTemplateComponent::Single(track(*t, cx)))
368 .collect(),
369 grid_template_rows: self
370 .grid_template_rows
371 .iter()
372 .map(|t| taffy::GridTemplateComponent::Single(track(*t, cx)))
373 .collect(),
374 grid_column: placement(self.grid_column),
375 grid_row: placement(self.grid_row),
376 ..taffy::Style::default()
377 }
378 }
379}
380
381impl Style {
382 pub fn is_hidden(&self) -> bool {
384 self.hidden || self.display == Display::None
385 }
386}
387
388macro_rules! static_value {
389 ($($t:ty),*) => {$(
390 impl mitsuami_reactive::IntoValue<$t> for $t {
391 fn into_value(self) -> mitsuami_reactive::Value<$t> {
392 mitsuami_reactive::Value::Static(self)
393 }
394 }
395 )*};
396}
397
398static_value!(Display, FlexDirection, Align, Justify, Position, TextDirection, GridPlacement);