Skip to main content

cranpose_render_common/scene_store/
leaf_write.rs

1// A leaf written in one step: a layout node drawn in its parent's layer,
2// placed by a translation alone. One pass over its rows takes the place of
3// the setters a leaf would go through one by one.
4
5use cranpose_core::{NodeId, RecordId};
6use cranpose_ui::{Point, Size};
7
8use super::{Item, RecordFlags, RecordKind, SceneStore, records::same_pair};
9
10/// What a leaf holds once a scene update wrote it, its draws aside.
11#[derive(Clone, Copy, Debug)]
12pub struct Leaf {
13    /// The layout node the leaf draws for.
14    pub node: NodeId,
15    /// The node's size.
16    pub size: Size,
17    /// Offset of the node's content box from its origin.
18    pub content_offset: Point,
19    /// Where layout placed the node within its parent's content, before the
20    /// parent's content offset.
21    pub origin_in_parent: Point,
22    /// The leaf's flags, such as [`RecordFlags::MOTION_ANIMATED`].
23    pub flags: RecordFlags,
24}
25
26// What writing a leaf's rows changed.
27#[derive(Clone, Copy)]
28struct LeafRows {
29    // The leaf moved in its parent, or its content within it.
30    moved: bool,
31    parent_stale: bool,
32}
33
34#[expect(clippy::multiple_inherent_impl, reason = "one file per concern")]
35impl SceneStore {
36    /// Writes `id` as `leaf`: a leaf of `leaf.node` without properties,
37    /// placed by a translation alone, whose draws changed in the update in
38    /// progress, to `items`. The draws and children `id` held before stay in
39    /// the store: the caller removes them.
40    pub fn write_leaf(&mut self, id: RecordId, leaf: Leaf, items: &[Item]) {
41        let rows = self.write_leaf_rows(id, leaf);
42        // The content change noted below leaves the leaf's containment and
43        // reach to be worked out again.
44        self.replace_items(id, items);
45        self.end_leaf_write(id, rows);
46    }
47
48    #[inline]
49    fn write_leaf_rows(&mut self, id: RecordId, leaf: Leaf) -> LeafRows {
50        let index = id.index();
51        self.set_kind(id, RecordKind::Leaf, Some(leaf.node));
52        let unpropped = self.warm[index].props.is_some();
53        if unpropped {
54            self.unprop(index);
55        }
56        let size = [leaf.size.width, leaf.size.height];
57        let resized = !same_pair(self.geo[index].size, size);
58        self.geo[index].size = size;
59        let offset = [leaf.content_offset.x, leaf.content_offset.y];
60        let shifted = !same_pair(self.warm[index].content_offset, offset);
61        self.warm[index].content_offset = offset;
62        let reflagged = self.write_flag_row(index, leaf.flags);
63        let moved = self.place_leaf_row(index, leaf.origin_in_parent) || shifted;
64        LeafRows {
65            moved,
66            parent_stale: unpropped || resized || reflagged || moved,
67        }
68    }
69
70    #[inline]
71    fn end_leaf_write(&mut self, id: RecordId, rows: LeafRows) {
72        let index = id.index();
73        if rows.moved {
74            self.moved(index);
75        }
76        if let Some(parent) = self.tree[index].parent
77            && rows.parent_stale
78        {
79            self.stale(parent.index());
80        }
81        self.note_content_change(id);
82    }
83
84    // Drops the properties of the record at `index`, which becomes a leaf,
85    // with the child facts they decided. A layer seldom turns into a leaf.
86    #[cold]
87    #[inline(never)]
88    fn unprop(&mut self, index: usize) {
89        self.drop_props(index);
90        super::facts::forget_children(self, index);
91    }
92
93    /// Places `id`, a leaf, at `origin_in_parent` by a translation alone,
94    /// with `flags`.
95    pub fn place_leaf(&mut self, id: RecordId, origin_in_parent: Point, flags: RecordFlags) {
96        let index = id.index();
97        let reflagged = self.write_flag_row(index, flags);
98        let moved = self.place_leaf_row(index, origin_in_parent);
99        if moved {
100            self.moved(index);
101        }
102        if reflagged {
103            self.stale(index);
104        }
105        if let Some(parent) = self.tree[index].parent
106            && (moved || reflagged)
107        {
108            self.stale(parent.index());
109        }
110    }
111
112    /// Moves `id`, a leaf, to `origin_in_parent` by a translation alone,
113    /// among the items of a parent whose content the update in progress
114    /// changes: the caller notes that change with
115    /// [`Self::note_content_change`] of the parent.
116    pub fn move_leaf(&mut self, id: RecordId, origin_in_parent: Point) {
117        let index = id.index();
118        if self.place_leaf_row(index, origin_in_parent) {
119            self.moved(index);
120        }
121    }
122
123    // Writes the flags of the record at `index` the store does not keep
124    // itself; returns whether they changed.
125    #[inline]
126    pub(super) fn write_flag_row(&mut self, index: usize, flags: RecordFlags) -> bool {
127        let current = self.tree[index].flags;
128        let flags = flags
129            .without(RecordFlags::STORE_KEPT)
130            .with(current.only(RecordFlags::STORE_KEPT));
131        if flags == current {
132            return false;
133        }
134        self.tree[index].flags = flags;
135        if flags.only(RecordFlags::ORIGIN_SINKS) != current.only(RecordFlags::ORIGIN_SINKS) {
136            super::facts::forget_children(self, index);
137        }
138        true
139    }
140
141    // Puts the leaf at `index` at `origin_in_parent`, its transform the
142    // translation there; returns whether it moved.
143    #[inline]
144    fn place_leaf_row(&mut self, index: usize, origin_in_parent: Point) -> bool {
145        let placement = [origin_in_parent.x, origin_in_parent.y];
146        let placed = !same_pair(self.geo[index].placement, placement);
147        self.geo[index].placement = placement;
148        let transformed = self.tree[index].transform.take();
149        if let Some(entry) = transformed {
150            self.transforms.remove(entry.index());
151        }
152        placed || transformed.is_some()
153    }
154}