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 /* setup terminal
*/ */
let app = TTYApplication::new({ let app = TTYApplication::new({
/* event handler /* event handler
*/ */
let ctx = ctx.clone(); let ctx = ctx.clone();
let digit_edittree = rt_digit.edittree( &ctx );
let mut edittree_digit = edittree_digit.clone();
move |ev| { 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| { .map_item(|p, a| {
a.add_style_back(TerminalStyle::fg_color(((25 * p.x % 255) as u8, 200, 0))) 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()); let label_str = ctx.read().unwrap().type_term_to_str(&rt_digit.read().unwrap().get_type());
comp.push( comp.push(
nested_tty::make_label(&label_str) nested_tty::make_label(&label_str)
.map_item(|_pt,atom| atom.add_style_front(TerminalStyle::fg_color((90,90,90)))) .map_item(|_pt,atom| atom.add_style_front(TerminalStyle::fg_color((90,90,90))))
.offset(Vector2::new(1, 1)) .offset(Vector2::new(1, 1)));
);
comp.push( comp.push(rt_digit
edittree_digit.get().read().unwrap().display_view() .edittree( &ctx ).get().read().unwrap()
.offset(Vector2::new(3,2)) .display_view()
); .offset(Vector2::new(3,2)));
comp.push(
digit_u64_buffer.get_port().map( comp.push(rt_digit
|d| nested_tty::make_label(&format!("Digit value={}", d)) .descend(Context::parse(&ctx, "_2^64~machine.UInt64")).unwrap()
) .view_u64()
.map(|d| nested_tty::make_label(&format!("Digit value={}", d)))
.to_grid() .to_grid()
.flatten() .flatten()
.offset(Vector2::new(5,3)) .offset(Vector2::new(5,3)));
);
} }
/* write the changes in the view of `term_port` to the terminal /* 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.. // .. avoid cycle of projections..
rt_string.write().unwrap().detach(&ctx); 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, /* 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 * we apply a morphism that, given the List of Edit-Trees, extracts
* the value from each EditTree and shows them in a ListView. * the value from each EditTree and shows them in a ListView.
@ -117,7 +109,7 @@ async fn main() {
/* setup terminal /* setup terminal
*/ */
let app = TTYApplication::new({ let app = TTYApplication::new({
let edittree_list = edittree_list.clone(); let edittree_list = rt_string.edittree(&ctx).clone();
/* event handler /* event handler
*/ */
@ -145,7 +137,7 @@ async fn main() {
.offset(Vector2::new(1,1))); .offset(Vector2::new(1,1)));
comp.push( comp.push(
edittree_list.get() rt_string.edittree(&ctx).get()
.read().unwrap() .read().unwrap()
.display_view() .display_view()
.offset(Vector2::new(3,2))); .offset(Vector2::new(3,2)));

View file

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

View file

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

View file

@ -206,8 +206,8 @@ impl Context {
pub fn setup_edittree( pub fn setup_edittree(
&self, &self,
rt: Arc<RwLock<ReprTree>>, rt: &Arc<RwLock<ReprTree>>
depth: OuterViewPort<dyn SingletonView<Item = usize>> // depth: OuterViewPort<dyn SingletonView<Item = usize>>
) -> Option<SingletonBuffer<Arc<RwLock<EditTree>>>> { ) -> Option<SingletonBuffer<Arc<RwLock<EditTree>>>> {
if let Some(new_edittree) = if let Some(new_edittree) =
rt.descend(self.type_term_from_str("EditTree").unwrap()) 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 singleton_buffer<T: Clone + Send + Sync + 'static>(&self) -> SingletonBuffer<T>;
fn vec_buffer<T: Clone + Send + Sync + 'static>(&self) -> VecBuffer<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>> { impl ReprTreeExt for Arc<RwLock<ReprTree>> {

View file

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