Skip to content

Commit

Permalink
Make it work better for vertical splits
Browse files Browse the repository at this point in the history
  • Loading branch information
Nehliin committed Oct 17, 2021
1 parent 787739e commit ad4b753
Showing 1 changed file with 36 additions and 35 deletions.
71 changes: 36 additions & 35 deletions helix-view/src/tree.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{graphics::Rect, View, ViewId};
use log::warn;
use slotmap::HopSlotMap;

// the dimensions are recomputed on window resize/tree change.
Expand Down Expand Up @@ -393,48 +394,43 @@ impl Tree {
Traverse::new(self)
}

// Focuses on the right split if there exists one
// Returns true if focus was changed otherwise returns false
pub fn focus_right(&mut self) -> bool {
let mut iter = self.traverse();
//let mut iter = iter.skip_while(|&(key, _view)| key != self.focus);
//iter.next(); // take the focused value
let current_area = iter.find(|(key, _view)| *key == self.focus).map(|(_, view)| view.inner_area()).unwrap();
log::info!("current_area: {:#?}", current_area);
// Skip while + skip 1
for (key, area) in self.traverse().filter(|(key, _)| *key != self.focus) {
let next_area_start = area.inner_area().left();
log::info!("current_area_start: {}, next_area_start: {}", current_area.left(), next_area_start);
if current_area.left() < next_area_start {
self.focus = key;
return true;
// Focuses on a split to the right if there exists one
pub fn focus_right(&mut self) {
let mut right_view_iter = self.traverse().skip_while(|(id, _)| *id != self.focus); // skip all up to current
if let Some((_, current_view)) = right_view_iter.next() {
let current_area_start = current_view.inner_area().left();
for (id, view) in right_view_iter {
let next_area_start = view.inner_area().left();
// Is it actually to the right of the current view
// and not stacked vertically?
if current_area_start < next_area_start {
self.focus = id;
break;
}
}
} else {
// should never happen
warn!("Current view not found when traversing");
}
false
}

// Focuses on the left split if there exists one
pub fn focus_left(&mut self) {
let mut prev_key = None;
for (key, _view) in self.traverse() {
if key == self.focus {
if let Some(prev_key) = prev_key {
self.focus = prev_key;
let views = self.traverse().collect::<Vec<(ViewId, &View)>>();
let mut left_view_iter = views.iter().rev().skip_while(|(id, _)| *id != self.focus);
if let Some((_, current_view)) = left_view_iter.next() {
let current_area_start = current_view.inner_area().left();
for (id, view) in left_view_iter {
let prev_area_start = view.inner_area().left();
if prev_area_start < current_area_start {
self.focus = *id;
break;
}
break;
}
prev_key = Some(key);
} else {
// should never happen
warn!("Current view not found when traversing");
}
/* let iter = self.traverse();
let mut iter = iter.skip_while(|&(key, _view)| key != self.focus);
iter.next(); // take the focused value
for (key, area) in iter {
let next_area_start = area.inner_area().left();
let current_area_start = self.area().left();
if current_area_start < next_area_start {
self.focus = key;
}
}*/
}

pub fn focus_next(&mut self) {
Expand Down Expand Up @@ -464,8 +460,13 @@ impl Tree {
// if found = container -> found = first child
// }

// Attempt to focus on the split to the right of the current one
if !self.focus_right() {
let mut views = self
.traverse()
.skip_while(|&(id, _view)| id != self.focus)
.skip(1); // Skip focused value
if let Some((id, _)) = views.next() {
self.focus = id;
} else {
// extremely crude, take the first item again
let (key, _) = self.traverse().next().unwrap();
self.focus = key;
Expand Down

0 comments on commit ad4b753

Please sign in to comment.