call edittree_hook in morphisms already, add edittree() getter in ReprTreeExt to further simplify application code

This commit is contained in:
Michael Sippel 2024-08-09 23:00:31 +02:00
parent 7762fa4b12
commit 0bcfd7a65a
Signed by: senvas
GPG key ID: F96CF119C34B64A6
9 changed files with 49 additions and 73 deletions

View file

@ -84,30 +84,15 @@ async fn main() {
}
);
/* setup TTY-Display for DigitEditor
* and call the hook defined above with `set_edittree_hook()`
*
*/
let edittree_digit = ctx.read().unwrap()
.setup_edittree(
rt_digit.clone(),
SingletonBuffer::new(0).get_port()
).unwrap();
let mut digit_u64_buffer = rt_digit
.descend(Context::parse(&ctx, "_2^64~machine.UInt64")).unwrap()
.singleton_buffer::<u64>();
/* setup terminal
*/
let app = TTYApplication::new({
/* event handler
*/
let ctx = ctx.clone();
let mut edittree_digit = edittree_digit.clone();
let digit_edittree = rt_digit.edittree( &ctx );
move |ev| {
edittree_digit.get().write().unwrap().send_cmd_obj(ev.to_repr_tree(&ctx));
digit_edittree.get().write().unwrap().send_cmd_obj(ev.to_repr_tree(&ctx));
}
});
@ -124,27 +109,26 @@ async fn main() {
.map_item(|p, a| {
a.add_style_back(TerminalStyle::fg_color(((25 * p.x % 255) as u8, 200, 0)))
})
.offset(Vector2::new(5, 0)),
);
.offset(Vector2::new(5, 0)));
let label_str = ctx.read().unwrap().type_term_to_str(&rt_digit.read().unwrap().get_type());
comp.push(
nested_tty::make_label(&label_str)
.map_item(|_pt,atom| atom.add_style_front(TerminalStyle::fg_color((90,90,90))))
.offset(Vector2::new(1, 1))
);
comp.push(
edittree_digit.get().read().unwrap().display_view()
.offset(Vector2::new(3,2))
);
comp.push(
digit_u64_buffer.get_port().map(
|d| nested_tty::make_label(&format!("Digit value={}", d))
)
.offset(Vector2::new(1, 1)));
comp.push(rt_digit
.edittree( &ctx ).get().read().unwrap()
.display_view()
.offset(Vector2::new(3,2)));
comp.push(rt_digit
.descend(Context::parse(&ctx, "_2^64~machine.UInt64")).unwrap()
.view_u64()
.map(|d| nested_tty::make_label(&format!("Digit value={}", d)))
.to_grid()
.flatten()
.offset(Vector2::new(5,3))
);
.offset(Vector2::new(5,3)));
}
/* write the changes in the view of `term_port` to the terminal

View file

@ -61,14 +61,6 @@ async fn main() {
// .. avoid cycle of projections..
rt_string.write().unwrap().detach(&ctx);
/* Setup the Editor-View for this ReprTree
*/
let edittree_list = ctx.read().unwrap()
.setup_edittree(
rt_string.clone(),
SingletonBuffer::new(0).get_port()
).unwrap();
/* In order to get access to the values that are modified by the Editor,
* we apply a morphism that, given the List of Edit-Trees, extracts
* the value from each EditTree and shows them in a ListView.
@ -117,7 +109,7 @@ async fn main() {
/* setup terminal
*/
let app = TTYApplication::new({
let edittree_list = edittree_list.clone();
let edittree_list = rt_string.edittree(&ctx).clone();
/* event handler
*/
@ -145,7 +137,7 @@ async fn main() {
.offset(Vector2::new(1,1)));
comp.push(
edittree_list.get()
rt_string.edittree(&ctx).get()
.read().unwrap()
.display_view()
.offset(Vector2::new(3,2)));

View file

@ -81,27 +81,21 @@ async fn main() {
}
);
let edittree_hex_be_list = ctx.read().unwrap()
.setup_edittree(
rt_int.descend(Context::parse(&ctx,"
let edittree_hex_be_list =
rt_int
.descend(Context::parse(&ctx, "
<PosInt 16 BigEndian>
~ <Seq <Digit 16>>
~ <List <Digit 16>>
~ <List Char>
")).expect("descend"),
SingletonBuffer::new(0).get_port()
).unwrap().get();
~ <Seq~List <Digit 16>~Char>
")).unwrap()
.edittree( &ctx );
let edittree_dec_be_list = ctx.read().unwrap()
.setup_edittree(
rt_int.descend(Context::parse(&ctx,"
let edittree_dec_be_list =
rt_int
.descend(Context::parse(&ctx, "
<PosInt 10 BigEndian>
~ <Seq <Digit 10>>
~ <List <Digit 10>>
~ <List Char>
")).expect("descend"),
SingletonBuffer::new(0).get_port()
).unwrap().get();
~ <Seq~List <Digit 10>~Char>
")).unwrap()
.edittree( &ctx );
let hex_digits_view = rt_int.descend(Context::parse(&ctx, "
<PosInt 16 LittleEndian>
@ -130,15 +124,15 @@ async fn main() {
/* list of both editors
*/
let mut list_editor = nested::editors::list::ListEditor::new(ctx.clone(), Context::parse(&ctx, "<Seq Char>"));
list_editor.data.push( edittree_hex_be_list.clone() );
list_editor.data.push( edittree_dec_be_list.clone() );
list_editor.data.push( edittree_hex_be_list.get() );
list_editor.data.push( edittree_dec_be_list.get() );
let mut edittree = list_editor.into_node(SingletonBuffer::new(0).get_port());
/* cursors are a bit screwed initially so fix them up
* TODO: how to fix this generally?
*/
edittree_hex_be_list.write().unwrap().goto(TreeCursor::none());
edittree_dec_be_list.write().unwrap().goto(TreeCursor::none());
edittree_hex_be_list.get().write().unwrap().goto(TreeCursor::none());
edittree_dec_be_list.get().write().unwrap().goto(TreeCursor::none());
edittree.goto(TreeCursor{
leaf_mode: nested::editors::list::ListCursorMode::Insert,
tree_addr: vec![0,0]

View file

@ -51,6 +51,8 @@ pub fn init_ctx( ctx: Arc<RwLock<Context>> ) {
SingletonBuffer::new(Arc::new(RwLock::new(edittree)))
)
);
ctx.read().unwrap().setup_edittree( src_rt );
}
}
);

View file

@ -54,6 +54,8 @@ pub fn init_ctx(ctx: Arc<RwLock<Context>>) {
SingletonBuffer::new(Arc::new(RwLock::new(edittree_list)))
)
);
ctx.read().unwrap().setup_edittree( &src_rt );
} else {
eprintln!("no item type");
}

View file

@ -321,8 +321,8 @@ impl ListEditor {
let rt = ReprTree::new_arc(self.typ.clone());
let mut et = self.ctx.read().unwrap()
.setup_edittree(
rt,
self.depth.map(|d| d+1)
&rt
// , self.depth.map(|d| d+1)
);
if let Some(edittree) = et.as_mut(){

View file

@ -206,8 +206,8 @@ impl Context {
pub fn setup_edittree(
&self,
rt: Arc<RwLock<ReprTree>>,
depth: OuterViewPort<dyn SingletonView<Item = usize>>
rt: &Arc<RwLock<ReprTree>>
// depth: OuterViewPort<dyn SingletonView<Item = usize>>
) -> Option<SingletonBuffer<Arc<RwLock<EditTree>>>> {
if let Some(new_edittree) =
rt.descend(self.type_term_from_str("EditTree").unwrap())

View file

@ -58,6 +58,11 @@ pub trait ReprTreeExt {
fn singleton_buffer<T: Clone + Send + Sync + 'static>(&self) -> SingletonBuffer<T>;
fn vec_buffer<T: Clone + Send + Sync + 'static>(&self) -> VecBuffer<T>;
fn edittree(&self, ctx: &Arc<RwLock<Context>>) -> SingletonBuffer< Arc<RwLock<crate::edit_tree::EditTree>> > {
self.descend(Context::parse(&ctx, "EditTree")).unwrap()
.singleton_buffer()
}
}
impl ReprTreeExt for Arc<RwLock<ReprTree>> {

View file

@ -236,10 +236,7 @@ impl PTYListController {
}
);
let new_edittree = ctx.setup_edittree(
rt,
self.depth.map(|d| d+1)
);
let new_edittree = ctx.setup_edittree( &rt );
if let Some(new_edittree) = new_edittree {