Skip to main content

cranpose_ui/widgets/nodes/layout_node/
tree_node.rs

1use std::any::TypeId;
2
3use cranpose_core::{Node, NodeId};
4
5use super::{LayoutEnvironment, LayoutNode, ModifierChainHandle, unregister_layout_node};
6use crate::widgets::nodes::node_pool_limit::{KEPT_CHILD_ROOM, node_pool_limit};
7
8impl Node for LayoutNode {
9    fn mount(&mut self) {
10        self.modifier_chain.remount(self.id.get());
11        crate::modal_nodes::reach_changed(self.id.get());
12    }
13
14    fn unmount(&mut self) {
15        self.modifier_chain.chain_mut().detach_nodes();
16    }
17
18    fn set_node_id(&mut self, id: NodeId) {
19        Self::set_node_id(self, id);
20    }
21
22    fn mount_under(&mut self, parent: NodeId, id: NodeId) {
23        self.mount_under_made_parent(parent, id);
24        Node::mount(self);
25    }
26
27    fn insert_child(&mut self, child: NodeId) -> bool {
28        if self.children.contains(&child) {
29            return false;
30        }
31        self.children.push(child);
32        self.parts.cache().clear();
33        self.mark_needs_measure();
34        self.note_semantics_layout_change();
35        true
36    }
37
38    fn remove_child(&mut self, child: NodeId) -> bool {
39        // A child is listed once, and a child sync removes children from
40        // the last: the search from the end finds it at once.
41        let at = self.children.iter().rposition(|&id| id == child);
42        let removed = at.map(|at| self.children.remove(at)).is_some();
43        if removed {
44            self.parts.cache().clear();
45            self.mark_needs_measure();
46            self.note_semantics_layout_change();
47        }
48        removed
49    }
50
51    fn move_child(&mut self, from: usize, to: usize) {
52        if from == to || from >= self.children.len() {
53            return;
54        }
55        let child = self.children.remove(from);
56        let target = to.min(self.children.len());
57        self.children.insert(target, child);
58        self.parts.cache().clear();
59        self.mark_needs_measure();
60        self.note_semantics_layout_change();
61    }
62
63    fn update_children(&mut self, children: &[NodeId]) {
64        self.children.clear();
65        self.children.extend_from_slice(children);
66        self.parts.cache().clear();
67        self.mark_needs_measure();
68        self.note_semantics_layout_change();
69    }
70
71    fn collect_children_into(&self, out: &mut smallvec::SmallVec<[NodeId; 8]>) {
72        out.clear();
73        out.extend_from_slice(&self.children);
74    }
75
76    fn for_each_child(&self, f: &mut dyn FnMut(NodeId)) {
77        for &child in &self.children {
78            f(child);
79        }
80    }
81
82    fn owned_child_index(&self, child: NodeId) -> Option<usize> {
83        self.children.iter().position(|&id| id == child)
84    }
85
86    fn on_attached_to_parent(&mut self, parent: NodeId) {
87        self.set_parent(parent);
88    }
89
90    fn on_removed_from_parent(&mut self) {
91        self.clear_parent();
92    }
93
94    fn parent(&self) -> Option<NodeId> {
95        self.parent.get()
96    }
97
98    fn mark_needs_layout(&self) {
99        Self::mark_needs_layout(self);
100    }
101
102    fn needs_layout(&self) -> bool {
103        Self::needs_layout(self)
104    }
105
106    fn mark_needs_measure(&self) {
107        Self::mark_needs_measure(self);
108    }
109
110    fn needs_measure(&self) -> bool {
111        Self::needs_measure(self)
112    }
113
114    fn mark_descendant_needs_layout(&self, measure: bool) {
115        self.parts.cache().mark_descendant_needs_layout(measure);
116    }
117
118    fn descendant_needs_layout(&self) -> bool {
119        self.parts.cache().dirt().descendant.layout
120    }
121
122    fn is_virtual(&self) -> bool {
123        self.is_virtual
124    }
125
126    fn descendant_needs_measure(&self) -> bool {
127        self.parts.cache().dirt().descendant.measure
128    }
129
130    fn mark_needs_semantics(&self) {
131        self.needs_semantics.set(true);
132        self.forget_semantics_reach();
133    }
134
135    fn mark_descendant_needs_semantics(&self) {
136        self.descendant_needs_semantics.set(true);
137    }
138
139    fn needs_semantics(&self) -> bool {
140        Self::needs_semantics(self)
141    }
142
143    fn set_parent_for_bubbling(&mut self, parent: NodeId) {
144        if self.parent.get().is_none() {
145            self.parent.set(Some(parent));
146        }
147    }
148
149    fn recycle_key(&self) -> Option<TypeId> {
150        Some(TypeId::of::<Self>())
151    }
152
153    fn recycle_pool_limit(&self) -> Option<usize> {
154        node_pool_limit()
155    }
156
157    // The node leaves the tree whole: it lets go of what a drop would let go
158    // of and keeps the room of its parts and its lists for the node that
159    // takes its place.
160    fn prepare_for_recycle(&mut self) {
161        let Self {
162            parts,
163            children,
164            density: _,
165            is_virtual: _,
166            needs_semantics,
167            descendant_needs_semantics,
168            needs_redraw,
169            needs_pointer_pass,
170            needs_focus_sync,
171            modifier_slices_dirty,
172            debug_modifiers,
173            id,
174            parent,
175            folded_parent,
176            modifier_slices_snapshot,
177            semantics_reach,
178            owner_context_id,
179            modifier_chain,
180            #[cfg(feature = "inspection")]
181                source_trace: _,
182        } = self;
183        if let (Some(id), Some(owner_context_id)) = (id.replace(None), owner_context_id.take()) {
184            unregister_layout_node(owner_context_id, id);
185        }
186        parts.release_for_recycle();
187        if children.capacity() > KEPT_CHILD_ROOM {
188            *children = Vec::new();
189        } else {
190            children.clear();
191        }
192        *modifier_chain = ModifierChainHandle::detached(LayoutEnvironment::default().direction);
193        *modifier_slices_snapshot = crate::modifier::SlicesSnapshot::default();
194        parent.set(None);
195        folded_parent.set(None);
196        semantics_reach.set(None);
197        needs_semantics.set(false);
198        descendant_needs_semantics.set(false);
199        needs_redraw.set(false);
200        needs_pointer_pass.set(false);
201        needs_focus_sync.set(false);
202        debug_modifiers.set(false);
203        modifier_slices_dirty.set(true);
204    }
205
206    fn rehouse_for_recycle(&self) -> Option<Box<dyn Node>> {
207        // The box comes first, so the shell is made in it.
208        Some(Box::write(
209            Box::new_uninit(),
210            Self::new_recycled_shell(self.is_virtual),
211        ))
212    }
213
214    fn rehouse_for_live_compaction(&mut self) -> Option<Box<dyn Node>> {
215        // The whole node moves into the new box. Its modifier nodes keep
216        // what the user is doing with it, such as a focus, a held press or an
217        // IME composition, and its caches and registry entry stay true for
218        // it. Only its child list, which a spike of children may have grown,
219        // is cut to size. The shell left behind has no id to unregister.
220        let mut node = std::mem::replace(self, Self::new_recycled_shell(self.is_virtual));
221        node.children.shrink_to_fit();
222        Some(Box::new(node))
223    }
224}