edit tree: split node struct into sub structs

This commit is contained in:
Michael Sippel 2023-11-29 18:12:04 +01:00
parent bee1b43ddc
commit be3eefc3a5
Signed by: senvas
GPG key ID: F96CF119C34B64A6
8 changed files with 72 additions and 109 deletions

View file

@ -28,7 +28,7 @@ use {
fn node_make_char_view(
node: NestedNode
) -> NestedNode {
node.display
node.disp.view
.write().unwrap()
.insert_branch(ReprTree::new_leaf(
Context::parse(&node.ctx, "TerminalView"),
@ -45,15 +45,13 @@ fn node_make_char_view(
node
}
fn node_make_list_view(
fn node_make_seq_view(
mut node: NestedNode
) -> NestedNode {
eprintln!("add list display type");
node.display
node.disp.view
.write().unwrap()
.insert_branch(ReprTree::new_leaf(
Context::parse(&node.ctx, "TerminalView"),
node.data
.read()
.unwrap()
@ -65,8 +63,13 @@ fn node_make_list_view(
.flatten()
.into()
));
node
}
// nested_tty::editors::list::PTYListStyle::for_node( &mut node, ("(", ",", ")") );
fn node_make_list_edit(
mut node: NestedNode
) -> NestedNode {
nested_tty::editors::list::PTYListStyle::for_node( &mut node, ("(", ",", ")") );
nested_tty::editors::list::PTYListController::for_node( &mut node, None, None );
node
@ -78,10 +81,9 @@ fn node_make_view(
if node.data.read().unwrap().get_type() == &Context::parse(&node.ctx, "Char") {
node_make_char_view( node )
} else if node.data.read().unwrap().get_type() == &Context::parse(&node.ctx, "<Seq Char>") {
node_make_list_view( node )
// node
node_make_seq_view( node )
} else if node.data.read().unwrap().get_type() == &Context::parse(&node.ctx, "<List Char>") {
node_make_list_view( node )
node_make_list_edit( node )
} else {
eprintln!("couldnt add view");
node
@ -151,7 +153,6 @@ async fn main() {
.offset(Vector2::new(5, 0)),
);
let label = ctx.read().unwrap().type_term_to_str( &node1.get_type() );
compositor.write().unwrap()
.push(nested_tty::make_label( &label ).offset(Vector2::new(0, 2)));

View file

@ -36,10 +36,6 @@ pub enum TreeNavCmd {
pxev_dn_qnexd
}
impl TreeNavCmd {
}
pub trait TreeNav {
/* CORE
*/

View file

@ -13,33 +13,31 @@ use {
}
};
//* TODO: refactoring proposal
/*
struct NestedNodeDisplay {
#[derive(Clone)]
pub struct NestedNodeDisplay {
/// display view
pub view: Option< Arc<RwLock<ReprTree>> >,
pub view: Arc<RwLock<ReprTree>>,
/// diagnostics
pub diag: Option< OuterViewPort<dyn SequenceView<Item = diagnostics::Message>> >,
pub diag: Option< OuterViewPort<dyn SequenceView<Item = Message>> >,
/// depth
pub depth: SingletonBuffer< usize >,
pub depth: OuterViewPort<dyn SingletonView<Item = usize>>,
}
struct NestedNodeEdit {
#[derive(Clone)]
pub struct NestedNodeEdit {
/// abstract editor
pub editor: SingletonBuffer<
Option< Arc<dyn Any + Send + Sync> >
>,
>,
pub spillbuf: VecBuffer< NestedNode >,
pub spillbuf: Arc<RwLock< Vec< Arc<RwLock< NestedNode >> > >>,
/// commander & navigation
pub cmd: SingletonBuffer<
Option< Arc<RwLock<dyn ObjCommander + Send + Sync>> >
>, /// abstract data view
pub data: Arc<RwLock<ReprTree>>,
pub close_char: SingletonBuffer< Option< char > >,
@ -49,7 +47,8 @@ struct NestedNodeEdit {
>,
}
pub struct NewNestedNode {
#[derive(Clone)]
pub struct NestedNode {
/// context
pub ctx: Arc<RwLock<Context>>,
@ -62,57 +61,23 @@ pub struct NewNestedNode {
/// editor & commander objects
pub edit: NestedNodeEdit
}
*/
#[derive(Clone)]
pub struct NestedNode {
/// context
pub ctx: Arc<RwLock<Context>>,
/// abstract data view
pub data: Arc<RwLock<ReprTree>>,
/// display view
pub display: Arc<RwLock<ReprTree>>,
/// diagnostics
pub diag: Option< OuterViewPort<dyn SequenceView<Item = Message>> >,
/// depth
pub depth: OuterViewPort< dyn SingletonView<Item = usize> >,
/// abstract editor
pub editor: SingletonBuffer<
Option< Arc<dyn Any + Send + Sync> >
>,
pub spillbuf: Arc<RwLock<Vec<Arc<RwLock<NestedNode>>>>>,
/// commander & navigation
pub cmd: SingletonBuffer<
Option< Arc<RwLock<dyn ObjCommander + Send + Sync>> >
>,
pub close_char: SingletonBuffer<
Option< char >
>,
pub tree_nav: SingletonBuffer<
Option< Arc<RwLock<dyn TreeNav + Send + Sync>> >
>,
}
impl NestedNode {
pub fn new(ctx: Arc<RwLock<Context>>, data: Arc<RwLock<ReprTree>>, depth: OuterViewPort<dyn SingletonView<Item = usize>>) -> Self {
NestedNode {
disp: NestedNodeDisplay {
view: ReprTree::new_arc(Context::parse(&ctx, "Display")),
diag: None,
depth,
},
edit: NestedNodeEdit {
editor: SingletonBuffer::new(None),
spillbuf: Arc::new(RwLock::new(Vec::new())),
cmd: SingletonBuffer::new(None),
close_char: SingletonBuffer::new(None),
tree_nav: SingletonBuffer::new(None),
},
data,
display: Arc::new(RwLock::new(ReprTree::new(Context::parse(&ctx, "Display")))),
diag: None,
depth,
editor: SingletonBuffer::new(None),
spillbuf: Arc::new(RwLock::new(Vec::new())),
cmd: SingletonBuffer::new(None),
close_char: SingletonBuffer::new(None),
tree_nav: SingletonBuffer::new(None),
ctx
}
}
@ -147,29 +112,29 @@ impl NestedNode {
//\\//\\
pub fn set_editor(mut self, editor: Arc<dyn Any + Send + Sync>) -> Self {
self.editor.set(Some(editor));
self.edit.editor.set(Some(editor));
self
}
pub fn set_cmd(mut self, cmd: Arc<RwLock<dyn ObjCommander + Send + Sync>>) -> Self {
self.cmd.set(Some(cmd));
self.edit.cmd.set(Some(cmd));
self
}
pub fn set_nav(mut self, nav: Arc<RwLock<dyn TreeNav + Send + Sync>>) -> Self {
self.tree_nav.set(Some(nav));
self.edit.tree_nav.set(Some(nav));
self
}
pub fn set_diag(mut self, diag: OuterViewPort<dyn SequenceView<Item = Message>>) -> Self {
self.diag = Some(diag);
self.disp.diag = Some(diag);
self
}
//\\//\\
pub fn get_diag(&self) -> OuterViewPort<dyn SequenceView<Item = Message>> {
self.diag.clone().unwrap_or(ViewPort::new().into_outer())
self.disp.diag.clone().unwrap_or(ViewPort::new().into_outer())
}
pub fn get_data_port<'a, V: View + ?Sized + 'static>(&'a self, type_str: impl Iterator<Item = &'a str>) -> Option<OuterViewPort<V>>
@ -200,7 +165,7 @@ impl NestedNode {
*/
pub fn get_edit<T: Send + Sync + 'static>(&self) -> Option<Arc<RwLock<T>>> {
if let Some(edit) = self.editor.get() {
if let Some(edit) = self.edit.editor.get() {
if let Ok(edit) = edit.downcast::<RwLock<T>>() {
Some(edit)
} else {
@ -226,7 +191,7 @@ impl TreeType for NestedNode {
impl TreeNav for NestedNode {
fn get_cursor(&self) -> TreeCursor {
if let Some(tn) = self.tree_nav.get() {
if let Some(tn) = self.edit.tree_nav.get() {
tn.read().unwrap().get_cursor()
} else {
TreeCursor::default()
@ -234,7 +199,7 @@ impl TreeNav for NestedNode {
}
fn get_addr_view(&self) -> OuterViewPort<dyn SequenceView<Item = isize>> {
if let Some(tn) = self.tree_nav.get() {
if let Some(tn) = self.edit.tree_nav.get() {
tn.read().unwrap().get_addr_view()
} else {
OuterViewPort::default()
@ -242,7 +207,7 @@ impl TreeNav for NestedNode {
}
fn get_mode_view(&self) -> OuterViewPort<dyn SingletonView<Item = ListCursorMode>> {
if let Some(tn) = self.tree_nav.get() {
if let Some(tn) = self.edit.tree_nav.get() {
tn.read().unwrap().get_mode_view()
} else {
OuterViewPort::default()
@ -250,7 +215,7 @@ impl TreeNav for NestedNode {
}
fn get_cursor_warp(&self) -> TreeCursor {
if let Some(tn) = self.tree_nav.get() {
if let Some(tn) = self.edit.tree_nav.get() {
tn.read().unwrap().get_cursor_warp()
} else {
TreeCursor::default()
@ -258,7 +223,7 @@ impl TreeNav for NestedNode {
}
fn get_height(&self, op: &TreeHeightOp) -> usize {
if let Some(tn) = self.tree_nav.get() {
if let Some(tn) = self.edit.tree_nav.get() {
tn.read().unwrap().get_height( op )
} else {
0
@ -266,7 +231,7 @@ impl TreeNav for NestedNode {
}
fn goby(&mut self, direction: Vector2<isize>) -> TreeNavResult {
if let Some(tn) = self.tree_nav.get() {
if let Some(tn) = self.edit.tree_nav.get() {
tn.write().unwrap().goby(direction)
} else {
TreeNavResult::Exit
@ -274,7 +239,7 @@ impl TreeNav for NestedNode {
}
fn goto(&mut self, new_cursor: TreeCursor) -> TreeNavResult {
if let Some(tn) = self.tree_nav.get() {
if let Some(tn) = self.edit.tree_nav.get() {
tn.write().unwrap().goto(new_cursor)
} else {
TreeNavResult::Exit
@ -303,7 +268,7 @@ impl ObjCommander for NestedNode {
} else {
TreeNavResult::Exit
}
} else if let Some(cmd) = self.cmd.get() {
} else if let Some(cmd) = self.edit.cmd.get() {
// todo: filter out tree-nav cmds and send them to tree_nav
cmd.write().unwrap().send_cmd_obj(cmd_obj)
} else {
@ -312,6 +277,7 @@ impl ObjCommander for NestedNode {
}
}
impl Diagnostics for NestedNode {
fn get_msg_port(&self) -> OuterViewPort<dyn SequenceView<Item = Message>> {
self.get_diag()

View file

@ -131,7 +131,7 @@ impl ListEditor {
.flatten()
);
node.spillbuf = e.spillbuf.clone();
node.edit.spillbuf = e.spillbuf.clone();
node
}
@ -229,7 +229,7 @@ impl ListEditor {
/// insert a new element
pub fn insert(&mut self, item: Arc<RwLock<NestedNode>>) {
item.read().unwrap().depth.0.set_view(
item.read().unwrap().disp.depth.0.set_view(
self.depth.map(|d| d+1).get_view()
);
@ -306,7 +306,7 @@ impl ListEditor {
self.set_leaf_mode(ListCursorMode::Insert);
self.nexd();
let mut b = item.spillbuf.write().unwrap();
let mut b = item.edit.spillbuf.write().unwrap();
let mut tail_node = Context::make_node(&self.ctx, self.typ.clone(), self.depth.map(|d| d+1)).unwrap();
tail_node.goto(TreeCursor::home());
@ -364,7 +364,7 @@ impl ListEditor {
let old_cur = pxv_editor.get_cursor();
let data = cur_editor.spillbuf.read().unwrap();
let data = cur_editor.edit.spillbuf.read().unwrap();
for x in data.iter() {
pxv_editor.send_cmd_obj(
ReprTree::new_leaf(
@ -422,7 +422,7 @@ impl ListEditor {
leaf_mode: ListCursorMode::Insert
});
let data = nxd_editor.spillbuf.read().unwrap();
let data = nxd_editor.edit.spillbuf.read().unwrap();
for x in data.iter() {
cur_editor.send_cmd_obj(

View file

@ -168,8 +168,8 @@ impl TypeTermEditor {
node.goto(TreeCursor::home());
let _editor = node.editor.get();
self.close_char.set(node.close_char.get());
let _editor = node.edit.editor.get();
self.close_char.set(node.edit.close_char.get());
self.cur_node.set(node);
self.state = new_state;
}
@ -204,7 +204,7 @@ impl TypeTermEditor {
cur_node: SingletonBuffer::new(cur_node.clone()),
close_char: SingletonBuffer::new(None),
spillbuf: Arc::new(RwLock::new(Vec::new())),
depth: cur_node.depth.clone()
depth: cur_node.disp.depth.clone()
};
/* FIXME
let view = editor.cur_node
@ -215,24 +215,24 @@ impl TypeTermEditor {
.to_grid()
.flatten();
*/
let _cc = editor.cur_node.get().close_char;
let _cc = editor.cur_node.get().edit.close_char;
let editor = Arc::new(RwLock::new(editor));
let mut super_node = NestedNode::new(ctx, data, cur_node.depth)
let mut super_node = NestedNode::new(ctx, data, cur_node.disp.depth)
// .set_view(view)
.set_nav(editor.clone())
.set_cmd(editor.clone())
.set_editor(editor.clone());
editor.write().unwrap().close_char = super_node.close_char.clone();
super_node.spillbuf = editor.read().unwrap().spillbuf.clone();
editor.write().unwrap().close_char = super_node.edit.close_char.clone();
super_node.edit.spillbuf = editor.read().unwrap().spillbuf.clone();
super_node
}
fn forward_spill(&mut self) {
let node = self.cur_node.get();
let mut buf = node.spillbuf.write().unwrap();
let mut buf = node.edit.spillbuf.write().unwrap();
for n in buf.iter() {
self.spillbuf.write().unwrap().push(n.clone());
}
@ -371,7 +371,7 @@ impl TypeTermEditor {
* that has same state & child-node as current node.
*/
let old_edit_node = TypeTermEditor::new_node( self.ctx.clone(), SingletonBuffer::new(0).get_port() );
old_node.depth.0.set_view( old_edit_node.depth.map(|x|x).get_view() );
old_node.disp.depth.0.set_view( old_edit_node.disp.depth.map(|x|x).get_view() );
let old_edit_clone = old_edit_node.get_edit::<TypeTermEditor>().unwrap();
old_edit_clone.write().unwrap().set_state( self.state );

View file

@ -260,7 +260,7 @@ impl Context {
drop(dict);
self.add_morphism(morphism_pattern, Arc::new(move |node, dst_type| {
mk_editor(node.ctx.clone(), dst_type, node.depth)
mk_editor(node.ctx.clone(), dst_type, node.disp.depth)
}));
}

View file

@ -46,7 +46,7 @@ impl DisplaySegment for ListSegment {
atom
.add_style_back(bg_style_from_depth(select))
.add_style_back(TerminalStyle::bold(select==1))
.add_style_back(fg_style_from_depth(e.depth.get_view().get()))
.add_style_back(fg_style_from_depth(e.disp.depth.get_view().get()))
})
}
}
@ -92,7 +92,7 @@ impl PTYListStyle {
}
pub fn for_node(node: &mut NestedNode, style: (&str, &str, &str)) {
node.display
node.disp.view
.write().unwrap()
.insert_branch(ReprTree::new_leaf(
Context::parse(&node.ctx, "TerminalView"),
@ -152,10 +152,10 @@ impl PTYListController {
}
let editor = node.get_edit::<ListEditor>().unwrap();
let controller = Arc::new(RwLock::new(PTYListController::from_editor( editor, split_char, close_char, node.depth.clone() )));
let controller = Arc::new(RwLock::new(PTYListController::from_editor( editor, split_char, close_char, node.disp.depth.clone() )));
node.cmd.set(Some(controller.clone()));
node.close_char.set(close_char);
node.edit.cmd.set(Some(controller.clone()));
node.edit.close_char.set(close_char);
}
pub fn get_data_port(&self) -> OuterViewPort<dyn SequenceView<Item = NestedNode>> {
@ -235,7 +235,7 @@ impl PTYListController {
ListCursorMode::Select => {
if let Some(item) = e.get_item_mut() {
let res = item.write().unwrap().send_cmd_obj(cmd_obj.clone());
let child_close_char = item.read().unwrap().close_char.get();
let child_close_char = item.read().unwrap().edit.close_char.get();
match res {
TreeNavResult::Continue => TreeNavResult::Continue,

View file

@ -44,7 +44,7 @@ use std::sync::{Arc, RwLock};
impl DisplaySegment for nested::edit_tree::NestedNode {
fn display_view(&self) -> OuterViewPort<dyn TerminalView> {
if let Some( tv_repr ) = self.display
if let Some( tv_repr ) = self.disp.view
.read().unwrap()
.descend( Context::parse(&self.ctx, "TerminalView") )
{