layout/taffy/
mod.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4mod 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;
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 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                if let Some(box_slot) = item.box_slot {
68                    box_slot.set(LayoutBox::TaffyItemBox(box_.clone()));
69                }
70
71                box_
72            })
73            .collect();
74
75        Self {
76            children,
77            style: info.style.clone(),
78        }
79    }
80
81    pub(crate) fn repair_style(&mut self, new_style: &Arc<ComputedValues>) {
82        self.style = new_style.clone();
83    }
84}
85
86#[derive(MallocSizeOf)]
87pub(crate) struct TaffyItemBox {
88    pub(crate) taffy_layout: taffy::Layout,
89    pub(crate) child_fragments: Vec<Fragment>,
90    pub(crate) positioning_context: PositioningContext,
91    pub(crate) style: Arc<ComputedValues>,
92    pub(crate) taffy_level_box: TaffyItemBoxInner,
93}
94
95#[allow(clippy::large_enum_variant)]
96#[derive(Debug, MallocSizeOf)]
97pub(crate) enum TaffyItemBoxInner {
98    InFlowBox(IndependentFormattingContext),
99    OutOfFlowAbsolutelyPositionedBox(ArcRefCell<AbsolutelyPositionedBox>),
100}
101
102impl fmt::Debug for TaffyItemBox {
103    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
104        f.debug_struct("TaffyItemBox")
105            .field("taffy_layout", &self.taffy_layout)
106            .field("child_fragments", &self.child_fragments.len())
107            .field("style", &self.style)
108            .field("taffy_level_box", &self.taffy_level_box)
109            .finish()
110    }
111}
112
113impl TaffyItemBox {
114    fn new(inner: TaffyItemBoxInner) -> Self {
115        let style: Arc<ComputedValues> = match &inner {
116            TaffyItemBoxInner::InFlowBox(item) => item.style().clone(),
117            TaffyItemBoxInner::OutOfFlowAbsolutelyPositionedBox(absbox) => {
118                (*absbox).borrow().context.style().clone()
119            },
120        };
121
122        Self {
123            taffy_layout: Default::default(),
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: &ServoThreadSafeLayoutNode,
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
179/// Details from Taffy grid layout that will be stored
180#[derive(Clone, Debug, MallocSizeOf)]
181pub(crate) struct SpecificTaffyGridInfo {
182    pub rows: SpecificTaffyGridTrackInfo,
183    pub columns: SpecificTaffyGridTrackInfo,
184}
185
186impl SpecificTaffyGridInfo {
187    fn from_detailed_grid_layout(grid_info: taffy::DetailedGridInfo) -> Self {
188        Self {
189            rows: SpecificTaffyGridTrackInfo {
190                sizes: grid_info
191                    .rows
192                    .sizes
193                    .iter()
194                    .map(|size| Au::from_f32_px(*size))
195                    .collect(),
196            },
197            columns: SpecificTaffyGridTrackInfo {
198                sizes: grid_info
199                    .columns
200                    .sizes
201                    .iter()
202                    .map(|size| Au::from_f32_px(*size))
203                    .collect(),
204            },
205        }
206    }
207}
208
209#[derive(Clone, Debug, MallocSizeOf)]
210pub(crate) struct SpecificTaffyGridTrackInfo {
211    pub sizes: Box<[Au]>,
212}