cranpose-core 0.0.58

Core runtime for a Jetpack Compose inspired UI framework in Rust
Documentation
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
use crate::{
    debug_scope_label, Applier, ChildList, Command, CommandQueue, Composer, DirtyBubble,
    EmittedNode, MutableState, Node, NodeError, NodeId, OwnedMutableState, ParentAttachMode,
    ParentFrame,
};
use std::any::TypeId;

impl Composer {
    pub fn use_state<T: Clone + 'static>(&self, init: impl FnOnce() -> T) -> MutableState<T> {
        let runtime = self.runtime_handle();
        let state = self.with_slots_mut(|slots| {
            slots.remember(|| OwnedMutableState::with_runtime(init(), runtime.clone()))
        });
        state.with(|state| state.handle())
    }

    fn emit_node_box<N: Node + 'static>(
        &self,
        make_node: impl FnOnce(&mut dyn Applier) -> EmittedNode,
    ) -> NodeId {
        // Peek at the slot without advancing cursor
        let (existing_id, type_matches, gen_matches) = {
            if let Some((id, slot_gen)) = self.with_slots_mut(|slots| slots.peek_node()) {
                let mut applier = self.borrow_applier();
                let gen_ok = applier.node_generation(id) == slot_gen;
                let type_ok = match applier.get_mut(id) {
                    Ok(node) => node.as_any_mut().downcast_ref::<N>().is_some(),
                    Err(_) => false,
                };
                (Some(id), type_ok, gen_ok)
            } else {
                (None, false, false)
            }
        };

        // If we have a matching node with correct generation, advance cursor and reuse it
        if let Some(id) = existing_id {
            if type_matches && gen_matches {
                self.core.last_node_reused.set(Some(true));
                let scope_debug = self
                    .current_recranpose_scope()
                    .map(|scope| (scope.id(), debug_scope_label(scope.id())))
                    .unwrap_or((0, None));
                log::trace!(
                    target: "cranpose::compose::emit",
                    "reusing node #{id} as {} [scope_id={} scope_label={:?}]",
                    std::any::type_name::<N>(),
                    scope_debug.0,
                    scope_debug.1,
                );
                self.with_slots_mut(|slots| slots.advance_after_node_read());

                self.commands_mut().push(Command::update_node::<N>(id));
                self.attach_to_parent(id);
                return id;
            }
        }

        // If there was a mismatched node in this slot (wrong type or stale generation),
        // schedule its removal before creating a new one.
        if let Some(old_id) = existing_id {
            if !gen_matches {
                // Stale generation: the slot points to a recycled index.
                // Don't remove the node — it belongs to a different composition context.
                log::trace!(
                    target: "cranpose::compose::emit",
                    "stale generation for node #{old_id} (current={})",
                    self.borrow_applier().node_generation(old_id)
                );
            } else if !type_matches {
                // Same generation but wrong type: genuinely needs replacement.
                log::trace!(
                    target: "cranpose::compose::emit",
                    "replacing node #{old_id} with new {}",
                    std::any::type_name::<N>()
                );
                self.commands_mut().push(Command::RemoveNode { id: old_id });
            }
        }

        // Type mismatch, stale generation, or no node: create new node
        let (id, gen) = {
            let mut applier = self.borrow_applier();
            let emitted = make_node(&mut *applier);
            let id = match emitted {
                EmittedNode::Fresh(node) => applier.create(node),
                EmittedNode::Recycled(recycled) => {
                    let (stable_id, node, warm_origin) = recycled.into_parts();
                    applier
                        .insert_with_id(stable_id, node)
                        .expect("recycled stable id should be available");
                    applier.set_recycled_node_origin(stable_id, warm_origin);
                    stable_id
                }
            };
            let gen = applier.node_generation(id);
            (id, gen)
        };
        self.core.last_node_reused.set(Some(false));
        let scope_debug = self
            .current_recranpose_scope()
            .map(|scope| (scope.id(), debug_scope_label(scope.id())))
            .unwrap_or((0, None));
        log::trace!(
            target: "cranpose::compose::emit",
            "creating node #{} (gen={}) as {} [scope_id={} scope_label={:?}]",
            id,
            gen,
            std::any::type_name::<N>(),
            scope_debug.0,
            scope_debug.1,
        );
        {
            self.with_slots_mut(|slots| slots.record_node(id, gen));
        }
        self.commands_mut().push(Command::MountNode { id });
        self.attach_to_parent(id);
        id
    }

    pub fn emit_node<N: Node + 'static>(&self, init: impl FnOnce() -> N) -> NodeId {
        self.emit_node_box::<N>(|_| EmittedNode::Fresh(Box::new(init())))
    }

    pub fn emit_recyclable_node<N: Node + 'static>(
        &self,
        init: impl FnOnce() -> N,
        reset: impl FnOnce(&mut N),
    ) -> NodeId {
        self.emit_node_box::<N>(|applier| {
            let key = TypeId::of::<N>();
            if let Some(mut recycled) = applier.take_recycled_node(key) {
                let typed = recycled
                    .node_mut()
                    .as_any_mut()
                    .downcast_mut::<N>()
                    .expect("recycled node type mismatch");
                reset(typed);
                EmittedNode::Recycled(recycled)
            } else {
                let node = Box::new(init());
                applier.record_fresh_recyclable_creation(key);
                if let Some(shell) = node.rehouse_for_recycle() {
                    applier.seed_recycled_node_shell(key, node.recycle_pool_limit(), shell);
                }
                EmittedNode::Fresh(node)
            }
        })
    }

    fn attach_to_parent(&self, id: NodeId) {
        self.attach_to_parent_with_mode(id, false);
    }

    pub(crate) fn attach_to_parent_with_mode(
        &self,
        id: NodeId,
        force_reparent_current_parent: bool,
    ) {
        // IMPORTANT: Check parent_stack FIRST.
        // During subcomposition, if there's an active parent (e.g., Row),
        // child nodes (e.g., Text) should attach to that parent, NOT to the
        // subcompose frame. Only ROOT nodes (nodes with no active parent)
        // should be added to the subcompose frame.
        let mut parent_stack = self.parent_stack();
        if let Some(parent_id) = parent_stack.last().map(|frame| frame.id) {
            let stale_root_parent = self.core.root.get() == Some(parent_id) && {
                let mut applier = self.borrow_applier();
                applier.get_mut(parent_id).is_err()
            };
            if stale_root_parent {
                parent_stack.pop();
                self.set_root(None);
            } else {
                let frame = parent_stack
                    .last_mut()
                    .expect("active parent frame should remain available");
                let attach_mode = frame.attach_mode;
                if parent_id == id {
                    return;
                }
                if matches!(attach_mode, ParentAttachMode::DeferredSync) {
                    frame.new_children.push(id);
                }
                drop(parent_stack);

                // KEY FIX: Set parent link IMMEDIATELY, matching Jetpack Compose's
                // LayoutNode.insertAt pattern where _foldedParent is set synchronously.
                // This ensures that when bubble_measure_dirty runs (in commands),
                // the parent chain is already established.
                //
                // IMPORTANT: Only set parent if node doesn't have one or if the new parent
                // is not the root. This prevents double-recomposition scenarios where a
                // child scope (invalidated by CompositionLocalProvider during parent's
                // recomposition) gets processed again with parent_stack=[root], which would
                // incorrectly reparent nodes to root.
                {
                    let mut applier = self.borrow_applier();
                    if let Ok(child_node) = applier.get_mut(id) {
                        let existing_parent = child_node.parent();
                        // Only set parent if:
                        // 1. Node has no parent, OR
                        // 2. New parent is NOT the root (parent_id != 0 or != self.root)
                        // This prevents root from stealing children that belong to intermediate nodes.
                        let should_set = if force_reparent_current_parent {
                            existing_parent != Some(parent_id)
                        } else {
                            match existing_parent {
                                None => true,
                                Some(existing) => {
                                    // Don't let root steal children from proper parents
                                    let root_id = self.core.root.get();
                                    parent_id != root_id.unwrap_or(0)
                                        || existing == root_id.unwrap_or(0)
                                }
                            }
                        };
                        if should_set {
                            child_node.set_parent_for_bubbling(parent_id);
                        }
                    }
                }
                if matches!(attach_mode, ParentAttachMode::ImmediateAppend) {
                    self.commands_mut().push(Command::AttachChild {
                        parent_id,
                        child_id: id,
                        bubble: DirtyBubble::LAYOUT_AND_MEASURE,
                    });
                }
                return;
            }
        }
        drop(parent_stack);

        // No active parent - check if we're in subcompose
        let in_subcompose = !self.subcompose_stack().is_empty();
        if in_subcompose {
            // During subcompose, only add ROOT nodes (nodes without a parent).
            // Child nodes already have their parent-child relationship from composition;
            // re-adding them to the subcompose frame would cause duplication.
            let has_parent = {
                let mut applier = self.borrow_applier();
                applier
                    .get_mut(id)
                    .map(|node| node.parent().is_some())
                    .unwrap_or(false)
            };

            if !has_parent {
                let mut subcompose_stack = self.subcompose_stack();
                if let Some(frame) = subcompose_stack.last_mut() {
                    frame.nodes.push(id);
                }
            }
            return;
        }

        // During recomposition, preserve the original parent when possible.
        if let Some(parent_hint) = self.core.recranpose_parent_hint.get() {
            let parent_status = {
                let mut applier = self.borrow_applier();
                applier
                    .get_mut(id)
                    .map(|node| node.parent())
                    .unwrap_or(None)
            };
            match parent_status {
                Some(existing) if existing == parent_hint => {}
                None => {
                    self.commands_mut().push(Command::AttachChild {
                        parent_id: parent_hint,
                        child_id: id,
                        bubble: DirtyBubble::LAYOUT_AND_MEASURE,
                    });
                }
                Some(_) => {}
            }
            return;
        }

        // Neither parent nor subcompose - check if this node already has a parent.
        // During recomposition, reused nodes already have their correct parent from
        // initial composition. We should NOT set them as root, as that would corrupt
        // the tree structure and cause duplication.
        let has_parent = {
            let mut applier = self.borrow_applier();
            applier
                .get_mut(id)
                .map(|node| node.parent().is_some())
                .unwrap_or(false)
        };
        if has_parent {
            // Node already has a parent, nothing to do
            return;
        }

        // Node has no parent and is not in subcompose - must be root
        self.set_root(Some(id));
    }

    pub fn with_node_mut<N: Node + 'static, R>(
        &self,
        id: NodeId,
        f: impl FnOnce(&mut N) -> R,
    ) -> Result<R, NodeError> {
        let mut applier = self.borrow_applier();
        let node = applier.get_mut(id)?;
        let typed = node
            .as_any_mut()
            .downcast_mut::<N>()
            .ok_or(NodeError::TypeMismatch {
                id,
                expected: std::any::type_name::<N>(),
            })?;
        Ok(f(typed))
    }

    pub fn push_parent(&self, id: NodeId) {
        let reused = self.core.last_node_reused.take().unwrap_or(true);
        let in_subcompose = !self.core.subcompose_stack.borrow().is_empty();

        // Fresh parents usually append children directly, but a restored or otherwise
        // non-reused node can still carry attached children in the applier. In that case
        // we must diff against the live child list or stale descendants remain mounted.
        let mut previous = ChildList::new();
        if reused || in_subcompose {
            previous.extend(self.get_node_children(id));
        } else {
            let existing_children = self.get_node_children(id);
            if !existing_children.is_empty() {
                previous.extend(existing_children);
            }
        }
        let attach_mode = if in_subcompose || !previous.is_empty() {
            ParentAttachMode::DeferredSync
        } else {
            ParentAttachMode::ImmediateAppend
        };

        self.parent_stack().push(ParentFrame {
            id,
            previous,
            new_children: ChildList::new(),
            attach_mode,
        });
    }

    pub fn pop_parent(&self) {
        let frame_opt = {
            let mut stack = self.parent_stack();
            stack.pop()
        };
        if let Some(frame) = frame_opt {
            let ParentFrame {
                id,
                previous,
                new_children,
                attach_mode,
            } = frame;

            log::trace!(target: "cranpose::compose::parent", "pop_parent: node #{}", id);
            log::trace!(
                target: "cranpose::compose::parent",
                "previous children: {:?}",
                previous
            );
            log::trace!(
                target: "cranpose::compose::parent",
                "new children: {:?}",
                new_children
            );
            if matches!(attach_mode, ParentAttachMode::DeferredSync) {
                let _ = previous;
                self.commands_mut().push(Command::SyncChildren {
                    parent_id: id,
                    expected_children: new_children,
                });
            }
        }
    }

    pub(crate) fn take_commands(&self) -> CommandQueue {
        std::mem::take(&mut *self.commands_mut())
    }

    /// Applies any pending applier commands and runtime updates.
    ///
    /// This is useful during measure-time subcomposition to ensure newly created
    /// nodes are available for measurement before the full composition is committed.
    pub fn apply_pending_commands(&self) -> Result<(), NodeError> {
        let commands = self.take_commands();
        let runtime_handle = self.runtime_handle();
        {
            let mut applier = self.borrow_applier();
            commands.apply(&mut *applier)?;
            for update in runtime_handle.take_updates() {
                update.apply(&mut *applier)?;
            }
        }
        runtime_handle.drain_ui();
        Ok(())
    }

    pub fn register_side_effect(&self, effect: impl FnOnce() + 'static) {
        self.side_effects_mut().push(Box::new(effect));
    }

    pub fn take_side_effects(&self) -> Vec<Box<dyn FnOnce()>> {
        std::mem::take(&mut *self.side_effects_mut())
    }

    pub(crate) fn root(&self) -> Option<NodeId> {
        self.core.root.get()
    }

    pub(crate) fn set_root(&self, node: Option<NodeId>) {
        self.core.root.set(node);
    }
}