cleanup product editor

This commit is contained in:
Michael Sippel 2022-06-24 15:54:05 +02:00
parent 5fef75e24f
commit d8e8f763a6
Signed by: senvas
GPG key ID: F96CF119C34B64A6
6 changed files with 188 additions and 287 deletions

View file

@ -165,7 +165,7 @@ where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static
// up
self.cursor.set(ListCursor::none());
TreeNavResult::Exit
TreeNavResult::Exit
} else {
// horizontal

View file

@ -138,6 +138,7 @@ where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static
let proj = Arc::new(RwLock::new(ListSegmentSequence {
cur_cursor: cursor_port.get_view().get(),
depth,
cursor: proj_helper.new_singleton_arg(0, cursor_port, |s: &mut Self, _msg| {
let _old_cursor = s.cur_cursor;
s.cur_cursor = s.cursor.get();

View file

@ -9,7 +9,7 @@ use {
tree_nav::{TreeNav, TerminalTreeEditor, TreeNavResult},
vec::{VecBuffer, MutableVecAccess},
list::ListCursorMode,
product::{element::ProductEditorElement},
product::{segment::ProductEditorSegment},
make_editor::make_editor
},
cgmath::Vector2,
@ -18,49 +18,33 @@ use {
};
pub struct ProductEditor {
elements: VecBuffer<ProductEditorElement>,
segments: VecBuffer<ProductEditorSegment>,
pub(super) n_indices: Vec<usize>,
el_port: OuterViewPort<dyn SequenceView<Item = ProductEditorElement>>,
el_view_port: OuterViewPort<dyn SequenceView<Item = OuterViewPort<dyn TerminalView>>>,
pub(super) ctx: Arc<RwLock<Context>>,
pub(super) ctx: Arc<RwLock<Context>>,
pub(super) cursor: Option<isize>,
pub(super) depth: usize,
}
impl ProductEditor {
pub fn new(depth: usize, ctx: Arc<RwLock<Context>>) -> Self {
let mut port = ViewPort::new();
let el_port = port.outer().to_sequence();
let el_view_port = el_port.map({
let ctx = ctx.clone();
move |e: &ProductEditorElement| { e.get_view(ctx.clone()) }
});
ProductEditor {
elements: VecBuffer::with_port(port.inner()),
el_port,
el_view_port,
segments: VecBuffer::new(),
n_indices: Vec::new(),
ctx,
cursor: None,
depth
}
}
pub fn with_t(mut self, t: &str) -> Self {
self.elements.push(ProductEditorElement::T(t.to_string(), self.depth));
self.segments.push(ProductEditorSegment::T(t.to_string(), self.depth));
self
}
pub fn with_n(mut self, n: TypeLadder) -> Self {
let elem_idx = self.elements.len();
self.elements.push(ProductEditorElement::N{
let elem_idx = self.segments.len();
self.segments.push(ProductEditorSegment::N{
t: n,
editor: None,
cur_depth: 0
@ -69,34 +53,34 @@ impl ProductEditor {
self
}
pub fn get_editor_element(&self, mut idx: isize) -> Option<ProductEditorElement> {
pub fn get_editor_element(&self, mut idx: isize) -> Option<ProductEditorSegment> {
idx = crate::modulo(idx, self.n_indices.len() as isize);
if let Some(i) = self.n_indices.get(idx as usize) {
Some(self.elements.get(*i))
Some(self.segments.get(*i))
} else {
None
}
}
pub fn get_editor_element_mut(&mut self, mut idx: isize) -> Option<MutableVecAccess<ProductEditorElement>> {
pub fn get_editor_element_mut(&mut self, mut idx: isize) -> Option<MutableVecAccess<ProductEditorSegment>> {
idx = crate::modulo(idx, self.n_indices.len() as isize);
if let Some(i) = self.n_indices.get(idx as usize) {
Some(self.elements.get_mut(*i))
Some(self.segments.get_mut(*i))
} else {
None
}
}
pub fn get_cur_element(&self) -> Option<ProductEditorElement> {
pub fn get_cur_element(&self) -> Option<ProductEditorSegment> {
self.get_editor_element(self.cursor?)
}
pub fn get_cur_element_mut(&mut self) -> Option<MutableVecAccess<ProductEditorElement>> {
pub fn get_cur_element_mut(&mut self) -> Option<MutableVecAccess<ProductEditorSegment>> {
self.get_editor_element_mut(self.cursor?)
}
pub fn get_editor(&self, idx: isize) -> Option<Arc<RwLock<dyn TerminalTreeEditor + Send + Sync>>> {
if let Some(ProductEditorElement::N{ t: _, editor, cur_depth: _ }) = self.get_editor_element(idx) {
if let Some(ProductEditorSegment::N{ t: _, editor, cur_depth: _ }) = self.get_editor_element(idx) {
editor
} else {
None
@ -116,14 +100,21 @@ impl ProductEditor {
impl TerminalEditor for ProductEditor {
fn get_term_view(&self) -> OuterViewPort<dyn TerminalView> {
self.el_view_port.to_grid_horizontal().flatten()
let ctx = self.ctx.clone();
self.segments
.get_port()
.to_sequence()
.map(move |e: &ProductEditorSegment| { e.get_view(ctx.clone()) })
.to_grid_horizontal()
.flatten()
}
fn handle_terminal_event(&mut self, event: &TerminalEvent) -> TerminalEditorResult {
if let Some(ProductEditorElement::N{ t, editor, cur_depth }) = self.get_cur_element_mut().as_deref_mut() {
if let Some(ProductEditorSegment::N{ t, editor, cur_depth }) = self.get_cur_element_mut().as_deref_mut() {
*cur_depth = self.get_cursor().tree_addr.len();
if let Some(e) = editor.clone() {
match e.clone().write().unwrap().handle_terminal_event(event) {
let mut ce = e.write().unwrap();
match ce.handle_terminal_event(event) {
TerminalEditorResult::Exit =>
match event {
TerminalEvent::Input(Event::Key(Key::Backspace)) => {
@ -132,7 +123,7 @@ impl TerminalEditor for ProductEditor {
TerminalEditorResult::Continue
}
_ => {
drop(e);
drop(ce);
match self.nexd() {
TreeNavResult::Continue => TerminalEditorResult::Continue,
TreeNavResult::Exit => TerminalEditorResult::Exit
@ -147,7 +138,7 @@ impl TerminalEditor for ProductEditor {
*editor = Some(e.clone());
e.write().unwrap().dn();
let x = e.write().unwrap().handle_terminal_event(event);
*cur_depth = self.get_cursor().tree_addr.len();
*cur_depth = self.get_cursor().tree_addr.len()+1;
x
}
} else {

View file

@ -1,6 +1,6 @@
pub mod editor;
pub mod element;
pub mod segment;
pub mod nav;
pub use editor::ProductEditor;

View file

@ -2,7 +2,8 @@ use {
crate::{
list::ListCursorMode,
tree_nav::{TreeNav, TreeNavResult, TreeCursor, TerminalTreeEditor},
product::{element::ProductEditorElement, ProductEditor}
product::{segment::ProductEditorSegment, ProductEditor},
make_editor::{make_editor}
},
cgmath::{Point2, Vector2},
std::{sync::{Arc, RwLock}, ops::{Deref, DerefMut}},
@ -26,29 +27,50 @@ impl TreeNav for ProductEditor {
}
}
} else {
TreeCursor::default()
TreeCursor::none()
}
}
fn get_cursor_warp(&self) -> TreeCursor {
if let Some(i) = self.cursor {
if let Some(e) = self.get_editor(i) {
let mut c = e.read().unwrap().get_cursor_warp();
if c.tree_addr.len() == 0 {
c.leaf_mode = ListCursorMode::Select;
}
c.tree_addr.insert(0, i as isize - self.n_indices.len() as isize);
c
} else {
TreeCursor {
leaf_mode: ListCursorMode::Select,
tree_addr: vec![ i as isize - self.n_indices.len() as isize ]
}
}
} else {
TreeCursor::none()
}
}
fn goto(&mut self, mut c: TreeCursor) -> TreeNavResult {
if let Some(mut element) = self.get_cur_element_mut() {
if let ProductEditorElement::N{ t, editor, cur_depth } = element.deref_mut() {
if let ProductEditorSegment::N{ t: _t, editor, cur_depth } = element.deref_mut() {
if let Some(e) = editor {
e.write().unwrap().goto(TreeCursor::default());
let mut e = e.write().unwrap();
e.goto(TreeCursor::none());
}
*cur_depth = self.get_cursor().tree_addr.len();
*cur_depth = 0;
}
}
if c.tree_addr.len() > 0 {
self.cursor = Some(c.clone().tree_addr.remove(0));
self.cursor = Some(crate::modulo(c.tree_addr.remove(0), self.n_indices.len() as isize));
if let Some(mut element) = self.get_cur_element_mut() {
if let ProductEditorElement::N{ t, editor, cur_depth } = element.deref_mut() {
if let ProductEditorSegment::N{ t: _t, editor, cur_depth } = element.deref_mut() {
if let Some(e) = editor {
e.write().unwrap().goto(c.clone());
}
*cur_depth = c.tree_addr.len() + 1;
*cur_depth = c.tree_addr.len();
}
}
@ -60,260 +82,147 @@ impl TreeNav for ProductEditor {
}
fn goby(&mut self, direction: Vector2<isize>) -> TreeNavResult {
TreeNavResult::Exit
}
/*
fn goto_home(&mut self) -> TreeNavResult {
if let Some(c) = self.cursor {
if let Some(ProductEditorElement::N{ t, editor, cur_depth }) = self.get_cur_element_mut().as_deref_mut() {
if let Some(e) = editor {
let mut ce = e.write().unwrap();
let mut cur = self.get_cursor();
match cur.tree_addr.len() {
0 => {
if direction.y > 0 {
self.cursor = Some(0);
let cur_mode = ce.get_cursor().leaf_mode;
let depth = ce.get_cursor().tree_addr.len();
if let Some(mut element) = self.get_cur_element_mut() {
if let ProductEditorSegment::N{ t, editor, cur_depth } = element.deref_mut() {
*cur_depth = 1;
}
}
if depth > 0 {
return match ce.goto_home() {
TreeNavResult::Exit => {
drop(ce);
self.goby(Vector2::new(direction.x, direction.y-1));
TreeNavResult::Continue
} else if direction.y < 0 {
TreeNavResult::Exit
} else {
TreeNavResult::Continue
}
}
1 => {
if direction.y > 0 {
// dn
if let Some(mut element) = self.get_cur_element_mut() {
if let ProductEditorSegment::N{ t, editor, cur_depth } = element.deref_mut() {
if let Some(e) = editor {
let mut e = e.write().unwrap();
e.goby(direction);
*cur_depth = e.get_cursor().tree_addr.len();
} else {
// create editor
let e = make_editor(self.ctx.clone(), t, self.depth+1);
*editor = Some(e.clone());
let mut e = e.write().unwrap();
e.goby(direction);
*cur_depth = e.get_cursor().tree_addr.len() + 1;
}
}
}
TreeNavResult::Continue
} else if direction.y < 0 {
// up
if let Some(mut element) = self.get_cur_element_mut() {
if let ProductEditorSegment::N{ t, editor, cur_depth } = element.deref_mut() {
*cur_depth = 0;
}
}
self.cursor = None;
TreeNavResult::Exit
} else {
// horizontal
if (cur.tree_addr[0]+direction.x >= 0) &&
(cur.tree_addr[0]+direction.x < self.n_indices.len() as isize)
{
if let Some(mut element) = self.get_cur_element_mut() {
if let ProductEditorSegment::N{ t, editor, cur_depth } = element.deref_mut() {
*cur_depth = 0;
match self.pxev() {
TreeNavResult::Exit => TreeNavResult::Exit,
TreeNavResult::Continue => {
for _x in 1..depth {
self.dn();
self.goto_end();
}
}
self.cursor = Some(cur.tree_addr[0] + direction.x);
if let Some(mut element) = self.get_cur_element_mut() {
if let ProductEditorSegment::N{ t, editor, cur_depth } = element.deref_mut() {
*cur_depth = 1;
}
}
TreeNavResult::Continue
} else {
self.cursor = None;
TreeNavResult::Exit
}
}
}
depth => {
if let Some(mut element) = self.get_cur_element_mut() {
if let ProductEditorSegment::N{ t, editor, cur_depth } = element.deref_mut() {
if let Some(e) = editor {
let mut ce = e.write().unwrap();
//\\//\\//\\//\\
// horizontal //
//\\//\\//\\//\\
match ce.goby(direction) {
TreeNavResult::Exit => {
*cur_depth = 0;
drop(ce);
drop(e);
if direction.y < 0 {
if depth <= (1-direction.y) as usize {
// up
TreeNavResult::Exit
} else {
TreeNavResult::Continue
}
self.dn();
self.set_leaf_mode(cur_mode);
} else if direction.y > 0 {
// dn
TreeNavResult::Continue
} else if direction.y == 0 {
// horizontal
if (cur.tree_addr[0]+direction.x >= 0) &&
(cur.tree_addr[0]+direction.x < self.n_indices.len() as isize)
{
if direction.x < 0 {
cur.tree_addr[0] -= 1;
for i in 1..depth {
cur.tree_addr[i] = -1;
}
} else {
cur.tree_addr[0] += 1;
for i in 1..depth {
cur.tree_addr[i] = 0;
}
}
self.goto(cur)
} else {
self.cursor = None;
TreeNavResult::Exit
}
} else {
TreeNavResult::Continue
}
}
},
TreeNavResult::Continue => TreeNavResult::Continue
};
}
}
*cur_depth = 0;
if c != 0 {
self.cursor = Some(0);
if let Some(ProductEditorElement::N{ t, editor, cur_depth }) = self.get_cur_element_mut().as_deref_mut() {
*cur_depth = self.get_cursor().tree_addr.len() + 1;
}
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, cur_depth }) = 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);
*cur_depth = 0;
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 => {
if direction.y > 0 {
*cur_depth = depth + direction.y as usize - 1;
}
}
},
TreeNavResult::Continue => {return TreeNavResult::Continue; }
}
}
}
*cur_depth = 0;
if c < self.n_indices.len()-1 {
self.cursor = Some(self.n_indices.len()-1);
if let Some(ProductEditorElement::N{ t, editor, cur_depth }) = self.get_cur_element_mut().as_deref_mut() {
*cur_depth = self.get_cursor().tree_addr.len();
}
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, cur_depth }) = 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);
*cur_depth = 0;
if c > 0 {
self.cursor = Some(c-1);
if let Some(ProductEditorElement::N{ t, editor, cur_depth }) = self.get_cur_element_mut().as_deref_mut() {
*cur_depth = self.get_cursor().tree_addr.len();
}
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
};
}
}
*cur_depth = 0;
if c > 0 {
self.cursor = Some(c-1);
if let Some(ProductEditorElement::N{ t, editor, cur_depth }) = self.get_cur_element_mut().as_deref_mut() {
*cur_depth = self.get_cursor().tree_addr.len();
}
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, cur_depth }) = 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);
*cur_depth = 0;
if c+1 < self.n_indices.len() {
self.cursor = Some(c+1);
if let Some(ProductEditorElement::N{ t, editor, cur_depth }) = self.get_cur_element_mut().as_deref_mut() {
*cur_depth = self.get_cursor().tree_addr.len();
}
for _x in 1..depth {
self.dn();
self.goto_home();
}
self.dn();
self.set_leaf_mode(cur_mode);
TreeNavResult::Continue
} else {
self.cursor = None;
TreeNavResult::Exit
}
}
TreeNavResult::Continue => TreeNavResult::Continue
};
} else {
TreeNavResult::Continue
}
} else {
TreeNavResult::Continue
}
}
*cur_depth = 0;
if c+1 < self.n_indices.len() {
self.cursor = Some(c+1);
if let Some(ProductEditorElement::N{ t, editor, cur_depth }) = self.get_cur_element_mut().as_deref_mut() {
*cur_depth = self.get_cursor().tree_addr.len();
}
return TreeNavResult::Continue;
}
}
}
self.cursor = None;
TreeNavResult::Exit
}
fn up(&mut self) -> TreeNavResult {
if let Some(ProductEditorElement::N{ t, editor, cur_depth }) = self.get_cur_element_mut().as_deref_mut() {
if let Some(e) = editor {
let mut ce = e.write().unwrap();
*cur_depth = ce.get_cursor().tree_addr.len();
if ce.get_cursor().tree_addr.len() > 0 {
ce.up();
return TreeNavResult::Continue;
}
}
*cur_depth = 0;
}
self.cursor = None;
TreeNavResult::Exit
}
fn dn(&mut self) -> TreeNavResult {
if let Some(c) = self.cursor {
if let Some(ProductEditorElement::N{ t, editor, cur_depth }) = 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().dn();
*editor = Some(e);
TreeNavResult::Continue
}
*cur_depth = self.get_cursor().tree_addr.len();
}
} else {
self.cursor = Some(0);
if let Some(ProductEditorElement::N{ t, editor, cur_depth }) = self.get_cur_element_mut().as_deref_mut() {
*cur_depth = self.get_cursor().tree_addr.len();
}
}
TreeNavResult::Continue
}
*/
}
}
impl TerminalTreeEditor for ProductEditor {}

View file

@ -13,7 +13,7 @@ use {
};
#[derive(Clone)]
pub enum ProductEditorElement {
pub enum ProductEditorSegment {
T( String, usize ),
N {
t: TypeLadder,
@ -22,10 +22,10 @@ pub enum ProductEditorElement {
}
}
impl ProductEditorElement {
impl ProductEditorSegment {
pub fn get_view(&self, ctx: Arc<RwLock<Context>>) -> OuterViewPort<dyn TerminalView> {
match self {
ProductEditorElement::T(t, depth) =>
ProductEditorSegment::T(t, depth) =>
make_label(t.as_str())
.map_item({
let depth = *depth;
@ -34,7 +34,7 @@ impl ProductEditorElement {
}
),
ProductEditorElement::N { t: _, editor: Some(e), cur_depth } =>
ProductEditorSegment::N { t: _, editor: Some(e), cur_depth } =>
e.read().unwrap()
.get_term_view()
.map_item({ let cur_depth = *cur_depth;//e.read().unwrap().get_cursor().tree_addr.len()+1;
@ -43,7 +43,7 @@ impl ProductEditorElement {
.add_style_back(bg_style_from_depth(cur_depth))
}),
ProductEditorElement::N{ t, editor: None, cur_depth } =>
ProductEditorSegment::N{ t, editor: None, cur_depth } =>
make_label(&ctx.read().unwrap().type_term_to_str(&t[0]))
.map_item({
let cur_depth = *cur_depth;