@@ -28,10 +28,10 @@ pub enum Content {
30impl Node {
30impl Node {
31− pub fn container() -> Self {
31+ pub fn container(layout: Layout) -> Self {
33 parent: ViewId::default(),
33 parent: ViewId::default(),
34− content: Content::Container(Box::new(Container::new())),
34+ content: Content::Container(Box::new(Container::new(layout))),
@@ -45,6 +45,7 @@ impl Node {
46// TODO: screen coord to container + container coordinate helpers
46// TODO: screen coord to container + container coordinate helpers
48+#[derive(PartialEq, Eq)]
48pub enum Layout {
49pub enum Layout {
49 Horizontal,
50 Horizontal,
@@ -58,9 +59,9 @@ pub struct Container {
60impl Container {
61impl Container {
61− pub fn new() -> Self {
62+ pub fn new(layout: Layout) -> Self {
63− layout: Layout::Horizontal,
64+ layout,
64 children: Vec::new(),
65 children: Vec::new(),
65 area: Rect::default(),
66 area: Rect::default(),
@@ -69,13 +70,13 @@ impl Container {
70impl Default for Container {
71impl Default for Container {
71 fn default() -> Self {
72 fn default() -> Self {
72− Self::new()
73+ Self::new(Layout::Horizontal)
76impl Tree {
77impl Tree {
77 pub fn new(area: Rect) -> Self {
78 pub fn new(area: Rect) -> Self {
78− let root = Node::container();
79+ let root = Node::container(Layout::Horizontal);
80 let mut nodes = HopSlotMap::with_key();
81 let mut nodes = HopSlotMap::with_key();
81 let root = nodes.insert(root);
82 let root = nodes.insert(root);
@@ -131,6 +132,79 @@ impl Tree {
135+ pub fn split(&mut self, view: View, layout: Layout) -> ViewId {
136+ let focus = self.focus;
137+ let parent = self.nodes[focus].parent;
139+ let node = Node::view(view);
140+ let node = self.nodes.insert(node);
141+ self.get_mut(node).id = node;
143+ let container = match &mut self.nodes[parent] {
145+ content: Content::Container(container),
148+ _ => unreachable!(),
151+ if container.layout == layout {
152+ // insert node after the current item if there is children already
153+ let pos = if container.children.is_empty() {
159+ .position(|&child| child == focus)
163+ container.children.insert(pos, node);
164+ self.nodes[node].parent = parent;
166+ let split = Node::container(layout);
167+ let split = self.nodes.insert(split);
169+ let container = match &mut self.nodes[split] {
171+ content: Content::Container(container),
174+ _ => unreachable!(),
176+ container.children.push(focus);
177+ container.children.push(node);
178+ self.nodes[focus].parent = split;
179+ self.nodes[node].parent = split;
181+ let container = match &mut self.nodes[parent] {
183+ content: Content::Container(container),
186+ _ => unreachable!(),
192+ .position(|&child| child == focus)
195+ // replace focus on parent with split
196+ container.children[pos] = split;
199+ // focus the new node
202+ // recalculate all the sizes
134 pub fn remove(&mut self, index: ViewId) {
208 pub fn remove(&mut self, index: ViewId) {
135 let mut stack = Vec::new();
209 let mut stack = Vec::new();