@@ -28,10 +28,10 @@ pub enum Content {
2828}
2929
3030impl Node {
31− pub fn container() -> Self {
31+ pub fn container(layout: Layout) -> Self {
3232 Node {
3333 parent: ViewId::default(),
34− content: Content::Container(Box::new(Container::new())),
34+ content: Content::Container(Box::new(Container::new(layout))),
3535 }
3636 }
3737
@@ -45,6 +45,7 @@ impl Node {
4545
4646// TODO: screen coord to container + container coordinate helpers
4747
48+#[derive(PartialEq, Eq)]
4849pub enum Layout {
4950 Horizontal,
5051 Vertical,
@@ -58,9 +59,9 @@ pub struct Container {
5859}
5960
6061impl Container {
61− pub fn new() -> Self {
62+ pub fn new(layout: Layout) -> Self {
6263 Self {
63− layout: Layout::Horizontal,
64+ layout,
6465 children: Vec::new(),
6566 area: Rect::default(),
6667 }
@@ -69,13 +70,13 @@ impl Container {
6970
7071impl Default for Container {
7172 fn default() -> Self {
72− Self::new()
73+ Self::new(Layout::Horizontal)
7374 }
7475}
7576
7677impl Tree {
7778 pub fn new(area: Rect) -> Self {
78− let root = Node::container();
79+ let root = Node::container(Layout::Horizontal);
7980
8081 let mut nodes = HopSlotMap::with_key();
8182 let root = nodes.insert(root);
@@ -131,6 +132,79 @@ impl Tree {
131132 node
132133 }
133134
135+ pub fn split(&mut self, view: View, layout: Layout) -> ViewId {
136+ let focus = self.focus;
137+ let parent = self.nodes[focus].parent;
138+
139+ let node = Node::view(view);
140+ let node = self.nodes.insert(node);
141+ self.get_mut(node).id = node;
142+
143+ let container = match &mut self.nodes[parent] {
144+ Node {
145+ content: Content::Container(container),
146+ ..
147+ } => container,
148+ _ => unreachable!(),
149+ };
150+
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() {
154+ 0
155+ } else {
156+ let pos = container
157+ .children
158+ .iter()
159+ .position(|&child| child == focus)
160+ .unwrap();
161+ pos + 1
162+ };
163+ container.children.insert(pos, node);
164+ self.nodes[node].parent = parent;
165+ } else {
166+ let split = Node::container(layout);
167+ let split = self.nodes.insert(split);
168+
169+ let container = match &mut self.nodes[split] {
170+ Node {
171+ content: Content::Container(container),
172+ ..
173+ } => container,
174+ _ => unreachable!(),
175+ };
176+ container.children.push(focus);
177+ container.children.push(node);
178+ self.nodes[focus].parent = split;
179+ self.nodes[node].parent = split;
180+
181+ let container = match &mut self.nodes[parent] {
182+ Node {
183+ content: Content::Container(container),
184+ ..
185+ } => container,
186+ _ => unreachable!(),
187+ };
188+
189+ let pos = container
190+ .children
191+ .iter()
192+ .position(|&child| child == focus)
193+ .unwrap();
194+
195+ // replace focus on parent with split
196+ container.children[pos] = split;
197+ }
198+
199+ // focus the new node
200+ self.focus = node;
201+
202+ // recalculate all the sizes
203+ self.recalculate();
204+
205+ node
206+ }
207+
134208 pub fn remove(&mut self, index: ViewId) {
135209 let mut stack = Vec::new();
136210