atagen

monolith

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