From a804427c7e1d8bf362cd79106380f08bd6532064 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Fri, 22 Nov 2024 17:56:12 +0100 Subject: [PATCH] some small changes --- src/app.rs | 12 +++++++----- src/examples.rs | 12 +++++++----- src/lib.rs | 26 ++++++++++++++++++++++++-- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/src/app.rs b/src/app.rs index efbeb82..5ef1b60 100644 --- a/src/app.rs +++ b/src/app.rs @@ -17,8 +17,8 @@ pub struct LArgs<'a> { pub srx: mpsc::Receiver, // for returning command output - pub rtx: mpsc::Sender>, - pub rrx: mpsc::Receiver>, + pub rtx: mpsc::Sender, + pub rrx: mpsc::Receiver, pub running: bool, } @@ -77,7 +77,7 @@ extern "C" fn event_loop(ptr: *mut c_void) -> *const c_void { let args = unsafe { &mut *(ptr as *mut LArgs) }; let document = Document { cmd_sender: args.stx.clone(), - cmd_recv: &args.rrx as *const mpsc::Receiver> as *const c_void, + cmd_recv: &args.rrx as *const mpsc::Receiver as *const c_void, }; let (width, height) = get_term_size(); @@ -115,9 +115,11 @@ extern "C" fn cmd_loop(ptr: *mut c_void) -> *const c_void { Command::GetElementById(id) => { args.rtx .send(if let Some(e) = args.element.get_element_by_id(&id) { - Some(e as *mut Element) + crate::CommandResult::Element(e as *mut Element) + } else if args.element.get_data().2 == id { + crate::CommandResult::Element(args.element as *mut Element) } else { - None + crate::CommandResult::None }) .unwrap(); } diff --git a/src/examples.rs b/src/examples.rs index c8225e4..5d4ab98 100644 --- a/src/examples.rs +++ b/src/examples.rs @@ -1,9 +1,11 @@ use crate::prelude::*; pub fn event_logger() -> Element { - rsx_elem! { - text { on_event: Handler::new(|txt: &mut Text, e, _| { - txt.children.set_text(&format!("current event: {e:?}")); - }), "current event: None" } + rsx! { + id: "root", + text { on_event: Handler::new(|_, _, document| { + let root = document.get_element_by_id::
("root").unwrap(); + root.children.add_child(rsx_elem!( text { "Ok" } )); + }), id: "txt", "current event: None" } } -} \ No newline at end of file +} diff --git a/src/lib.rs b/src/lib.rs index abc6784..51926fc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -208,6 +208,11 @@ impl Children { } } } + pub fn add_child(&mut self, element: Element) { + if let Children::Children(children, _) = self { + children.push(element); + } + } } /// Converts a `Element` into the struct @@ -221,6 +226,11 @@ pub enum Command { GetElementById(String), } +pub enum CommandResult { + Element(*mut Element), + None +} + pub struct Document { cmd_sender: std::sync::mpsc::Sender, cmd_recv: *const std::ffi::c_void, @@ -235,13 +245,25 @@ impl Document { .send(Command::GetElementById(id.to_string())) .unwrap(); let rx = - unsafe { &*(self.cmd_recv as *const std::sync::mpsc::Receiver>) }; - if let Ok(Some(e)) = rx.recv() { + unsafe { &*(self.cmd_recv as *const std::sync::mpsc::Receiver) }; + if let Ok(CommandResult::Element(e)) = rx.recv() { Some(convert(unsafe { &mut *e })) } else { None } } + pub fn get_element_by_id_raw(&self, id: &str) -> Option<*mut Element> { + self.cmd_sender + .send(Command::GetElementById(id.to_string())) + .unwrap(); + let rx = + unsafe { &*(self.cmd_recv as *const std::sync::mpsc::Receiver) }; + if let Ok(CommandResult::Element(e)) = rx.recv() { + Some(e) + } else { + None + } + } pub fn render(&self) { self.cmd_sender.send(Command::Render).unwrap(); }