atagen

monolith

public
Star0
Watch0
8f974106aae3cb42125335d1835db9f3c9c20941
1
ssh://brewery@pls.lobotomise.me:33/atagen/monolith.git
crates/layout/src/tree.rsRust730 lines23.5 KB
1
use monolith_planner::Rect;
2
use slotmap::{SlotMap, new_key_type};
3
use std::collections::HashMap;
4
use std::hash::Hash;
5
6
new_key_type! { pub struct NodeId; }
7
8
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
9
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10
#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
11
pub enum Axis {
12
    Horizontal,
13
    Vertical,
14
}
15
16
impl Axis {
17
    pub fn other(self) -> Self {
18
        match self {
19
            Self::Horizontal => Self::Vertical,
20
            Self::Vertical => Self::Horizontal,
21
        }
22
    }
23
}
24
25
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
26
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
27
#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
28
pub enum Dir {
29
    Left,
30
    Right,
31
    Up,
32
    Down,
33
}
34
35
impl Dir {
36
    pub fn axis(self) -> Axis {
37
        match self {
38
            Self::Left | Self::Right => Axis::Horizontal,
39
            Self::Up | Self::Down => Axis::Vertical,
40
        }
41
    }
42
43
    pub fn forward(self) -> bool {
44
        matches!(self, Self::Right | Self::Down)
45
    }
46
}
47
48
const MIN_SHARE: f64 = 0.05;
49
50
#[derive(Clone, Debug)]
51
struct Child {
52
    node: NodeId,
53
    weight: f64,
54
}
55
56
#[derive(Clone, Debug)]
57
enum Kind<W> {
58
    Leaf(W),
59
    Container {
60
        axis: Axis,
61
        children: Vec<Child>,
62
        expanded: Option<NodeId>,
63
    },
64
}
65
66
#[derive(Clone, Debug)]
67
struct Node<W> {
68
    parent: Option<NodeId>,
69
    kind: Kind<W>,
70
}
71
72
/// One output's tiling tree. Every tiled window of every tag lives here, the
73
/// caller's visibility filter decides which ones take space.
74
///
75
/// Children run on the axis opposite their parent. Containers hold at least
76
/// two children, hidden ones included, and a container left with one child
77
/// is replaced by it.
78
#[derive(Clone, Debug)]
79
pub struct Tree<W> {
80
    nodes: SlotMap<NodeId, Node<W>>,
81
    root: Option<NodeId>,
82
    leaves: HashMap<W, NodeId>,
83
    stack: Axis,
84
}
85
86
impl<W: Copy + Eq + Hash> Default for Tree<W> {
87
    fn default() -> Self {
88
        Self {
89
            nodes: SlotMap::with_key(),
90
            root: None,
91
            leaves: HashMap::new(),
92
            stack: Axis::Vertical,
93
        }
94
    }
95
}
96
97
impl<W: Copy + Eq + Hash> Tree<W> {
98
    /// The axis tag rows stack along.
99
    pub fn stack_axis(&self) -> Axis {
100
        self.stack
101
    }
102
103
    pub fn contains(&self, w: W) -> bool {
104
        self.leaves.contains_key(&w)
105
    }
106
107
    pub fn is_empty(&self) -> bool {
108
        self.root.is_none()
109
    }
110
111
    pub fn windows(&self) -> impl Iterator<Item = W> + '_ {
112
        self.leaves.keys().copied()
113
    }
114
115
    /// Inserts `w` as the next sibling of `anchor`, or as a new row if
116
    /// `anchor` is not in the tree.
117
    pub fn insert_after(&mut self, w: W, anchor: W) {
118
        let Some(&a) = self.leaves.get(&anchor) else {
119
            return self.insert_row(w);
120
        };
121
        let n = self.new_leaf(w);
122
        match self.position(a) {
123
            Some((p, i)) if Some(p) == self.root && self.axis(p) == Some(self.stack) => {
124
                let weight = self.unlink(p, i).weight;
125
                let c = self.new_container(self.stack.other());
126
                self.attach(c, 0, a, 1.0);
127
                self.attach(c, 1, n, 1.0);
128
                self.attach(p, i, c, weight);
129
            }
130
            Some((p, i)) => {
131
                let weight = self.mean_weight(p);
132
                self.attach(p, i + 1, n, weight);
133
            }
134
            None => {
135
                let c = self.new_container(self.stack.other());
136
                self.attach(c, 0, a, 1.0);
137
                self.attach(c, 1, n, 1.0);
138
                self.root = Some(c);
139
            }
140
        }
141
    }
142
143
    /// Appends `w` as a row of its own along the stacking axis.
144
    pub fn insert_row(&mut self, w: W) {
145
        let n = self.new_leaf(w);
146
        match self.root {
147
            None => self.root = Some(n),
148
            Some(r) if self.axis(r) == Some(self.stack) => {
149
                let weight = self.mean_weight(r);
150
                let end = self.children(r).len();
151
                self.attach(r, end, n, weight);
152
            }
153
            Some(r) => {
154
                let c = self.new_container(self.stack);
155
                self.attach(c, 0, r, 1.0);
156
                self.attach(c, 1, n, 1.0);
157
                self.root = Some(c);
158
            }
159
        }
160
    }
161
162
    pub fn remove(&mut self, w: W) -> bool {
163
        let Some(n) = self.leaves.remove(&w) else {
164
            return false;
165
        };
166
        self.detach(n);
167
        self.nodes.remove(n);
168
        true
169
    }
170
171
    pub fn retain(&mut self, mut keep: impl FnMut(W) -> bool) {
172
        let gone: Vec<W> = self.windows().filter(|&w| !keep(w)).collect();
173
        for w in gone {
174
            self.remove(w);
175
        }
176
    }
177
178
    /// Moves a window one step in `dir`. Leaves swap slots with the next
179
    /// visible sibling, enter a sibling container, or step out beside their
180
    /// container. Hidden windows never move.
181
    pub fn move_window(&mut self, w: W, dir: Dir, vis: impl Fn(W) -> bool) -> bool {
182
        let Some(&n) = self.leaves.get(&w) else {
183
            return false;
184
        };
185
        self.move_node(n, dir, &vis, true)
186
    }
187
188
    /// Moves the container holding `w` as a rigid block. A window sitting
189
    /// directly in the root is its own block.
190
    pub fn move_container(&mut self, w: W, dir: Dir, vis: impl Fn(W) -> bool) -> bool {
191
        let Some(&n) = self.leaves.get(&w) else {
192
            return false;
193
        };
194
        let block = self.nodes[n]
195
            .parent
196
            .filter(|&p| Some(p) != self.root)
197
            .unwrap_or(n);
198
        self.move_node(block, dir, &vis, false)
199
    }
200
201
    /// Moves a window to the end of the next visible lane in `dir` among its
202
    /// siblings. A lone window there becomes a stack with it. A window with no
203
    /// sibling lane that way leaves its stack as `move_window` would, so it
204
    /// takes one press to step out and another to merge.
205
    pub fn merge_window(&mut self, w: W, dir: Dir, vis: impl Fn(W) -> bool) -> bool {
206
        let Some(&n) = self.leaves.get(&w) else {
207
            return false;
208
        };
209
        if let Some((p, i)) = self.position(n)
210
            && self.axis(p) == Some(dir.axis())
211
            && let Some(j) = self.next_shown(p, i, dir, &vis)
212
        {
213
            self.join_lane(n, p, j);
214
            return true;
215
        }
216
        self.move_node(n, dir, &vis, true)
217
    }
218
219
    /// Whether every container above `w` is expanded towards it.
220
    pub fn maximized(&self, w: W) -> bool {
221
        let Some(&leaf) = self.leaves.get(&w) else {
222
            return false;
223
        };
224
        let mut cur = leaf;
225
        let mut any = false;
226
        while let Some(p) = self.nodes[cur].parent {
227
            if self.expanded(p) != Some(cur) {
228
                return false;
229
            }
230
            any = true;
231
            cur = p;
232
        }
233
        any
234
    }
235
236
    /// Transposes the whole tree.
237
    pub fn flip(&mut self) {
238
        self.stack = self.stack.other();
239
        for node in self.nodes.values_mut() {
240
            if let Kind::Container { axis, .. } = &mut node.kind {
241
                *axis = axis.other();
242
            }
243
        }
244
    }
245
246
    /// Toggles expansion for `count` levels of `w`'s ancestry, starting
247
    /// `skip` levels up. Level 0 expands the window inside its container.
248
    pub fn toggle_expand(&mut self, w: W, skip: usize, count: usize) -> bool {
249
        let Some(&leaf) = self.leaves.get(&w) else {
250
            return false;
251
        };
252
        let mut path = Vec::new();
253
        let mut cur = leaf;
254
        while let Some(p) = self.nodes[cur].parent {
255
            path.push((p, cur));
256
            cur = p;
257
        }
258
        let path: Vec<_> = path.into_iter().skip(skip).take(count).collect();
259
        if path.is_empty() {
260
            return false;
261
        }
262
        let all = path.iter().all(|&(p, c)| self.expanded(p) == Some(c));
263
        for (p, c) in path {
264
            if let Kind::Container { expanded, .. } = &mut self.nodes[p].kind {
265
                *expanded = (!all).then_some(c);
266
            }
267
        }
268
        true
269
    }
270
271
    /// Grows `w` by `delta`, a fraction of the visible length of the nearest
272
    /// container along `axis` where it has a visible sibling. Hidden siblings
273
    /// keep their weights, and with them their share.
274
    pub fn resize(&mut self, w: W, axis: Axis, delta: f64, vis: impl Fn(W) -> bool) -> bool {
275
        let Some(&leaf) = self.leaves.get(&w) else {
276
            return false;
277
        };
278
        let mut cur = leaf;
279
        while let Some((p, i)) = self.position(cur) {
280
            if self.axis(p) == Some(axis) {
281
                let shown: Vec<usize> = (0..self.children(p).len())
282
                    .filter(|&j| self.visible(self.children(p)[j].node, &vis))
283
                    .collect();
284
                if shown.len() >= 2 {
285
                    let children = self.children_mut(p);
286
                    let sum: f64 = shown.iter().map(|&j| children[j].weight).sum();
287
                    let old = children[i].weight;
288
                    let max = 1.0 - MIN_SHARE * (shown.len() - 1) as f64;
289
                    let new = (old / sum + delta).clamp(MIN_SHARE, max) * sum;
290
                    let scale = (sum - new) / (sum - old);
291
                    for j in shown {
292
                        children[j].weight = if j == i {
293
                            new
294
                        } else {
295
                            children[j].weight * scale
296
                        };
297
                    }
298
                    return true;
299
                }
300
            }
301
            cur = p;
302
        }
303
        false
304
    }
305
306
    /// Lays the tree out in `area`. Hidden windows come back as zero-size
307
    /// rects on the boundary where they would reappear.
308
    pub fn arrange(&self, area: Rect, gap: f64, vis: impl Fn(W) -> bool) -> Vec<(W, Rect)> {
309
        let mut out = Vec::with_capacity(self.leaves.len());
310
        if let Some(r) = self.root {
311
            let area = if self.visible(r, &vis) {
312
                area
313
            } else {
314
                Rect::new(area.x + area.w / 2.0, area.y, 0.0, area.h)
315
            };
316
            self.place(r, area, gap, &vis, &mut out);
317
        }
318
        out
319
    }
320
321
    fn place(
322
        &self,
323
        n: NodeId,
324
        r: Rect,
325
        gap: f64,
326
        vis: &impl Fn(W) -> bool,
327
        out: &mut Vec<(W, Rect)>,
328
    ) {
329
        let (axis, children, expanded) = match &self.nodes[n].kind {
330
            Kind::Leaf(w) => return out.push((*w, r)),
331
            Kind::Container {
332
                axis,
333
                children,
334
                expanded,
335
            } => (*axis, children, *expanded),
336
        };
337
        let shown: Vec<bool> = children.iter().map(|c| self.visible(c.node, vis)).collect();
338
        let (start, len) = span(r, axis);
339
        if let Some(e) = expanded.and_then(|e| children.iter().position(|c| c.node == e))
340
            && shown[e]
341
        {
342
            for (i, c) in children.iter().enumerate() {
343
                let sub = match i.cmp(&e) {
344
                    std::cmp::Ordering::Equal => r,
345
                    std::cmp::Ordering::Less => with_span(r, axis, start, 0.0),
346
                    std::cmp::Ordering::Greater => with_span(r, axis, start + len, 0.0),
347
                };
348
                self.place(c.node, sub, gap, vis, out);
349
            }
350
            return;
351
        }
352
        let count = shown.iter().filter(|s| **s).count();
353
        let total: f64 = children
354
            .iter()
355
            .zip(&shown)
356
            .filter(|(_, s)| **s)
357
            .map(|(c, _)| c.weight)
358
            .sum();
359
        let gaps = count.saturating_sub(1) as f64;
360
        let gap = if gaps > 0.0 { gap.min(len / gaps) } else { gap };
361
        let avail = len - gap * gaps;
362
        let last = shown.iter().rposition(|s| *s);
363
        let mut pos = start;
364
        let mut seen = false;
365
        for (i, c) in children.iter().enumerate() {
366
            if shown[i] {
367
                if seen {
368
                    pos += gap;
369
                }
370
                let l = avail * c.weight / total;
371
                self.place(c.node, with_span(r, axis, pos, l), gap, vis, out);
372
                pos += l;
373
                seen = true;
374
            } else {
375
                let between = seen && last.is_some_and(|l| l > i);
376
                let at = if between { pos + gap / 2.0 } else { pos };
377
                self.place(c.node, with_span(r, axis, at, 0.0), gap, vis, out);
378
            }
379
        }
380
    }
381
382
    fn move_node(&mut self, n: NodeId, dir: Dir, vis: &impl Fn(W) -> bool, enter: bool) -> bool {
383
        let mut cur = n;
384
        while let Some((p, i)) = self.position(cur) {
385
            if self.axis(p) == Some(dir.axis()) {
386
                let hidden_lane = self
387
                    .adjacent(p, i, dir)
388
                    .filter(|&j| enter && !self.visible(self.children(p)[j].node, vis));
389
                if cur != n {
390
                    if let Some(j) = hidden_lane {
391
                        self.join_lane(n, p, j);
392
                    } else {
393
                        let at = if dir.forward() { i + 1 } else { i };
394
                        self.relocate(n, p, at);
395
                    }
396
                    return true;
397
                }
398
                if let Some(j) = self.next_shown(p, i, dir, vis) {
399
                    let sib = self.children(p)[j].node;
400
                    if enter && self.axis(sib).is_some() {
401
                        let end = self.children(sib).len();
402
                        self.relocate(n, sib, end);
403
                    } else {
404
                        let children = self.children_mut(p);
405
                        children[i].node = sib;
406
                        children[j].node = n;
407
                    }
408
                    return true;
409
                }
410
                if let Some(j) = hidden_lane {
411
                    self.join_lane(n, p, j);
412
                    return true;
413
                }
414
            }
415
            cur = p;
416
        }
417
        if cur == n || self.axis(cur) == Some(dir.axis()) {
418
            return false;
419
        }
420
        let (old, i) = self.position(n).expect("n is below the root");
421
        self.unlink(old, i);
422
        self.normalize(old);
423
        let r = self.root.expect("the rest of the tree survives");
424
        if self.axis(r) == Some(dir.axis()) {
425
            let weight = self.mean_weight(r);
426
            let at = if dir.forward() {
427
                self.children(r).len()
428
            } else {
429
                0
430
            };
431
            self.attach(r, at, n, weight);
432
        } else {
433
            let c = self.new_container(dir.axis());
434
            self.attach(c, 0, r, 1.0);
435
            let at = if dir.forward() {
436
                self.children(c).len()
437
            } else {
438
                0
439
            };
440
            self.attach(c, at, n, 1.0);
441
            self.root = Some(c);
442
        }
443
        true
444
    }
445
446
    fn next_shown(&self, p: NodeId, i: usize, dir: Dir, vis: &impl Fn(W) -> bool) -> Option<usize> {
447
        let children = self.children(p);
448
        if dir.forward() {
449
            (i + 1..children.len()).find(|&j| self.visible(children[j].node, vis))
450
        } else {
451
            (0..i).rev().find(|&j| self.visible(children[j].node, vis))
452
        }
453
    }
454
455
    fn adjacent(&self, p: NodeId, i: usize, dir: Dir) -> Option<usize> {
456
        let j = if dir.forward() {
457
            i + 1
458
        } else {
459
            i.checked_sub(1)?
460
        };
461
        (j < self.children(p).len()).then_some(j)
462
    }
463
464
    /// Moves `n` to the end of the lane at `p`'s child `j`. A lone window
465
    /// there becomes a lane of its own, perpendicular to `p`.
466
    fn join_lane(&mut self, n: NodeId, p: NodeId, j: usize) {
467
        let lane = self.children(p)[j].node;
468
        let lane = if self.axis(lane).is_some() {
469
            lane
470
        } else {
471
            let c = self.new_container(self.axis(p).expect("p is a container").other());
472
            self.children_mut(p)[j].node = c;
473
            self.nodes[c].parent = Some(p);
474
            if let Kind::Container { expanded, .. } = &mut self.nodes[p].kind
475
                && *expanded == Some(lane)
476
            {
477
                *expanded = Some(c);
478
            }
479
            self.attach(c, 0, lane, 1.0);
480
            c
481
        };
482
        let end = self.children(lane).len();
483
        self.relocate(n, lane, end);
484
    }
485
486
    fn relocate(&mut self, n: NodeId, p: NodeId, at: usize) {
487
        let (old, i) = self.position(n).expect("relocated nodes have a parent");
488
        let weight = self.mean_weight(p);
489
        let at = if old == p && i < at { at - 1 } else { at };
490
        self.unlink(old, i);
491
        self.attach(p, at, n, weight);
492
        self.normalize(old);
493
    }
494
495
    /// Places `node` among `p`'s children. A container running along `p`'s
496
    /// axis is dissolved into `p` so axes keep alternating.
497
    fn attach(&mut self, p: NodeId, at: usize, node: NodeId, weight: f64) {
498
        if self.axis(node) == self.axis(p) {
499
            let Some(Node {
500
                kind: Kind::Container { children, .. },
501
                ..
502
            }) = self.nodes.remove(node)
503
            else {
504
                unreachable!("only containers have an axis");
505
            };
506
            let total: f64 = children.iter().map(|c| c.weight).sum();
507
            for (k, c) in children.into_iter().enumerate() {
508
                self.nodes[c.node].parent = Some(p);
509
                let weight = weight * c.weight / total;
510
                self.children_mut(p).insert(
511
                    at + k,
512
                    Child {
513
                        node: c.node,
514
                        weight,
515
                    },
516
                );
517
            }
518
        } else {
519
            self.nodes[node].parent = Some(p);
520
            self.children_mut(p).insert(at, Child { node, weight });
521
        }
522
    }
523
524
    fn unlink(&mut self, p: NodeId, i: usize) -> Child {
525
        let Kind::Container {
526
            children, expanded, ..
527
        } = &mut self.nodes[p].kind
528
        else {
529
            unreachable!("parents are containers");
530
        };
531
        let child = children.remove(i);
532
        if *expanded == Some(child.node) {
533
            *expanded = None;
534
        }
535
        self.nodes[child.node].parent = None;
536
        child
537
    }
538
539
    fn detach(&mut self, n: NodeId) {
540
        match self.position(n) {
541
            Some((p, i)) => {
542
                self.unlink(p, i);
543
                self.normalize(p);
544
            }
545
            None => self.root = None,
546
        }
547
    }
548
549
    /// Repairs a container that lost a child.
550
    fn normalize(&mut self, c: NodeId) {
551
        if self.children(c).len() >= 2 {
552
            return;
553
        }
554
        let pos = self.position(c);
555
        let was_expanded = pos.is_some_and(|(p, _)| self.expanded(p) == Some(c));
556
        let Some(Node {
557
            kind: Kind::Container { children, .. },
558
            ..
559
        }) = self.nodes.remove(c)
560
        else {
561
            unreachable!("only containers are normalized");
562
        };
563
        let only = children.into_iter().next();
564
        match (only, pos) {
565
            (None, Some((p, i))) => {
566
                self.unlink_dead(p, i);
567
                self.normalize(p);
568
            }
569
            (None, None) => self.root = None,
570
            (Some(only), Some((p, i))) => {
571
                let weight = self.unlink_dead(p, i).weight;
572
                let leaf_like = self.axis(only.node) != self.axis(p);
573
                self.attach(p, i, only.node, weight);
574
                if was_expanded
575
                    && leaf_like
576
                    && let Kind::Container { expanded, .. } = &mut self.nodes[p].kind
577
                {
578
                    *expanded = Some(only.node);
579
                }
580
            }
581
            (Some(only), None) => {
582
                self.nodes[only.node].parent = None;
583
                self.root = Some(only.node);
584
            }
585
        }
586
    }
587
588
    /// Like `unlink`, for a child whose node is already gone.
589
    fn unlink_dead(&mut self, p: NodeId, i: usize) -> Child {
590
        let Kind::Container {
591
            children, expanded, ..
592
        } = &mut self.nodes[p].kind
593
        else {
594
            unreachable!("parents are containers");
595
        };
596
        let child = children.remove(i);
597
        if *expanded == Some(child.node) {
598
            *expanded = None;
599
        }
600
        child
601
    }
602
603
    fn new_leaf(&mut self, w: W) -> NodeId {
604
        let n = self.nodes.insert(Node {
605
            parent: None,
606
            kind: Kind::Leaf(w),
607
        });
608
        self.leaves.insert(w, n);
609
        n
610
    }
611
612
    fn new_container(&mut self, axis: Axis) -> NodeId {
613
        self.nodes.insert(Node {
614
            parent: None,
615
            kind: Kind::Container {
616
                axis,
617
                children: Vec::new(),
618
                expanded: None,
619
            },
620
        })
621
    }
622
623
    fn position(&self, n: NodeId) -> Option<(NodeId, usize)> {
624
        let p = self.nodes[n].parent?;
625
        let i = self.children(p).iter().position(|c| c.node == n)?;
626
        Some((p, i))
627
    }
628
629
    fn axis(&self, n: NodeId) -> Option<Axis> {
630
        match &self.nodes[n].kind {
631
            Kind::Container { axis, .. } => Some(*axis),
632
            Kind::Leaf(_) => None,
633
        }
634
    }
635
636
    fn expanded(&self, n: NodeId) -> Option<NodeId> {
637
        match &self.nodes[n].kind {
638
            Kind::Container { expanded, .. } => *expanded,
639
            Kind::Leaf(_) => None,
640
        }
641
    }
642
643
    fn children(&self, n: NodeId) -> &[Child] {
644
        match &self.nodes[n].kind {
645
            Kind::Container { children, .. } => children,
646
            Kind::Leaf(_) => &[],
647
        }
648
    }
649
650
    fn children_mut(&mut self, n: NodeId) -> &mut Vec<Child> {
651
        match &mut self.nodes[n].kind {
652
            Kind::Container { children, .. } => children,
653
            Kind::Leaf(_) => unreachable!("leaves have no children"),
654
        }
655
    }
656
657
    fn mean_weight(&self, p: NodeId) -> f64 {
658
        let children = self.children(p);
659
        if children.is_empty() {
660
            1.0
661
        } else {
662
            children.iter().map(|c| c.weight).sum::<f64>() / children.len() as f64
663
        }
664
    }
665
666
    fn visible(&self, n: NodeId, vis: &impl Fn(W) -> bool) -> bool {
667
        match &self.nodes[n].kind {
668
            Kind::Leaf(w) => vis(*w),
669
            Kind::Container { children, .. } => children.iter().any(|c| self.visible(c.node, vis)),
670
        }
671
    }
672
673
    #[cfg(test)]
674
    pub(crate) fn check(&self) {
675
        let reachable = match self.root {
676
            Some(r) => {
677
                assert!(self.nodes[r].parent.is_none());
678
                self.check_node(r)
679
            }
680
            None => 0,
681
        };
682
        assert_eq!(reachable, self.nodes.len(), "orphaned nodes");
683
        for (&w, &n) in &self.leaves {
684
            assert!(matches!(self.nodes[n].kind, Kind::Leaf(x) if x == w));
685
        }
686
    }
687
688
    #[cfg(test)]
689
    fn check_node(&self, n: NodeId) -> usize {
690
        match &self.nodes[n].kind {
691
            Kind::Leaf(_) => 1,
692
            Kind::Container {
693
                axis,
694
                children,
695
                expanded,
696
            } => {
697
                assert!(
698
                    children.len() >= 2,
699
                    "container with {} children",
700
                    children.len()
701
                );
702
                if let Some(e) = expanded {
703
                    assert!(children.iter().any(|c| c.node == *e));
704
                }
705
                let mut count = 1;
706
                for c in children {
707
                    assert!(c.weight > 0.0 && c.weight.is_finite());
708
                    assert_eq!(self.nodes[c.node].parent, Some(n));
709
                    assert_ne!(self.axis(c.node), Some(*axis), "axes must alternate");
710
                    count += self.check_node(c.node);
711
                }
712
                count
713
            }
714
        }
715
    }
716
}
717
718
fn span(r: Rect, axis: Axis) -> (f64, f64) {
719
    match axis {
720
        Axis::Horizontal => (r.x, r.w),
721
        Axis::Vertical => (r.y, r.h),
722
    }
723
}
724
725
fn with_span(r: Rect, axis: Axis, start: f64, len: f64) -> Rect {
726
    match axis {
727
        Axis::Horizontal => Rect::new(start, r.y, len, r.h),
728
        Axis::Vertical => Rect::new(r.x, start, r.w, len),
729
    }
730
}