1mod layout;
5mod stylo_taffy;
6use std::fmt;
7
8use app_units::Au;
9use malloc_size_of_derive::MallocSizeOf;
10use script::layout_dom::ServoThreadSafeLayoutNode;
11use servo_arc::Arc;
12use style::context::SharedStyleContext;
13use style::properties::ComputedValues;
14use stylo_taffy::TaffyStyloStyle;
15
16use crate::PropagatedBoxTreeData;
17use crate::cell::ArcRefCell;
18use crate::construct_modern::{ModernContainerBuilder, ModernItemKind};
19use crate::context::LayoutContext;
20use crate::dom::{LayoutBox, WeakLayoutBox};
21use crate::dom_traversal::{NodeAndStyleInfo, NonReplacedContents};
22use crate::formatting_contexts::IndependentFormattingContext;
23use crate::fragment_tree::Fragment;
24use crate::layout_box_base::LayoutBoxBase;
25use crate::positioned::{AbsolutelyPositionedBox, PositioningContext};
26
27#[derive(Debug, MallocSizeOf)]
28pub(crate) struct TaffyContainer {
29 children: Vec<ArcRefCell<TaffyItemBox>>,
30 style: Arc<ComputedValues>,
31}
32
33impl TaffyContainer {
34 pub fn construct(
35 context: &LayoutContext,
36 info: &NodeAndStyleInfo,
37 contents: NonReplacedContents,
38 propagated_data: PropagatedBoxTreeData,
39 ) -> Self {
40 let mut builder = ModernContainerBuilder::new(context, info, propagated_data);
41 contents.traverse(context, info, &mut builder);
42 let items = builder.finish();
43
44 let children = items
45 .into_iter()
46 .map(|item| {
47 let taffy_item_box = match item.kind {
48 ModernItemKind::InFlow(independent_formatting_context) => {
49 ArcRefCell::new(TaffyItemBox::new(TaffyItemBoxInner::InFlowBox(
50 independent_formatting_context,
51 )))
52 },
53 ModernItemKind::OutOfFlow(independent_formatting_context) => {
54 let abs_pos_box = ArcRefCell::new(AbsolutelyPositionedBox::new(
55 independent_formatting_context,
56 ));
57 ArcRefCell::new(TaffyItemBox::new(
58 TaffyItemBoxInner::OutOfFlowAbsolutelyPositionedBox(abs_pos_box),
59 ))
60 },
61 ModernItemKind::ReusedBox(layout_box) => match layout_box {
62 LayoutBox::TaffyItemBox(taffy_item_box) => taffy_item_box,
63 _ => unreachable!("Undamaged taffy level element should be associated with taffy level box"),
64 },
65 };
66
67 item.box_slot.set(LayoutBox::TaffyItemBox(taffy_item_box.clone()));
68 taffy_item_box
69 })
70 .collect();
71
72 Self {
73 children,
74 style: info.style.clone(),
75 }
76 }
77
78 pub(crate) fn repair_style(&mut self, new_style: &Arc<ComputedValues>) {
79 self.style = new_style.clone();
80 }
81}
82
83#[derive(MallocSizeOf)]
84pub(crate) struct TaffyItemBox {
85 pub(crate) taffy_layout: taffy::Layout,
86 pub(crate) child_fragments: Vec<Fragment>,
87 pub(crate) positioning_context: PositioningContext,
88 pub(crate) style: Arc<ComputedValues>,
89 pub(crate) taffy_level_box: TaffyItemBoxInner,
90}
91
92#[expect(clippy::large_enum_variant)]
93#[derive(Debug, MallocSizeOf)]
94pub(crate) enum TaffyItemBoxInner {
95 InFlowBox(IndependentFormattingContext),
96 OutOfFlowAbsolutelyPositionedBox(ArcRefCell<AbsolutelyPositionedBox>),
97}
98
99impl fmt::Debug for TaffyItemBox {
100 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
101 f.debug_struct("TaffyItemBox")
102 .field("taffy_layout", &self.taffy_layout)
103 .field("child_fragments", &self.child_fragments.len())
104 .field("style", &self.style)
105 .field("taffy_level_box", &self.taffy_level_box)
106 .finish()
107 }
108}
109
110impl TaffyItemBox {
111 fn new(inner: TaffyItemBoxInner) -> Self {
112 let style: Arc<ComputedValues> = match &inner {
113 TaffyItemBoxInner::InFlowBox(item) => item.style().clone(),
114 TaffyItemBoxInner::OutOfFlowAbsolutelyPositionedBox(absbox) => {
115 (*absbox).borrow().context.style().clone()
116 },
117 };
118
119 Self {
120 taffy_layout: Default::default(),
121 child_fragments: Vec::new(),
122 positioning_context: PositioningContext::default(),
123 style,
124 taffy_level_box: inner,
125 }
126 }
127
128 pub(crate) fn with_base<T>(&self, callback: impl FnOnce(&LayoutBoxBase) -> T) -> T {
129 match self.taffy_level_box {
130 TaffyItemBoxInner::InFlowBox(ref independent_formatting_context) => {
131 callback(&independent_formatting_context.base)
132 },
133 TaffyItemBoxInner::OutOfFlowAbsolutelyPositionedBox(ref positioned_box) => {
134 callback(&positioned_box.borrow().context.base)
135 },
136 }
137 }
138
139 pub(crate) fn with_base_mut<T>(&mut self, callback: impl FnOnce(&mut LayoutBoxBase) -> T) -> T {
140 match &mut self.taffy_level_box {
141 TaffyItemBoxInner::InFlowBox(independent_formatting_context) => {
142 callback(&mut independent_formatting_context.base)
143 },
144 TaffyItemBoxInner::OutOfFlowAbsolutelyPositionedBox(positioned_box) => {
145 callback(&mut positioned_box.borrow_mut().context.base)
146 },
147 }
148 }
149
150 pub(crate) fn repair_style(
151 &mut self,
152 context: &SharedStyleContext,
153 node: &ServoThreadSafeLayoutNode,
154 new_style: &Arc<ComputedValues>,
155 ) {
156 self.style = new_style.clone();
157 match &mut self.taffy_level_box {
158 TaffyItemBoxInner::InFlowBox(independent_formatting_context) => {
159 independent_formatting_context.repair_style(context, node, new_style)
160 },
161 TaffyItemBoxInner::OutOfFlowAbsolutelyPositionedBox(positioned_box) => positioned_box
162 .borrow_mut()
163 .context
164 .repair_style(context, node, new_style),
165 }
166 }
167
168 fn is_in_flow_replaced(&self) -> bool {
169 match &self.taffy_level_box {
170 TaffyItemBoxInner::InFlowBox(fc) => fc.is_replaced(),
171 TaffyItemBoxInner::OutOfFlowAbsolutelyPositionedBox(_) => false,
172 }
173 }
174
175 pub(crate) fn attached_to_tree(&self, layout_box: WeakLayoutBox) {
176 match &self.taffy_level_box {
177 TaffyItemBoxInner::InFlowBox(formatting_context) => {
178 formatting_context.attached_to_tree(layout_box)
179 },
180 TaffyItemBoxInner::OutOfFlowAbsolutelyPositionedBox(positioned_box) => positioned_box
181 .borrow_mut()
182 .context
183 .attached_to_tree(layout_box),
184 }
185 }
186}
187
188#[derive(Clone, Debug, MallocSizeOf)]
190pub(crate) struct SpecificTaffyGridInfo {
191 pub rows: SpecificTaffyGridTrackInfo,
192 pub columns: SpecificTaffyGridTrackInfo,
193}
194
195impl SpecificTaffyGridInfo {
196 fn from_detailed_grid_layout(grid_info: taffy::DetailedGridInfo) -> Self {
197 Self {
198 rows: SpecificTaffyGridTrackInfo {
199 sizes: grid_info
200 .rows
201 .sizes
202 .iter()
203 .map(|size| Au::from_f32_px(*size))
204 .collect(),
205 },
206 columns: SpecificTaffyGridTrackInfo {
207 sizes: grid_info
208 .columns
209 .sizes
210 .iter()
211 .map(|size| Au::from_f32_px(*size))
212 .collect(),
213 },
214 }
215 }
216}
217
218#[derive(Clone, Debug, MallocSizeOf)]
219pub(crate) struct SpecificTaffyGridTrackInfo {
220 pub sizes: Box<[Au]>,
221}