1mod layout;
5mod stylo_taffy;
6use std::fmt;
7
8use malloc_size_of_derive::MallocSizeOf;
9use script::layout_dom::ServoLayoutNode;
10use servo_arc::Arc;
11use style::Atom;
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) taffy_baselines: taffy::Baselines,
87 pub(crate) child_fragments: Vec<Fragment>,
88 pub(crate) positioning_context: PositioningContext,
89 pub(crate) style: Arc<ComputedValues>,
90 pub(crate) taffy_level_box: TaffyItemBoxInner,
91}
92
93#[expect(clippy::large_enum_variant)]
94#[derive(Debug, MallocSizeOf)]
95pub(crate) enum TaffyItemBoxInner {
96 InFlowBox(IndependentFormattingContext),
97 OutOfFlowAbsolutelyPositionedBox(ArcRefCell<AbsolutelyPositionedBox>),
98}
99
100impl fmt::Debug for TaffyItemBox {
101 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
102 f.debug_struct("TaffyItemBox")
103 .field("taffy_layout", &self.taffy_layout)
104 .field("taffy_baselines", &self.taffy_baselines)
105 .field("child_fragments", &self.child_fragments.len())
106 .field("style", &self.style)
107 .field("taffy_level_box", &self.taffy_level_box)
108 .finish()
109 }
110}
111
112impl TaffyItemBox {
113 fn new(inner: TaffyItemBoxInner) -> Self {
114 let style: Arc<ComputedValues> = match &inner {
115 TaffyItemBoxInner::InFlowBox(item) => item.style().clone(),
116 TaffyItemBoxInner::OutOfFlowAbsolutelyPositionedBox(absbox) => {
117 (*absbox).borrow().context.style().clone()
118 },
119 };
120
121 Self {
122 taffy_layout: Default::default(),
123 taffy_baselines: taffy::Baselines::NONE,
124 child_fragments: Vec::new(),
125 positioning_context: PositioningContext::default(),
126 style,
127 taffy_level_box: inner,
128 }
129 }
130
131 pub(crate) fn with_base<T>(&self, callback: impl FnOnce(&LayoutBoxBase) -> T) -> T {
132 match self.taffy_level_box {
133 TaffyItemBoxInner::InFlowBox(ref independent_formatting_context) => {
134 callback(&independent_formatting_context.base)
135 },
136 TaffyItemBoxInner::OutOfFlowAbsolutelyPositionedBox(ref positioned_box) => {
137 callback(&positioned_box.borrow().context.base)
138 },
139 }
140 }
141
142 pub(crate) fn with_base_mut<T>(&mut self, callback: impl FnOnce(&mut LayoutBoxBase) -> T) -> T {
143 match &mut self.taffy_level_box {
144 TaffyItemBoxInner::InFlowBox(independent_formatting_context) => {
145 callback(&mut independent_formatting_context.base)
146 },
147 TaffyItemBoxInner::OutOfFlowAbsolutelyPositionedBox(positioned_box) => {
148 callback(&mut positioned_box.borrow_mut().context.base)
149 },
150 }
151 }
152
153 pub(crate) fn repair_style(
154 &mut self,
155 context: &SharedStyleContext,
156 node: &ServoLayoutNode,
157 new_style: &Arc<ComputedValues>,
158 ) {
159 self.style = new_style.clone();
160 match &mut self.taffy_level_box {
161 TaffyItemBoxInner::InFlowBox(independent_formatting_context) => {
162 independent_formatting_context.repair_style(context, node, new_style)
163 },
164 TaffyItemBoxInner::OutOfFlowAbsolutelyPositionedBox(positioned_box) => positioned_box
165 .borrow_mut()
166 .context
167 .repair_style(context, node, new_style),
168 }
169 }
170
171 fn is_in_flow_replaced(&self) -> bool {
172 match &self.taffy_level_box {
173 TaffyItemBoxInner::InFlowBox(fc) => fc.is_replaced(),
174 TaffyItemBoxInner::OutOfFlowAbsolutelyPositionedBox(_) => false,
175 }
176 }
177
178 pub(crate) fn attached_to_tree(&self, layout_box: WeakLayoutBox) {
179 match &self.taffy_level_box {
180 TaffyItemBoxInner::InFlowBox(formatting_context) => {
181 formatting_context.attached_to_tree(layout_box)
182 },
183 TaffyItemBoxInner::OutOfFlowAbsolutelyPositionedBox(positioned_box) => positioned_box
184 .borrow_mut()
185 .context
186 .attached_to_tree(layout_box),
187 }
188 }
189}
190
191#[derive(Clone, Debug, MallocSizeOf)]
193pub(crate) struct SpecificTaffyGridInfo {
194 pub info: taffy::DetailedGridInfo<Atom>,
195}
196
197impl SpecificTaffyGridInfo {
198 fn from_detailed_grid_layout(grid_info: taffy::DetailedGridInfo<Atom>) -> Self {
199 Self { info: grid_info }
200 }
201}