layout/fragment_tree/
positioning_fragment.rs1use 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#[derive(MallocSizeOf)]
24pub(crate) struct PositioningFragment {
25 pub base: BaseFragment,
26
27 pub style: SharedStyle,
29
30 pub children: Vec<Fragment>,
31
32 scrollable_overflow: SyncPhysicalRectAu,
34 scrollable_overflow_is_up_to_date: AtomicBool,
35
36 pub cumulative_parent_content_rect: SyncPhysicalRectAu,
40
41 is_line_box: bool,
43
44 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 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 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}