Skip to content

Commit

Permalink
When buffer closes, focus on parent buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
wes adams committed Nov 15, 2022
1 parent 70ebbd0 commit 54ef4a3
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions helix-view/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct Tree {
pub struct Node {
parent: ViewId,
content: Content,
spawned_by: ViewId,
}

#[derive(Debug)]
Expand All @@ -30,17 +31,19 @@ pub enum Content {
}

impl Node {
pub fn container(layout: Layout) -> Self {
pub fn container(layout: Layout, focus: ViewId) -> Self {
Self {
parent: ViewId::default(),
content: Content::Container(Box::new(Container::new(layout))),
spawned_by: focus,
}
}

pub fn view(view: View) -> Self {
pub fn view(view: View, focus: ViewId) -> Self {
Self {
parent: ViewId::default(),
content: Content::View(Box::new(view)),
spawned_by: focus,
}
}
}
Expand Down Expand Up @@ -85,10 +88,11 @@ impl Default for Container {

impl Tree {
pub fn new(area: Rect) -> Self {
let root = Node::container(Layout::Vertical);
let root = Node::container(Layout::Vertical, ViewId::default());

let mut nodes = HopSlotMap::with_key();
let root = nodes.insert(root);
//nodes[root].spawned_by = root;

// root is it's own parent
nodes[root].parent = root;
Expand All @@ -106,7 +110,7 @@ impl Tree {
pub fn insert(&mut self, view: View) -> ViewId {
let focus = self.focus;
let parent = self.nodes[focus].parent;
let mut node = Node::view(view);
let mut node = Node::view(view, focus);
node.parent = parent;
let node = self.nodes.insert(node);
self.get_mut(node).id = node;
Expand Down Expand Up @@ -145,7 +149,7 @@ impl Tree {
let focus = self.focus;
let parent = self.nodes[focus].parent;

let node = Node::view(view);
let node = Node::view(view, focus);
let node = self.nodes.insert(node);
self.get_mut(node).id = node;

Expand All @@ -171,7 +175,7 @@ impl Tree {
container.children.insert(pos, node);
self.nodes[node].parent = parent;
} else {
let mut split = Node::container(layout);
let mut split = Node::container(layout, focus);
split.parent = parent;
let split = self.nodes.insert(split);

Expand Down Expand Up @@ -219,7 +223,11 @@ impl Tree {

if self.focus == index {
// focus on something else
self.focus_next();
if self.contains(self.nodes[index].spawned_by) {
self.focus = self.nodes[index].spawned_by;
} else {
self.focus_next();
}
}

stack.push(index);
Expand Down

0 comments on commit 54ef4a3

Please sign in to comment.