| 1 |
use crate::{graphics::Rect, View, ViewId}; |
| 2 |
use slotmap::SlotMap; |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#[derive(Debug)] |
| 7 |
pub struct Tree { |
| 8 |
root: ViewId, |
| 9 |
|
| 10 |
pub focus: ViewId, |
| 11 |
|
| 12 |
area: Rect, |
| 13 |
|
| 14 |
nodes: SlotMap<ViewId, Node>, |
| 15 |
|
| 16 |
|
| 17 |
stack: Vec<(ViewId, Rect)>, |
| 18 |
} |
| 19 |
|
| 20 |
#[derive(Debug)] |
| 21 |
pub struct Node { |
| 22 |
parent: ViewId, |
| 23 |
content: Content, |
| 24 |
} |
| 25 |
|
| 26 |
#[derive(Debug)] |
| 27 |
pub enum Content { |
| 28 |
View(Box<View>), |
| 29 |
Container(Box<Container>), |
| 30 |
} |
| 31 |
|
| 32 |
impl Node { |
| 33 |
pub fn container(layout: Layout) -> Self { |
| 34 |
Self { |
| 35 |
parent: ViewId::default(), |
| 36 |
content: Content::Container(Box::new(Container::new(layout))), |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
pub fn view(view: View) -> Self { |
| 41 |
Self { |
| 42 |
parent: ViewId::default(), |
| 43 |
content: Content::View(Box::new(view)), |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 49 |
pub enum Layout { |
| 50 |
Horizontal, |
| 51 |
Vertical, |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
#[derive(Debug, Clone, Copy)] |
| 56 |
pub enum Direction { |
| 57 |
Up, |
| 58 |
Down, |
| 59 |
Left, |
| 60 |
Right, |
| 61 |
} |
| 62 |
|
| 63 |
#[derive(Debug)] |
| 64 |
pub struct Container { |
| 65 |
layout: Layout, |
| 66 |
children: Vec<ViewId>, |
| 67 |
area: Rect, |
| 68 |
} |
| 69 |
|
| 70 |
impl Container { |
| 71 |
pub fn new(layout: Layout) -> Self { |
| 72 |
Self { |
| 73 |
layout, |
| 74 |
children: Vec::new(), |
| 75 |
area: Rect::default(), |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
impl Default for Container { |
| 81 |
fn default() -> Self { |
| 82 |
Self::new(Layout::Vertical) |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
impl Tree { |
| 87 |
pub fn new(area: Rect) -> Self { |
| 88 |
let root = Node::container(Layout::Vertical); |
| 89 |
|
| 90 |
let mut nodes = SlotMap::with_key(); |
| 91 |
let root = nodes.insert(root); |
| 92 |
|
| 93 |
|
| 94 |
nodes[root].parent = root; |
| 95 |
|
| 96 |
Self { |
| 97 |
root, |
| 98 |
focus: root, |
| 99 |
|
| 100 |
area, |
| 101 |
nodes, |
| 102 |
stack: Vec::new(), |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
pub fn insert(&mut self, view: View) -> ViewId { |
| 107 |
let focus = self.focus; |
| 108 |
let parent = self.nodes[focus].parent; |
| 109 |
let mut node = Node::view(view); |
| 110 |
node.parent = parent; |
| 111 |
let node = self.nodes.insert(node); |
| 112 |
self.get_mut(node).id = node; |
| 113 |
|
| 114 |
let container = match &mut self.nodes[parent] { |
| 115 |
Node { |
| 116 |
content: Content::Container(container), |
| 117 |
.. |
| 118 |
} => container, |
| 119 |
_ => unreachable!(), |
| 120 |
}; |
| 121 |
|
| 122 |
|
| 123 |
let pos = if container.children.is_empty() { |
| 124 |
0 |
| 125 |
} else { |
| 126 |
let pos = container |
| 127 |
.children |
| 128 |
.iter() |
| 129 |
.position(|&child| child == focus) |
| 130 |
.unwrap(); |
| 131 |
pos + 1 |
| 132 |
}; |
| 133 |
|
| 134 |
container.children.insert(pos, node); |
| 135 |
|
| 136 |
self.focus = node; |
| 137 |
|
| 138 |
|
| 139 |
self.recalculate(); |
| 140 |
|
| 141 |
node |
| 142 |
} |
| 143 |
|
| 144 |
pub fn split(&mut self, view: View, layout: Layout) -> ViewId { |
| 145 |
let focus = self.focus; |
| 146 |
let parent = self.nodes[focus].parent; |
| 147 |
|
| 148 |
let node = Node::view(view); |
| 149 |
let node = self.nodes.insert(node); |
| 150 |
self.get_mut(node).id = node; |
| 151 |
|
| 152 |
let container = match &mut self.nodes[parent] { |
| 153 |
Node { |
| 154 |
content: Content::Container(container), |
| 155 |
.. |
| 156 |
} => container, |
| 157 |
_ => unreachable!(), |
| 158 |
}; |
| 159 |
if container.layout == layout { |
| 160 |
|
| 161 |
let pos = if container.children.is_empty() { |
| 162 |
0 |
| 163 |
} else { |
| 164 |
let pos = container |
| 165 |
.children |
| 166 |
.iter() |
| 167 |
.position(|&child| child == focus) |
| 168 |
.unwrap(); |
| 169 |
pos + 1 |
| 170 |
}; |
| 171 |
container.children.insert(pos, node); |
| 172 |
self.nodes[node].parent = parent; |
| 173 |
} else { |
| 174 |
let mut split = Node::container(layout); |
| 175 |
split.parent = parent; |
| 176 |
let split = self.nodes.insert(split); |
| 177 |
|
| 178 |
let container = match &mut self.nodes[split] { |
| 179 |
Node { |
| 180 |
content: Content::Container(container), |
| 181 |
.. |
| 182 |
} => container, |
| 183 |
_ => unreachable!(), |
| 184 |
}; |
| 185 |
container.children.push(focus); |
| 186 |
container.children.push(node); |
| 187 |
self.nodes[focus].parent = split; |
| 188 |
self.nodes[node].parent = split; |
| 189 |
|
| 190 |
let container = match &mut self.nodes[parent] { |
| 191 |
Node { |
| 192 |
content: Content::Container(container), |
| 193 |
.. |
| 194 |
} => container, |
| 195 |
_ => unreachable!(), |
| 196 |
}; |
| 197 |
|
| 198 |
let pos = container |
| 199 |
.children |
| 200 |
.iter() |
| 201 |
.position(|&child| child == focus) |
| 202 |
.unwrap(); |
| 203 |
|
| 204 |
|
| 205 |
container.children[pos] = split; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
self.focus = node; |
| 210 |
|
| 211 |
|
| 212 |
self.recalculate(); |
| 213 |
|
| 214 |
node |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
fn container_mut(&mut self, index: ViewId) -> &mut Container { |
| 221 |
match &mut self.nodes[index] { |
| 222 |
Node { |
| 223 |
content: Content::Container(container), |
| 224 |
.. |
| 225 |
} => container, |
| 226 |
_ => unreachable!(), |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
fn remove_or_replace(&mut self, child: ViewId, replacement: Option<ViewId>) { |
| 231 |
let parent = self.nodes[child].parent; |
| 232 |
|
| 233 |
self.nodes.remove(child); |
| 234 |
|
| 235 |
let container = self.container_mut(parent); |
| 236 |
let pos = container |
| 237 |
.children |
| 238 |
.iter() |
| 239 |
.position(|&item| item == child) |
| 240 |
.unwrap(); |
| 241 |
|
| 242 |
if let Some(new) = replacement { |
| 243 |
container.children[pos] = new; |
| 244 |
self.nodes[new].parent = parent; |
| 245 |
} else { |
| 246 |
container.children.remove(pos); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
pub fn remove(&mut self, index: ViewId) { |
| 251 |
if self.focus == index { |
| 252 |
|
| 253 |
self.focus = self.prev(); |
| 254 |
} |
| 255 |
|
| 256 |
let parent = self.nodes[index].parent; |
| 257 |
let parent_is_root = parent == self.root; |
| 258 |
|
| 259 |
self.remove_or_replace(index, None); |
| 260 |
|
| 261 |
let parent_container = self.container_mut(parent); |
| 262 |
if parent_container.children.len() == 1 && !parent_is_root { |
| 263 |
|
| 264 |
|
| 265 |
let sibling = parent_container.children.pop().unwrap(); |
| 266 |
self.remove_or_replace(parent, Some(sibling)); |
| 267 |
} |
| 268 |
|
| 269 |
self.recalculate() |
| 270 |
} |
| 271 |
|
| 272 |
pub fn views(&self) -> impl Iterator<Item = (&View, bool)> { |
| 273 |
let focus = self.focus; |
| 274 |
self.nodes.iter().filter_map(move |(key, node)| match node { |
| 275 |
Node { |
| 276 |
content: Content::View(view), |
| 277 |
.. |
| 278 |
} => Some((view.as_ref(), focus == key)), |
| 279 |
_ => None, |
| 280 |
}) |
| 281 |
} |
| 282 |
|
| 283 |
pub fn views_mut(&mut self) -> impl Iterator<Item = (&mut View, bool)> { |
| 284 |
let focus = self.focus; |
| 285 |
self.nodes |
| 286 |
.iter_mut() |
| 287 |
.filter_map(move |(key, node)| match node { |
| 288 |
Node { |
| 289 |
content: Content::View(view), |
| 290 |
.. |
| 291 |
} => Some((view.as_mut(), focus == key)), |
| 292 |
_ => None, |
| 293 |
}) |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
pub fn get(&self, index: ViewId) -> &View { |
| 301 |
self.try_get(index).unwrap() |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
pub fn try_get(&self, index: ViewId) -> Option<&View> { |
| 308 |
match self.nodes.get(index) { |
| 309 |
Some(Node { |
| 310 |
content: Content::View(view), |
| 311 |
.. |
| 312 |
}) => Some(view), |
| 313 |
_ => None, |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
pub fn get_mut(&mut self, index: ViewId) -> &mut View { |
| 322 |
match &mut self.nodes[index] { |
| 323 |
Node { |
| 324 |
content: Content::View(view), |
| 325 |
.. |
| 326 |
} => view, |
| 327 |
_ => unreachable!(), |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
pub fn contains(&self, index: ViewId) -> bool { |
| 333 |
self.nodes.contains_key(index) |
| 334 |
} |
| 335 |
|
| 336 |
pub fn is_empty(&self) -> bool { |
| 337 |
match &self.nodes[self.root] { |
| 338 |
Node { |
| 339 |
content: Content::Container(container), |
| 340 |
.. |
| 341 |
} => container.children.is_empty(), |
| 342 |
_ => unreachable!(), |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
pub fn resize(&mut self, area: Rect) -> bool { |
| 347 |
if self.area != area { |
| 348 |
self.area = area; |
| 349 |
self.recalculate(); |
| 350 |
return true; |
| 351 |
} |
| 352 |
false |
| 353 |
} |
| 354 |
|
| 355 |
pub fn recalculate(&mut self) { |
| 356 |
if self.is_empty() { |
| 357 |
|
| 358 |
self.focus = self.root; |
| 359 |
|
| 360 |
return; |
| 361 |
} |
| 362 |
|
| 363 |
self.stack.push((self.root, self.area)); |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
while let Some((key, area)) = self.stack.pop() { |
| 371 |
let node = &mut self.nodes[key]; |
| 372 |
|
| 373 |
match &mut node.content { |
| 374 |
Content::View(view) => { |
| 375 |
|
| 376 |
view.area = area; |
| 377 |
} |
| 378 |
Content::Container(container) => { |
| 379 |
|
| 380 |
container.area = area; |
| 381 |
|
| 382 |
match container.layout { |
| 383 |
Layout::Horizontal => { |
| 384 |
let len = container.children.len(); |
| 385 |
|
| 386 |
let height = area.height / len as u16; |
| 387 |
|
| 388 |
let mut child_y = area.y; |
| 389 |
|
| 390 |
for (i, child) in container.children.iter().enumerate() { |
| 391 |
let mut area = Rect::new( |
| 392 |
container.area.x, |
| 393 |
child_y, |
| 394 |
container.area.width, |
| 395 |
height, |
| 396 |
); |
| 397 |
child_y += height; |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
if i == len - 1 { |
| 402 |
area.height = container.area.y + container.area.height - area.y; |
| 403 |
} |
| 404 |
|
| 405 |
self.stack.push((*child, area)); |
| 406 |
} |
| 407 |
} |
| 408 |
Layout::Vertical => { |
| 409 |
let len = container.children.len(); |
| 410 |
let len_u16 = len as u16; |
| 411 |
|
| 412 |
let inner_gap = 1u16; |
| 413 |
let total_gap = inner_gap * len_u16.saturating_sub(2); |
| 414 |
|
| 415 |
let used_area = area.width.saturating_sub(total_gap); |
| 416 |
let width = used_area / len_u16; |
| 417 |
|
| 418 |
let mut child_x = area.x; |
| 419 |
|
| 420 |
for (i, child) in container.children.iter().enumerate() { |
| 421 |
let mut area = Rect::new( |
| 422 |
child_x, |
| 423 |
container.area.y, |
| 424 |
width, |
| 425 |
container.area.height, |
| 426 |
); |
| 427 |
child_x += width + inner_gap; |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
if i == len - 1 { |
| 432 |
area.width = container.area.x + container.area.width - area.x; |
| 433 |
} |
| 434 |
|
| 435 |
self.stack.push((*child, area)); |
| 436 |
} |
| 437 |
} |
| 438 |
} |
| 439 |
} |
| 440 |
} |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
pub fn traverse(&self) -> Traverse<'_> { |
| 445 |
Traverse::new(self) |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
pub fn find_split_in_direction(&self, id: ViewId, direction: Direction) -> Option<ViewId> { |
| 450 |
let parent = self.nodes[id].parent; |
| 451 |
|
| 452 |
if parent == id { |
| 453 |
return None; |
| 454 |
} |
| 455 |
|
| 456 |
let parent_container = match &self.nodes[parent].content { |
| 457 |
Content::Container(container) => container, |
| 458 |
Content::View(_) => unreachable!(), |
| 459 |
}; |
| 460 |
|
| 461 |
match (direction, parent_container.layout) { |
| 462 |
(Direction::Up, Layout::Vertical) |
| 463 |
| (Direction::Left, Layout::Horizontal) |
| 464 |
| (Direction::Right, Layout::Horizontal) |
| 465 |
| (Direction::Down, Layout::Vertical) => { |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
self.find_split_in_direction(parent, direction) |
| 470 |
} |
| 471 |
(Direction::Up, Layout::Horizontal) |
| 472 |
| (Direction::Down, Layout::Horizontal) |
| 473 |
| (Direction::Left, Layout::Vertical) |
| 474 |
| (Direction::Right, Layout::Vertical) => { |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
match self.find_child(id, &parent_container.children, direction) { |
| 479 |
|
| 480 |
Some(id) => Some(id), |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
None => self.find_split_in_direction(parent, direction), |
| 492 |
} |
| 493 |
} |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
fn find_child(&self, id: ViewId, children: &[ViewId], direction: Direction) -> Option<ViewId> { |
| 498 |
let mut child_id = match direction { |
| 499 |
|
| 500 |
|
| 501 |
Direction::Up | Direction::Left => children |
| 502 |
.iter() |
| 503 |
.rev() |
| 504 |
.skip_while(|i| **i != id) |
| 505 |
.copied() |
| 506 |
.nth(1)?, |
| 507 |
|
| 508 |
Direction::Down | Direction::Right => { |
| 509 |
children.iter().skip_while(|i| **i != id).copied().nth(1)? |
| 510 |
} |
| 511 |
}; |
| 512 |
let (current_x, current_y) = match &self.nodes[self.focus].content { |
| 513 |
Content::View(current_view) => (current_view.area.left(), current_view.area.top()), |
| 514 |
Content::Container(_) => unreachable!(), |
| 515 |
}; |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
while let Content::Container(container) = &self.nodes[child_id].content { |
| 520 |
match (direction, container.layout) { |
| 521 |
(_, Layout::Vertical) => { |
| 522 |
|
| 523 |
|
| 524 |
child_id = *container.children.iter().min_by_key(|id| { |
| 525 |
let x = match &self.nodes[**id].content { |
| 526 |
Content::View(view) => view.area.left(), |
| 527 |
Content::Container(container) => container.area.left(), |
| 528 |
}; |
| 529 |
(current_x as i16 - x as i16).abs() |
| 530 |
})?; |
| 531 |
} |
| 532 |
(_, Layout::Horizontal) => { |
| 533 |
|
| 534 |
|
| 535 |
child_id = *container.children.iter().min_by_key(|id| { |
| 536 |
let y = match &self.nodes[**id].content { |
| 537 |
Content::View(view) => view.area.top(), |
| 538 |
Content::Container(container) => container.area.top(), |
| 539 |
}; |
| 540 |
(current_y as i16 - y as i16).abs() |
| 541 |
})?; |
| 542 |
} |
| 543 |
} |
| 544 |
} |
| 545 |
Some(child_id) |
| 546 |
} |
| 547 |
|
| 548 |
pub fn prev(&self) -> ViewId { |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
let mut views = self |
| 555 |
.traverse() |
| 556 |
.rev() |
| 557 |
.skip_while(|&(id, _view)| id != self.focus) |
| 558 |
.skip(1); |
| 559 |
if let Some((id, _)) = views.next() { |
| 560 |
id |
| 561 |
} else { |
| 562 |
|
| 563 |
let (key, _) = self.traverse().next_back().unwrap(); |
| 564 |
key |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
pub fn next(&self) -> ViewId { |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
let mut views = self |
| 575 |
.traverse() |
| 576 |
.skip_while(|&(id, _view)| id != self.focus) |
| 577 |
.skip(1); |
| 578 |
if let Some((id, _)) = views.next() { |
| 579 |
id |
| 580 |
} else { |
| 581 |
|
| 582 |
let (key, _) = self.traverse().next().unwrap(); |
| 583 |
key |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
pub fn transpose(&mut self) { |
| 588 |
let focus = self.focus; |
| 589 |
let parent = self.nodes[focus].parent; |
| 590 |
if let Content::Container(container) = &mut self.nodes[parent].content { |
| 591 |
container.layout = match container.layout { |
| 592 |
Layout::Vertical => Layout::Horizontal, |
| 593 |
Layout::Horizontal => Layout::Vertical, |
| 594 |
}; |
| 595 |
self.recalculate(); |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
pub fn swap_split_in_direction(&mut self, direction: Direction) -> Option<()> { |
| 600 |
let focus = self.focus; |
| 601 |
let target = self.find_split_in_direction(focus, direction)?; |
| 602 |
let focus_parent = self.nodes[focus].parent; |
| 603 |
let target_parent = self.nodes[target].parent; |
| 604 |
|
| 605 |
if focus_parent == target_parent { |
| 606 |
let parent = focus_parent; |
| 607 |
let [parent, focus, target] = self.nodes.get_disjoint_mut([parent, focus, target])?; |
| 608 |
match (&mut parent.content, &mut focus.content, &mut target.content) { |
| 609 |
( |
| 610 |
Content::Container(parent), |
| 611 |
Content::View(focus_view), |
| 612 |
Content::View(target_view), |
| 613 |
) => { |
| 614 |
let focus_pos = parent.children.iter().position(|id| focus_view.id == *id)?; |
| 615 |
let target_pos = parent |
| 616 |
.children |
| 617 |
.iter() |
| 618 |
.position(|id| target_view.id == *id)?; |
| 619 |
|
| 620 |
parent.children[focus_pos] = target_view.id; |
| 621 |
parent.children[target_pos] = focus_view.id; |
| 622 |
|
| 623 |
std::mem::swap(&mut focus_view.area, &mut target_view.area); |
| 624 |
|
| 625 |
Some(()) |
| 626 |
} |
| 627 |
_ => unreachable!(), |
| 628 |
} |
| 629 |
} else { |
| 630 |
let [focus_parent, target_parent, focus, target] = |
| 631 |
self.nodes |
| 632 |
.get_disjoint_mut([focus_parent, target_parent, focus, target])?; |
| 633 |
match ( |
| 634 |
&mut focus_parent.content, |
| 635 |
&mut target_parent.content, |
| 636 |
&mut focus.content, |
| 637 |
&mut target.content, |
| 638 |
) { |
| 639 |
( |
| 640 |
Content::Container(focus_parent), |
| 641 |
Content::Container(target_parent), |
| 642 |
Content::View(focus_view), |
| 643 |
Content::View(target_view), |
| 644 |
) => { |
| 645 |
let focus_pos = focus_parent |
| 646 |
.children |
| 647 |
.iter() |
| 648 |
.position(|id| focus_view.id == *id)?; |
| 649 |
let target_pos = target_parent |
| 650 |
.children |
| 651 |
.iter() |
| 652 |
.position(|id| target_view.id == *id)?; |
| 653 |
|
| 654 |
std::mem::swap( |
| 655 |
&mut focus_parent.children[focus_pos], |
| 656 |
&mut target_parent.children[target_pos], |
| 657 |
); |
| 658 |
std::mem::swap(&mut focus.parent, &mut target.parent); |
| 659 |
|
| 660 |
std::mem::swap(&mut focus_view.area, &mut target_view.area); |
| 661 |
|
| 662 |
Some(()) |
| 663 |
} |
| 664 |
_ => unreachable!(), |
| 665 |
} |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
pub fn area(&self) -> Rect { |
| 670 |
self.area |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
#[derive(Debug)] |
| 675 |
pub struct Traverse<'a> { |
| 676 |
tree: &'a Tree, |
| 677 |
stack: Vec<ViewId>, |
| 678 |
} |
| 679 |
|
| 680 |
impl<'a> Traverse<'a> { |
| 681 |
fn new(tree: &'a Tree) -> Self { |
| 682 |
Self { |
| 683 |
tree, |
| 684 |
stack: vec![tree.root], |
| 685 |
} |
| 686 |
} |
| 687 |
} |
| 688 |
|
| 689 |
impl<'a> Iterator for Traverse<'a> { |
| 690 |
type Item = (ViewId, &'a View); |
| 691 |
|
| 692 |
fn next(&mut self) -> Option<Self::Item> { |
| 693 |
loop { |
| 694 |
let key = self.stack.pop()?; |
| 695 |
|
| 696 |
let node = &self.tree.nodes[key]; |
| 697 |
|
| 698 |
match &node.content { |
| 699 |
Content::View(view) => return Some((key, view)), |
| 700 |
Content::Container(container) => { |
| 701 |
self.stack.extend(container.children.iter().rev()); |
| 702 |
} |
| 703 |
} |
| 704 |
} |
| 705 |
} |
| 706 |
} |
| 707 |
|
| 708 |
impl DoubleEndedIterator for Traverse<'_> { |
| 709 |
fn next_back(&mut self) -> Option<Self::Item> { |
| 710 |
loop { |
| 711 |
let key = self.stack.pop()?; |
| 712 |
|
| 713 |
let node = &self.tree.nodes[key]; |
| 714 |
|
| 715 |
match &node.content { |
| 716 |
Content::View(view) => return Some((key, view)), |
| 717 |
Content::Container(container) => { |
| 718 |
self.stack.extend(container.children.iter()); |
| 719 |
} |
| 720 |
} |
| 721 |
} |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
#[cfg(test)] |
| 726 |
mod test { |
| 727 |
use super::*; |
| 728 |
use crate::editor::GutterConfig; |
| 729 |
use crate::DocumentId; |
| 730 |
|
| 731 |
#[test] |
| 732 |
fn find_split_in_direction() { |
| 733 |
let mut tree = Tree::new(Rect { |
| 734 |
x: 0, |
| 735 |
y: 0, |
| 736 |
width: 180, |
| 737 |
height: 80, |
| 738 |
}); |
| 739 |
let mut view = View::new(DocumentId::default(), GutterConfig::default()); |
| 740 |
view.area = Rect::new(0, 0, 180, 80); |
| 741 |
tree.insert(view); |
| 742 |
|
| 743 |
let l0 = tree.focus; |
| 744 |
let view = View::new(DocumentId::default(), GutterConfig::default()); |
| 745 |
tree.split(view, Layout::Vertical); |
| 746 |
let r0 = tree.focus; |
| 747 |
|
| 748 |
tree.focus = l0; |
| 749 |
let view = View::new(DocumentId::default(), GutterConfig::default()); |
| 750 |
tree.split(view, Layout::Horizontal); |
| 751 |
let l1 = tree.focus; |
| 752 |
|
| 753 |
tree.focus = l0; |
| 754 |
let view = View::new(DocumentId::default(), GutterConfig::default()); |
| 755 |
tree.split(view, Layout::Vertical); |
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
let l2 = tree.focus; |
| 761 |
assert_eq!(Some(l0), tree.find_split_in_direction(l2, Direction::Left)); |
| 762 |
assert_eq!(Some(l1), tree.find_split_in_direction(l2, Direction::Down)); |
| 763 |
assert_eq!(Some(r0), tree.find_split_in_direction(l2, Direction::Right)); |
| 764 |
assert_eq!(None, tree.find_split_in_direction(l2, Direction::Up)); |
| 765 |
|
| 766 |
tree.focus = l1; |
| 767 |
assert_eq!(None, tree.find_split_in_direction(l1, Direction::Left)); |
| 768 |
assert_eq!(None, tree.find_split_in_direction(l1, Direction::Down)); |
| 769 |
assert_eq!(Some(r0), tree.find_split_in_direction(l1, Direction::Right)); |
| 770 |
assert_eq!(Some(l0), tree.find_split_in_direction(l1, Direction::Up)); |
| 771 |
|
| 772 |
tree.focus = l0; |
| 773 |
assert_eq!(None, tree.find_split_in_direction(l0, Direction::Left)); |
| 774 |
assert_eq!(Some(l1), tree.find_split_in_direction(l0, Direction::Down)); |
| 775 |
assert_eq!(Some(l2), tree.find_split_in_direction(l0, Direction::Right)); |
| 776 |
assert_eq!(None, tree.find_split_in_direction(l0, Direction::Up)); |
| 777 |
|
| 778 |
tree.focus = r0; |
| 779 |
assert_eq!(Some(l2), tree.find_split_in_direction(r0, Direction::Left)); |
| 780 |
assert_eq!(None, tree.find_split_in_direction(r0, Direction::Down)); |
| 781 |
assert_eq!(None, tree.find_split_in_direction(r0, Direction::Right)); |
| 782 |
assert_eq!(None, tree.find_split_in_direction(r0, Direction::Up)); |
| 783 |
} |
| 784 |
|
| 785 |
#[test] |
| 786 |
fn swap_split_in_direction() { |
| 787 |
let mut tree = Tree::new(Rect { |
| 788 |
x: 0, |
| 789 |
y: 0, |
| 790 |
width: 180, |
| 791 |
height: 80, |
| 792 |
}); |
| 793 |
|
| 794 |
let doc_l0 = DocumentId::default(); |
| 795 |
let mut view = View::new(doc_l0, GutterConfig::default()); |
| 796 |
view.area = Rect::new(0, 0, 180, 80); |
| 797 |
tree.insert(view); |
| 798 |
|
| 799 |
let l0 = tree.focus; |
| 800 |
|
| 801 |
let doc_r0 = DocumentId::default(); |
| 802 |
let view = View::new(doc_r0, GutterConfig::default()); |
| 803 |
tree.split(view, Layout::Vertical); |
| 804 |
let r0 = tree.focus; |
| 805 |
|
| 806 |
tree.focus = l0; |
| 807 |
|
| 808 |
let doc_l1 = DocumentId::default(); |
| 809 |
let view = View::new(doc_l1, GutterConfig::default()); |
| 810 |
tree.split(view, Layout::Horizontal); |
| 811 |
let l1 = tree.focus; |
| 812 |
|
| 813 |
tree.focus = l0; |
| 814 |
|
| 815 |
let doc_l2 = DocumentId::default(); |
| 816 |
let view = View::new(doc_l2, GutterConfig::default()); |
| 817 |
tree.split(view, Layout::Vertical); |
| 818 |
let l2 = tree.focus; |
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
fn doc_id(tree: &Tree, view_id: ViewId) -> Option<DocumentId> { |
| 829 |
if let Content::View(view) = &tree.nodes[view_id].content { |
| 830 |
Some(view.doc) |
| 831 |
} else { |
| 832 |
None |
| 833 |
} |
| 834 |
} |
| 835 |
|
| 836 |
tree.focus = l0; |
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
tree.swap_split_in_direction(Direction::Down); |
| 841 |
|
| 842 |
|
| 843 |
assert_eq!(tree.focus, l0); |
| 844 |
assert_eq!(doc_id(&tree, l0), Some(doc_l1)); |
| 845 |
assert_eq!(doc_id(&tree, l1), Some(doc_l0)); |
| 846 |
assert_eq!(doc_id(&tree, l2), Some(doc_l2)); |
| 847 |
assert_eq!(doc_id(&tree, r0), Some(doc_r0)); |
| 848 |
|
| 849 |
tree.swap_split_in_direction(Direction::Right); |
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
assert_eq!(tree.focus, l0); |
| 854 |
assert_eq!(doc_id(&tree, l0), Some(doc_l1)); |
| 855 |
assert_eq!(doc_id(&tree, l1), Some(doc_r0)); |
| 856 |
assert_eq!(doc_id(&tree, l2), Some(doc_l2)); |
| 857 |
assert_eq!(doc_id(&tree, r0), Some(doc_l0)); |
| 858 |
|
| 859 |
|
| 860 |
tree.swap_split_in_direction(Direction::Up); |
| 861 |
|
| 862 |
|
| 863 |
assert_eq!(tree.focus, l0); |
| 864 |
assert_eq!(doc_id(&tree, l0), Some(doc_l1)); |
| 865 |
assert_eq!(doc_id(&tree, l1), Some(doc_r0)); |
| 866 |
assert_eq!(doc_id(&tree, l2), Some(doc_l2)); |
| 867 |
assert_eq!(doc_id(&tree, r0), Some(doc_l0)); |
| 868 |
|
| 869 |
|
| 870 |
tree.swap_split_in_direction(Direction::Down); |
| 871 |
|
| 872 |
|
| 873 |
assert_eq!(tree.focus, l0); |
| 874 |
assert_eq!(doc_id(&tree, l0), Some(doc_l1)); |
| 875 |
assert_eq!(doc_id(&tree, l1), Some(doc_r0)); |
| 876 |
assert_eq!(doc_id(&tree, l2), Some(doc_l2)); |
| 877 |
assert_eq!(doc_id(&tree, r0), Some(doc_l0)); |
| 878 |
|
| 879 |
tree.focus = l2; |
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
tree.swap_split_in_direction(Direction::Down); |
| 884 |
|
| 885 |
|
| 886 |
assert_eq!(tree.focus, l2); |
| 887 |
assert_eq!(doc_id(&tree, l0), Some(doc_l1)); |
| 888 |
assert_eq!(doc_id(&tree, l1), Some(doc_l2)); |
| 889 |
assert_eq!(doc_id(&tree, l2), Some(doc_r0)); |
| 890 |
assert_eq!(doc_id(&tree, r0), Some(doc_l0)); |
| 891 |
|
| 892 |
tree.swap_split_in_direction(Direction::Up); |
| 893 |
|
| 894 |
|
| 895 |
assert_eq!(tree.focus, l2); |
| 896 |
assert_eq!(doc_id(&tree, l0), Some(doc_l2)); |
| 897 |
assert_eq!(doc_id(&tree, l1), Some(doc_l1)); |
| 898 |
assert_eq!(doc_id(&tree, l2), Some(doc_r0)); |
| 899 |
assert_eq!(doc_id(&tree, r0), Some(doc_l0)); |
| 900 |
} |
| 901 |
|
| 902 |
#[test] |
| 903 |
fn all_vertical_views_have_same_width() { |
| 904 |
let tree_area_width = 180; |
| 905 |
let mut tree = Tree::new(Rect { |
| 906 |
x: 0, |
| 907 |
y: 0, |
| 908 |
width: tree_area_width, |
| 909 |
height: 80, |
| 910 |
}); |
| 911 |
let mut view = View::new(DocumentId::default(), GutterConfig::default()); |
| 912 |
view.area = Rect::new(0, 0, 180, 80); |
| 913 |
tree.insert(view); |
| 914 |
|
| 915 |
let view = View::new(DocumentId::default(), GutterConfig::default()); |
| 916 |
tree.split(view, Layout::Vertical); |
| 917 |
|
| 918 |
let view = View::new(DocumentId::default(), GutterConfig::default()); |
| 919 |
tree.split(view, Layout::Horizontal); |
| 920 |
|
| 921 |
tree.remove(tree.focus); |
| 922 |
|
| 923 |
let view = View::new(DocumentId::default(), GutterConfig::default()); |
| 924 |
tree.split(view, Layout::Vertical); |
| 925 |
|
| 926 |
|
| 927 |
assert_eq!(3, tree.views().count()); |
| 928 |
assert_eq!( |
| 929 |
vec![ |
| 930 |
tree_area_width / 3 - 1, |
| 931 |
tree_area_width / 3 - 1, |
| 932 |
tree_area_width / 3 |
| 933 |
], |
| 934 |
tree.views() |
| 935 |
.map(|(view, _)| view.area.width) |
| 936 |
.collect::<Vec<_>>() |
| 937 |
); |
| 938 |
} |
| 939 |
|
| 940 |
#[test] |
| 941 |
fn vsplit_gap_rounding() { |
| 942 |
let (tree_area_width, tree_area_height) = (80, 24); |
| 943 |
let mut tree = Tree::new(Rect { |
| 944 |
x: 0, |
| 945 |
y: 0, |
| 946 |
width: tree_area_width, |
| 947 |
height: tree_area_height, |
| 948 |
}); |
| 949 |
let mut view = View::new(DocumentId::default(), GutterConfig::default()); |
| 950 |
view.area = Rect::new(0, 0, tree_area_width, tree_area_height); |
| 951 |
tree.insert(view); |
| 952 |
|
| 953 |
for _ in 0..9 { |
| 954 |
let view = View::new(DocumentId::default(), GutterConfig::default()); |
| 955 |
tree.split(view, Layout::Vertical); |
| 956 |
} |
| 957 |
|
| 958 |
assert_eq!(10, tree.views().count()); |
| 959 |
assert_eq!( |
| 960 |
std::iter::repeat_n(7, 9) |
| 961 |
.chain(Some(8)) |
| 962 |
.collect::<Vec<_>>(), |
| 963 |
tree.views() |
| 964 |
.map(|(view, _)| view.area.width) |
| 965 |
.collect::<Vec<_>>() |
| 966 |
); |
| 967 |
} |
| 968 |
} |