1use super::{AlignContent, AlignItems, AlignSelf, CoreStyle, JustifyContent, LengthPercentage, Style};
3use crate::compute::grid::{GridCoordinate, GridLine, OriginZeroLine};
4use crate::geometry::{AbsoluteAxis, AbstractAxis, Line, MinMax, Size};
5use crate::style_helpers::*;
6use crate::util::sys::GridTrackVec;
7use core::borrow::Borrow;
8use core::cmp::{max, min};
9use core::convert::Infallible;
10
11pub trait GridContainerStyle: CoreStyle {
13 type TemplateTrackList<'a>: Borrow<[TrackSizingFunction]>
15 where
16 Self: 'a;
17 type AutoTrackList<'a>: Borrow<[NonRepeatedTrackSizingFunction]>
19 where
20 Self: 'a;
21
22 fn grid_template_rows(&self) -> Self::TemplateTrackList<'_>;
27 fn grid_template_columns(&self) -> Self::TemplateTrackList<'_>;
29 fn grid_auto_rows(&self) -> Self::AutoTrackList<'_>;
31 fn grid_auto_columns(&self) -> Self::AutoTrackList<'_>;
33
34 #[inline(always)]
36 fn grid_auto_flow(&self) -> GridAutoFlow {
37 Style::DEFAULT.grid_auto_flow
38 }
39
40 #[inline(always)]
42 fn gap(&self) -> Size<LengthPercentage> {
43 Style::DEFAULT.gap
44 }
45
46 #[inline(always)]
50 fn align_content(&self) -> Option<AlignContent> {
51 Style::DEFAULT.align_content
52 }
53 #[inline(always)]
55 fn justify_content(&self) -> Option<JustifyContent> {
56 Style::DEFAULT.justify_content
57 }
58 #[inline(always)]
60 fn align_items(&self) -> Option<AlignItems> {
61 Style::DEFAULT.align_items
62 }
63 #[inline(always)]
65 fn justify_items(&self) -> Option<AlignItems> {
66 Style::DEFAULT.justify_items
67 }
68
69 #[inline(always)]
71 fn grid_template_tracks(&self, axis: AbsoluteAxis) -> Self::TemplateTrackList<'_> {
72 match axis {
73 AbsoluteAxis::Horizontal => self.grid_template_columns(),
74 AbsoluteAxis::Vertical => self.grid_template_rows(),
75 }
76 }
77
78 #[inline(always)]
80 fn grid_align_content(&self, axis: AbstractAxis) -> AlignContent {
81 match axis {
82 AbstractAxis::Inline => self.justify_content().unwrap_or(AlignContent::Stretch),
83 AbstractAxis::Block => self.align_content().unwrap_or(AlignContent::Stretch),
84 }
85 }
86}
87
88pub trait GridItemStyle: CoreStyle {
90 #[inline(always)]
92 fn grid_row(&self) -> Line<GridPlacement> {
93 Style::DEFAULT.grid_row
94 }
95 #[inline(always)]
97 fn grid_column(&self) -> Line<GridPlacement> {
98 Style::DEFAULT.grid_column
99 }
100
101 #[inline(always)]
104 fn align_self(&self) -> Option<AlignSelf> {
105 Style::DEFAULT.align_self
106 }
107 #[inline(always)]
110 fn justify_self(&self) -> Option<AlignSelf> {
111 Style::DEFAULT.justify_self
112 }
113
114 #[inline(always)]
116 fn grid_placement(&self, axis: AbsoluteAxis) -> Line<GridPlacement> {
117 match axis {
118 AbsoluteAxis::Horizontal => self.grid_column(),
119 AbsoluteAxis::Vertical => self.grid_row(),
120 }
121 }
122}
123
124#[derive(Copy, Clone, PartialEq, Eq, Debug)]
132#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
133pub enum GridAutoFlow {
134 Row,
136 Column,
138 RowDense,
140 ColumnDense,
142}
143
144impl Default for GridAutoFlow {
145 fn default() -> Self {
146 Self::Row
147 }
148}
149
150impl GridAutoFlow {
151 pub fn is_dense(&self) -> bool {
154 match self {
155 Self::Row | Self::Column => false,
156 Self::RowDense | Self::ColumnDense => true,
157 }
158 }
159
160 pub fn primary_axis(&self) -> AbsoluteAxis {
163 match self {
164 Self::Row | Self::RowDense => AbsoluteAxis::Horizontal,
165 Self::Column | Self::ColumnDense => AbsoluteAxis::Vertical,
166 }
167 }
168}
169
170#[derive(Copy, Clone, PartialEq, Eq, Debug)]
178#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
179pub enum GenericGridPlacement<LineType: GridCoordinate> {
180 Auto,
182 Line(LineType),
184 Span(u16),
186}
187
188pub(crate) type OriginZeroGridPlacement = GenericGridPlacement<OriginZeroLine>;
190
191pub type GridPlacement = GenericGridPlacement<GridLine>;
197impl TaffyAuto for GridPlacement {
198 const AUTO: Self = Self::Auto;
199}
200impl TaffyGridLine for GridPlacement {
201 fn from_line_index(index: i16) -> Self {
202 GridPlacement::Line(GridLine::from(index))
203 }
204}
205impl TaffyGridLine for Line<GridPlacement> {
206 fn from_line_index(index: i16) -> Self {
207 Line { start: GridPlacement::from_line_index(index), end: GridPlacement::Auto }
208 }
209}
210impl TaffyGridSpan for GridPlacement {
211 fn from_span(span: u16) -> Self {
212 GridPlacement::Span(span)
213 }
214}
215impl TaffyGridSpan for Line<GridPlacement> {
216 fn from_span(span: u16) -> Self {
217 Line { start: GridPlacement::from_span(span), end: GridPlacement::Auto }
218 }
219}
220
221impl Default for GridPlacement {
222 fn default() -> Self {
223 Self::Auto
224 }
225}
226
227impl GridPlacement {
228 pub fn into_origin_zero_placement(self, explicit_track_count: u16) -> OriginZeroGridPlacement {
230 match self {
231 Self::Auto => OriginZeroGridPlacement::Auto,
232 Self::Span(span) => OriginZeroGridPlacement::Span(span),
233 Self::Line(line) => match line.as_i16() {
236 0 => OriginZeroGridPlacement::Auto,
237 _ => OriginZeroGridPlacement::Line(line.into_origin_zero_line(explicit_track_count)),
238 },
239 }
240 }
241}
242
243impl<T: GridCoordinate> Line<GenericGridPlacement<T>> {
244 pub fn indefinite_span(&self) -> u16 {
247 use GenericGridPlacement as GP;
248 match (self.start, self.end) {
249 (GP::Line(_), GP::Auto) => 1,
250 (GP::Auto, GP::Line(_)) => 1,
251 (GP::Auto, GP::Auto) => 1,
252 (GP::Line(_), GP::Span(span)) => span,
253 (GP::Span(span), GP::Line(_)) => span,
254 (GP::Span(span), GP::Auto) => span,
255 (GP::Auto, GP::Span(span)) => span,
256 (GP::Span(span), GP::Span(_)) => span,
257 (GP::Line(_), GP::Line(_)) => panic!("indefinite_span should only be called on indefinite grid tracks"),
258 }
259 }
260}
261
262impl Line<GridPlacement> {
263 #[inline]
264 pub fn is_definite(&self) -> bool {
268 match (self.start, self.end) {
269 (GenericGridPlacement::Line(line), _) if line.as_i16() != 0 => true,
270 (_, GenericGridPlacement::Line(line)) if line.as_i16() != 0 => true,
271 _ => false,
272 }
273 }
274
275 pub fn into_origin_zero(&self, explicit_track_count: u16) -> Line<OriginZeroGridPlacement> {
277 Line {
278 start: self.start.into_origin_zero_placement(explicit_track_count),
279 end: self.end.into_origin_zero_placement(explicit_track_count),
280 }
281 }
282}
283
284impl Line<OriginZeroGridPlacement> {
285 #[inline]
286 pub fn is_definite(&self) -> bool {
289 matches!((self.start, self.end), (GenericGridPlacement::Line(_), _) | (_, GenericGridPlacement::Line(_)))
290 }
291
292 pub fn resolve_definite_grid_lines(&self) -> Line<OriginZeroLine> {
295 use OriginZeroGridPlacement as GP;
296 match (self.start, self.end) {
297 (GP::Line(line1), GP::Line(line2)) => {
298 if line1 == line2 {
299 Line { start: line1, end: line1 + 1 }
300 } else {
301 Line { start: min(line1, line2), end: max(line1, line2) }
302 }
303 }
304 (GP::Line(line), GP::Span(span)) => Line { start: line, end: line + span },
305 (GP::Line(line), GP::Auto) => Line { start: line, end: line + 1 },
306 (GP::Span(span), GP::Line(line)) => Line { start: line - span, end: line },
307 (GP::Auto, GP::Line(line)) => Line { start: line - 1, end: line },
308 _ => panic!("resolve_definite_grid_tracks should only be called on definite grid tracks"),
309 }
310 }
311
312 pub fn resolve_absolutely_positioned_grid_tracks(&self) -> Line<Option<OriginZeroLine>> {
322 use OriginZeroGridPlacement as GP;
323 match (self.start, self.end) {
324 (GP::Line(track1), GP::Line(track2)) => {
325 if track1 == track2 {
326 Line { start: Some(track1), end: Some(track1 + 1) }
327 } else {
328 Line { start: Some(min(track1, track2)), end: Some(max(track1, track2)) }
329 }
330 }
331 (GP::Line(track), GP::Span(span)) => Line { start: Some(track), end: Some(track + span) },
332 (GP::Line(track), GP::Auto) => Line { start: Some(track), end: None },
333 (GP::Span(span), GP::Line(track)) => Line { start: Some(track - span), end: Some(track) },
334 (GP::Auto, GP::Line(track)) => Line { start: None, end: Some(track) },
335 _ => Line { start: None, end: None },
336 }
337 }
338
339 pub fn resolve_indefinite_grid_tracks(&self, start: OriginZeroLine) -> Line<OriginZeroLine> {
342 use OriginZeroGridPlacement as GP;
343 match (self.start, self.end) {
344 (GP::Auto, GP::Auto) => Line { start, end: start + 1 },
345 (GP::Span(span), GP::Auto) => Line { start, end: start + span },
346 (GP::Auto, GP::Span(span)) => Line { start, end: start + span },
347 (GP::Span(span), GP::Span(_)) => Line { start, end: start + span },
348 _ => panic!("resolve_indefinite_grid_tracks should only be called on indefinite grid tracks"),
349 }
350 }
351}
352
353impl Default for Line<GridPlacement> {
355 fn default() -> Self {
356 Line { start: GridPlacement::Auto, end: GridPlacement::Auto }
357 }
358}
359
360#[derive(Copy, Clone, PartialEq, Debug)]
366#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
367pub enum MaxTrackSizingFunction {
368 Fixed(LengthPercentage),
370 MinContent,
372 MaxContent,
374 FitContent(LengthPercentage),
376 Auto,
378 Fraction(f32),
382}
383impl TaffyAuto for MaxTrackSizingFunction {
384 const AUTO: Self = Self::Auto;
385}
386impl TaffyMinContent for MaxTrackSizingFunction {
387 const MIN_CONTENT: Self = Self::MinContent;
388}
389impl TaffyMaxContent for MaxTrackSizingFunction {
390 const MAX_CONTENT: Self = Self::MaxContent;
391}
392impl TaffyFitContent for MaxTrackSizingFunction {
393 fn fit_content(argument: LengthPercentage) -> Self {
394 Self::FitContent(argument)
395 }
396}
397impl TaffyZero for MaxTrackSizingFunction {
398 const ZERO: Self = Self::Fixed(LengthPercentage::ZERO);
399}
400impl FromLength for MaxTrackSizingFunction {
401 fn from_length<Input: Into<f32> + Copy>(value: Input) -> Self {
402 Self::Fixed(LengthPercentage::from_length(value))
403 }
404}
405impl FromPercent for MaxTrackSizingFunction {
406 fn from_percent<Input: Into<f32> + Copy>(percent: Input) -> Self {
407 Self::Fixed(LengthPercentage::from_percent(percent))
408 }
409}
410impl FromFlex for MaxTrackSizingFunction {
411 fn from_flex<Input: Into<f32> + Copy>(flex: Input) -> Self {
412 Self::Fraction(flex.into())
413 }
414}
415
416impl MaxTrackSizingFunction {
417 #[inline(always)]
419 pub fn is_intrinsic(&self) -> bool {
420 matches!(self, Self::MinContent | Self::MaxContent | Self::FitContent(_) | Self::Auto)
421 }
422
423 #[inline(always)]
427 pub fn is_max_content_alike(&self) -> bool {
428 matches!(self, Self::MaxContent | Self::FitContent(_) | Self::Auto)
429 }
430
431 #[inline(always)]
433 pub fn is_flexible(&self) -> bool {
434 matches!(self, Self::Fraction(_))
435 }
436
437 #[inline(always)]
441 pub fn definite_value(self, parent_size: Option<f32>) -> Option<f32> {
442 use MaxTrackSizingFunction::*;
443 match self {
444 Fixed(LengthPercentage::Length(size)) => Some(size),
445 Fixed(LengthPercentage::Percent(fraction)) => parent_size.map(|size| fraction * size),
446 MinContent | MaxContent | FitContent(_) | Auto | Fraction(_) => None,
447 }
448 }
449
450 #[inline(always)]
457 pub fn definite_limit(self, parent_size: Option<f32>) -> Option<f32> {
458 use MaxTrackSizingFunction::FitContent;
459 match self {
460 FitContent(LengthPercentage::Length(size)) => Some(size),
461 FitContent(LengthPercentage::Percent(fraction)) => parent_size.map(|size| fraction * size),
462 _ => self.definite_value(parent_size),
463 }
464 }
465
466 #[inline(always)]
469 pub fn resolved_percentage_size(self, parent_size: f32) -> Option<f32> {
470 use MaxTrackSizingFunction::*;
471 match self {
472 Fixed(LengthPercentage::Percent(fraction)) => Some(fraction * parent_size),
473 Fixed(LengthPercentage::Length(_)) | MinContent | MaxContent | FitContent(_) | Auto | Fraction(_) => None,
474 }
475 }
476
477 #[inline(always)]
479 pub fn uses_percentage(self) -> bool {
480 use MaxTrackSizingFunction::*;
481 matches!(self, Fixed(LengthPercentage::Percent(_)) | FitContent(LengthPercentage::Percent(_)))
482 }
483}
484
485#[derive(Copy, Clone, PartialEq, Debug)]
491#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
492pub enum MinTrackSizingFunction {
493 Fixed(LengthPercentage),
495 MinContent,
497 MaxContent,
499 Auto,
501}
502impl TaffyAuto for MinTrackSizingFunction {
503 const AUTO: Self = Self::Auto;
504}
505impl TaffyMinContent for MinTrackSizingFunction {
506 const MIN_CONTENT: Self = Self::MinContent;
507}
508impl TaffyMaxContent for MinTrackSizingFunction {
509 const MAX_CONTENT: Self = Self::MaxContent;
510}
511impl TaffyZero for MinTrackSizingFunction {
512 const ZERO: Self = Self::Fixed(LengthPercentage::ZERO);
513}
514impl FromLength for MinTrackSizingFunction {
515 fn from_length<Input: Into<f32> + Copy>(value: Input) -> Self {
516 Self::Fixed(LengthPercentage::from_length(value))
517 }
518}
519impl FromPercent for MinTrackSizingFunction {
520 fn from_percent<Input: Into<f32> + Copy>(percent: Input) -> Self {
521 Self::Fixed(LengthPercentage::from_percent(percent))
522 }
523}
524
525impl MinTrackSizingFunction {
526 #[inline(always)]
528 pub fn is_intrinsic(&self) -> bool {
529 matches!(self, Self::MinContent | Self::MaxContent | Self::Auto)
530 }
531
532 #[inline(always)]
536 pub fn definite_value(self, parent_size: Option<f32>) -> Option<f32> {
537 use MinTrackSizingFunction::*;
538 match self {
539 Fixed(LengthPercentage::Length(size)) => Some(size),
540 Fixed(LengthPercentage::Percent(fraction)) => parent_size.map(|size| fraction * size),
541 MinContent | MaxContent | Auto => None,
542 }
543 }
544
545 #[inline(always)]
548 pub fn resolved_percentage_size(self, parent_size: f32) -> Option<f32> {
549 use MinTrackSizingFunction::*;
550 match self {
551 Fixed(LengthPercentage::Percent(fraction)) => Some(fraction * parent_size),
552 Fixed(LengthPercentage::Length(_)) | MinContent | MaxContent | Auto => None,
553 }
554 }
555
556 #[inline(always)]
558 pub fn uses_percentage(self) -> bool {
559 use MinTrackSizingFunction::*;
560 matches!(self, Fixed(LengthPercentage::Percent(_)))
561 }
562}
563
564pub type NonRepeatedTrackSizingFunction = MinMax<MinTrackSizingFunction, MaxTrackSizingFunction>;
569impl NonRepeatedTrackSizingFunction {
570 pub fn min_sizing_function(&self) -> MinTrackSizingFunction {
572 self.min
573 }
574 pub fn max_sizing_function(&self) -> MaxTrackSizingFunction {
576 self.max
577 }
578 pub fn has_fixed_component(&self) -> bool {
580 matches!(self.min, MinTrackSizingFunction::Fixed(_)) || matches!(self.max, MaxTrackSizingFunction::Fixed(_))
581 }
582}
583impl TaffyAuto for NonRepeatedTrackSizingFunction {
584 const AUTO: Self = Self { min: MinTrackSizingFunction::AUTO, max: MaxTrackSizingFunction::AUTO };
585}
586impl TaffyMinContent for NonRepeatedTrackSizingFunction {
587 const MIN_CONTENT: Self =
588 Self { min: MinTrackSizingFunction::MIN_CONTENT, max: MaxTrackSizingFunction::MIN_CONTENT };
589}
590impl TaffyMaxContent for NonRepeatedTrackSizingFunction {
591 const MAX_CONTENT: Self =
592 Self { min: MinTrackSizingFunction::MAX_CONTENT, max: MaxTrackSizingFunction::MAX_CONTENT };
593}
594impl TaffyFitContent for NonRepeatedTrackSizingFunction {
595 fn fit_content(argument: LengthPercentage) -> Self {
596 Self { min: MinTrackSizingFunction::AUTO, max: MaxTrackSizingFunction::FitContent(argument) }
597 }
598}
599impl TaffyZero for NonRepeatedTrackSizingFunction {
600 const ZERO: Self = Self { min: MinTrackSizingFunction::ZERO, max: MaxTrackSizingFunction::ZERO };
601}
602impl FromLength for NonRepeatedTrackSizingFunction {
603 fn from_length<Input: Into<f32> + Copy>(value: Input) -> Self {
604 Self { min: MinTrackSizingFunction::from_length(value), max: MaxTrackSizingFunction::from_length(value) }
605 }
606}
607impl FromPercent for NonRepeatedTrackSizingFunction {
608 fn from_percent<Input: Into<f32> + Copy>(percent: Input) -> Self {
609 Self { min: MinTrackSizingFunction::from_percent(percent), max: MaxTrackSizingFunction::from_percent(percent) }
610 }
611}
612impl FromFlex for NonRepeatedTrackSizingFunction {
613 fn from_flex<Input: Into<f32> + Copy>(flex: Input) -> Self {
614 Self { min: MinTrackSizingFunction::AUTO, max: MaxTrackSizingFunction::from_flex(flex) }
615 }
616}
617
618#[derive(Clone, Copy, Debug, PartialEq, Eq)]
623#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
624pub enum GridTrackRepetition {
625 AutoFill,
628 AutoFit,
631 Count(u16),
633}
634impl TryFrom<u16> for GridTrackRepetition {
635 type Error = Infallible;
636 fn try_from(value: u16) -> Result<Self, Infallible> {
637 Ok(Self::Count(value))
638 }
639}
640
641#[derive(Debug)]
644pub struct InvalidStringRepetitionValue;
645#[cfg(feature = "std")]
646impl std::error::Error for InvalidStringRepetitionValue {}
647impl core::fmt::Display for InvalidStringRepetitionValue {
648 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
649 f.write_str("&str can only be converted to GridTrackRepetition if it's value is 'auto-fit' or 'auto-fill'")
650 }
651}
652impl TryFrom<&str> for GridTrackRepetition {
653 type Error = InvalidStringRepetitionValue;
654 fn try_from(value: &str) -> Result<Self, InvalidStringRepetitionValue> {
655 match value {
656 "auto-fit" => Ok(Self::AutoFit),
657 "auto-fill" => Ok(Self::AutoFill),
658 _ => Err(InvalidStringRepetitionValue),
659 }
660 }
661}
662
663#[derive(Clone, PartialEq, Debug)]
666#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
667pub enum TrackSizingFunction {
668 Single(NonRepeatedTrackSizingFunction),
670 Repeat(GridTrackRepetition, GridTrackVec<NonRepeatedTrackSizingFunction>),
673}
674impl TrackSizingFunction {
675 pub fn is_auto_repetition(&self) -> bool {
677 matches!(self, Self::Repeat(GridTrackRepetition::AutoFit | GridTrackRepetition::AutoFill, _))
678 }
679}
680impl TaffyAuto for TrackSizingFunction {
681 const AUTO: Self = Self::Single(NonRepeatedTrackSizingFunction::AUTO);
682}
683impl TaffyMinContent for TrackSizingFunction {
684 const MIN_CONTENT: Self = Self::Single(NonRepeatedTrackSizingFunction::MIN_CONTENT);
685}
686impl TaffyMaxContent for TrackSizingFunction {
687 const MAX_CONTENT: Self = Self::Single(NonRepeatedTrackSizingFunction::MAX_CONTENT);
688}
689impl TaffyFitContent for TrackSizingFunction {
690 fn fit_content(argument: LengthPercentage) -> Self {
691 Self::Single(NonRepeatedTrackSizingFunction::fit_content(argument))
692 }
693}
694impl TaffyZero for TrackSizingFunction {
695 const ZERO: Self = Self::Single(NonRepeatedTrackSizingFunction::ZERO);
696}
697impl FromLength for TrackSizingFunction {
698 fn from_length<Input: Into<f32> + Copy>(value: Input) -> Self {
699 Self::Single(NonRepeatedTrackSizingFunction::from_length(value))
700 }
701}
702impl FromPercent for TrackSizingFunction {
703 fn from_percent<Input: Into<f32> + Copy>(percent: Input) -> Self {
704 Self::Single(NonRepeatedTrackSizingFunction::from_percent(percent))
705 }
706}
707impl FromFlex for TrackSizingFunction {
708 fn from_flex<Input: Into<f32> + Copy>(flex: Input) -> Self {
709 Self::Single(NonRepeatedTrackSizingFunction::from_flex(flex))
710 }
711}
712impl From<MinMax<MinTrackSizingFunction, MaxTrackSizingFunction>> for TrackSizingFunction {
713 fn from(input: MinMax<MinTrackSizingFunction, MaxTrackSizingFunction>) -> Self {
714 Self::Single(input)
715 }
716}