cranpose-ui 0.21.1

Composable widgets, text, layouts and input for Cranpose apps
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
//! A parent's record of each child it measures, and the bind of the
//! records to the children before a pass over them.

use std::{
    cell::{Cell, RefCell},
    rc::{Rc, Weak},
};

use cranpose_core::{MemoryApplier, Node, NodeError, NodeId, PackedU64};
use cranpose_ui_layout::{AlignmentLines, Constraints, MeasurePolicy, ParentData, Placement};

use super::{
    LayoutBuilderState, LayoutRuntimeState, MeasuredChild, MeasuredNode, core::Measurable,
    layout_child_measurable::LayoutChildMeasurable,
};
use crate::{
    modifier::Point,
    render_state::AppContext,
    subcompose_layout::SubcomposeLayoutNode,
    widgets::nodes::{IntrinsicKind, LayoutNode, LayoutParts, LayoutState, NodeCache},
};

#[expect(
    clippy::multiple_inherent_impl,
    reason = "the bind of a parent's child records in their file"
)]
impl LayoutBuilderState {
    pub(super) fn bind_layout_children(
        self: &Rc<Self>,
        applier: &mut MemoryApplier,
        (node_id, parts): (NodeId, &Rc<LayoutParts>),
        child_ids: &[NodeId],
        pass: ChildPass,
    ) -> Result<(), NodeError> {
        let mut runtime_state = parts.runtime_state().borrow_mut();
        let Some(children) =
            runtime_state.bind_children(self, (node_id, parts), pass, child_ids.len())
        else {
            return Ok(());
        };
        let mut bound = 0;
        for &child_id in child_ids {
            if self.bind_layout_child(applier, children, bound, child_id)? {
                bound += 1;
            }
        }
        children.truncate(bound);
        Ok(())
    }

    fn bind_layout_child(
        &self,
        applier: &mut MemoryApplier,
        children: &mut LayoutChildren,
        position: usize,
        child_id: NodeId,
    ) -> Result<bool, NodeError> {
        let unchanged = children.ids.get(position) == Some(&child_id)
            && children.states[position].rebind_unchanged(self);
        if unchanged {
            return Ok(true);
        }
        let bound = applier.with_node::<LayoutNode, _>(child_id, |child| {
            children.state_at(position, child_id).bind(
                LayoutChildBinding {
                    parts: child.layout_parts(),
                    layout_node: true,
                    parent_data: Some(child.parent_data()),
                    dirty: child.needs_layout() || child.needs_measure(),
                    descendant_dirty: Node::descendant_needs_layout(child),
                },
                self,
            );
        });
        match bound {
            Ok(()) => Ok(true),
            Err(NodeError::TypeMismatch { .. }) => {
                match applier.with_node::<SubcomposeLayoutNode, _>(child_id, |child| {
                    children.state_at(position, child_id).bind(
                        LayoutChildBinding {
                            parts: child.layout_parts(),
                            layout_node: false,
                            parent_data: Some(child.parent_data()),
                            dirty: child.needs_layout() || child.needs_measure(),
                            descendant_dirty: Node::descendant_needs_layout(child),
                        },
                        self,
                    );
                }) {
                    Ok(()) => Ok(true),
                    Err(NodeError::TypeMismatch { .. } | NodeError::Missing { .. }) => Ok(false),
                    Err(err) => Err(err),
                }
            }
            Err(NodeError::Missing { .. }) => Ok(false),
            Err(err) => Err(err),
        }
    }
}

#[derive(Default)]
pub(super) struct LayoutChildFrame {
    pub(super) builder: RefCell<Option<Rc<LayoutBuilderState>>>,
    pub(super) error: RefCell<Option<NodeError>>,
    pass: Cell<ChildPass>,
    /// The node whose children the frame binds, which places them.
    parent: Cell<Option<NodeId>>,
    // How many of the children have a z index other than 0, as their
    // records last placed or read it.
    placed_with_z_index: Cell<u32>,
    // The parts of the parent, whose layout state notes whether it placed
    // a child with a z index.
    parent_parts: Weak<LayoutParts>,
}

/// What a node binds its children for.
#[derive(Clone, Copy, Default, PartialEq, Eq)]
pub(super) enum ChildPass {
    /// A measure of the node, which records how it read each child, for a
    /// later keep of its measurement.
    #[default]
    Measure,
    /// An intrinsic size of the node, which its parent asks: the record of
    /// the node's last measure stays as it was.
    Intrinsics,
}

impl LayoutChildFrame {
    pub(super) fn bind(&self, builder: &Rc<LayoutBuilderState>, parent: NodeId, pass: ChildPass) {
        self.error.borrow_mut().take();
        *self.builder.borrow_mut() = Some(Rc::clone(builder));
        self.pass.set(pass);
        self.parent.set(Some(parent));
    }

    // Counts a child whose z index went from `before` to `after`. The
    // parent's layout state notes when the count leaves or reaches 0.
    fn count_placed_z_index(&self, before: f32, after: f32) {
        let count = match (before != 0.0, after != 0.0) {
            (false, true) => self.placed_with_z_index.get() + 1,
            (true, false) => self.placed_with_z_index.get() - 1,
            _ => return,
        };
        self.placed_with_z_index.set(count);
        if count <= 1
            && let Some(parent) = self.parent_parts.upgrade()
        {
            parent
                .layout_state()
                .borrow_mut()
                .set_places_children_with_z_index(count > 0);
        }
    }

    pub(super) fn measures(&self) -> bool {
        self.pass.get() == ChildPass::Measure
    }

    pub(super) fn unbind(&self) {
        self.builder.borrow_mut().take();
    }

    fn record_error(&self, err: NodeError) {
        if self.builder.borrow().is_none() {
            return;
        }
        let mut slot = self.error.borrow_mut();
        if slot.is_none() {
            *slot = Some(err);
        }
    }
}

pub(super) struct LayoutChildBinding<'a> {
    pub(super) parts: &'a Rc<LayoutParts>,
    // Whether the child is a layout node, whose size and placed flag its
    // parent's measure writes: a subcompose node writes its own.
    pub(super) layout_node: bool,
    pub(super) parent_data: Option<ParentData>,
    /// The child's own layout or measure is dirty.
    pub(super) dirty: bool,
    /// Only nodes below the child are dirty.
    pub(super) descendant_dirty: bool,
}

/// A parent's record of one child, for each child of every parent: its
/// shared parts sit in `Cell`s, read by take and put back, where a
/// `RefCell`'s borrow flag would take a word of every record.
pub(super) struct LayoutChildMeasureState {
    pub(super) node_id: NodeId,
    pub(super) frame: Rc<LayoutChildFrame>,
    // The child's shared parts, `None` until the record is first bound.
    pub(super) parts: Cell<Option<Rc<LayoutParts>>>,
    // Whether the record writes the child's size and placed flag: see
    // `LayoutChildBinding::layout_node`.
    pub(super) layout_node: Cell<bool>,
    // The revision of the child's cache when the record last read the child.
    revision: Cell<u32>,
    pub(super) cache_epoch: Cell<PackedU64>,
    pub(super) force_remeasure: Cell<bool>,
    pub(super) parent_data: RefCell<Option<ParentData>>,
    /// The constraints the parent's last measure gave the child.
    pub(super) measured_constraints: Cell<Option<Constraints>>,
    /// Whether the parent's last measure read the child's intrinsic sizes.
    pub(super) read_intrinsics: Cell<bool>,
    pub(super) measured: Cell<Option<Rc<MeasuredNode>>>,
    pub(super) last_position: Cell<Option<Point>>,
    // The child's z index, as the record last placed it or, at a bind that
    // read the child, found it.
    placed_z_index: Cell<f32>,
}

/// Runs `read` on what `cell` holds and puts it back. `read` must not reach
/// the same cell again.
pub(super) fn read_cell<T: Default, R>(cell: &Cell<T>, read: impl FnOnce(&T) -> R) -> R {
    let value = cell.take();
    let result = read(&value);
    cell.set(value);
    result
}

impl LayoutChildMeasureState {
    pub(super) fn placement_position(&self, placement: Option<&Placement>) -> Point {
        placement
            .map(|placement| Point {
                x: placement.x,
                y: placement.y,
            })
            .or_else(|| self.last_position.get())
            .unwrap_or_default()
    }

    pub(super) fn new(node_id: NodeId, frame: Rc<LayoutChildFrame>) -> Rc<Self> {
        Rc::new(Self {
            node_id,
            frame,
            parts: Cell::new(None),
            layout_node: Cell::new(false),
            revision: Cell::new(0),
            cache_epoch: Cell::default(),
            force_remeasure: Cell::new(true),
            parent_data: RefCell::new(None),
            measured_constraints: Cell::new(None),
            read_intrinsics: Cell::new(false),
            measured: Cell::new(None),
            last_position: Cell::new(None),
            placed_z_index: Cell::new(0.0),
        })
    }

    /// Binds the child for a measure of its parent. A child with only dirty
    /// nodes below it keeps its cache: it measures again, but may keep its
    /// measurement.
    pub(super) fn bind(&self, binding: LayoutChildBinding<'_>, pass: &LayoutBuilderState) {
        let cache = binding.parts.cache();
        self.bind_cache(cache, binding.dirty, binding.descendant_dirty, pass);
        self.revision.set(cache.revision());
        self.note_z_index(binding.parts.layout_state().borrow().z_index());
        let parts = self.parts.take();
        let shared = parts
            .as_ref()
            .is_some_and(|current| Rc::ptr_eq(current, binding.parts));
        self.parts.set(if shared {
            parts
        } else {
            Some(Rc::clone(binding.parts))
        });
        self.layout_node.set(binding.layout_node);
        *self.parent_data.borrow_mut() = binding.parent_data;
    }

    // Whether `policy` reads a change from the parent data of the record's
    // last bind to `data`. A record bound to none reads it as changed.
    pub(super) fn reads_parent_data_change(
        &self,
        policy: &dyn MeasurePolicy,
        data: &ParentData,
    ) -> bool {
        self.parent_data
            .borrow()
            .as_ref()
            .is_none_or(|bound| policy.reads_parent_data_change(bound, data))
    }

    // Binds the record again to the layout node it holds, without a look at
    // the node, while the node's cache has the revision of the record's last
    // read: the node's parts and parent data are then as the record holds
    // them, and the node's flags sit in its cache. `false` otherwise.
    fn rebind_unchanged(&self, pass: &LayoutBuilderState) -> bool {
        self.layout_node.get()
            && read_cell(&self.parts, |parts| {
                parts.as_deref().is_some_and(|parts| {
                    let cache = parts.cache();
                    let unchanged = cache.revision() == self.revision.get();
                    if unchanged {
                        let dirt = cache.dirt();
                        let dirty = dirt.measure || dirt.layout;
                        self.bind_cache(cache, dirty, dirt.descendant.layout, pass);
                    }
                    unchanged
                })
            })
    }

    fn bind_cache(
        &self,
        cache: &NodeCache,
        dirty: bool,
        descendant_dirty: bool,
        pass: &LayoutBuilderState,
    ) {
        let child_epoch = cache.epoch();
        let stale = dirty || child_epoch < pass.cache_floor;
        let cache_epoch = if stale { pass.cache_epoch } else { child_epoch };
        cache.activate(cache_epoch);
        self.cache_epoch.set(PackedU64::new(cache_epoch));
        self.force_remeasure.set(stale || descendant_dirty);
        if self.frame.measures() {
            self.measured.take();
            self.last_position.set(None);
            self.measured_constraints.set(None);
            self.read_intrinsics.set(false);
        }
    }

    /// Runs `f` on the child's layout state with the context of the pass it
    /// measures in; nothing while it is bound to no pass.
    pub(super) fn with_pass_layout_state(&self, f: impl FnOnce(&mut LayoutState, &AppContext)) {
        if !self.layout_node.get() {
            return;
        }
        let builder = self.frame.builder.borrow();
        let Some(builder) = builder.as_ref() else {
            return;
        };
        read_cell(&self.parts, |parts| {
            if let Some(parts) = parts {
                f(&mut parts.layout_state().borrow_mut(), &builder.context);
            }
        });
    }

    // Places the child where its parent's placement says. A policy that
    // placed its placeable there in this pass, as one that places its
    // placeables does, left only the z index to set.
    pub(super) fn place_at(&self, position: Point, z_index: f32) {
        if self.last_position.get() != Some(position) {
            self.place_retained(position);
        }
        if self.placed_z_index.get().to_bits() != z_index.to_bits() {
            self.note_z_index(z_index);
            read_cell(&self.parts, |parts| {
                if let Some(parts) = parts {
                    parts.layout_state().borrow_mut().set_z_index(z_index);
                }
            });
        }
    }

    // Takes `z_index` as the child's, and counts the child among its
    // parent's children with a z index.
    fn note_z_index(&self, z_index: f32) {
        let before = self.placed_z_index.replace(z_index);
        self.frame.count_placed_z_index(before, z_index);
    }

    pub(super) fn place_retained(&self, position: Point) {
        self.last_position.set(Some(position));
        let builder = self.frame.builder.borrow();
        let Some(builder) = builder.as_ref() else {
            return;
        };
        read_cell(&self.parts, |parts| {
            if let Some(parts) = parts {
                parts.layout_state().borrow_mut().place(
                    position,
                    self.frame.parent.get(),
                    &builder.context,
                );
            }
        });
    }

    /// The intrinsic size of a child that is a layout node, from its
    /// modifier chain and measure policy, without measuring it. `None` for
    /// another node type, which measures for it.
    pub(super) fn layout_intrinsic(&self, kind: IntrinsicKind) -> Option<f32> {
        let builder = self.frame.builder.borrow();
        let builder = builder.as_ref()?;
        match builder.layout_node_intrinsic(self.node_id, kind) {
            Ok(value) => value,
            Err(err) => {
                self.frame.record_error(err);
                Some(0.0)
            }
        }
    }

    fn perform_measure(&self, constraints: Constraints) -> Result<Rc<MeasuredNode>, NodeError> {
        let builder = self.frame.builder.borrow();
        let builder = builder.as_ref().ok_or(NodeError::MissingContext {
            id: self.node_id,
            reason: "layout child applier not configured",
        })?;
        builder.measure_node(self.node_id, constraints)
    }

    pub(super) fn measure_cached(&self, constraints: Constraints) -> Option<Rc<MeasuredNode>> {
        if self.frame.measures() {
            self.measured_constraints.set(Some(constraints));
        }
        let parts = read_cell(&self.parts, Clone::clone);
        let cache = parts.as_deref().map(LayoutParts::cache);
        if let Some(cache) = cache {
            cache.activate(self.cache_epoch.get().get());
        }
        if !self.force_remeasure.get()
            && let Some(cached) = cache.and_then(|cache| cache.get_measurement(constraints))
        {
            // A measure clears the placed flag the parent's placement sets
            // again; a child it no longer places then leaves the scene.
            if self.frame.measures() {
                self.with_pass_layout_state(LayoutState::clear_placed);
            }
            return Some(cached);
        }

        match self.perform_measure(constraints) {
            Ok(measured) => {
                self.force_remeasure.set(false);
                if let Some(cache) = cache {
                    cache.store_measurement(constraints, Rc::clone(&measured));
                }
                Some(measured)
            }
            Err(err) => {
                self.frame.record_error(err);
                None
            }
        }
    }
}

// A record that leaves its parent takes its child out of the parent's count
// of children placed with a z index.
impl Drop for LayoutChildMeasureState {
    fn drop(&mut self) {
        self.frame
            .count_placed_z_index(self.placed_z_index.get(), 0.0);
    }
}

#[expect(
    clippy::multiple_inherent_impl,
    reason = "a node's children records in their file"
)]
impl LayoutRuntimeState {
    pub(super) fn child_states(&self) -> &[Rc<LayoutChildMeasureState>] {
        self.children
            .as_deref()
            .map_or(&[], |children| children.states.as_slice())
    }

    pub(super) fn child_measurables(&self) -> &[Box<dyn Measurable>] {
        self.children
            .as_deref()
            .map_or(&[], |children| children.measurables.as_slice())
    }

    /// Binds the children's frame of the node `parent` to `builder` for a
    /// pass over `count` children. A node that never had one binds nothing.
    pub(super) fn bind_children(
        &mut self,
        builder: &Rc<LayoutBuilderState>,
        (parent, parts): (NodeId, &Rc<LayoutParts>),
        pass: ChildPass,
        count: usize,
    ) -> Option<&mut LayoutChildren> {
        if count == 0 && self.children.is_none() {
            return None;
        }
        let children = self
            .children
            .get_or_insert_with(|| Box::new(LayoutChildren::with_capacity(count, parts)));
        children.frame.bind(builder, parent, pass);
        Some(children)
    }

    pub(super) fn unbind_children(&self) {
        if let Some(children) = &self.children {
            children.frame.unbind();
        }
    }

    /// The first error a child recorded in the pass, taken.
    pub(super) fn take_child_error(&self) -> Option<NodeError> {
        self.children
            .as_ref()
            .and_then(|children| children.frame.error.borrow_mut().take())
    }

    /// Puts the children the last measure measured into `measured_children`.
    pub(super) fn measured_children(
        &self,
        placements: &[Placement],
        placement_indices: &[usize],
        content_offset: Point,
        measured_children: &mut Vec<MeasuredChild>,
    ) {
        let child_states = self.child_states();
        measured_children.reserve_exact(child_states.len());
        for (index, child_state) in child_states.iter().enumerate() {
            let Some(measured) = child_state.measured.take() else {
                continue;
            };
            let placement =
                placement_for_child(placements, placement_indices, index, child_state.node_id);
            let base_position = child_state.placement_position(placement);
            let position = Point {
                x: base_position.x + measured.offset.x,
                y: base_position.y + measured.offset.y,
            };
            if let Some(placement) = placement {
                child_state.place_at(position, placement.z_index);
            }
            measured_children.push(MeasuredChild::new(
                &measured,
                Point {
                    x: content_offset.x + base_position.x,
                    y: content_offset.y + base_position.y,
                },
            ));
        }
    }

    // Whether the node's last placement gave a child a z index other than 0.
}

pub(super) fn inherited_alignment_lines(
    child_states: &[Rc<LayoutChildMeasureState>],
    placements: &[Placement],
    placement_indices: &mut Vec<usize>,
) -> AlignmentLines {
    placement_indices.clear();
    let in_child_order = placements.len() == child_states.len()
        && placements
            .iter()
            .zip(child_states)
            .all(|(placement, child)| placement.node_id == child.node_id);
    if !in_child_order {
        placement_indices.extend(0..placements.len());
        placement_indices.sort_unstable_by_key(|&index| (placements[index].node_id, index));
    }
    let mut lines = AlignmentLines::default();
    for (index, child) in child_states.iter().enumerate() {
        let placement = placement_for_child(placements, placement_indices, index, child.node_id);
        read_cell(&child.measured, |measured| {
            if let Some(measured) = measured {
                lines.merge(
                    measured
                        .alignment_lines_for_parent()
                        .translated(child.placement_position(placement).y),
                );
            }
        });
    }
    lines
}

pub(super) fn placement_for_child<'a>(
    placements: &'a [Placement],
    indices: &[usize],
    child_index: usize,
    node_id: NodeId,
) -> Option<&'a Placement> {
    let index = if indices.is_empty() {
        child_index
    } else {
        let first = indices.partition_point(|&index| placements[index].node_id < node_id);
        *indices.get(first)?
    };
    placements
        .get(index)
        .filter(|placement| placement.node_id == node_id)
}

/// What a node keeps for measuring its children, in their order.
pub(super) struct LayoutChildren {
    pub(super) ids: Vec<NodeId>,
    states: Vec<Rc<LayoutChildMeasureState>>,
    measurables: Vec<Box<dyn Measurable>>,
    frame: Rc<LayoutChildFrame>,
}

impl LayoutChildren {
    /// Room for exactly `count` children, where growth would make room for
    /// four in each of the three lists. Children added later grow the lists
    /// as usual.
    fn with_capacity(count: usize, parent_parts: &Rc<LayoutParts>) -> Self {
        Self {
            ids: Vec::with_capacity(count),
            states: Vec::with_capacity(count),
            measurables: Vec::with_capacity(count),
            frame: Rc::new(LayoutChildFrame {
                parent_parts: Rc::downgrade(parent_parts),
                ..LayoutChildFrame::default()
            }),
        }
    }

    /// The state of the child at `position`, for a pass `bind_children`
    /// bound. Inlined: the child is nearly always where it was.
    #[inline]
    fn state_at(&mut self, position: usize, child_id: NodeId) -> &LayoutChildMeasureState {
        if self.ids.get(position) != Some(&child_id) {
            self.bind_at(position, child_id);
        }
        &self.states[position]
    }

    fn truncate(&mut self, len: usize) {
        self.ids.truncate(len);
        self.states.truncate(len);
        self.measurables.truncate(len);
    }

    /// Puts the state of `child_id` at `position`: the one it had further
    /// on, or a new one.
    #[inline(never)]
    fn bind_at(&mut self, position: usize, child_id: NodeId) {
        let from = match self.ids[position..].iter().position(|&id| id == child_id) {
            Some(offset) => position + offset,
            None => {
                let state = LayoutChildMeasureState::new(child_id, Rc::clone(&self.frame));
                self.ids.push(child_id);
                self.states.push(Rc::clone(&state));
                self.measurables
                    .push(Box::new(LayoutChildMeasurable::new(state)));
                self.ids.len() - 1
            }
        };
        self.ids.swap(position, from);
        self.states.swap(position, from);
        self.measurables.swap(position, from);
    }
}