|
|
@ -7,7 +7,6 @@ use tui::layout::Rect;
|
|
|
|
pub struct Tree {
|
|
|
|
pub struct Tree {
|
|
|
|
root: Key,
|
|
|
|
root: Key,
|
|
|
|
// (container, index inside the container)
|
|
|
|
// (container, index inside the container)
|
|
|
|
current: (Key, usize),
|
|
|
|
|
|
|
|
pub focus: Key,
|
|
|
|
pub focus: Key,
|
|
|
|
// fullscreen: bool,
|
|
|
|
// fullscreen: bool,
|
|
|
|
area: Rect,
|
|
|
|
area: Rect,
|
|
|
@ -18,18 +17,29 @@ pub struct Tree {
|
|
|
|
stack: Vec<(Key, Rect)>,
|
|
|
|
stack: Vec<(Key, Rect)>,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub enum Node {
|
|
|
|
pub struct Node {
|
|
|
|
|
|
|
|
parent: Key,
|
|
|
|
|
|
|
|
content: Content,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub enum Content {
|
|
|
|
View(Box<View>),
|
|
|
|
View(Box<View>),
|
|
|
|
Container(Box<Container>),
|
|
|
|
Container(Box<Container>),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Node {
|
|
|
|
impl Node {
|
|
|
|
pub fn container() -> Self {
|
|
|
|
pub fn container() -> Self {
|
|
|
|
Self::Container(Box::new(Container::new()))
|
|
|
|
Node {
|
|
|
|
|
|
|
|
parent: Key::default(),
|
|
|
|
|
|
|
|
content: Content::Container(Box::new(Container::new())),
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn view(view: View) -> Self {
|
|
|
|
pub fn view(view: View) -> Self {
|
|
|
|
Self::View(Box::new(view))
|
|
|
|
Node {
|
|
|
|
|
|
|
|
parent: Key::default(),
|
|
|
|
|
|
|
|
content: Content::View(Box::new(view)),
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -66,13 +76,16 @@ impl Default for Container {
|
|
|
|
impl Tree {
|
|
|
|
impl Tree {
|
|
|
|
pub fn new(area: Rect) -> Self {
|
|
|
|
pub fn new(area: Rect) -> Self {
|
|
|
|
let root = Node::container();
|
|
|
|
let root = Node::container();
|
|
|
|
|
|
|
|
|
|
|
|
let mut nodes = HopSlotMap::new();
|
|
|
|
let mut nodes = HopSlotMap::new();
|
|
|
|
let root = nodes.insert(root);
|
|
|
|
let root = nodes.insert(root);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// root is it's own parent
|
|
|
|
|
|
|
|
nodes[root].parent = root;
|
|
|
|
|
|
|
|
|
|
|
|
Self {
|
|
|
|
Self {
|
|
|
|
root,
|
|
|
|
root,
|
|
|
|
current: (root, 0),
|
|
|
|
focus: root,
|
|
|
|
focus: Key::default(),
|
|
|
|
|
|
|
|
// fullscreen: false,
|
|
|
|
// fullscreen: false,
|
|
|
|
area,
|
|
|
|
area,
|
|
|
|
nodes,
|
|
|
|
nodes,
|
|
|
@ -81,23 +94,34 @@ impl Tree {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn insert(&mut self, view: View) -> Key {
|
|
|
|
pub fn insert(&mut self, view: View) -> Key {
|
|
|
|
let node = self.nodes.insert(Node::view(view));
|
|
|
|
let focus = self.focus;
|
|
|
|
let (id, pos) = self.current;
|
|
|
|
let parent = self.nodes[focus].parent;
|
|
|
|
let container = match &mut self.nodes[id] {
|
|
|
|
let mut node = Node::view(view);
|
|
|
|
Node::Container(container) => container,
|
|
|
|
node.parent = parent;
|
|
|
|
|
|
|
|
let node = self.nodes.insert(node);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let container = match &mut self.nodes[parent] {
|
|
|
|
|
|
|
|
Node {
|
|
|
|
|
|
|
|
content: Content::Container(container),
|
|
|
|
|
|
|
|
..
|
|
|
|
|
|
|
|
} => container,
|
|
|
|
_ => unreachable!(),
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// insert node after the current item if there is children already
|
|
|
|
// insert node after the current item if there is children already
|
|
|
|
let pos = if container.children.is_empty() {
|
|
|
|
let pos = if container.children.is_empty() {
|
|
|
|
pos
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
|
|
|
|
let pos = container
|
|
|
|
|
|
|
|
.children
|
|
|
|
|
|
|
|
.iter()
|
|
|
|
|
|
|
|
.position(|&child| child == focus)
|
|
|
|
|
|
|
|
.unwrap();
|
|
|
|
pos + 1
|
|
|
|
pos + 1
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
container.children.insert(pos, node);
|
|
|
|
container.children.insert(pos, node);
|
|
|
|
// focus the new node
|
|
|
|
// focus the new node
|
|
|
|
self.current = (id, pos);
|
|
|
|
|
|
|
|
self.focus = node;
|
|
|
|
self.focus = node;
|
|
|
|
|
|
|
|
|
|
|
|
// recalculate all the sizes
|
|
|
|
// recalculate all the sizes
|
|
|
@ -106,26 +130,78 @@ impl Tree {
|
|
|
|
node
|
|
|
|
node
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn remove(&mut self, index: Key) {
|
|
|
|
|
|
|
|
let mut stack = Vec::new();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if self.focus == index {
|
|
|
|
|
|
|
|
// focus on something else
|
|
|
|
|
|
|
|
self.focus_next();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
stack.push(index);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while let Some(index) = stack.pop() {
|
|
|
|
|
|
|
|
let parent_id = self.nodes[index].parent;
|
|
|
|
|
|
|
|
if let Node {
|
|
|
|
|
|
|
|
content: Content::Container(container),
|
|
|
|
|
|
|
|
..
|
|
|
|
|
|
|
|
} = &mut self.nodes[parent_id]
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if let Some(pos) = container.children.iter().position(|&child| child == index) {
|
|
|
|
|
|
|
|
container.children.remove(pos);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: if container now only has one child, remove it and place child in parent
|
|
|
|
|
|
|
|
if container.children.is_empty() && parent_id != self.root {
|
|
|
|
|
|
|
|
// if container now empty, remove it
|
|
|
|
|
|
|
|
stack.push(parent_id);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
self.nodes.remove(index);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.recalculate()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn views(&mut self) -> impl Iterator<Item = (&mut View, bool)> {
|
|
|
|
pub fn views(&mut self) -> impl Iterator<Item = (&mut View, bool)> {
|
|
|
|
let focus = self.focus;
|
|
|
|
let focus = self.focus;
|
|
|
|
self.nodes
|
|
|
|
self.nodes
|
|
|
|
.iter_mut()
|
|
|
|
.iter_mut()
|
|
|
|
.filter_map(move |(key, node)| match node {
|
|
|
|
.filter_map(move |(key, node)| match node {
|
|
|
|
Node::View(view) => Some((view.as_mut(), focus == key)),
|
|
|
|
Node {
|
|
|
|
Node::Container(..) => None,
|
|
|
|
content: Content::View(view),
|
|
|
|
|
|
|
|
..
|
|
|
|
|
|
|
|
} => Some((view.as_mut(), focus == key)),
|
|
|
|
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn get(&self, index: Key) -> &View {
|
|
|
|
pub fn get(&self, index: Key) -> &View {
|
|
|
|
match &self.nodes[index] {
|
|
|
|
match &self.nodes[index] {
|
|
|
|
Node::View(view) => view,
|
|
|
|
Node {
|
|
|
|
|
|
|
|
content: Content::View(view),
|
|
|
|
|
|
|
|
..
|
|
|
|
|
|
|
|
} => view,
|
|
|
|
_ => unreachable!(),
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn get_mut(&mut self, index: Key) -> &mut View {
|
|
|
|
pub fn get_mut(&mut self, index: Key) -> &mut View {
|
|
|
|
match &mut self.nodes[index] {
|
|
|
|
match &mut self.nodes[index] {
|
|
|
|
Node::View(view) => view,
|
|
|
|
Node {
|
|
|
|
|
|
|
|
content: Content::View(view),
|
|
|
|
|
|
|
|
..
|
|
|
|
|
|
|
|
} => view,
|
|
|
|
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
|
|
|
|
match &self.nodes[self.root] {
|
|
|
|
|
|
|
|
Node {
|
|
|
|
|
|
|
|
content: Content::Container(container),
|
|
|
|
|
|
|
|
..
|
|
|
|
|
|
|
|
} => container.children.is_empty(),
|
|
|
|
_ => unreachable!(),
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -136,6 +212,10 @@ impl Tree {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn recalculate(&mut self) {
|
|
|
|
pub fn recalculate(&mut self) {
|
|
|
|
|
|
|
|
if self.is_empty() {
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
self.stack.push((self.root, self.area));
|
|
|
|
self.stack.push((self.root, self.area));
|
|
|
|
|
|
|
|
|
|
|
|
// take the area
|
|
|
|
// take the area
|
|
|
@ -146,12 +226,12 @@ impl Tree {
|
|
|
|
while let Some((key, area)) = self.stack.pop() {
|
|
|
|
while let Some((key, area)) = self.stack.pop() {
|
|
|
|
let node = &mut self.nodes[key];
|
|
|
|
let node = &mut self.nodes[key];
|
|
|
|
|
|
|
|
|
|
|
|
match node {
|
|
|
|
match &mut node.content {
|
|
|
|
Node::View(view) => {
|
|
|
|
Content::View(view) => {
|
|
|
|
// debug!!("setting view area {:?}", area);
|
|
|
|
// debug!!("setting view area {:?}", area);
|
|
|
|
view.area = area;
|
|
|
|
view.area = area;
|
|
|
|
} // TODO: call f()
|
|
|
|
} // TODO: call f()
|
|
|
|
Node::Container(container) => {
|
|
|
|
Content::Container(container) => {
|
|
|
|
// debug!!("setting container area {:?}", area);
|
|
|
|
// debug!!("setting container area {:?}", area);
|
|
|
|
container.area = area;
|
|
|
|
container.area = area;
|
|
|
|
|
|
|
|
|
|
|
@ -263,9 +343,9 @@ impl<'a> Iterator for Traverse<'a> {
|
|
|
|
|
|
|
|
|
|
|
|
let node = &self.tree.nodes[key];
|
|
|
|
let node = &self.tree.nodes[key];
|
|
|
|
|
|
|
|
|
|
|
|
match node {
|
|
|
|
match &node.content {
|
|
|
|
Node::View(view) => return Some((key, view)),
|
|
|
|
Content::View(view) => return Some((key, view)),
|
|
|
|
Node::Container(container) => {
|
|
|
|
Content::Container(container) => {
|
|
|
|
self.stack.extend(container.children.iter().rev());
|
|
|
|
self.stack.extend(container.children.iter().rev());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|