nsh-core 0.3.0

Platform-independent core: shadow tree, styles, layout and mount instructions.
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
//! The core's layout tree: Taffy's algorithms (flexbox, grid, block, out of
//! flow) run over nodes the core keeps, through Taffy's low-level traits,
//! as `TaffyTree` runs them over its own (`TaffyView`, which this follows
//! call for call). What the core does differently is its own:
//!
//! - **dirty marking goes up to the root, always.** `TaffyTree::mark_dirty`
//!   stops at the first node whose cache is already empty, taking its
//!   ancestors for dirty too; a node of a `display: none` subtree or an
//!   absolutely positioned one always has an empty cache while the nodes
//!   above keep theirs, so a change there was never laid out.
//! - **no hoisted children.** Taffy records, on each containing block, the
//!   absolutely positioned nodes it laid out, for its rounding pass alone;
//!   the core rounds layouts itself (`core::rounded`), so nothing is kept,
//!   and nothing can go stale.
//! - **no rounding**: [`LayoutTree::unrounded_layout`] is what the
//!   algorithms computed.
//! - **relayout boundaries.** A change inside a node whose size its content
//!   cannot change (fixed points both ways, in a flex container, not
//!   shrinking nor aligned by its baseline: [`LayoutTree::is_boundary`])
//!   dirties nothing above it: [`LayoutTree::relayout_boundaries`] lays its
//!   subtree out again with what its parent gave it last time, and only
//!   when what it gives back differs (its overflow, say) is its parent laid
//!   out again. A large page with values changing often costs what changes,
//!   not what the page is.
//! - **leaves that measure as before.** A leaf whose content changed (its
//!   text) dirties nothing when it measures the same for every input the
//!   layout asked of it since its cache was last cleared
//!   ([`LayoutTree::measures_as_before`]): every layout above was computed
//!   from those answers alone, so all of them stand. A price that keeps its
//!   width, a wrapped paragraph that keeps its lines, cost their text.

use slab::Slab;
use taffy::{
    AvailableSpace, BlockContext, Cache, CacheTree, DetailedGridInfo, DetailedLayoutInfo, Display,
    Layout, LayoutBlockContainer, LayoutContainingBlock, LayoutFlexboxContainer,
    LayoutGridContainer, LayoutInput, LayoutOutput, LayoutPartialTree, NodeId, RunMode, Size,
    Style, TraversePartialTree, TraverseTree, compute_block_layout, compute_cached_layout,
    compute_flexbox_layout, compute_grid_layout, compute_hidden_layout, compute_oof_layout,
    compute_root_layout,
};

/// A node: its style, place in the tree, cache and layout, and what
/// measures it when it is a leaf.
struct LayoutNode<C> {
    style: Style,
    parent: Option<NodeId>,
    children: Vec<NodeId>,
    cache: Cache,
    unrounded: Layout,
    detailed: DetailedLayoutInfo,
    context: Option<C>,
    /// The core's node it lays out ([`LayoutTree::set_tag`]).
    tag: Option<u32>,
    /// What its parent's last full layout gave it and what it gave back:
    /// what a boundary is laid out again with, and compared to.
    last: Option<(LayoutInput, LayoutOutput)>,
    /// It is a boundary waiting in [`LayoutTree::dirty_boundaries`].
    queued: bool,
    /// A leaf's answers to the inputs the layout asked of it since its
    /// cache was last cleared, each once: what the layouts above it were
    /// computed from. `None` once there were more than [`MEASURED`] to keep.
    measured: Option<Vec<(LayoutInput, LayoutOutput)>>,
}

/// How many answers of a leaf [`LayoutNode::measured`] keeps: a layout asks
/// a few (its sizes under a measuring pass and the final one); more come of
/// many layouts at other sizes (a window resized), and are not worth
/// measuring again.
const MEASURED: usize = 8;

impl<C> LayoutNode<C> {
    fn new(style: Style, context: Option<C>) -> Self {
        LayoutNode {
            style,
            parent: None,
            children: Vec::new(),
            cache: Cache::new(),
            unrounded: Layout::new(),
            detailed: DetailedLayoutInfo::None,
            context,
            tag: None,
            last: None,
            queued: false,
            measured: Some(Vec::new()),
        }
    }

    /// Clears its cached layouts, and so what they were computed from.
    fn clear_cache(&mut self) {
        self.cache.clear();
        self.measured = Some(Vec::new());
    }
}

/// The layout nodes of a core, each with a context `C` its leaves are
/// measured with.
pub(crate) struct LayoutTree<C> {
    nodes: Slab<LayoutNode<C>>,
    /// Boundaries a change inside made dirty, to lay out again.
    dirty_boundaries: Vec<NodeId>,
}

fn key(node: NodeId) -> usize {
    usize::from(node)
}

impl<C> LayoutTree<C> {
    pub(crate) fn with_capacity(capacity: usize) -> Self {
        LayoutTree {
            nodes: Slab::with_capacity(capacity),
            dirty_boundaries: Vec::new(),
        }
    }

    /// A new detached node.
    pub(crate) fn new_leaf(&mut self, style: Style) -> NodeId {
        NodeId::from(self.nodes.insert(LayoutNode::new(style, None)))
    }

    /// A new detached node measured with `context`.
    pub(crate) fn new_leaf_with_context(&mut self, style: Style, context: C) -> NodeId {
        NodeId::from(self.nodes.insert(LayoutNode::new(style, Some(context))))
    }

    /// A new detached node holding `children` (detached nodes).
    pub(crate) fn new_with_children(&mut self, style: Style, children: &[NodeId]) -> NodeId {
        let id = NodeId::from(self.nodes.insert(LayoutNode::new(style, None)));
        for &child in children {
            self.nodes[key(child)].parent = Some(id);
        }
        self.nodes[key(id)].children = children.to_vec();
        id
    }

    /// Records that `node` lays out the core's node `tag`.
    pub(crate) fn set_tag(&mut self, node: NodeId, tag: u32) {
        self.nodes[key(node)].tag = Some(tag);
    }

    /// Frees `node`, detaching it from its parent; its children are left
    /// without one.
    pub(crate) fn remove(&mut self, node: NodeId) {
        if let Some(parent) = self.nodes[key(node)].parent {
            self.nodes[key(parent)].children.retain(|&c| c != node);
            self.mark_dirty(parent);
        }
        if self.nodes[key(node)].queued {
            self.dirty_boundaries.retain(|&b| b != node);
        }
        let removed = self.nodes.remove(key(node));
        for child in removed.children {
            self.nodes[key(child)].parent = None;
        }
    }

    pub(crate) fn style(&self, node: NodeId) -> &Style {
        &self.nodes[key(node)].style
    }

    /// Replaces `node`'s style: its own size may change, so its parent is
    /// laid out again (whether it is a boundary or not).
    pub(crate) fn set_style(&mut self, node: NodeId, style: Style) {
        let n = &mut self.nodes[key(node)];
        n.style = style;
        n.clear_cache();
        if let Some(parent) = n.parent {
            self.mark_dirty(parent);
        }
    }

    /// Replaces what measures `node`.
    pub(crate) fn set_node_context(&mut self, node: NodeId, context: Option<C>) {
        self.nodes[key(node)].context = context;
        self.mark_dirty(node);
    }

    pub(crate) fn get_node_context_mut(&mut self, node: NodeId) -> Option<&mut C> {
        self.nodes[key(node)].context.as_mut()
    }

    pub(crate) fn parent(&self, node: NodeId) -> Option<NodeId> {
        self.nodes[key(node)].parent
    }

    #[cfg(test)]
    pub(crate) fn children(&self, node: NodeId) -> &[NodeId] {
        &self.nodes[key(node)].children
    }

    /// Appends `child` (detached) to `parent`'s children.
    pub(crate) fn add_child(&mut self, parent: NodeId, child: NodeId) {
        let index = self.nodes[key(parent)].children.len();
        self.insert_child_at_index(parent, index, child);
    }

    /// Inserts `child` (detached) into `parent` at `index`.
    pub(crate) fn insert_child_at_index(&mut self, parent: NodeId, index: usize, child: NodeId) {
        self.nodes[key(child)].parent = Some(parent);
        self.nodes[key(parent)].children.insert(index, child);
        self.mark_dirty(parent);
    }

    /// Detaches `child` from `parent`.
    pub(crate) fn remove_child(&mut self, parent: NodeId, child: NodeId) {
        self.nodes[key(parent)].children.retain(|&c| c != child);
        self.nodes[key(child)].parent = None;
        self.mark_dirty(parent);
    }

    /// Something in `node` changed (its children, what measures it):
    /// clears the cached layouts of `node` and of every node above it up to
    /// the root (never stopping at a node whose cache is already empty: see
    /// the module's docs), or up to the first boundary on the way, `node`
    /// included, which is queued to be laid out again on its own.
    pub(crate) fn mark_dirty(&mut self, node: NodeId) {
        let mut at = Some(node);
        while let Some(node) = at {
            self.nodes[key(node)].clear_cache();
            if self.is_boundary(node) {
                let n = &mut self.nodes[key(node)];
                if !n.queued {
                    n.queued = true;
                    self.dirty_boundaries.push(node);
                }
                return;
            }
            at = self.nodes[key(node)].parent;
        }
    }

    /// Whether what changes inside `node` cannot change what its parent's
    /// layout takes from it, so that layout stands: it has a size of fixed
    /// points both ways in a flex container, and everything the flex
    /// algorithm takes from an item follows from that size alone. Its flex
    /// basis is `auto`, its main size (a percentage basis against a parent
    /// of no definite size, or another length under a content-based
    /// automatic minimum, would follow its content); it does not shrink (its
    /// automatic minimum, its content's, is never above its size, so it
    /// only matters to shrinking); growing takes the free space its parent
    /// and siblings leave; it is not aligned by its baseline; its intrinsic
    /// contributions are its size. And its parent laid it out before. What
    /// it gives back that the size does not fix (overflow, margins) is
    /// compared after ([`LayoutTree::relayout_boundaries`]).
    fn is_boundary(&self, node: NodeId) -> bool {
        let n = &self.nodes[key(node)];
        let Some(parent) = n.parent else {
            return false;
        };
        let fixed = |d: taffy::Dimension| d.into_raw().tag() == taffy::CompactLength::LENGTH_TAG;
        let (style, parent) = (&n.style, &self.nodes[key(parent)].style);
        let baseline = |a: taffy::AlignItems| a.keyword() == taffy::AlignItemsKeyword::Baseline;
        n.last.is_some()
            && parent.display == Display::Flex
            && style.display != Display::None
            && fixed(style.size.width)
            && fixed(style.size.height)
            && style.flex_shrink == 0.0
            && style.flex_basis == taffy::Dimension::auto()
            && !baseline(parent.align_items)
            && !style.align_self.is_some_and(baseline)
    }

    /// What measures the leaf `node` changed (its text, say): whether it
    /// measures with `measure` as it did for every input the layout asked of
    /// it since its cache was last cleared, so that every layout above it
    /// stands and nothing needs laying out again. When it does not, or it
    /// was asked too much to tell, the caller marks it dirty
    /// ([`LayoutTree::mark_dirty`]). A leaf never asked (new, or dirty
    /// already) measures as before: nothing was computed from it.
    pub(crate) fn measures_as_before<M>(&mut self, node: NodeId, mut measure: M) -> bool
    where
        M: FnMut(LayoutInput, NodeId, Option<&mut C>, &Style) -> LayoutOutput,
    {
        let n = &mut self.nodes[key(node)];
        let Some(measured) = &n.measured else {
            return false;
        };
        measured
            .iter()
            .all(|(input, output)| measure(*input, node, n.context.as_mut(), &n.style) == *output)
    }

    /// Whether `node` has no cached layout: it changed, or something in it
    /// did, since it was last laid out.
    pub(crate) fn dirty(&self, node: NodeId) -> bool {
        self.nodes[key(node)].cache.is_empty()
    }

    /// `node`'s layout as the algorithms computed it, relative to its
    /// parent, unrounded.
    pub(crate) fn unrounded_layout(&self, node: NodeId) -> &Layout {
        &self.nodes[key(node)].unrounded
    }

    /// Lays out the boundaries a change inside made dirty, each with what
    /// its parent gave it last time; one that gives back what it gave
    /// before (its size, overflow, baselines, margins) leaves its parent's
    /// layout standing, another dirties its parent, as a change of its own
    /// would. Returns the tags of those laid out on their own: their
    /// subtrees' frames are new, nothing else's is.
    pub(crate) fn relayout_boundaries<M>(&mut self, measure: M) -> Vec<u32>
    where
        M: FnMut(LayoutInput, NodeId, Option<&mut C>, &Style) -> LayoutOutput,
    {
        let mut relaid = Vec::new();
        if self.dirty_boundaries.is_empty() {
            return relaid;
        }
        let mut view = View {
            tree: self,
            measure,
        };
        while let Some(node) = view.tree.dirty_boundaries.pop() {
            let n = &mut view.tree.nodes[key(node)];
            n.queued = false;
            let parent = n.parent;
            // Moved or restyled since: no longer one on its own.
            if !view.tree.is_boundary(node) {
                if let Some(parent) = parent {
                    view.tree.mark_dirty(parent);
                }
                continue;
            }
            let (input, before) = view.tree.nodes[key(node)].last.clone().expect("laid out");
            let after = view.compute_child_layout(node, input, None);
            if after == before {
                relaid.extend(view.tree.nodes[key(node)].tag);
            } else if let Some(parent) = parent {
                view.tree.mark_dirty(parent);
            }
        }
        relaid
    }

    /// Lays out the tree under `root` within `available`, measuring leaves
    /// with `measure` (`taffy::TaffyTree::compute_layout_with_measure`'s).
    pub(crate) fn compute_layout_with_measure<M>(
        &mut self,
        root: NodeId,
        available: Size<AvailableSpace>,
        measure: M,
    ) where
        M: FnMut(LayoutInput, NodeId, Option<&mut C>, &Style) -> LayoutOutput,
    {
        let mut view = View {
            tree: self,
            measure,
        };
        compute_root_layout(&mut view, root, available);
    }
}

/// The tree being laid out, with what measures its leaves: what Taffy's
/// algorithms run over.
struct View<'t, C, M> {
    tree: &'t mut LayoutTree<C>,
    measure: M,
}

impl<C, M> View<'_, C, M>
where
    M: FnMut(LayoutInput, NodeId, Option<&mut C>, &Style) -> LayoutOutput,
{
    fn node(&self, node: NodeId) -> &LayoutNode<C> {
        &self.tree.nodes[key(node)]
    }

    /// What both `compute_child_layout` and `compute_block_child_layout`
    /// run (`TaffyView::compute_child_layout`).
    fn compute_child_layout(
        &mut self,
        node: NodeId,
        inputs: LayoutInput,
        block: Option<&mut BlockContext<'_>>,
    ) -> LayoutOutput {
        // An ancestor is `display: none`: hidden whatever its own display.
        if inputs.run_mode == RunMode::PerformHiddenLayout {
            self.tree.nodes[key(node)].last = None;
            return compute_hidden_layout(self, node);
        }
        let output = compute_cached_layout(self, node, inputs, |view, node, inputs| {
            let display = view.node(node).style.display;
            let has_children = !view.node(node).children.is_empty();
            let mut output = match (display, has_children) {
                (Display::None, _) => compute_hidden_layout(view, node),
                (Display::Block, true) => compute_block_layout(view, node, inputs, block),
                (Display::FlowRoot, true) => compute_block_layout(view, node, inputs, None),
                (Display::Flex, true) => compute_flexbox_layout(view, node, inputs),
                (Display::Grid, true) => compute_grid_layout(view, node, inputs),
                (_, false) => {
                    let n = &mut view.tree.nodes[key(node)];
                    let output = (view.measure)(inputs, node, n.context.as_mut(), &n.style);
                    if let Some(measured) = &mut n.measured
                        && !measured.iter().any(|(asked, _)| *asked == inputs)
                    {
                        if measured.len() < MEASURED {
                            measured.push((inputs, output.clone()));
                        } else {
                            n.measured = None;
                        }
                    }
                    output
                }
            };
            // The out-of-flow nodes this one is the containing block of, in
            // full layout passes only, as Taffy's tree does.
            if inputs.run_mode == RunMode::PerformLayout {
                compute_oof_layout(view, node, &mut output);
            }
            output
        });
        if inputs.run_mode == RunMode::PerformLayout {
            self.tree.nodes[key(node)].last = Some((inputs, output.clone()));
        }
        output
    }
}

impl<C, M> TraversePartialTree for View<'_, C, M>
where
    M: FnMut(LayoutInput, NodeId, Option<&mut C>, &Style) -> LayoutOutput,
{
    type ChildIter<'a>
        = std::iter::Copied<std::slice::Iter<'a, NodeId>>
    where
        Self: 'a;

    fn child_ids(&self, parent: NodeId) -> Self::ChildIter<'_> {
        self.node(parent).children.iter().copied()
    }

    fn child_count(&self, parent: NodeId) -> usize {
        self.node(parent).children.len()
    }

    fn get_child_id(&self, parent: NodeId, index: usize) -> NodeId {
        self.node(parent).children[index]
    }
}

impl<C, M> TraverseTree for View<'_, C, M> where
    M: FnMut(LayoutInput, NodeId, Option<&mut C>, &Style) -> LayoutOutput
{
}

impl<C, M> LayoutPartialTree for View<'_, C, M>
where
    M: FnMut(LayoutInput, NodeId, Option<&mut C>, &Style) -> LayoutOutput,
{
    type CoreContainerStyle<'a>
        = &'a Style
    where
        Self: 'a;

    // `Style`'s default, Taffy's `DefaultCheapStr` with `std`.
    type CustomIdent = String;

    fn get_core_container_style(&self, node: NodeId) -> Self::CoreContainerStyle<'_> {
        &self.node(node).style
    }

    fn set_unrounded_layout(&mut self, node: NodeId, layout: &Layout) {
        self.tree.nodes[key(node)].unrounded = *layout;
    }

    fn resolve_calc_value(&self, _: *const (), _: f32) -> f32 {
        0.0
    }

    fn compute_child_layout(&mut self, node: NodeId, inputs: LayoutInput) -> LayoutOutput {
        View::compute_child_layout(self, node, inputs, None)
    }
}

impl<C, M> LayoutContainingBlock for View<'_, C, M>
where
    M: FnMut(LayoutInput, NodeId, Option<&mut C>, &Style) -> LayoutOutput,
{
    type OofItemStyle<'a>
        = &'a Style
    where
        Self: 'a;

    fn get_oof_item_style(&self, node: NodeId) -> Self::OofItemStyle<'_> {
        &self.node(node).style
    }

    // Kept for Taffy's rounding pass alone, which the core does not run.
    fn clear_hoisted_children(&mut self, _: NodeId) {}

    fn add_hoisted_children(&mut self, _: NodeId, _: &[NodeId]) {}

    fn get_detailed_layout_info(&self, node: NodeId) -> &DetailedLayoutInfo {
        &self.node(node).detailed
    }
}

impl<C, M> CacheTree for View<'_, C, M>
where
    M: FnMut(LayoutInput, NodeId, Option<&mut C>, &Style) -> LayoutOutput,
{
    fn cache_get(&mut self, node: NodeId, input: &LayoutInput) -> Option<LayoutOutput> {
        self.tree.nodes[key(node)].cache.get(input)
    }

    fn cache_store(&mut self, node: NodeId, input: &LayoutInput, output: LayoutOutput) {
        self.tree.nodes[key(node)].cache.store(input, output);
    }

    fn cache_clear(&mut self, node: NodeId) {
        self.tree.nodes[key(node)].clear_cache();
    }
}

impl<C, M> LayoutBlockContainer for View<'_, C, M>
where
    M: FnMut(LayoutInput, NodeId, Option<&mut C>, &Style) -> LayoutOutput,
{
    type BlockContainerStyle<'a>
        = &'a Style
    where
        Self: 'a;
    type BlockItemStyle<'a>
        = &'a Style
    where
        Self: 'a;

    fn get_block_container_style(&self, node: NodeId) -> Self::BlockContainerStyle<'_> {
        &self.node(node).style
    }

    fn get_block_child_style(&self, child: NodeId) -> Self::BlockItemStyle<'_> {
        &self.node(child).style
    }

    fn compute_block_child_layout(
        &mut self,
        node: NodeId,
        inputs: LayoutInput,
        block: Option<&mut BlockContext<'_>>,
    ) -> LayoutOutput {
        View::compute_child_layout(self, node, inputs, block)
    }
}

impl<C, M> LayoutFlexboxContainer for View<'_, C, M>
where
    M: FnMut(LayoutInput, NodeId, Option<&mut C>, &Style) -> LayoutOutput,
{
    type FlexboxContainerStyle<'a>
        = &'a Style
    where
        Self: 'a;
    type FlexboxItemStyle<'a>
        = &'a Style
    where
        Self: 'a;

    fn get_flexbox_container_style(&self, node: NodeId) -> Self::FlexboxContainerStyle<'_> {
        &self.node(node).style
    }

    fn get_flexbox_child_style(&self, child: NodeId) -> Self::FlexboxItemStyle<'_> {
        &self.node(child).style
    }
}

impl<C, M> LayoutGridContainer for View<'_, C, M>
where
    M: FnMut(LayoutInput, NodeId, Option<&mut C>, &Style) -> LayoutOutput,
{
    type GridContainerStyle<'a>
        = &'a Style
    where
        Self: 'a;
    type GridItemStyle<'a>
        = &'a Style
    where
        Self: 'a;

    fn get_grid_container_style(&self, node: NodeId) -> Self::GridContainerStyle<'_> {
        &self.node(node).style
    }

    fn get_grid_child_style(&self, child: NodeId) -> Self::GridItemStyle<'_> {
        &self.node(child).style
    }

    fn set_detailed_grid_info(&mut self, node: NodeId, info: DetailedGridInfo) {
        self.tree.nodes[key(node)].detailed = DetailedLayoutInfo::Grid(Box::new(info));
    }
}