1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//! A layout node's layout state as layout writes it: one cell a field, so a
//! write takes no borrow of the whole state.
use std::cell::Cell;
use cranpose_core::{NodeId, PackedU64};
use super::layout_state::{ChildPlacement, LayoutState, count_z_index_placer};
use crate::{
modifier::{Point, Size},
render_state::AppContext,
};
// The size a node measured, where its parent placed it, and the stamps that
// note the node once in each list of changed nodes a take empties.
#[derive(Default)]
pub(crate) struct LayoutCells {
size: Cell<Size>,
position: Cell<Point>,
is_placed: Cell<bool>,
// The layout pass that cleared the placed flag while the node was placed.
cleared_in_pass: Cell<PackedU64>,
content_offset: Cell<Point>,
semantics_stamp: Cell<PackedU64>,
// The take of the moved-node list this node was last noted in.
moved_noted_in: Cell<PackedU64>,
// The take of the reshaped-node list this node was last noted in.
reshaped_noted_in: Cell<PackedU64>,
z_index: Cell<f32>,
// Whether the node's last placement gave a child a z index other than 0.
places_children_with_z_index: Cell<bool>,
}
impl LayoutCells {
// The state as a reader outside layout sees it.
pub(crate) fn snapshot(&self) -> LayoutState {
LayoutState {
size: self.size.get(),
position: self.position.get(),
is_placed: self.is_placed.get(),
cleared_in_pass: self.cleared_in_pass.get(),
content_offset: self.content_offset.get(),
z_index: self.z_index.get(),
places_children_with_z_index: self.places_children_with_z_index.get(),
}
}
pub(crate) fn size(&self) -> Size {
self.size.get()
}
pub(crate) fn position(&self) -> Point {
self.position.get()
}
pub(crate) fn is_placed(&self) -> bool {
self.is_placed.get()
}
pub(crate) fn z_index(&self) -> f32 {
self.z_index.get()
}
pub(crate) fn places_children_with_z_index(&self) -> bool {
self.places_children_with_z_index.get()
}
// Writes the content offset of node `node_id`, reporting an actual
// change to the semantics tree.
pub(crate) fn set_content_offset(&self, node_id: Option<NodeId>, offset: Point) {
if self.content_offset.get() != offset {
self.content_offset.set(offset);
self.note_semantics_change(node_id);
}
}
// Notes to the scene phase of `context` that the layout pass changed the
// geometry of this node, `node_id`.
#[inline]
pub(crate) fn note_geometry_change(&self, node_id: NodeId, context: &AppContext) {
stamp(&self.reshaped_noted_in, |noted| {
context.note_geometry_scene_node(node_id, noted);
});
}
// Writes the measured size of node `node_id`, self-reporting an actual
// change to the scene phase of `context`, the node's app context.
#[inline]
pub(crate) fn set_size(&self, node_id: Option<NodeId>, size: Size, context: &AppContext) {
if self.size.get() != size {
if let Some(id) = node_id {
self.note_geometry_change(id, context);
}
self.size.set(size);
self.note_semantics_change(node_id);
}
}
pub(crate) fn set_places_children_with_z_index(&self, places: bool) {
if self.places_children_with_z_index.replace(places) != places {
count_z_index_placer(places);
}
}
// Places node `node_id` where `at` says, with its z index.
pub(crate) fn place_child(
&self,
node_id: Option<NodeId>,
at: ChildPlacement,
placer: Option<NodeId>,
context: &AppContext,
) {
self.place(node_id, at.position, placer, context);
self.set_z_index(node_id, at.z_index);
}
// Sets the z index the parent placed node `node_id` with. A change
// reorders the node among its siblings, which its parent's layer holds.
pub(crate) fn set_z_index(&self, node_id: Option<NodeId>, z_index: f32) {
if self.z_index.replace(z_index).to_bits() == z_index.to_bits() {
return;
}
if let Some(id) = node_id {
crate::schedule_z_order_change(id);
}
}
// Writes the placed position of node `node_id` and marks it placed,
// self-reporting to the scene phase of `context`, the node's app
// context, an actual move, and a placement of a node the scene does not
// draw: one the running pass did not find placed. A move is reported
// apart, with `placer`, the node whose placement put the node there, so
// the scene can keep what it drew for the node.
pub(crate) fn place(
&self,
node_id: Option<NodeId>,
position: Point,
placer: Option<NodeId>,
context: &AppContext,
) {
let was_placed = self.is_placed.get();
// A node the running pass cleared while it was placed changes
// nothing the scene draws by being placed again.
let newly_placed = !was_placed
&& !context
.placement_pass()
.restore(self.cleared_in_pass.get().get());
let moved = self.position.replace(position) != position;
if let Some(id) = node_id {
if newly_placed {
self.note_geometry_change(id, context);
} else if moved {
stamp(&self.moved_noted_in, |noted| {
context.record_moved_scene_node(id, placer, noted);
});
}
}
if moved || !was_placed {
self.is_placed.set(true);
self.note_semantics_change(node_id);
}
}
// Clears the placed flag of node `node_id` at the start of a layout pass
// of `context`. Until its parent places it again the pass counts the
// node unplaced, and the pass names it to the scene phase if it ends
// that way.
pub(crate) fn clear_placed(&self, node_id: Option<NodeId>, context: &AppContext) {
if !self.is_placed.replace(false) {
return;
}
self.cleared_in_pass
.set(PackedU64::new(context.placement_pass().clear(node_id)));
self.note_semantics_change(node_id);
}
#[inline]
pub(crate) fn note_semantics_change(&self, node_id: Option<NodeId>) {
if let Some(id) = node_id {
stamp(&self.semantics_stamp, |noted| {
crate::semantics_layout_log::record_layout_change(id, noted);
});
}
}
}
// Runs `note` on the stamp `cell` holds, which it may write.
#[inline]
fn stamp(cell: &Cell<PackedU64>, note: impl FnOnce(&mut PackedU64)) {
let mut noted = cell.get();
note(&mut noted);
cell.set(noted);
}