From 0ded47410c4ef6dc38ccf2739139d0b373dbbfb6 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Mon, 23 May 2022 22:51:23 +0200 Subject: [PATCH] product editor: move tree nav to separate file --- nested/src/product/editor.rs | 319 ++-------------------------------- nested/src/product/mod.rs | 3 + nested/src/product/nav.rs | 324 +++++++++++++++++++++++++++++++++++ 3 files changed, 338 insertions(+), 308 deletions(-) create mode 100644 nested/src/product/nav.rs diff --git a/nested/src/product/editor.rs b/nested/src/product/editor.rs index 3a6a77e..8dfd0aa 100644 --- a/nested/src/product/editor.rs +++ b/nested/src/product/editor.rs @@ -24,15 +24,15 @@ use { pub struct ProductEditor { elements: VecBuffer, - n_indices: Vec, + pub(super) n_indices: Vec, el_port: OuterViewPort>, el_view_port: OuterViewPort>>, - ctx: Arc>, + pub(super) ctx: Arc>, - cursor: Option, - depth: usize + pub(super) cursor: Option, + pub(super) depth: usize } impl ProductEditor { @@ -74,7 +74,7 @@ impl ProductEditor { self } - fn get_editor_element(&self, idx: usize) -> Option { + pub fn get_editor_element(&self, idx: usize) -> Option { if let Some(i) = self.n_indices.get(idx) { Some(self.elements.get(*i)) } else { @@ -82,7 +82,7 @@ impl ProductEditor { } } - fn get_editor_element_mut(&mut self, idx: usize) -> Option> { + pub fn get_editor_element_mut(&mut self, idx: usize) -> Option> { if let Some(i) = self.n_indices.get(idx) { Some(self.elements.get_mut(*i)) } else { @@ -90,15 +90,15 @@ impl ProductEditor { } } - fn get_cur_element(&self) -> Option { + pub fn get_cur_element(&self) -> Option { self.get_editor_element(self.cursor?) } - fn get_cur_element_mut(&mut self) -> Option> { + pub fn get_cur_element_mut(&mut self) -> Option> { self.get_editor_element_mut(self.cursor?) } - fn get_editor(&self, idx: usize) -> Option>> { + pub fn get_editor(&self, idx: usize) -> Option>> { if let Some(ProductEditorElement::N{ t: _, editor, select: _ }) = self.get_editor_element(idx) { editor } else { @@ -106,312 +106,17 @@ impl ProductEditor { } } - fn get_cur_editor(&self) -> Option>> { + pub fn get_cur_editor(&self) -> Option>> { self.get_editor(self.cursor?) } - fn set_leaf_mode(&mut self, mode: ListCursorMode) { + pub fn set_leaf_mode(&mut self, mode: ListCursorMode) { let mut c = self.get_cursor(); c.leaf_mode = mode; self.goto(c); } } -impl TreeNav for ProductEditor { - fn get_cursor(&self) -> TreeCursor { - if let Some(i) = self.cursor { - if let Some(e) = self.get_editor(i) { - let mut c = e.read().unwrap().get_cursor(); - if c.tree_addr.len() == 0 { - c.leaf_mode = ListCursorMode::Select; - } - c.tree_addr.insert(0, i); - c - } else { - TreeCursor { - leaf_mode: ListCursorMode::Select, - tree_addr: vec![ i ] - } - } - } else { - TreeCursor::default() - } - } - - fn goto(&mut self, mut c: TreeCursor) -> TreeNavResult { - if let Some(mut element) = self.get_cur_element_mut() { - if let ProductEditorElement::N{ t, editor, select } = element.deref_mut() { - if let Some(e) = editor { - e.write().unwrap().goto(TreeCursor::default()); - } - *select = false; - } - } - - if c.tree_addr.len() > 0 { - self.cursor = Some(c.tree_addr.remove(0)); - - if let Some(mut element) = self.get_cur_element_mut() { - if let ProductEditorElement::N{ t, editor, select } = element.deref_mut() { - if let Some(e) = editor { - e.write().unwrap().goto(c); - } - *select = true; - } - } - - TreeNavResult::Continue - } else { - self.cursor = None; - TreeNavResult::Exit - } - } - - fn goto_home(&mut self) -> TreeNavResult { - if let Some(c) = self.cursor { - if let Some(ProductEditorElement::N{ t, editor, select }) = self.get_cur_element_mut().as_deref_mut() { - if let Some(e) = editor { - let mut ce = e.write().unwrap(); - - let cur_mode = ce.get_cursor().leaf_mode; - let depth = ce.get_cursor().tree_addr.len(); - - if depth > 0 { - return match ce.goto_home() { - TreeNavResult::Exit => { - drop(ce); - *select = false; - - match self.pxev() { - TreeNavResult::Exit => TreeNavResult::Exit, - TreeNavResult::Continue => { - for _x in 1..depth { - self.dn(); - self.goto_end(); - } - self.dn(); - self.set_leaf_mode(cur_mode); - - TreeNavResult::Continue - } - } - }, - TreeNavResult::Continue => TreeNavResult::Continue - }; - } - } - - *select = false; - if c != 0 { - self.cursor = Some(0); - if let Some(ProductEditorElement::N{ t, editor, select }) = self.get_cur_element_mut().as_deref_mut() { - *select = true; - } - return TreeNavResult::Continue; - } - } - } - self.cursor = None; - TreeNavResult::Exit - } - - fn goto_end(&mut self) -> TreeNavResult { - if let Some(c) = self.cursor { - if let Some(ProductEditorElement::N{ t, editor, select }) = self.get_cur_element_mut().as_deref_mut() { - if let Some(e) = editor { - let mut ce = e.write().unwrap(); - - let cur_mode = ce.get_cursor().leaf_mode; - let depth = ce.get_cursor().tree_addr.len(); - - if depth > 0 { - return match ce.goto_end() { - TreeNavResult::Exit => { - drop(ce); - *select = false; - - match self.nexd() { - TreeNavResult::Exit => TreeNavResult::Exit, - TreeNavResult::Continue => { - for _x in 1..depth { - self.dn(); - } - - self.dn(); - self.set_leaf_mode(cur_mode); - self.goto_end(); - - TreeNavResult::Continue - } - } - }, - TreeNavResult::Continue => TreeNavResult::Continue - }; - } - } - - *select = false; - if c < self.n_indices.len()-1 { - self.cursor = Some(self.n_indices.len()-1); - if let Some(ProductEditorElement::N{ t, editor, select }) = self.get_cur_element_mut().as_deref_mut() { - *select = true; - } - return TreeNavResult::Continue; - } - } - } - self.cursor = None; - TreeNavResult::Exit - } - - fn pxev(&mut self) -> TreeNavResult { - if let Some(c) = self.cursor { - if let Some(ProductEditorElement::N{ t, editor, select }) = self.get_editor_element_mut(c).as_deref_mut() { - if let Some(e) = editor { - let mut ce = e.write().unwrap(); - - let depth = ce.get_cursor().tree_addr.len(); - let cur_mode = ce.get_cursor().leaf_mode; - - if depth > 0 { - return match ce.pxev() { - TreeNavResult::Exit => { - drop(ce); - *select = false; - - if c > 0 { - self.cursor = Some(c-1); - if let Some(ProductEditorElement::N{ t, editor, select }) = self.get_cur_element_mut().as_deref_mut() { - *select = true; - } - - for _x in 1..depth { - self.dn(); - self.goto_end(); - } - - self.dn(); - self.set_leaf_mode(cur_mode); - self.goto_end(); - - TreeNavResult::Continue - } else { - TreeNavResult::Exit - } - } - TreeNavResult::Continue => TreeNavResult::Continue - }; - } - } - - *select = false; - if c > 0 { - self.cursor = Some(c-1); - if let Some(ProductEditorElement::N{ t, editor, select }) = self.get_cur_element_mut().as_deref_mut() { - *select = true; - } - return TreeNavResult::Continue; - } - } - } - - self.cursor = None; - TreeNavResult::Exit - } - - fn nexd(&mut self) -> TreeNavResult { - if let Some(c) = self.cursor.clone() { - if let Some(ProductEditorElement::N{ t, editor, select }) = self.get_editor_element_mut(c).as_deref_mut() { - if let Some(e) = editor { - let mut ce = e.write().unwrap(); - - let depth = ce.get_cursor().tree_addr.len(); - let cur_mode = ce.get_cursor().leaf_mode; - - if depth > 0 { - return match ce.nexd() { - TreeNavResult::Exit => { - drop(ce); - *select = false; - - if c+1 < self.n_indices.len() { - self.cursor = Some(c+1); - if let Some(ProductEditorElement::N{ t, editor, select }) = self.get_cur_element_mut().as_deref_mut() { - *select = true; - } - - for _x in 1..depth { - self.dn(); - } - - self.dn(); - self.set_leaf_mode(cur_mode); - - TreeNavResult::Continue - } else { - self.cursor = None; - TreeNavResult::Exit - } - } - TreeNavResult::Continue => TreeNavResult::Continue - }; - } - } - - *select = false; - if c+1 < self.n_indices.len() { - self.cursor = Some(c+1); - if let Some(ProductEditorElement::N{ t, editor, select }) = self.get_cur_element_mut().as_deref_mut() { - *select = true; - } - - return TreeNavResult::Continue; - } - } - } - - self.cursor = None; - TreeNavResult::Exit - } - - fn up(&mut self) -> TreeNavResult { - if let Some(ProductEditorElement::N{ t, editor, select }) = self.get_cur_element_mut().as_deref_mut() { - if let Some(e) = editor { - let mut ce = e.write().unwrap(); - if ce.get_cursor().tree_addr.len() > 0 { - ce.up(); - return TreeNavResult::Continue; - } - } - *select = false; - } - - self.cursor = None; - TreeNavResult::Exit - } - - fn dn(&mut self) -> TreeNavResult { - if let Some(c) = self.cursor { - if let Some(ProductEditorElement::N{ t, editor, select }) = self.get_editor_element_mut(c).as_deref_mut() { - if let Some(e) = editor { - e.write().unwrap().dn(); - } else { - let e = make_editor(self.ctx.clone(), t, self.depth+1); - e.write().unwrap().goto_home(); - *editor = Some(e); - } - } - } else { - self.cursor = Some(0); - if let Some(ProductEditorElement::N{ t, editor, select }) = self.get_cur_element_mut().as_deref_mut() { - *select = true; - } - } - - TreeNavResult::Continue - } -} - impl TerminalEditor for ProductEditor { fn get_term_view(&self) -> OuterViewPort { self.el_view_port.to_grid_horizontal().flatten() @@ -452,5 +157,3 @@ impl TerminalEditor for ProductEditor { } } -impl TerminalTreeEditor for ProductEditor {} - diff --git a/nested/src/product/mod.rs b/nested/src/product/mod.rs index 1e44f4b..b0c13ef 100644 --- a/nested/src/product/mod.rs +++ b/nested/src/product/mod.rs @@ -1,4 +1,7 @@ pub mod editor; pub mod element; +pub mod nav; + +pub use editor::ProductEditor; diff --git a/nested/src/product/nav.rs b/nested/src/product/nav.rs new file mode 100644 index 0000000..10c83ac --- /dev/null +++ b/nested/src/product/nav.rs @@ -0,0 +1,324 @@ +use { + crate::{ + core::{ViewPort, OuterViewPort, Observer, port::UpdateTask, TypeTerm, TypeLadder, Context}, + terminal::{ + Terminal, TerminalAtom, TerminalCompositor, TerminalEditor, + TerminalEditorResult, TerminalEvent, TerminalStyle, TerminalView, + make_label + }, + sequence::{SequenceView}, + tree_nav::{TreeNav, TerminalTreeEditor, TreeCursor, TreeNavResult}, + vec::{VecBuffer, MutableVecAccess}, + index::buffer::IndexBuffer, + integer::PosIntEditor, + string_editor::{StringEditor, CharEditor}, + list::{ListEditor, ListCursorMode, ListEditorStyle}, + product::{element::ProductEditorElement, ProductEditor}, + make_editor::make_editor + }, + cgmath::{Point2, Vector2}, + std::{sync::{Arc, RwLock}, ops::{Deref, DerefMut}}, + termion::event::{Event, Key}, +}; + +impl TreeNav for ProductEditor { + fn get_cursor(&self) -> TreeCursor { + if let Some(i) = self.cursor { + if let Some(e) = self.get_editor(i) { + let mut c = e.read().unwrap().get_cursor(); + if c.tree_addr.len() == 0 { + c.leaf_mode = ListCursorMode::Select; + } + c.tree_addr.insert(0, i); + c + } else { + TreeCursor { + leaf_mode: ListCursorMode::Select, + tree_addr: vec![ i ] + } + } + } else { + TreeCursor::default() + } + } + + fn goto(&mut self, mut c: TreeCursor) -> TreeNavResult { + if let Some(mut element) = self.get_cur_element_mut() { + if let ProductEditorElement::N{ t, editor, select } = element.deref_mut() { + if let Some(e) = editor { + e.write().unwrap().goto(TreeCursor::default()); + } + *select = false; + } + } + + if c.tree_addr.len() > 0 { + self.cursor = Some(c.tree_addr.remove(0)); + + if let Some(mut element) = self.get_cur_element_mut() { + if let ProductEditorElement::N{ t, editor, select } = element.deref_mut() { + if let Some(e) = editor { + e.write().unwrap().goto(c); + } + *select = true; + } + } + + TreeNavResult::Continue + } else { + self.cursor = None; + TreeNavResult::Exit + } + } + + fn goto_home(&mut self) -> TreeNavResult { + if let Some(c) = self.cursor { + if let Some(ProductEditorElement::N{ t, editor, select }) = self.get_cur_element_mut().as_deref_mut() { + if let Some(e) = editor { + let mut ce = e.write().unwrap(); + + let cur_mode = ce.get_cursor().leaf_mode; + let depth = ce.get_cursor().tree_addr.len(); + + if depth > 0 { + return match ce.goto_home() { + TreeNavResult::Exit => { + drop(ce); + *select = false; + + match self.pxev() { + TreeNavResult::Exit => TreeNavResult::Exit, + TreeNavResult::Continue => { + for _x in 1..depth { + self.dn(); + self.goto_end(); + } + self.dn(); + self.set_leaf_mode(cur_mode); + + TreeNavResult::Continue + } + } + }, + TreeNavResult::Continue => TreeNavResult::Continue + }; + } + } + + *select = false; + if c != 0 { + self.cursor = Some(0); + if let Some(ProductEditorElement::N{ t, editor, select }) = self.get_cur_element_mut().as_deref_mut() { + *select = true; + } + return TreeNavResult::Continue; + } + } + } + self.cursor = None; + TreeNavResult::Exit + } + + fn goto_end(&mut self) -> TreeNavResult { + if let Some(c) = self.cursor { + if let Some(ProductEditorElement::N{ t, editor, select }) = self.get_cur_element_mut().as_deref_mut() { + if let Some(e) = editor { + let mut ce = e.write().unwrap(); + + let cur_mode = ce.get_cursor().leaf_mode; + let depth = ce.get_cursor().tree_addr.len(); + + if depth > 0 { + match ce.goto_end() { + TreeNavResult::Exit => { + drop(ce); + *select = false; + + if c+1 < self.n_indices.len() { + match self.nexd() { + TreeNavResult::Exit => { + return TreeNavResult::Exit + }, + TreeNavResult::Continue => { + for _x in 1..depth { + self.dn(); + } + + self.dn(); + self.set_leaf_mode(cur_mode); + self.goto_end(); + + return TreeNavResult::Continue; + } + } + } + }, + TreeNavResult::Continue => {return TreeNavResult::Continue; } + } + } + } + + *select = false; + if c < self.n_indices.len()-1 { + self.cursor = Some(self.n_indices.len()-1); + if let Some(ProductEditorElement::N{ t, editor, select }) = self.get_cur_element_mut().as_deref_mut() { + *select = true; + } + return TreeNavResult::Continue; + } + } + } + self.cursor = None; + TreeNavResult::Exit + } + + fn pxev(&mut self) -> TreeNavResult { + if let Some(c) = self.cursor { + if let Some(ProductEditorElement::N{ t, editor, select }) = self.get_editor_element_mut(c).as_deref_mut() { + if let Some(e) = editor { + let mut ce = e.write().unwrap(); + + let depth = ce.get_cursor().tree_addr.len(); + let cur_mode = ce.get_cursor().leaf_mode; + + if depth > 0 { + return match ce.pxev() { + TreeNavResult::Exit => { + drop(ce); + *select = false; + + if c > 0 { + self.cursor = Some(c-1); + if let Some(ProductEditorElement::N{ t, editor, select }) = self.get_cur_element_mut().as_deref_mut() { + *select = true; + } + + for _x in 1..depth { + self.dn(); + self.goto_end(); + } + + self.dn(); + self.set_leaf_mode(cur_mode); + self.goto_end(); + + TreeNavResult::Continue + } else { + TreeNavResult::Exit + } + } + TreeNavResult::Continue => TreeNavResult::Continue + }; + } + } + + *select = false; + if c > 0 { + self.cursor = Some(c-1); + if let Some(ProductEditorElement::N{ t, editor, select }) = self.get_cur_element_mut().as_deref_mut() { + *select = true; + } + return TreeNavResult::Continue; + } + } + } + + self.cursor = None; + TreeNavResult::Exit + } + + fn nexd(&mut self) -> TreeNavResult { + if let Some(c) = self.cursor.clone() { + if let Some(ProductEditorElement::N{ t, editor, select }) = self.get_editor_element_mut(c).as_deref_mut() { + if let Some(e) = editor { + let mut ce = e.write().unwrap(); + + let depth = ce.get_cursor().tree_addr.len(); + let cur_mode = ce.get_cursor().leaf_mode; + + if depth > 0 { + return match ce.nexd() { + TreeNavResult::Exit => { + drop(ce); + *select = false; + + if c+1 < self.n_indices.len() { + self.cursor = Some(c+1); + if let Some(ProductEditorElement::N{ t, editor, select }) = self.get_cur_element_mut().as_deref_mut() { + *select = true; + } + + for _x in 1..depth { + self.dn(); + } + + self.dn(); + self.set_leaf_mode(cur_mode); + + TreeNavResult::Continue + } else { + self.cursor = None; + TreeNavResult::Exit + } + } + TreeNavResult::Continue => TreeNavResult::Continue + }; + } + } + + *select = false; + if c+1 < self.n_indices.len() { + self.cursor = Some(c+1); + if let Some(ProductEditorElement::N{ t, editor, select }) = self.get_cur_element_mut().as_deref_mut() { + *select = true; + } + + return TreeNavResult::Continue; + } + } + } + + self.cursor = None; + TreeNavResult::Exit + } + + fn up(&mut self) -> TreeNavResult { + if let Some(ProductEditorElement::N{ t, editor, select }) = self.get_cur_element_mut().as_deref_mut() { + if let Some(e) = editor { + let mut ce = e.write().unwrap(); + if ce.get_cursor().tree_addr.len() > 0 { + ce.up(); + return TreeNavResult::Continue; + } + } + *select = false; + } + + self.cursor = None; + TreeNavResult::Exit + } + + fn dn(&mut self) -> TreeNavResult { + if let Some(c) = self.cursor { + if let Some(ProductEditorElement::N{ t, editor, select }) = self.get_editor_element_mut(c).as_deref_mut() { + if let Some(e) = editor { + e.write().unwrap().dn(); + } else { + let e = make_editor(self.ctx.clone(), t, self.depth+1); + e.write().unwrap().goto_home(); + *editor = Some(e); + } + } + } else { + self.cursor = Some(0); + if let Some(ProductEditorElement::N{ t, editor, select }) = self.get_cur_element_mut().as_deref_mut() { + *select = true; + } + } + + TreeNavResult::Continue + } +} + +impl TerminalTreeEditor for ProductEditor {} +