devpage-design 0.2.5

The design engine: parses, resolves, lays out and paints devpage designs
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
//! An update: a drawing brought from one scene to the next by what changed
//! between them, node by node, and the damage it leaves.

use std::collections::{BTreeSet, HashMap};

use kurbo::{Rect as KRect, Vec2};

use crate::display::{Order, Space};
use crate::scene::{Node, Scene};
use crate::tree::OrderKey;

use super::*;
impl Renderer {
    pub(super) fn update(
        &mut self,
        d: &mut Drawing,
        new: &Scene,
        applied: &crate::scene::Applied,
    ) -> Result<Damage, Undrawable> {
        self.register(&new.fonts);
        d.visited = 0;
        let changes = Changes::of(d, new, applied);
        let mut damage = Vec::new();
        let mut all = false;
        let mut images = false;
        // What is gone: subtrees removed, moved away or made again — their
        // layout nodes, chunks, records and property-tree nodes.
        for order in &changes.removed {
            images |= self.unplace(d, new, order, &mut damage);
        }
        // What is placed, parents before children and siblings in order, so
        // the siblings before each are in the layout tree already.
        for order in &changes.added {
            self.place(d, new, order);
        }
        for order in &changes.attrs {
            restyle(d, new, order);
        }
        // Every frame with children is a run, which hugs them itself.
        d.laid.settle_top(self);
        // Only the changed nodes' paths are walked, and below them only
        // what moved, changed size or was made again.
        let mut path: HashMap<Order, BTreeSet<OrderKey>> = HashMap::new();
        for order in changes.touched() {
            for k in 0..order.len() {
                path.entry(order[..k].to_vec())
                    .or_default()
                    .insert(order[k].clone());
            }
        }
        let attrs: BTreeSet<Order> = changes.attrs.iter().cloned().collect();
        let dirty = |o: &Order| attrs.contains(o);
        // The nodes whose attributes that place or cut their children
        // changed; a node's look alone reaches none of them.
        let placing: BTreeSet<Order> = applied
            .changed
            .iter()
            .filter_map(|(id, old)| {
                let o = new.ids.get(id)?;
                let n = new.at(o)?;
                let moves = old.layout != n.layout
                    || old.gap != n.gap
                    || old.padding != n.padding
                    || old.align != n.align
                    || old.justify != n.justify
                    || old.width != n.width
                    || old.height != n.height
                    || old.clip != n.clip
                    || old.radius != n.radius;
                moves.then(|| o.clone())
            })
            .collect();
        let relaid = |o: &Order| placing.contains(o);
        self.blobs = std::mem::take(&mut d.blobs);
        let (t, c) = (d.list.root_transform, d.list.root_clip);
        let mut walk = Walk {
            scene: new,
            dirty: &dirty,
            relaid: &relaid,
            path: Some(&path),
            damage: Some(&mut damage),
            fresh: None,
            images: false,
        };
        let root = d.laid.root;
        self.sync(
            d,
            &mut walk,
            &new.root,
            Vec::new(),
            Owner::Top,
            root,
            t,
            Place::default(),
            (&Space::Root, Vec2::ZERO),
            c,
            None,
            None,
            (false, false),
            None,
        );
        d.list.flush();
        images |= walk.images;
        d.blobs = std::mem::take(&mut self.blobs);
        let area = d.list.area;
        self.settle_area(d);
        let now = d.list.area;
        if now != area {
            if (now.x, now.y) != (area.x, area.y) {
                all = true;
            } else {
                // What the area gained or lost along its right and bottom.
                let (w0, w1) = (area.width.min(now.width), area.width.max(now.width));
                let (h0, h1) = (area.height.min(now.height), area.height.max(now.height));
                let (x, y) = (now.x as f64, now.y as f64);
                if w0 < w1 {
                    damage.push(KRect::new(x + w0 as f64, y, x + w1 as f64, y + h1 as f64));
                }
                if h0 < h1 {
                    damage.push(KRect::new(x, y + h0 as f64, x + w1 as f64, y + h1 as f64));
                }
            }
        }
        if images {
            self.settle_images(d)?;
        } else {
            d.fits.clone()?;
        }
        Ok(if all {
            Damage::All
        } else {
            Damage::Rects {
                origin: (now.x as f64, now.y as f64),
                rects: damage,
            }
        })
    }
}

/// What a patch changed, by order in the drawing: nodes whose own
/// attributes changed, subtrees that went — removed, moved away, or made
/// again because a node became a leaf of the layout tree or stopped being
/// one — where they were, and subtrees placed where they are now.
#[derive(Default, Debug)]
pub(super) struct Changes {
    pub(super) attrs: Vec<Order>,
    pub(super) added: Vec<Order>,
    pub(super) removed: Vec<Order>,
}

impl Changes {
    /// What `applied` did, found in the drawing `d` showed before and in
    /// `new`: each subtree's root once — none under another — and each in
    /// tree order.
    pub(super) fn of(d: &Drawing, new: &Scene, applied: &crate::scene::Applied) -> Changes {
        let mut removed: BTreeSet<Order> = applied
            .removed
            .iter()
            .filter_map(|id| d.orders.get(id).cloned())
            .collect();
        let mut added: BTreeSet<Order> = applied
            .placed
            .iter()
            .filter_map(|id| new.ids.get(id).cloned())
            .collect();
        let mut attrs = BTreeSet::new();
        let under =
            |set: &BTreeSet<Order>, o: &[OrderKey]| (0..=o.len()).any(|k| set.contains(&o[..k]));
        for (id, old) in &applied.changed {
            let Some(o) = new.ids.get(id) else { continue };
            // A node placed in this patch is made whole.
            if !d.orders.get(id).is_some_and(|was| was == o) || under(&added, o) {
                continue;
            }
            let node = new.at(o).expect("indexed");
            // A frame turning to no layout, or from it, is another kind of
            // run — a free one, or one along a line: made again.
            let free = |n: &Node| n.layout == crate::scene::Layout::None;
            if leafy(old) != leafy(node) || (free(old) != free(node) && !node.children.is_empty()) {
                removed.insert(o.clone());
                added.insert(o.clone());
                continue;
            }
            attrs.insert(o.clone());
            // Its layout decides part of each child's style.
            if old.layout != node.layout {
                for k in node.children.entries().map(|(k, _)| k) {
                    let mut c = o.clone();
                    c.push(k.clone());
                    attrs.insert(c);
                }
            }
        }
        // A frame that starts or stops being a run is laid out another way:
        // made again whole. Only what can turn it is looked at — its own
        // `layout` or `justify`, a child turning `"fill"` along it or no
        // longer, a child coming or going — never its children one by one,
        // unless one of those happened.
        let mut maybe: BTreeSet<Order> = BTreeSet::new();
        let along = |n: &Node, c: &Node| match n.layout {
            crate::scene::Layout::Row => c.width,
            crate::scene::Layout::Column => c.height,
            crate::scene::Layout::None => crate::scene::Size::Hug,
        };
        let fill = crate::scene::Size::Fill;
        for (id, old) in &applied.changed {
            let Some(o) = new.ids.get(id) else { continue };
            let Some(node) = new.at(o) else { continue };
            if old.layout != node.layout || old.justify != node.justify {
                maybe.insert(o.clone());
            }
            // A child turning `"fill"` along its run, or no longer, is laid
            // out another way in it: placed again.
            if !o.is_empty()
                && let Some(p) = new.at(&o[..o.len() - 1])
                && ((along(p, old) == fill) != (along(p, node) == fill))
                && !under(&added, o)
            {
                removed.insert(o.clone());
                added.insert(o.clone());
            }
        }
        for o in added.iter().chain(&removed) {
            if !o.is_empty() {
                maybe.insert(o[..o.len() - 1].to_vec());
            }
        }
        for p in maybe {
            let Some(n) = new.at(&p) else { continue };
            if d.orders.get(&n.id) != Some(&p) || under(&added, &p) {
                continue;
            }
            let was = d.laid.run_of.contains_key(&p);
            // A run stays one while every child it gains takes its own
            // length and it keeps some: no scan of its children.
            let now = if was
                && !n.children.is_empty()
                && n.justify != crate::scene::Align::SpaceBetween
                && n.layout != crate::scene::Layout::None
                && added
                    .iter()
                    .filter(|o| o.len() == p.len() + 1 && o.starts_with(&p))
                    .all(|o| new.at(o).is_none_or(|c| along(n, c) != fill))
                && !applied.changed.iter().any(|(id, _)| {
                    new.ids.get(id).is_some_and(|o| {
                        o.len() == p.len() + 1
                            && o.starts_with(&p)
                            && new.at(o).is_some_and(|c| along(n, c) == fill)
                    })
                }) {
                true
            } else {
                is_run(n)
            };
            if was != now {
                removed.insert(p.clone());
                added.insert(p);
            }
        }
        let tops = |set: &BTreeSet<Order>| -> Vec<Order> {
            set.iter()
                .filter(|o| !(0..o.len()).any(|k| set.contains(&o[..k])))
                .cloned()
                .collect()
        };
        let removed = tops(&removed);
        let added = tops(&added);
        let attrs = attrs
            .into_iter()
            .filter(|o| !under(&added.iter().cloned().collect(), o))
            .collect();
        Changes {
            attrs,
            added,
            removed,
        }
    }

    /// The nodes of the new scene a change is at: every changed and placed
    /// node, and the parent of each removed one.
    pub(super) fn touched(&self) -> impl Iterator<Item = &[OrderKey]> {
        self.attrs
            .iter()
            .chain(&self.added)
            .map(|o| o.as_slice())
            .chain(
                self.removed
                    .iter()
                    .filter(|o| !o.is_empty())
                    .map(|o| &o[..o.len() - 1]),
            )
    }
}

impl Drawing {
    /// Takes out everything at and under `order` — chunks, records, frames,
    /// runs and the property-tree nodes they made — adding where the chunks
    /// were to `damage`. True when any of them painted an image.
    pub(super) fn forget(&mut self, order: &Order, damage: &mut Vec<KRect>) -> bool {
        let under: Vec<Order> = self
            .recs
            .range(order.clone()..)
            .take_while(|(o, _)| o.starts_with(order))
            .map(|(o, _)| o.clone())
            .collect();
        let mut images = false;
        for o in under.iter().rev() {
            let at = self.list.chunk(o).map(|c| self.list.space_at(&c.space));
            if let Some(c) = self.list.take(o) {
                let at = at.unwrap_or(Vec2::ZERO);
                damage.extend(c.bounds.map(|b| b + at));
                self.list.free(&c.made);
            }
            if let Some(rid) = self.laid.run_of.get(o)
                && let Some(drid) = self.shown.remove(rid)
            {
                self.list.remove_run(drid);
                self.shifts.remove(&drid);
                self.contexts.remove(&drid);
            }
            if let Some(rec) = self.recs.remove(o) {
                if self.orders.get(&rec.node) == Some(o) {
                    self.orders.remove(&rec.node);
                }
                images |= !rec.painted.is_empty();
                self.painters.remove(o);
                self.list.trees.transforms.remove(rec.transform);
                if let Some(e) = rec.effect {
                    self.list.trees.effects.remove(e);
                }
                if let Some(c) = rec.clip {
                    self.list.trees.clips.remove(c);
                }
            }
            self.frames.remove(o);
        }
        images
    }

    /// The key of the run's child that `order` is inside, when the node is in
    /// a run's tree.
    pub(super) fn inside(&self, owner: Owner, order: &Order) -> Option<OrderKey> {
        match owner {
            Owner::Top => None,
            Owner::Run(p) => {
                let at = self.laid.runs.get(p).order.len();
                order.get(at).cloned()
            }
        }
    }
}

impl Renderer {
    /// Takes the subtree at `order` out: from the list, and from the layout
    /// tree it is in — a run's child out of its run.
    fn unplace(
        &mut self,
        d: &mut Drawing,
        new: &Scene,
        order: &Order,
        damage: &mut Vec<KRect>,
    ) -> bool {
        let parent = (!order.is_empty()).then(|| order[..order.len() - 1].to_vec());
        let run = parent.as_ref().and_then(|p| d.laid.run_of.get(p).copied());
        let key = order.last().cloned();
        if !order.is_empty() {
            d.laid.sens_drop(&new.root, order);
        }
        // Where a run's child started: what follows it moves from there.
        if let (Some(rid), Some(k)) = (run, &key)
            && let Some(&drid) = d.shown.get(&rid)
            && let Some(at) = d.list.run(drid).child_at(k)
        {
            let r = d.list.run(drid);
            let abs = d.list.space_at(&r.space) + at;
            let along = if r.row { abs.x } else { abs.y };
            let s = d.shifts.entry(drid).or_insert(along);
            *s = s.min(along);
        }
        let images = d.forget(order, damage);
        if let (Some(rid), Some(k)) = (run, &key)
            && let Some(&drid) = d.shown.get(&rid)
        {
            d.list.place_child(drid, k.clone(), None);
        }
        if !d.laid.nodes.contains_key(order) {
            return images;
        }
        // The layout's records of everything under it, its runs freed.
        let nodes: Vec<Order> = d
            .laid
            .nodes
            .range(order.clone()..)
            .take_while(|(o, _)| o.starts_with(order))
            .map(|(o, _)| o.clone())
            .collect();
        let mut freed = Vec::new();
        for o in &nodes {
            d.laid.nodes.remove(o);
            if let Some(rid) = d.laid.run_of.remove(o) {
                freed.push(rid);
            }
        }
        if order.is_empty() {
            for rid in freed {
                d.laid.runs.free(rid);
            }
            return images;
        }
        match (run, key) {
            (Some(rid), Some(k)) => {
                let r = d.laid.runs.get_mut(rid);
                if let Some(c) = r.remove(&k) {
                    remove_subtree(&mut r.tree, c.node);
                }
                let laid = &mut d.laid;
                laid.runs.touch(&mut laid.top, rid, k);
            }
            // A frame that loses its last child stops being a run and is
            // made again whole: every parent here is one.
            _ => unreachable!("a child taken from a frame that isn't a run"),
        }
        for rid in freed {
            d.laid.runs.free(rid);
        }
        images
    }

    /// Puts the subtree at `order` in the layout: into its parent's run —
    /// every frame with children is one — or as a new root.
    fn place(&mut self, d: &mut Drawing, new: &Scene, order: &Order) {
        let node = find_node(&new.root, order);
        if order.is_empty() {
            d.laid = Laid::build(self, new);
            return;
        }
        let parent_order = order[..order.len() - 1].to_vec();
        let parent = find_node(&new.root, &parent_order);
        let key = order.last().expect("a key").clone();
        let (w, h) = d.laid.sens_build(node, order.clone());
        if w || h {
            d.laid
                .sens_settle(&new.root, &parent_order, (w as isize, h as isize));
        }
        if let Some(&rid) = d.laid.run_of.get(&parent_order) {
            let mut run = d.laid.runs.take(rid);
            d.laid.add_child(
                &mut run,
                rid,
                &key,
                node,
                parent,
                &new.images,
                &parent_order,
            );
            d.laid.runs.put(rid, run);
            let laid = &mut d.laid;
            laid.runs.touch(&mut laid.top, rid, key);
            return;
        }
        // A frame that gains its first child becomes a run, and is made
        // again whole: every parent here is one.
        unreachable!("a child placed under a frame that isn't a run");
    }
}

/// A node whose own attributes changed, restyled in its layout tree — a run
/// told how it now sets its children.
fn restyle(d: &mut Drawing, new: &Scene, order: &Order) {
    let node = find_node(&new.root, order);
    let parent = (!order.is_empty()).then(|| find_node(&new.root, &order[..order.len() - 1]));
    let Some(&(owner, id)) = d.laid.nodes.get(order) else {
        return;
    };
    let style = style_of(node, parent);
    d.laid.sens_settle(&new.root, order, (0, 0));
    if let Some(&rid) = d.laid.run_of.get(order) {
        // Its children are laid out again only along a new direction; its
        // leaf, restyled below, is measured again.
        let run = d.laid.runs.get_mut(rid);
        run.set(node);
        run.hug = hugs_by_extent(node, parent).unwrap_or((false, false));
    }
    let tree = d.laid.tree_mut(owner);
    tree.set_style(id, style).expect("style");
    if let Some(leaf) = leaf_of(node, &new.images) {
        tree.set_node_context(id, Some(Some(leaf))).expect("leaf");
    }
    if let Some(p) = parent
        && let Some(&rid) = d.laid.run_of.get(&order[..order.len() - 1])
    {
        let k = order.last().expect("a key").clone();
        let row = d.laid.runs.get(rid).row;
        let across = if row { node.height } else { node.width };
        let sens = d.laid.sensitive(order);
        d.laid
            .runs
            .get_mut(rid)
            .classify(&k, across == crate::scene::Size::Fill, sens);
        let _ = p;
        let laid = &mut d.laid;
        laid.runs.touch(&mut laid.top, rid, k);
        return;
    }
    if let Some(k) = d.inside(owner, order)
        && let Owner::Run(p) = owner
    {
        let laid = &mut d.laid;
        laid.runs.touch(&mut laid.top, p, k);
    }
}

/// What an edit costs, by the kind of frame it is in.
#[cfg(test)]
mod costs {
    mod tests;
}
#[cfg(test)]
mod tests;