2021-03-24 09:03:20 +04:00
|
|
|
use crate::{View, ViewId};
|
|
|
|
use slotmap::HopSlotMap;
|
2021-02-04 12:26:11 +04:00
|
|
|
use tui::layout::Rect;
|
|
|
|
|
|
|
|
// the dimensions are recomputed on windo resize/tree change.
|
|
|
|
//
|
|
|
|
pub struct Tree {
|
2021-03-24 09:03:20 +04:00
|
|
|
root: ViewId,
|
2021-02-04 12:26:11 +04:00
|
|
|
// (container, index inside the container)
|
2021-03-24 09:03:20 +04:00
|
|
|
pub focus: ViewId,
|
2021-02-18 13:36:24 +04:00
|
|
|
// fullscreen: bool,
|
2021-02-04 12:26:11 +04:00
|
|
|
area: Rect,
|
|
|
|
|
2021-03-24 09:03:20 +04:00
|
|
|
nodes: HopSlotMap<ViewId, Node>,
|
2021-02-04 12:26:11 +04:00
|
|
|
|
|
|
|
// used for traversals
|
2021-03-24 09:03:20 +04:00
|
|
|
stack: Vec<(ViewId, Rect)>,
|
2021-02-04 12:26:11 +04:00
|
|
|
}
|
|
|
|
|
2021-02-19 12:46:43 +04:00
|
|
|
pub struct Node {
|
2021-03-24 09:03:20 +04:00
|
|
|
parent: ViewId,
|
2021-02-19 12:46:43 +04:00
|
|
|
content: Content,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum Content {
|
2021-02-04 12:26:11 +04:00
|
|
|
View(Box<View>),
|
|
|
|
Container(Box<Container>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Node {
|
2021-03-24 09:28:26 +04:00
|
|
|
pub fn container(layout: Layout) -> Self {
|
2021-03-31 18:42:16 +04:00
|
|
|
Self {
|
2021-03-24 09:03:20 +04:00
|
|
|
parent: ViewId::default(),
|
2021-03-24 09:28:26 +04:00
|
|
|
content: Content::Container(Box::new(Container::new(layout))),
|
2021-02-19 12:46:43 +04:00
|
|
|
}
|
2021-02-04 12:26:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn view(view: View) -> Self {
|
2021-03-31 18:42:16 +04:00
|
|
|
Self {
|
2021-03-24 09:03:20 +04:00
|
|
|
parent: ViewId::default(),
|
2021-02-19 12:46:43 +04:00
|
|
|
content: Content::View(Box::new(view)),
|
|
|
|
}
|
2021-02-04 12:26:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: screen coord to container + container coordinate helpers
|
|
|
|
|
2021-03-24 09:28:26 +04:00
|
|
|
#[derive(PartialEq, Eq)]
|
2021-02-04 12:26:11 +04:00
|
|
|
pub enum Layout {
|
|
|
|
Horizontal,
|
|
|
|
Vertical,
|
|
|
|
// could explore stacked/tabbed
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Container {
|
|
|
|
layout: Layout,
|
2021-03-24 09:03:20 +04:00
|
|
|
children: Vec<ViewId>,
|
2021-02-04 12:26:11 +04:00
|
|
|
area: Rect,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Container {
|
2021-03-24 09:28:26 +04:00
|
|
|
pub fn new(layout: Layout) -> Self {
|
2021-02-04 12:26:11 +04:00
|
|
|
Self {
|
2021-03-24 09:28:26 +04:00
|
|
|
layout,
|
2021-02-04 12:26:11 +04:00
|
|
|
children: Vec::new(),
|
|
|
|
area: Rect::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-05 11:06:48 +04:00
|
|
|
impl Default for Container {
|
|
|
|
fn default() -> Self {
|
2021-04-08 10:52:04 +04:00
|
|
|
Self::new(Layout::Vertical)
|
2021-02-05 11:06:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-04 12:26:11 +04:00
|
|
|
impl Tree {
|
|
|
|
pub fn new(area: Rect) -> Self {
|
2021-04-08 10:52:04 +04:00
|
|
|
let root = Node::container(Layout::Vertical);
|
2021-02-19 12:46:43 +04:00
|
|
|
|
2021-03-24 09:03:20 +04:00
|
|
|
let mut nodes = HopSlotMap::with_key();
|
2021-02-04 12:26:11 +04:00
|
|
|
let root = nodes.insert(root);
|
|
|
|
|
2021-02-19 12:46:43 +04:00
|
|
|
// root is it's own parent
|
|
|
|
nodes[root].parent = root;
|
|
|
|
|
2021-02-04 12:26:11 +04:00
|
|
|
Self {
|
|
|
|
root,
|
2021-02-19 12:46:43 +04:00
|
|
|
focus: root,
|
2021-02-18 13:36:24 +04:00
|
|
|
// fullscreen: false,
|
2021-02-04 12:26:11 +04:00
|
|
|
area,
|
|
|
|
nodes,
|
|
|
|
stack: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-24 09:03:20 +04:00
|
|
|
pub fn insert(&mut self, view: View) -> ViewId {
|
2021-02-19 12:46:43 +04:00
|
|
|
let focus = self.focus;
|
|
|
|
let parent = self.nodes[focus].parent;
|
|
|
|
let mut node = Node::view(view);
|
|
|
|
node.parent = parent;
|
|
|
|
let node = self.nodes.insert(node);
|
2021-02-21 14:04:31 +04:00
|
|
|
self.get_mut(node).id = node;
|
2021-02-19 12:46:43 +04:00
|
|
|
|
|
|
|
let container = match &mut self.nodes[parent] {
|
|
|
|
Node {
|
|
|
|
content: Content::Container(container),
|
|
|
|
..
|
|
|
|
} => container,
|
2021-02-04 12:26:11 +04:00
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
// insert node after the current item if there is children already
|
|
|
|
let pos = if container.children.is_empty() {
|
2021-02-19 12:46:43 +04:00
|
|
|
0
|
2021-02-04 12:26:11 +04:00
|
|
|
} else {
|
2021-02-19 12:46:43 +04:00
|
|
|
let pos = container
|
|
|
|
.children
|
|
|
|
.iter()
|
|
|
|
.position(|&child| child == focus)
|
|
|
|
.unwrap();
|
2021-02-04 12:26:11 +04:00
|
|
|
pos + 1
|
|
|
|
};
|
|
|
|
|
|
|
|
container.children.insert(pos, node);
|
|
|
|
// focus the new node
|
|
|
|
self.focus = node;
|
|
|
|
|
|
|
|
// recalculate all the sizes
|
2021-03-24 09:28:26 +04:00
|
|
|
self.recalculate();
|
|
|
|
|
|
|
|
node
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn split(&mut self, view: View, layout: Layout) -> ViewId {
|
|
|
|
let focus = self.focus;
|
|
|
|
let parent = self.nodes[focus].parent;
|
|
|
|
|
|
|
|
let node = Node::view(view);
|
|
|
|
let node = self.nodes.insert(node);
|
|
|
|
self.get_mut(node).id = node;
|
|
|
|
|
|
|
|
let container = match &mut self.nodes[parent] {
|
|
|
|
Node {
|
|
|
|
content: Content::Container(container),
|
|
|
|
..
|
|
|
|
} => container,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
if container.layout == layout {
|
|
|
|
// insert node after the current item if there is children already
|
|
|
|
let pos = if container.children.is_empty() {
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
let pos = container
|
|
|
|
.children
|
|
|
|
.iter()
|
|
|
|
.position(|&child| child == focus)
|
|
|
|
.unwrap();
|
|
|
|
pos + 1
|
|
|
|
};
|
|
|
|
container.children.insert(pos, node);
|
|
|
|
self.nodes[node].parent = parent;
|
|
|
|
} else {
|
2021-04-08 11:53:57 +04:00
|
|
|
let mut split = Node::container(layout);
|
|
|
|
split.parent = parent;
|
2021-03-24 09:28:26 +04:00
|
|
|
let split = self.nodes.insert(split);
|
|
|
|
|
|
|
|
let container = match &mut self.nodes[split] {
|
|
|
|
Node {
|
|
|
|
content: Content::Container(container),
|
|
|
|
..
|
|
|
|
} => container,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
container.children.push(focus);
|
|
|
|
container.children.push(node);
|
|
|
|
self.nodes[focus].parent = split;
|
|
|
|
self.nodes[node].parent = split;
|
|
|
|
|
|
|
|
let container = match &mut self.nodes[parent] {
|
|
|
|
Node {
|
|
|
|
content: Content::Container(container),
|
|
|
|
..
|
|
|
|
} => container,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let pos = container
|
|
|
|
.children
|
|
|
|
.iter()
|
|
|
|
.position(|&child| child == focus)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// replace focus on parent with split
|
|
|
|
container.children[pos] = split;
|
|
|
|
}
|
|
|
|
|
|
|
|
// focus the new node
|
|
|
|
self.focus = node;
|
|
|
|
|
|
|
|
// recalculate all the sizes
|
2021-02-04 14:49:29 +04:00
|
|
|
self.recalculate();
|
2021-02-04 12:26:11 +04:00
|
|
|
|
|
|
|
node
|
|
|
|
}
|
|
|
|
|
2021-03-24 09:03:20 +04:00
|
|
|
pub fn remove(&mut self, index: ViewId) {
|
2021-02-19 12:46:43 +04:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2021-03-23 12:47:40 +04:00
|
|
|
pub fn views(&self) -> impl Iterator<Item = (&View, bool)> {
|
|
|
|
let focus = self.focus;
|
|
|
|
self.nodes.iter().filter_map(move |(key, node)| match node {
|
|
|
|
Node {
|
|
|
|
content: Content::View(view),
|
|
|
|
..
|
|
|
|
} => Some((view.as_ref(), focus == key)),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn views_mut(&mut self) -> impl Iterator<Item = (&mut View, bool)> {
|
2021-02-04 12:26:11 +04:00
|
|
|
let focus = self.focus;
|
|
|
|
self.nodes
|
|
|
|
.iter_mut()
|
|
|
|
.filter_map(move |(key, node)| match node {
|
2021-02-19 12:46:43 +04:00
|
|
|
Node {
|
|
|
|
content: Content::View(view),
|
|
|
|
..
|
|
|
|
} => Some((view.as_mut(), focus == key)),
|
|
|
|
_ => None,
|
2021-02-04 12:26:11 +04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-24 09:03:20 +04:00
|
|
|
pub fn get(&self, index: ViewId) -> &View {
|
2021-02-04 12:26:11 +04:00
|
|
|
match &self.nodes[index] {
|
2021-02-19 12:46:43 +04:00
|
|
|
Node {
|
|
|
|
content: Content::View(view),
|
|
|
|
..
|
|
|
|
} => view,
|
2021-02-04 12:26:11 +04:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-24 09:03:20 +04:00
|
|
|
pub fn get_mut(&mut self, index: ViewId) -> &mut View {
|
2021-02-04 12:26:11 +04:00
|
|
|
match &mut self.nodes[index] {
|
2021-02-19 12:46:43 +04:00
|
|
|
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(),
|
2021-02-04 12:26:11 +04:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn resize(&mut self, area: Rect) {
|
|
|
|
self.area = area;
|
2021-02-04 14:49:29 +04:00
|
|
|
self.recalculate();
|
2021-02-04 12:26:11 +04:00
|
|
|
}
|
|
|
|
|
2021-02-04 14:49:29 +04:00
|
|
|
pub fn recalculate(&mut self) {
|
2021-02-19 12:46:43 +04:00
|
|
|
if self.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-04 12:26:11 +04:00
|
|
|
self.stack.push((self.root, self.area));
|
|
|
|
|
|
|
|
// take the area
|
|
|
|
// fetch the node
|
|
|
|
// a) node is view, give it whole area
|
|
|
|
// b) node is container, calculate areas for each child and push them on the stack
|
|
|
|
|
|
|
|
while let Some((key, area)) = self.stack.pop() {
|
|
|
|
let node = &mut self.nodes[key];
|
|
|
|
|
2021-02-19 12:46:43 +04:00
|
|
|
match &mut node.content {
|
|
|
|
Content::View(view) => {
|
2021-02-04 12:26:11 +04:00
|
|
|
// debug!!("setting view area {:?}", area);
|
|
|
|
view.area = area;
|
|
|
|
} // TODO: call f()
|
2021-02-19 12:46:43 +04:00
|
|
|
Content::Container(container) => {
|
2021-02-04 12:26:11 +04:00
|
|
|
// debug!!("setting container area {:?}", area);
|
|
|
|
container.area = area;
|
|
|
|
|
|
|
|
match container.layout {
|
2021-04-08 10:52:04 +04:00
|
|
|
Layout::Horizontal => {
|
2021-02-09 10:59:42 +04:00
|
|
|
let len = container.children.len();
|
|
|
|
|
|
|
|
let height = area.height / len as u16;
|
|
|
|
|
|
|
|
let mut child_y = area.y;
|
|
|
|
|
|
|
|
for (i, child) in container.children.iter().enumerate() {
|
|
|
|
let mut area = Rect::new(
|
|
|
|
container.area.x,
|
|
|
|
child_y,
|
|
|
|
container.area.width,
|
|
|
|
height,
|
|
|
|
);
|
|
|
|
child_y += height;
|
|
|
|
|
|
|
|
// last child takes the remaining width because we can get uneven
|
|
|
|
// space from rounding
|
|
|
|
if i == len - 1 {
|
|
|
|
area.height = container.area.y + container.area.height - area.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.stack.push((*child, area));
|
|
|
|
}
|
|
|
|
}
|
2021-04-08 10:52:04 +04:00
|
|
|
Layout::Vertical => {
|
2021-02-05 09:42:56 +04:00
|
|
|
let len = container.children.len();
|
2021-02-04 12:26:11 +04:00
|
|
|
|
2021-02-05 09:42:56 +04:00
|
|
|
let width = area.width / len as u16;
|
2021-02-04 12:26:11 +04:00
|
|
|
|
2021-04-08 11:54:55 +04:00
|
|
|
let inner_gap = 1u16;
|
|
|
|
// let total_gap = inner_gap * (len as u16 - 1);
|
|
|
|
|
2021-02-04 12:26:11 +04:00
|
|
|
let mut child_x = area.x;
|
|
|
|
|
2021-02-05 09:42:56 +04:00
|
|
|
for (i, child) in container.children.iter().enumerate() {
|
|
|
|
let mut area = Rect::new(
|
|
|
|
child_x,
|
|
|
|
container.area.y,
|
|
|
|
width,
|
|
|
|
container.area.height,
|
|
|
|
);
|
2021-04-08 11:54:55 +04:00
|
|
|
child_x += width + inner_gap;
|
2021-02-04 12:26:11 +04:00
|
|
|
|
2021-02-05 09:42:56 +04:00
|
|
|
// last child takes the remaining width because we can get uneven
|
|
|
|
// space from rounding
|
|
|
|
if i == len - 1 {
|
|
|
|
area.width = container.area.x + container.area.width - area.x;
|
|
|
|
}
|
|
|
|
|
2021-02-04 12:26:11 +04:00
|
|
|
self.stack.push((*child, area));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-04 14:49:29 +04:00
|
|
|
|
|
|
|
pub fn traverse(&self) -> Traverse {
|
|
|
|
Traverse::new(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn focus_next(&mut self) {
|
|
|
|
// This function is very dumb, but that's because we don't store any parent links.
|
|
|
|
// (we'd be able to go parent.next_sibling() recursively until we find something)
|
|
|
|
// For now that's okay though, since it's unlikely you'll be able to open a large enough
|
|
|
|
// number of splits to notice.
|
|
|
|
|
2021-02-22 07:06:28 +04:00
|
|
|
// current = focus
|
|
|
|
// let found = loop do {
|
|
|
|
// node = focus.parent;
|
|
|
|
// let found = node.next_sibling_of(current)
|
|
|
|
// if some {
|
|
|
|
// break found;
|
|
|
|
// }
|
|
|
|
// // else
|
|
|
|
// if node == root {
|
|
|
|
// return first child of root;
|
|
|
|
// };
|
|
|
|
// current = parent;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// use found next sibling
|
|
|
|
// loop do {
|
|
|
|
// if found = view -> focus = found, return
|
|
|
|
// if found = container -> found = first child
|
|
|
|
// }
|
|
|
|
|
2021-02-04 14:49:29 +04:00
|
|
|
let iter = self.traverse();
|
|
|
|
|
|
|
|
let mut iter = iter.skip_while(|&(key, _view)| key != self.focus);
|
|
|
|
iter.next(); // take the focused value
|
|
|
|
|
2021-03-31 18:42:16 +04:00
|
|
|
if let Some((key, _)) = iter.next() {
|
|
|
|
self.focus = key;
|
|
|
|
} else {
|
|
|
|
// extremely crude, take the first item again
|
|
|
|
let (key, _) = self.traverse().next().unwrap();
|
|
|
|
self.focus = key;
|
2021-02-04 14:49:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Traverse<'a> {
|
|
|
|
tree: &'a Tree,
|
2021-03-24 09:03:20 +04:00
|
|
|
stack: Vec<ViewId>, // TODO: reuse the one we use on update
|
2021-02-04 14:49:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Traverse<'a> {
|
|
|
|
fn new(tree: &'a Tree) -> Self {
|
|
|
|
Self {
|
|
|
|
tree,
|
|
|
|
stack: vec![tree.root],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Iterator for Traverse<'a> {
|
2021-03-24 09:03:20 +04:00
|
|
|
type Item = (ViewId, &'a View);
|
2021-02-04 14:49:29 +04:00
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
loop {
|
|
|
|
let key = self.stack.pop()?;
|
|
|
|
|
|
|
|
let node = &self.tree.nodes[key];
|
|
|
|
|
2021-02-19 12:46:43 +04:00
|
|
|
match &node.content {
|
|
|
|
Content::View(view) => return Some((key, view)),
|
|
|
|
Content::Container(container) => {
|
2021-02-04 14:49:29 +04:00
|
|
|
self.stack.extend(container.children.iter().rev());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-04 12:26:11 +04:00
|
|
|
}
|