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
|
use crate::{
pane_grid::{Pane, Split},
Rectangle, Size,
};
use std::collections::HashMap;
#[derive(Debug, Clone, Hash)]
pub enum Node {
Split {
id: usize,
kind: Split,
ratio: u32,
a: Box<Node>,
b: Box<Node>,
},
Pane(Pane),
}
impl Node {
pub fn find(&mut self, pane: &Pane) -> Option<&mut Node> {
match self {
Node::Split { a, b, .. } => {
a.find(pane).or_else(move || b.find(pane))
}
Node::Pane(p) => {
if p == pane {
Some(self)
} else {
None
}
}
}
}
pub fn split(&mut self, id: usize, kind: Split, new_pane: Pane) {
*self = Node::Split {
id,
kind,
ratio: 500_000,
a: Box::new(self.clone()),
b: Box::new(Node::Pane(new_pane)),
};
}
pub fn update(&mut self, f: &impl Fn(&mut Node)) {
match self {
Node::Split { a, b, .. } => {
a.update(f);
b.update(f);
}
_ => {}
}
f(self);
}
pub fn remove(&mut self, pane: &Pane) -> Option<Pane> {
match self {
Node::Split { a, b, .. } => {
if a.pane() == Some(*pane) {
*self = *b.clone();
Some(self.first_pane())
} else if b.pane() == Some(*pane) {
*self = *a.clone();
Some(self.first_pane())
} else {
a.remove(pane).or_else(|| b.remove(pane))
}
}
Node::Pane(_) => None,
}
}
pub fn regions(
&self,
spacing: f32,
size: Size,
) -> HashMap<Pane, Rectangle> {
let mut regions = HashMap::new();
self.compute_regions(
spacing / 2.0,
&Rectangle {
x: 0.0,
y: 0.0,
width: size.width,
height: size.height,
},
&mut regions,
);
regions
}
pub fn pane(&self) -> Option<Pane> {
match self {
Node::Split { .. } => None,
Node::Pane(pane) => Some(*pane),
}
}
pub fn first_pane(&self) -> Pane {
match self {
Node::Split { a, .. } => a.first_pane(),
Node::Pane(pane) => *pane,
}
}
fn compute_regions(
&self,
halved_spacing: f32,
current: &Rectangle,
regions: &mut HashMap<Pane, Rectangle>,
) {
match self {
Node::Split {
kind, ratio, a, b, ..
} => {
let ratio = *ratio as f32 / 1_000_000.0;
let (region_a, region_b) =
kind.apply(current, ratio, halved_spacing);
a.compute_regions(halved_spacing, ®ion_a, regions);
b.compute_regions(halved_spacing, ®ion_b, regions);
}
Node::Pane(pane) => {
let _ = regions.insert(*pane, *current);
}
}
}
}
|