Skip to main content

layout/fragment_tree/
positioning_fragment.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/. */
4
5use std::sync::Arc;
6use std::sync::atomic::{AtomicBool, Ordering};
7
8use app_units::Au;
9use malloc_size_of_derive::MallocSizeOf;
10use servo_arc::Arc as ServoArc;
11use servo_base::id::AtomicOptionScrollTreeNodeId;
12use servo_base::print_tree::PrintTree;
13use style::properties::ComputedValues;
14
15use super::{BaseFragment, BaseFragmentInfo, Fragment};
16use crate::SharedStyle;
17use crate::fragment_tree::{CumulativeContentRectCalculation, FragmentChildren};
18use crate::geom::{LogicalVec2, PhysicalRect, SyncPhysicalRectAu};
19
20/// Can contain child fragments with relative coordinates, but does not contribute to painting
21/// itself. [`PositioningFragment`]s may be completely anonymous, or just non-painting Fragments
22/// generated by boxes.
23#[derive(MallocSizeOf)]
24pub(crate) struct PositioningFragment {
25    pub base: BaseFragment,
26
27    /// The style for this [`PositioningFragment`].
28    pub style: SharedStyle,
29
30    pub children: Vec<Fragment>,
31
32    /// The scrollable overflow of this anonymous fragment's children.
33    scrollable_overflow: SyncPhysicalRectAu,
34    scrollable_overflow_is_up_to_date: AtomicBool,
35
36    /// The cumulative content rect of this [`PositioningFragment`]'s parent in
37    /// coordinates relative to the initial containing block, but not taking into account
38    /// any transforms.
39    pub cumulative_parent_content_rect: SyncPhysicalRectAu,
40
41    /// Whether or not this [`PositioningFragment`] is a line box.
42    is_line_box: bool,
43
44    /// The containing spatial tree node of this [`PositioningFragment`]. This is assigned
45    /// during `StackingContextTree` construction, so isn't available before that time.
46    /// This is used to for determining final viewport size and position of this node and
47    /// will also be used in the future for hit testing.
48    pub spatial_tree_node: AtomicOptionScrollTreeNodeId,
49}
50
51impl PositioningFragment {
52    pub fn new_anonymous(
53        style: ServoArc<ComputedValues>,
54        rect: PhysicalRect<Au>,
55        children: FragmentChildren,
56        is_line_box: bool,
57    ) -> Arc<Self> {
58        let children = children.into_physical_fragments(
59            style.writing_mode,
60            LogicalVec2::from_physical_size(&rect.size, style.writing_mode),
61        );
62        Self::new_with_base_fragment_info(
63            BaseFragmentInfo::anonymous(),
64            style,
65            rect,
66            children,
67            is_line_box,
68        )
69    }
70
71    pub fn new_empty(
72        base_fragment_info: BaseFragmentInfo,
73        rect: PhysicalRect<Au>,
74        style: ServoArc<ComputedValues>,
75    ) -> Arc<Self> {
76        Self::new_with_base_fragment_info(base_fragment_info, style, rect, Vec::new(), false)
77    }
78
79    fn new_with_base_fragment_info(
80        base_fragment_info: BaseFragmentInfo,
81        style: ServoArc<ComputedValues>,
82        rect: PhysicalRect<Au>,
83        children: Vec<Fragment>,
84        is_line_box: bool,
85    ) -> Arc<Self> {
86        Arc::new(Self {
87            base: BaseFragment::new(base_fragment_info, rect),
88            style: style.into(),
89            children,
90            scrollable_overflow: Default::default(),
91            scrollable_overflow_is_up_to_date: AtomicBool::new(false),
92            cumulative_parent_content_rect: Default::default(),
93            is_line_box,
94            spatial_tree_node: Default::default(),
95        })
96    }
97
98    #[inline]
99    pub(crate) fn set_cumulative_content_rect(&self, content_rect: &PhysicalRect<Au>) {
100        self.cumulative_parent_content_rect.set(*content_rect);
101    }
102
103    pub fn offset_by_containing_block(
104        &self,
105        rect: &PhysicalRect<Au>,
106        cumulative_content_rect_calculation: CumulativeContentRectCalculation<'_>,
107    ) -> PhysicalRect<Au> {
108        cumulative_content_rect_calculation.ensure();
109        rect.translate(self.cumulative_parent_content_rect.origin().to_vector())
110    }
111
112    /// Get the scrollable overflow for this [`PositioningFragment`] relative to its
113    /// containing block, recalculating scrollable overflow when necessary, for instance
114    /// after a style change.
115    pub(crate) fn scrollable_overflow_for_parent(&self) -> PhysicalRect<Au> {
116        if self
117            .scrollable_overflow_is_up_to_date
118            .load(Ordering::Acquire)
119        {
120            self.scrollable_overflow.get()
121        } else {
122            let rect = self.calculate_scrollable_overflow();
123            self.scrollable_overflow.set(rect);
124            self.scrollable_overflow_is_up_to_date
125                .store(true, Ordering::Release);
126            rect
127        }
128    }
129
130    /// Clear the scrollable overflow on this [`PositioningFragment`]. This is called
131    /// during damage propagation when a fragment is preserved, itself or one of its
132    /// descendants has scrollable overflow damage.
133    pub(crate) fn clear_scrollable_overflow(&self) {
134        self.scrollable_overflow_is_up_to_date
135            .store(false, Ordering::Release);
136    }
137
138    fn calculate_scrollable_overflow(&self) -> PhysicalRect<Au> {
139        self.children
140            .iter()
141            .fold(PhysicalRect::zero(), |acc, child| {
142                acc.union(
143                    &child
144                        .scrollable_overflow_for_parent()
145                        .translate(self.base.rect().origin.to_vector()),
146                )
147            })
148    }
149
150    pub(crate) fn is_line_box(&self) -> bool {
151        self.is_line_box
152    }
153
154    pub fn print(&self, tree: &mut PrintTree) {
155        tree.new_level(format!(
156            "PositioningFragment\
157                \nbase={:?}\
158                \nrect={:?}\
159                \nscrollable_overflow={:?}",
160            self.base,
161            self.base.rect(),
162            self.scrollable_overflow
163        ));
164
165        for child in &self.children {
166            child.print(tree);
167        }
168        tree.end_level();
169    }
170}