1use crate::core::{Point, Size};
5use crate::pane_grid::{
6 Axis, Configuration, Direction, Edge, Node, Pane, Region, Split, Target,
7};
8
9use std::borrow::Cow;
10use std::collections::BTreeMap;
11
12#[derive(Debug, Clone)]
25pub struct State<T> {
26 pub panes: BTreeMap<Pane, T>,
30
31 pub internal: Internal,
35}
36
37impl<T> State<T> {
38 pub fn new(first_pane_state: T) -> (Self, Pane) {
43 (
44 Self::with_configuration(Configuration::Pane(first_pane_state)),
45 Pane(0),
46 )
47 }
48
49 pub fn with_configuration(config: impl Into<Configuration<T>>) -> Self {
51 let mut panes = BTreeMap::default();
52
53 let internal =
54 Internal::from_configuration(&mut panes, config.into(), 0);
55
56 State { panes, internal }
57 }
58
59 pub fn len(&self) -> usize {
61 self.panes.len()
62 }
63
64 pub fn is_empty(&self) -> bool {
66 self.len() == 0
67 }
68
69 pub fn get(&self, pane: Pane) -> Option<&T> {
71 self.panes.get(&pane)
72 }
73
74 pub fn get_mut(&mut self, pane: Pane) -> Option<&mut T> {
77 self.panes.get_mut(&pane)
78 }
79
80 pub fn iter(&self) -> impl Iterator<Item = (&Pane, &T)> {
83 self.panes.iter()
84 }
85
86 pub fn iter_mut(&mut self) -> impl Iterator<Item = (&Pane, &mut T)> {
89 self.panes.iter_mut()
90 }
91
92 pub fn layout(&self) -> &Node {
94 &self.internal.layout
95 }
96
97 pub fn adjacent(&self, pane: Pane, direction: Direction) -> Option<Pane> {
100 let regions = self.internal.layout.pane_regions(
101 0.0,
102 0.0,
103 Size::new(4096.0, 4096.0),
104 );
105
106 let current_region = regions.get(&pane)?;
107
108 let target = match direction {
109 Direction::Left => {
110 Point::new(current_region.x - 1.0, current_region.y + 1.0)
111 }
112 Direction::Right => Point::new(
113 current_region.x + current_region.width + 1.0,
114 current_region.y + 1.0,
115 ),
116 Direction::Up => {
117 Point::new(current_region.x + 1.0, current_region.y - 1.0)
118 }
119 Direction::Down => Point::new(
120 current_region.x + 1.0,
121 current_region.y + current_region.height + 1.0,
122 ),
123 };
124
125 let mut colliding_regions =
126 regions.iter().filter(|(_, region)| region.contains(target));
127
128 let (pane, _) = colliding_regions.next()?;
129
130 Some(*pane)
131 }
132
133 pub fn split(
136 &mut self,
137 axis: Axis,
138 pane: Pane,
139 state: T,
140 ) -> Option<(Pane, Split)> {
141 self.split_node(axis, Some(pane), state, false)
142 }
143
144 pub fn split_with(&mut self, target: Pane, pane: Pane, region: Region) {
148 match region {
149 Region::Center => self.swap(pane, target),
150 Region::Edge(edge) => match edge {
151 Edge::Top => {
152 self.split_and_swap(Axis::Horizontal, target, pane, true);
153 }
154 Edge::Bottom => {
155 self.split_and_swap(Axis::Horizontal, target, pane, false);
156 }
157 Edge::Left => {
158 self.split_and_swap(Axis::Vertical, target, pane, true);
159 }
160 Edge::Right => {
161 self.split_and_swap(Axis::Vertical, target, pane, false);
162 }
163 },
164 }
165 }
166
167 pub fn drop(&mut self, pane: Pane, target: Target) {
169 match target {
170 Target::Edge(edge) => self.move_to_edge(pane, edge),
171 Target::Pane(target, region) => {
172 self.split_with(target, pane, region);
173 }
174 }
175 }
176
177 fn split_node(
178 &mut self,
179 axis: Axis,
180 pane: Option<Pane>,
181 state: T,
182 inverse: bool,
183 ) -> Option<(Pane, Split)> {
184 let node = if let Some(pane) = pane {
185 self.internal.layout.find(pane)?
186 } else {
187 &mut self.internal.layout
189 };
190
191 let new_pane = {
192 self.internal.last_id = self.internal.last_id.checked_add(1)?;
193
194 Pane(self.internal.last_id)
195 };
196
197 let new_split = {
198 self.internal.last_id = self.internal.last_id.checked_add(1)?;
199
200 Split(self.internal.last_id)
201 };
202
203 if inverse {
204 node.split_inverse(new_split, axis, new_pane);
205 } else {
206 node.split(new_split, axis, new_pane);
207 }
208
209 let _ = self.panes.insert(new_pane, state);
210 let _ = self.internal.maximized.take();
211
212 Some((new_pane, new_split))
213 }
214
215 fn split_and_swap(
216 &mut self,
217 axis: Axis,
218 target: Pane,
219 pane: Pane,
220 swap: bool,
221 ) {
222 if let Some((state, _)) = self.close(pane)
223 && let Some((new_pane, _)) = self.split(axis, target, state)
224 {
225 self.relabel(new_pane, pane);
227
228 if swap {
229 self.swap(target, pane);
230 }
231 }
232 }
233
234 pub fn move_to_edge(&mut self, pane: Pane, edge: Edge) {
238 match edge {
239 Edge::Top => {
240 self.split_major_node_and_swap(Axis::Horizontal, pane, true);
241 }
242 Edge::Bottom => {
243 self.split_major_node_and_swap(Axis::Horizontal, pane, false);
244 }
245 Edge::Left => {
246 self.split_major_node_and_swap(Axis::Vertical, pane, true);
247 }
248 Edge::Right => {
249 self.split_major_node_and_swap(Axis::Vertical, pane, false);
250 }
251 }
252 }
253
254 fn split_major_node_and_swap(
255 &mut self,
256 axis: Axis,
257 pane: Pane,
258 inverse: bool,
259 ) {
260 if let Some((state, _)) = self.close(pane)
261 && let Some((new_pane, _)) =
262 self.split_node(axis, None, state, inverse)
263 {
264 self.relabel(new_pane, pane);
266 }
267 }
268
269 fn relabel(&mut self, target: Pane, label: Pane) {
270 self.swap(target, label);
271
272 let _ = self
273 .panes
274 .remove(&target)
275 .and_then(|state| self.panes.insert(label, state));
276 }
277
278 pub fn swap(&mut self, a: Pane, b: Pane) {
286 self.internal.layout.update(&|node| match node {
287 Node::Split { .. } => {}
288 Node::Pane(pane) => {
289 if *pane == a {
290 *node = Node::Pane(b);
291 } else if *pane == b {
292 *node = Node::Pane(a);
293 }
294 }
295 });
296 }
297
298 pub fn resize(&mut self, split: Split, ratio: f32) {
309 let _ = self.internal.layout.resize(split, ratio);
310 }
311
312 pub fn close(&mut self, pane: Pane) -> Option<(T, Pane)> {
315 if self.internal.maximized == Some(pane) {
316 let _ = self.internal.maximized.take();
317 }
318
319 if let Some(sibling) = self.internal.layout.remove(pane) {
320 self.panes.remove(&pane).map(|state| (state, sibling))
321 } else {
322 None
323 }
324 }
325
326 pub fn maximize(&mut self, pane: Pane) {
331 self.internal.maximized = Some(pane);
332 }
333
334 pub fn restore(&mut self) {
339 let _ = self.internal.maximized.take();
340 }
341
342 pub fn maximized(&self) -> Option<Pane> {
346 self.internal.maximized
347 }
348}
349
350#[derive(Debug, Clone)]
354pub struct Internal {
355 layout: Node,
356 last_id: usize,
357 maximized: Option<Pane>,
358}
359
360impl Internal {
361 pub fn from_configuration<T>(
366 panes: &mut BTreeMap<Pane, T>,
367 content: Configuration<T>,
368 next_id: usize,
369 ) -> Self {
370 let (layout, last_id) = match content {
371 Configuration::Split { axis, ratio, a, b } => {
372 let Internal {
373 layout: a,
374 last_id: next_id,
375 ..
376 } = Self::from_configuration(panes, *a, next_id);
377
378 let Internal {
379 layout: b,
380 last_id: next_id,
381 ..
382 } = Self::from_configuration(panes, *b, next_id);
383
384 (
385 Node::Split {
386 id: Split(next_id),
387 axis,
388 ratio,
389 a: Box::new(a),
390 b: Box::new(b),
391 },
392 next_id + 1,
393 )
394 }
395 Configuration::Pane(state) => {
396 let id = Pane(next_id);
397 let _ = panes.insert(id, state);
398
399 (Node::Pane(id), next_id + 1)
400 }
401 };
402
403 Self {
404 layout,
405 last_id,
406 maximized: None,
407 }
408 }
409
410 pub(super) fn layout(&self) -> Cow<'_, Node> {
411 match self.maximized {
412 Some(pane) => Cow::Owned(Node::Pane(pane)),
413 None => Cow::Borrowed(&self.layout),
414 }
415 }
416
417 pub(super) fn maximized(&self) -> Option<Pane> {
418 self.maximized
419 }
420}
421
422#[derive(Debug, Clone, Copy, PartialEq, Default)]
426pub enum Action {
427 #[default]
431 Idle,
432 Dragging {
436 pane: Pane,
438 origin: Point,
440 },
441 Resizing {
445 split: Split,
447 axis: Axis,
449 },
450}
451
452impl Action {
453 pub fn picked_pane(&self) -> Option<(Pane, Point)> {
455 match *self {
456 Action::Dragging { pane, origin, .. } => Some((pane, origin)),
457 _ => None,
458 }
459 }
460
461 pub fn picked_split(&self) -> Option<(Split, Axis)> {
463 match *self {
464 Action::Resizing { split, axis, .. } => Some((split, axis)),
465 _ => None,
466 }
467 }
468}