1mod alignment;
3mod available_space;
4mod compact_length;
5mod dimension;
6
7#[cfg(feature = "block_layout")]
8mod block;
9#[cfg(feature = "flexbox")]
10mod flex;
11#[cfg(feature = "grid")]
12mod grid;
13
14pub use self::alignment::{AlignContent, AlignItems, AlignSelf, JustifyContent, JustifyItems, JustifySelf};
15pub use self::available_space::AvailableSpace;
16pub use self::compact_length::CompactLength;
17pub use self::dimension::{Dimension, LengthPercentage, LengthPercentageAuto};
18use crate::sys::DefaultCheapStr;
19
20#[cfg(feature = "block_layout")]
21pub use self::block::{BlockContainerStyle, BlockItemStyle, TextAlign};
22#[cfg(feature = "flexbox")]
23pub use self::flex::{FlexDirection, FlexWrap, FlexboxContainerStyle, FlexboxItemStyle};
24#[cfg(feature = "grid")]
25pub use self::grid::{
26    GenericGridPlacement, GenericGridTemplateComponent, GenericRepetition, GridAutoFlow, GridContainerStyle,
27    GridItemStyle, GridPlacement, GridTemplateComponent, GridTemplateRepetition, MaxTrackSizingFunction,
28    MinTrackSizingFunction, RepetitionCount, TrackSizingFunction,
29};
30#[cfg(feature = "grid")]
31pub(crate) use self::grid::{GridAreaAxis, GridAreaEnd};
32#[cfg(feature = "grid")]
33pub use self::grid::{GridTemplateArea, NamedGridLine, TemplateLineNames};
34#[cfg(feature = "grid")]
35pub(crate) use self::grid::{NonNamedGridPlacement, OriginZeroGridPlacement};
36
37use crate::geometry::{Point, Rect, Size};
38use crate::style_helpers::TaffyAuto as _;
39use core::fmt::Debug;
40
41#[cfg(feature = "grid")]
42use crate::geometry::Line;
43#[cfg(feature = "serde")]
44use crate::style_helpers;
45#[cfg(feature = "grid")]
46use crate::util::sys::GridTrackVec;
47
48use crate::sys::String;
49
50#[cfg(any(feature = "alloc", feature = "std"))]
53pub trait CheapCloneStr:
54    AsRef<str> + for<'a> From<&'a str> + From<String> + PartialEq + Eq + Clone + Default + Debug + 'static
55{
56}
57#[cfg(any(feature = "alloc", feature = "std"))]
58impl<T> CheapCloneStr for T where
59    T: AsRef<str> + for<'a> From<&'a str> + From<String> + PartialEq + Eq + Clone + Default + Debug + 'static
60{
61}
62
63#[cfg(not(any(feature = "alloc", feature = "std")))]
66pub trait CheapCloneStr {}
67#[cfg(not(any(feature = "alloc", feature = "std")))]
68impl<T> CheapCloneStr for T {}
69
70pub trait CoreStyle {
76    type CustomIdent: CheapCloneStr;
78
79    #[inline(always)]
81    fn box_generation_mode(&self) -> BoxGenerationMode {
82        BoxGenerationMode::DEFAULT
83    }
84    #[inline(always)]
86    fn is_block(&self) -> bool {
87        false
88    }
89    #[inline(always)]
92    fn is_compressible_replaced(&self) -> bool {
93        false
94    }
95    #[inline(always)]
97    fn box_sizing(&self) -> BoxSizing {
98        BoxSizing::BorderBox
99    }
100
101    #[inline(always)]
104    fn overflow(&self) -> Point<Overflow> {
105        Style::<Self::CustomIdent>::DEFAULT.overflow
106    }
107    #[inline(always)]
109    fn scrollbar_width(&self) -> f32 {
110        0.0
111    }
112
113    #[inline(always)]
116    fn position(&self) -> Position {
117        Style::<Self::CustomIdent>::DEFAULT.position
118    }
119    #[inline(always)]
121    fn inset(&self) -> Rect<LengthPercentageAuto> {
122        Style::<Self::CustomIdent>::DEFAULT.inset
123    }
124
125    #[inline(always)]
128    fn size(&self) -> Size<Dimension> {
129        Style::<Self::CustomIdent>::DEFAULT.size
130    }
131    #[inline(always)]
133    fn min_size(&self) -> Size<Dimension> {
134        Style::<Self::CustomIdent>::DEFAULT.min_size
135    }
136    #[inline(always)]
138    fn max_size(&self) -> Size<Dimension> {
139        Style::<Self::CustomIdent>::DEFAULT.max_size
140    }
141    #[inline(always)]
144    fn aspect_ratio(&self) -> Option<f32> {
145        Style::<Self::CustomIdent>::DEFAULT.aspect_ratio
146    }
147
148    #[inline(always)]
151    fn margin(&self) -> Rect<LengthPercentageAuto> {
152        Style::<Self::CustomIdent>::DEFAULT.margin
153    }
154    #[inline(always)]
156    fn padding(&self) -> Rect<LengthPercentage> {
157        Style::<Self::CustomIdent>::DEFAULT.padding
158    }
159    #[inline(always)]
161    fn border(&self) -> Rect<LengthPercentage> {
162        Style::<Self::CustomIdent>::DEFAULT.border
163    }
164}
165
166#[derive(Copy, Clone, PartialEq, Eq, Debug)]
170#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
171pub enum Display {
172    #[cfg(feature = "block_layout")]
174    Block,
175    #[cfg(feature = "flexbox")]
177    Flex,
178    #[cfg(feature = "grid")]
180    Grid,
181    None,
183}
184
185impl Display {
186    #[cfg(feature = "flexbox")]
188    pub const DEFAULT: Display = Display::Flex;
189
190    #[cfg(all(feature = "grid", not(feature = "flexbox")))]
192    pub const DEFAULT: Display = Display::Grid;
193
194    #[cfg(all(feature = "block_layout", not(feature = "flexbox"), not(feature = "grid")))]
196    pub const DEFAULT: Display = Display::Block;
197
198    #[cfg(all(not(feature = "flexbox"), not(feature = "grid"), not(feature = "block_layout")))]
200    pub const DEFAULT: Display = Display::None;
201}
202
203impl Default for Display {
204    fn default() -> Self {
205        Self::DEFAULT
206    }
207}
208
209impl core::fmt::Display for Display {
210    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
211        match self {
212            Display::None => write!(f, "NONE"),
213            #[cfg(feature = "block_layout")]
214            Display::Block => write!(f, "BLOCK"),
215            #[cfg(feature = "flexbox")]
216            Display::Flex => write!(f, "FLEX"),
217            #[cfg(feature = "grid")]
218            Display::Grid => write!(f, "GRID"),
219        }
220    }
221}
222
223#[derive(Copy, Clone, PartialEq, Eq, Debug)]
226#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
227pub enum BoxGenerationMode {
228    Normal,
230    None,
232}
233
234impl BoxGenerationMode {
235    pub const DEFAULT: BoxGenerationMode = BoxGenerationMode::Normal;
237}
238
239impl Default for BoxGenerationMode {
240    fn default() -> Self {
241        Self::DEFAULT
242    }
243}
244
245#[derive(Copy, Clone, PartialEq, Eq, Debug)]
255#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
256pub enum Position {
257    Relative,
260    Absolute,
266}
267
268impl Default for Position {
269    fn default() -> Self {
270        Self::Relative
271    }
272}
273
274#[derive(Copy, Clone, PartialEq, Eq, Debug)]
288#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
289pub enum BoxSizing {
290    BorderBox,
292    ContentBox,
294}
295
296impl Default for BoxSizing {
297    fn default() -> Self {
298        Self::BorderBox
299    }
300}
301
302#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
316#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
317pub enum Overflow {
318    #[default]
321    Visible,
322    Clip,
325    Hidden,
328    Scroll,
332}
333
334impl Overflow {
335    #[inline(always)]
338    pub(crate) fn is_scroll_container(self) -> bool {
339        match self {
340            Self::Visible | Self::Clip => false,
341            Self::Hidden | Self::Scroll => true,
342        }
343    }
344
345    #[inline(always)]
348    pub(crate) fn maybe_into_automatic_min_size(self) -> Option<f32> {
349        match self.is_scroll_container() {
350            true => Some(0.0),
351            false => None,
352        }
353    }
354}
355
356#[derive(Clone, PartialEq, Debug)]
371#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
372#[cfg_attr(feature = "serde", serde(default))]
373pub struct Style<S: CheapCloneStr = DefaultCheapStr> {
374    pub dummy: core::marker::PhantomData<S>,
377    pub display: Display,
379    pub item_is_table: bool,
382    pub item_is_replaced: bool,
385    pub box_sizing: BoxSizing,
387
388    pub overflow: Point<Overflow>,
391    pub scrollbar_width: f32,
393
394    pub position: Position,
397    #[cfg_attr(feature = "serde", serde(default = "style_helpers::auto"))]
399    pub inset: Rect<LengthPercentageAuto>,
400
401    #[cfg_attr(feature = "serde", serde(default = "style_helpers::auto"))]
404    pub size: Size<Dimension>,
405    #[cfg_attr(feature = "serde", serde(default = "style_helpers::auto"))]
407    pub min_size: Size<Dimension>,
408    #[cfg_attr(feature = "serde", serde(default = "style_helpers::auto"))]
410    pub max_size: Size<Dimension>,
411    pub aspect_ratio: Option<f32>,
415
416    #[cfg_attr(feature = "serde", serde(default = "style_helpers::zero"))]
419    pub margin: Rect<LengthPercentageAuto>,
420    #[cfg_attr(feature = "serde", serde(default = "style_helpers::zero"))]
422    pub padding: Rect<LengthPercentage>,
423    #[cfg_attr(feature = "serde", serde(default = "style_helpers::zero"))]
425    pub border: Rect<LengthPercentage>,
426
427    #[cfg(any(feature = "flexbox", feature = "grid"))]
430    pub align_items: Option<AlignItems>,
431    #[cfg(any(feature = "flexbox", feature = "grid"))]
434    pub align_self: Option<AlignSelf>,
435    #[cfg(feature = "grid")]
437    pub justify_items: Option<AlignItems>,
438    #[cfg(feature = "grid")]
441    pub justify_self: Option<AlignSelf>,
442    #[cfg(any(feature = "flexbox", feature = "grid"))]
444    pub align_content: Option<AlignContent>,
445    #[cfg(any(feature = "flexbox", feature = "grid"))]
447    pub justify_content: Option<JustifyContent>,
448    #[cfg(any(feature = "flexbox", feature = "grid"))]
450    #[cfg_attr(feature = "serde", serde(default = "style_helpers::zero"))]
451    pub gap: Size<LengthPercentage>,
452
453    #[cfg(feature = "block_layout")]
456    pub text_align: TextAlign,
457
458    #[cfg(feature = "flexbox")]
461    pub flex_direction: FlexDirection,
462    #[cfg(feature = "flexbox")]
464    pub flex_wrap: FlexWrap,
465
466    #[cfg(feature = "flexbox")]
469    pub flex_basis: Dimension,
470    #[cfg(feature = "flexbox")]
474    pub flex_grow: f32,
475    #[cfg(feature = "flexbox")]
479    pub flex_shrink: f32,
480
481    #[cfg(feature = "grid")]
484    pub grid_template_rows: GridTrackVec<GridTemplateComponent<S>>,
485    #[cfg(feature = "grid")]
487    pub grid_template_columns: GridTrackVec<GridTemplateComponent<S>>,
488    #[cfg(feature = "grid")]
490    pub grid_auto_rows: GridTrackVec<TrackSizingFunction>,
491    #[cfg(feature = "grid")]
493    pub grid_auto_columns: GridTrackVec<TrackSizingFunction>,
494    #[cfg(feature = "grid")]
496    pub grid_auto_flow: GridAutoFlow,
497
498    #[cfg(feature = "grid")]
501    pub grid_template_areas: GridTrackVec<GridTemplateArea<S>>,
502    #[cfg(feature = "grid")]
504    pub grid_template_column_names: GridTrackVec<GridTrackVec<S>>,
505    #[cfg(feature = "grid")]
507    pub grid_template_row_names: GridTrackVec<GridTrackVec<S>>,
508
509    #[cfg(feature = "grid")]
512    pub grid_row: Line<GridPlacement<S>>,
513    #[cfg(feature = "grid")]
515    pub grid_column: Line<GridPlacement<S>>,
516}
517
518impl<S: CheapCloneStr> Style<S> {
519    pub const DEFAULT: Style<S> = Style {
521        dummy: core::marker::PhantomData,
522        display: Display::DEFAULT,
523        item_is_table: false,
524        item_is_replaced: false,
525        box_sizing: BoxSizing::BorderBox,
526        overflow: Point { x: Overflow::Visible, y: Overflow::Visible },
527        scrollbar_width: 0.0,
528        position: Position::Relative,
529        inset: Rect::auto(),
530        margin: Rect::zero(),
531        padding: Rect::zero(),
532        border: Rect::zero(),
533        size: Size::auto(),
534        min_size: Size::auto(),
535        max_size: Size::auto(),
536        aspect_ratio: None,
537        #[cfg(any(feature = "flexbox", feature = "grid"))]
538        gap: Size::zero(),
539        #[cfg(any(feature = "flexbox", feature = "grid"))]
541        align_items: None,
542        #[cfg(any(feature = "flexbox", feature = "grid"))]
543        align_self: None,
544        #[cfg(feature = "grid")]
545        justify_items: None,
546        #[cfg(feature = "grid")]
547        justify_self: None,
548        #[cfg(any(feature = "flexbox", feature = "grid"))]
549        align_content: None,
550        #[cfg(any(feature = "flexbox", feature = "grid"))]
551        justify_content: None,
552        #[cfg(feature = "block_layout")]
554        text_align: TextAlign::Auto,
555        #[cfg(feature = "flexbox")]
557        flex_direction: FlexDirection::Row,
558        #[cfg(feature = "flexbox")]
559        flex_wrap: FlexWrap::NoWrap,
560        #[cfg(feature = "flexbox")]
561        flex_grow: 0.0,
562        #[cfg(feature = "flexbox")]
563        flex_shrink: 1.0,
564        #[cfg(feature = "flexbox")]
565        flex_basis: Dimension::AUTO,
566        #[cfg(feature = "grid")]
568        grid_template_rows: GridTrackVec::new(),
569        #[cfg(feature = "grid")]
570        grid_template_columns: GridTrackVec::new(),
571        #[cfg(feature = "grid")]
572        grid_template_areas: GridTrackVec::new(),
573        #[cfg(feature = "grid")]
574        grid_template_column_names: GridTrackVec::new(),
575        #[cfg(feature = "grid")]
576        grid_template_row_names: GridTrackVec::new(),
577        #[cfg(feature = "grid")]
578        grid_auto_rows: GridTrackVec::new(),
579        #[cfg(feature = "grid")]
580        grid_auto_columns: GridTrackVec::new(),
581        #[cfg(feature = "grid")]
582        grid_auto_flow: GridAutoFlow::Row,
583        #[cfg(feature = "grid")]
584        grid_row: Line { start: GridPlacement::<S>::Auto, end: GridPlacement::<S>::Auto },
585        #[cfg(feature = "grid")]
586        grid_column: Line { start: GridPlacement::<S>::Auto, end: GridPlacement::<S>::Auto },
587    };
588}
589
590impl<S: CheapCloneStr> Default for Style<S> {
591    fn default() -> Self {
592        Style::DEFAULT
593    }
594}
595
596impl<S: CheapCloneStr> CoreStyle for Style<S> {
597    type CustomIdent = S;
598
599    #[inline(always)]
600    fn box_generation_mode(&self) -> BoxGenerationMode {
601        match self.display {
602            Display::None => BoxGenerationMode::None,
603            _ => BoxGenerationMode::Normal,
604        }
605    }
606    #[inline(always)]
607    #[cfg(feature = "block_layout")]
608    fn is_block(&self) -> bool {
609        matches!(self.display, Display::Block)
610    }
611    #[inline(always)]
612    fn is_compressible_replaced(&self) -> bool {
613        self.item_is_replaced
614    }
615    #[inline(always)]
616    fn box_sizing(&self) -> BoxSizing {
617        self.box_sizing
618    }
619    #[inline(always)]
620    fn overflow(&self) -> Point<Overflow> {
621        self.overflow
622    }
623    #[inline(always)]
624    fn scrollbar_width(&self) -> f32 {
625        self.scrollbar_width
626    }
627    #[inline(always)]
628    fn position(&self) -> Position {
629        self.position
630    }
631    #[inline(always)]
632    fn inset(&self) -> Rect<LengthPercentageAuto> {
633        self.inset
634    }
635    #[inline(always)]
636    fn size(&self) -> Size<Dimension> {
637        self.size
638    }
639    #[inline(always)]
640    fn min_size(&self) -> Size<Dimension> {
641        self.min_size
642    }
643    #[inline(always)]
644    fn max_size(&self) -> Size<Dimension> {
645        self.max_size
646    }
647    #[inline(always)]
648    fn aspect_ratio(&self) -> Option<f32> {
649        self.aspect_ratio
650    }
651    #[inline(always)]
652    fn margin(&self) -> Rect<LengthPercentageAuto> {
653        self.margin
654    }
655    #[inline(always)]
656    fn padding(&self) -> Rect<LengthPercentage> {
657        self.padding
658    }
659    #[inline(always)]
660    fn border(&self) -> Rect<LengthPercentage> {
661        self.border
662    }
663}
664
665impl<T: CoreStyle> CoreStyle for &'_ T {
666    type CustomIdent = T::CustomIdent;
667
668    #[inline(always)]
669    fn box_generation_mode(&self) -> BoxGenerationMode {
670        (*self).box_generation_mode()
671    }
672    #[inline(always)]
673    fn is_block(&self) -> bool {
674        (*self).is_block()
675    }
676    #[inline(always)]
677    fn is_compressible_replaced(&self) -> bool {
678        (*self).is_compressible_replaced()
679    }
680    #[inline(always)]
681    fn box_sizing(&self) -> BoxSizing {
682        (*self).box_sizing()
683    }
684    #[inline(always)]
685    fn overflow(&self) -> Point<Overflow> {
686        (*self).overflow()
687    }
688    #[inline(always)]
689    fn scrollbar_width(&self) -> f32 {
690        (*self).scrollbar_width()
691    }
692    #[inline(always)]
693    fn position(&self) -> Position {
694        (*self).position()
695    }
696    #[inline(always)]
697    fn inset(&self) -> Rect<LengthPercentageAuto> {
698        (*self).inset()
699    }
700    #[inline(always)]
701    fn size(&self) -> Size<Dimension> {
702        (*self).size()
703    }
704    #[inline(always)]
705    fn min_size(&self) -> Size<Dimension> {
706        (*self).min_size()
707    }
708    #[inline(always)]
709    fn max_size(&self) -> Size<Dimension> {
710        (*self).max_size()
711    }
712    #[inline(always)]
713    fn aspect_ratio(&self) -> Option<f32> {
714        (*self).aspect_ratio()
715    }
716    #[inline(always)]
717    fn margin(&self) -> Rect<LengthPercentageAuto> {
718        (*self).margin()
719    }
720    #[inline(always)]
721    fn padding(&self) -> Rect<LengthPercentage> {
722        (*self).padding()
723    }
724    #[inline(always)]
725    fn border(&self) -> Rect<LengthPercentage> {
726        (*self).border()
727    }
728}
729
730#[cfg(feature = "block_layout")]
731impl<S: CheapCloneStr> BlockContainerStyle for Style<S> {
732    #[inline(always)]
733    fn text_align(&self) -> TextAlign {
734        self.text_align
735    }
736}
737
738#[cfg(feature = "block_layout")]
739impl<T: BlockContainerStyle> BlockContainerStyle for &'_ T {
740    #[inline(always)]
741    fn text_align(&self) -> TextAlign {
742        (*self).text_align()
743    }
744}
745
746#[cfg(feature = "block_layout")]
747impl<S: CheapCloneStr> BlockItemStyle for Style<S> {
748    #[inline(always)]
749    fn is_table(&self) -> bool {
750        self.item_is_table
751    }
752}
753
754#[cfg(feature = "block_layout")]
755impl<T: BlockItemStyle> BlockItemStyle for &'_ T {
756    #[inline(always)]
757    fn is_table(&self) -> bool {
758        (*self).is_table()
759    }
760}
761
762#[cfg(feature = "flexbox")]
763impl<S: CheapCloneStr> FlexboxContainerStyle for Style<S> {
764    #[inline(always)]
765    fn flex_direction(&self) -> FlexDirection {
766        self.flex_direction
767    }
768    #[inline(always)]
769    fn flex_wrap(&self) -> FlexWrap {
770        self.flex_wrap
771    }
772    #[inline(always)]
773    fn gap(&self) -> Size<LengthPercentage> {
774        self.gap
775    }
776    #[inline(always)]
777    fn align_content(&self) -> Option<AlignContent> {
778        self.align_content
779    }
780    #[inline(always)]
781    fn align_items(&self) -> Option<AlignItems> {
782        self.align_items
783    }
784    #[inline(always)]
785    fn justify_content(&self) -> Option<JustifyContent> {
786        self.justify_content
787    }
788}
789
790#[cfg(feature = "flexbox")]
791impl<T: FlexboxContainerStyle> FlexboxContainerStyle for &'_ T {
792    #[inline(always)]
793    fn flex_direction(&self) -> FlexDirection {
794        (*self).flex_direction()
795    }
796    #[inline(always)]
797    fn flex_wrap(&self) -> FlexWrap {
798        (*self).flex_wrap()
799    }
800    #[inline(always)]
801    fn gap(&self) -> Size<LengthPercentage> {
802        (*self).gap()
803    }
804    #[inline(always)]
805    fn align_content(&self) -> Option<AlignContent> {
806        (*self).align_content()
807    }
808    #[inline(always)]
809    fn align_items(&self) -> Option<AlignItems> {
810        (*self).align_items()
811    }
812    #[inline(always)]
813    fn justify_content(&self) -> Option<JustifyContent> {
814        (*self).justify_content()
815    }
816}
817
818#[cfg(feature = "flexbox")]
819impl<S: CheapCloneStr> FlexboxItemStyle for Style<S> {
820    #[inline(always)]
821    fn flex_basis(&self) -> Dimension {
822        self.flex_basis
823    }
824    #[inline(always)]
825    fn flex_grow(&self) -> f32 {
826        self.flex_grow
827    }
828    #[inline(always)]
829    fn flex_shrink(&self) -> f32 {
830        self.flex_shrink
831    }
832    #[inline(always)]
833    fn align_self(&self) -> Option<AlignSelf> {
834        self.align_self
835    }
836}
837
838#[cfg(feature = "flexbox")]
839impl<T: FlexboxItemStyle> FlexboxItemStyle for &'_ T {
840    #[inline(always)]
841    fn flex_basis(&self) -> Dimension {
842        (*self).flex_basis()
843    }
844    #[inline(always)]
845    fn flex_grow(&self) -> f32 {
846        (*self).flex_grow()
847    }
848    #[inline(always)]
849    fn flex_shrink(&self) -> f32 {
850        (*self).flex_shrink()
851    }
852    #[inline(always)]
853    fn align_self(&self) -> Option<AlignSelf> {
854        (*self).align_self()
855    }
856}
857
858#[cfg(feature = "grid")]
859impl<S: CheapCloneStr> GridContainerStyle for Style<S> {
860    type Repetition<'a>
861        = &'a GridTemplateRepetition<S>
862    where
863        Self: 'a;
864
865    type TemplateTrackList<'a>
866        = core::iter::Map<
867        core::slice::Iter<'a, GridTemplateComponent<S>>,
868        fn(&'a GridTemplateComponent<S>) -> GenericGridTemplateComponent<S, &'a GridTemplateRepetition<S>>,
869    >
870    where
871        Self: 'a;
872
873    type AutoTrackList<'a>
874        = core::iter::Copied<core::slice::Iter<'a, TrackSizingFunction>>
875    where
876        Self: 'a;
877
878    #[cfg(feature = "grid")]
879    type TemplateLineNames<'a>
880        = core::iter::Map<core::slice::Iter<'a, GridTrackVec<S>>, fn(&GridTrackVec<S>) -> core::slice::Iter<'_, S>>
881    where
882        Self: 'a;
883    #[cfg(feature = "grid")]
884    type GridTemplateAreas<'a>
885        = core::iter::Cloned<core::slice::Iter<'a, GridTemplateArea<S>>>
886    where
887        Self: 'a;
888
889    #[inline(always)]
890    fn grid_template_rows(&self) -> Option<Self::TemplateTrackList<'_>> {
891        Some(self.grid_template_rows.iter().map(|c| c.as_component_ref()))
892    }
893    #[inline(always)]
894    fn grid_template_columns(&self) -> Option<Self::TemplateTrackList<'_>> {
895        Some(self.grid_template_columns.iter().map(|c| c.as_component_ref()))
896    }
897    #[inline(always)]
898    fn grid_auto_rows(&self) -> Self::AutoTrackList<'_> {
899        self.grid_auto_rows.iter().copied()
900    }
901    #[inline(always)]
902    fn grid_auto_columns(&self) -> Self::AutoTrackList<'_> {
903        self.grid_auto_columns.iter().copied()
904    }
905    #[inline(always)]
906    fn grid_auto_flow(&self) -> GridAutoFlow {
907        self.grid_auto_flow
908    }
909    #[inline(always)]
910    fn gap(&self) -> Size<LengthPercentage> {
911        self.gap
912    }
913    #[inline(always)]
914    fn align_content(&self) -> Option<AlignContent> {
915        self.align_content
916    }
917    #[inline(always)]
918    fn justify_content(&self) -> Option<JustifyContent> {
919        self.justify_content
920    }
921    #[inline(always)]
922    fn align_items(&self) -> Option<AlignItems> {
923        self.align_items
924    }
925    #[inline(always)]
926    fn justify_items(&self) -> Option<AlignItems> {
927        self.justify_items
928    }
929
930    #[inline(always)]
931    #[cfg(feature = "grid")]
932    fn grid_template_areas(&self) -> Option<Self::GridTemplateAreas<'_>> {
933        Some(self.grid_template_areas.iter().cloned())
934    }
935
936    #[inline(always)]
937    #[cfg(feature = "grid")]
938    fn grid_template_column_names(&self) -> Option<Self::TemplateLineNames<'_>> {
939        Some(self.grid_template_column_names.iter().map(|names| names.iter()))
940    }
941
942    #[inline(always)]
943    #[cfg(feature = "grid")]
944    fn grid_template_row_names(&self) -> Option<Self::TemplateLineNames<'_>> {
945        Some(self.grid_template_row_names.iter().map(|names| names.iter()))
946    }
947}
948
949#[cfg(feature = "grid")]
950impl<T: GridContainerStyle> GridContainerStyle for &'_ T {
951    type Repetition<'a>
952        = T::Repetition<'a>
953    where
954        Self: 'a;
955
956    type TemplateTrackList<'a>
957        = T::TemplateTrackList<'a>
958    where
959        Self: 'a;
960
961    type AutoTrackList<'a>
962        = T::AutoTrackList<'a>
963    where
964        Self: 'a;
965
966    #[cfg(feature = "grid")]
968    type TemplateLineNames<'a>
969        = T::TemplateLineNames<'a>
970    where
971        Self: 'a;
972    #[cfg(feature = "grid")]
973    type GridTemplateAreas<'a>
974        = T::GridTemplateAreas<'a>
975    where
976        Self: 'a;
977
978    #[inline(always)]
979    fn grid_template_rows(&self) -> Option<Self::TemplateTrackList<'_>> {
980        (*self).grid_template_rows()
981    }
982    #[inline(always)]
983    fn grid_template_columns(&self) -> Option<Self::TemplateTrackList<'_>> {
984        (*self).grid_template_columns()
985    }
986    #[inline(always)]
987    fn grid_auto_rows(&self) -> Self::AutoTrackList<'_> {
988        (*self).grid_auto_rows()
989    }
990    #[inline(always)]
991    fn grid_auto_columns(&self) -> Self::AutoTrackList<'_> {
992        (*self).grid_auto_columns()
993    }
994    #[cfg(feature = "grid")]
995    #[inline(always)]
996    fn grid_template_areas(&self) -> Option<Self::GridTemplateAreas<'_>> {
997        (*self).grid_template_areas()
998    }
999    #[cfg(feature = "grid")]
1000    #[inline(always)]
1001    fn grid_template_column_names(&self) -> Option<Self::TemplateLineNames<'_>> {
1002        (*self).grid_template_column_names()
1003    }
1004    #[cfg(feature = "grid")]
1005    #[inline(always)]
1006    fn grid_template_row_names(&self) -> Option<Self::TemplateLineNames<'_>> {
1007        (*self).grid_template_row_names()
1008    }
1009    #[inline(always)]
1010    fn grid_auto_flow(&self) -> GridAutoFlow {
1011        (*self).grid_auto_flow()
1012    }
1013    #[inline(always)]
1014    fn gap(&self) -> Size<LengthPercentage> {
1015        (*self).gap()
1016    }
1017    #[inline(always)]
1018    fn align_content(&self) -> Option<AlignContent> {
1019        (*self).align_content()
1020    }
1021    #[inline(always)]
1022    fn justify_content(&self) -> Option<JustifyContent> {
1023        (*self).justify_content()
1024    }
1025    #[inline(always)]
1026    fn align_items(&self) -> Option<AlignItems> {
1027        (*self).align_items()
1028    }
1029    #[inline(always)]
1030    fn justify_items(&self) -> Option<AlignItems> {
1031        (*self).justify_items()
1032    }
1033}
1034
1035#[cfg(feature = "grid")]
1036impl<S: CheapCloneStr> GridItemStyle for Style<S> {
1037    #[inline(always)]
1038    fn grid_row(&self) -> Line<GridPlacement<S>> {
1039        self.grid_row.clone()
1041    }
1042    #[inline(always)]
1043    fn grid_column(&self) -> Line<GridPlacement<S>> {
1044        self.grid_column.clone()
1046    }
1047    #[inline(always)]
1048    fn align_self(&self) -> Option<AlignSelf> {
1049        self.align_self
1050    }
1051    #[inline(always)]
1052    fn justify_self(&self) -> Option<AlignSelf> {
1053        self.justify_self
1054    }
1055}
1056
1057#[cfg(feature = "grid")]
1058impl<T: GridItemStyle> GridItemStyle for &'_ T {
1059    #[inline(always)]
1060    fn grid_row(&self) -> Line<GridPlacement<Self::CustomIdent>> {
1061        (*self).grid_row()
1062    }
1063    #[inline(always)]
1064    fn grid_column(&self) -> Line<GridPlacement<Self::CustomIdent>> {
1065        (*self).grid_column()
1066    }
1067    #[inline(always)]
1068    fn align_self(&self) -> Option<AlignSelf> {
1069        (*self).align_self()
1070    }
1071    #[inline(always)]
1072    fn justify_self(&self) -> Option<AlignSelf> {
1073        (*self).justify_self()
1074    }
1075}
1076
1077#[cfg(test)]
1078mod tests {
1079    use std::sync::Arc;
1080
1081    use super::Style;
1082    use crate::sys::DefaultCheapStr;
1083    use crate::{geometry::*, style_helpers::TaffyAuto as _};
1084
1085    #[test]
1086    fn defaults_match() {
1087        #[cfg(feature = "grid")]
1088        use super::GridPlacement;
1089
1090        let old_defaults: Style<DefaultCheapStr> = Style {
1091            dummy: core::marker::PhantomData,
1092            display: Default::default(),
1093            item_is_table: false,
1094            item_is_replaced: false,
1095            box_sizing: Default::default(),
1096            overflow: Default::default(),
1097            scrollbar_width: 0.0,
1098            position: Default::default(),
1099            #[cfg(feature = "flexbox")]
1100            flex_direction: Default::default(),
1101            #[cfg(feature = "flexbox")]
1102            flex_wrap: Default::default(),
1103            #[cfg(any(feature = "flexbox", feature = "grid"))]
1104            align_items: Default::default(),
1105            #[cfg(any(feature = "flexbox", feature = "grid"))]
1106            align_self: Default::default(),
1107            #[cfg(feature = "grid")]
1108            justify_items: Default::default(),
1109            #[cfg(feature = "grid")]
1110            justify_self: Default::default(),
1111            #[cfg(any(feature = "flexbox", feature = "grid"))]
1112            align_content: Default::default(),
1113            #[cfg(any(feature = "flexbox", feature = "grid"))]
1114            justify_content: Default::default(),
1115            inset: Rect::auto(),
1116            margin: Rect::zero(),
1117            padding: Rect::zero(),
1118            border: Rect::zero(),
1119            gap: Size::zero(),
1120            #[cfg(feature = "block_layout")]
1121            text_align: Default::default(),
1122            #[cfg(feature = "flexbox")]
1123            flex_grow: 0.0,
1124            #[cfg(feature = "flexbox")]
1125            flex_shrink: 1.0,
1126            #[cfg(feature = "flexbox")]
1127            flex_basis: super::Dimension::AUTO,
1128            size: Size::auto(),
1129            min_size: Size::auto(),
1130            max_size: Size::auto(),
1131            aspect_ratio: Default::default(),
1132            #[cfg(feature = "grid")]
1133            grid_template_rows: Default::default(),
1134            #[cfg(feature = "grid")]
1135            grid_template_columns: Default::default(),
1136            #[cfg(feature = "grid")]
1137            grid_template_row_names: Default::default(),
1138            #[cfg(feature = "grid")]
1139            grid_template_column_names: Default::default(),
1140            #[cfg(feature = "grid")]
1141            grid_template_areas: Default::default(),
1142            #[cfg(feature = "grid")]
1143            grid_auto_rows: Default::default(),
1144            #[cfg(feature = "grid")]
1145            grid_auto_columns: Default::default(),
1146            #[cfg(feature = "grid")]
1147            grid_auto_flow: Default::default(),
1148            #[cfg(feature = "grid")]
1149            grid_row: Line { start: GridPlacement::Auto, end: GridPlacement::Auto },
1150            #[cfg(feature = "grid")]
1151            grid_column: Line { start: GridPlacement::Auto, end: GridPlacement::Auto },
1152        };
1153
1154        assert_eq!(Style::DEFAULT, Style::<DefaultCheapStr>::default());
1155        assert_eq!(Style::DEFAULT, old_defaults);
1156    }
1157
1158    #[test]
1161    fn style_sizes() {
1162        use super::*;
1163        type S = crate::sys::DefaultCheapStr;
1164
1165        fn assert_type_size<T>(expected_size: usize) {
1166            let name = ::core::any::type_name::<T>();
1167            let name = name.replace("taffy::geometry::", "");
1168            let name = name.replace("taffy::style::dimension::", "");
1169            let name = name.replace("taffy::style::alignment::", "");
1170            let name = name.replace("taffy::style::flex::", "");
1171            let name = name.replace("taffy::style::grid::", "");
1172
1173            assert_eq!(
1174                ::core::mem::size_of::<T>(),
1175                expected_size,
1176                "Expected {} for be {} byte(s) but it was {} byte(s)",
1177                name,
1178                expected_size,
1179                ::core::mem::size_of::<T>(),
1180            );
1181        }
1182
1183        assert_type_size::<Display>(1);
1185        assert_type_size::<BoxSizing>(1);
1186        assert_type_size::<Position>(1);
1187        assert_type_size::<Overflow>(1);
1188
1189        assert_type_size::<f32>(4);
1191        assert_type_size::<LengthPercentage>(8);
1192        assert_type_size::<LengthPercentageAuto>(8);
1193        assert_type_size::<Dimension>(8);
1194        assert_type_size::<Size<LengthPercentage>>(16);
1195        assert_type_size::<Size<LengthPercentageAuto>>(16);
1196        assert_type_size::<Size<Dimension>>(16);
1197        assert_type_size::<Rect<LengthPercentage>>(32);
1198        assert_type_size::<Rect<LengthPercentageAuto>>(32);
1199        assert_type_size::<Rect<Dimension>>(32);
1200
1201        assert_type_size::<AlignContent>(1);
1203        assert_type_size::<AlignItems>(1);
1204        assert_type_size::<Option<AlignItems>>(1);
1205
1206        assert_type_size::<FlexDirection>(1);
1208        assert_type_size::<FlexWrap>(1);
1209
1210        assert_type_size::<GridAutoFlow>(1);
1212        assert_type_size::<MinTrackSizingFunction>(8);
1213        assert_type_size::<MaxTrackSizingFunction>(8);
1214        assert_type_size::<TrackSizingFunction>(16);
1215        assert_type_size::<Vec<TrackSizingFunction>>(24);
1216        assert_type_size::<Vec<GridTemplateComponent<S>>>(24);
1217
1218        assert_type_size::<GridTemplateComponent<String>>(56);
1220        assert_type_size::<GridPlacement<String>>(32);
1221        assert_type_size::<Line<GridPlacement<String>>>(64);
1222        assert_type_size::<Style<String>>(536);
1223
1224        assert_type_size::<GridTemplateComponent<Arc<str>>>(56);
1226        assert_type_size::<GridPlacement<Arc<str>>>(24);
1227        assert_type_size::<Line<GridPlacement<Arc<str>>>>(48);
1228        assert_type_size::<Style<Arc<str>>>(504);
1229    }
1230}