some small changes

This commit is contained in:
2024-11-22 17:56:12 +01:00
parent 129725dce2
commit a804427c7e
3 changed files with 38 additions and 12 deletions
+7 -5
View File
@@ -17,8 +17,8 @@ pub struct LArgs<'a> {
pub srx: mpsc::Receiver<Command>, pub srx: mpsc::Receiver<Command>,
// for returning command output // for returning command output
pub rtx: mpsc::Sender<Option<*mut Element>>, pub rtx: mpsc::Sender<crate::CommandResult>,
pub rrx: mpsc::Receiver<Option<*mut Element>>, pub rrx: mpsc::Receiver<crate::CommandResult>,
pub running: bool, 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 args = unsafe { &mut *(ptr as *mut LArgs) };
let document = Document { let document = Document {
cmd_sender: args.stx.clone(), cmd_sender: args.stx.clone(),
cmd_recv: &args.rrx as *const mpsc::Receiver<Option<*mut Element>> as *const c_void, cmd_recv: &args.rrx as *const mpsc::Receiver<crate::CommandResult> as *const c_void,
}; };
let (width, height) = get_term_size(); 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) => { Command::GetElementById(id) => {
args.rtx args.rtx
.send(if let Some(e) = args.element.get_element_by_id(&id) { .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 { } else {
None crate::CommandResult::None
}) })
.unwrap(); .unwrap();
} }
+7 -5
View File
@@ -1,9 +1,11 @@
use crate::prelude::*; use crate::prelude::*;
pub fn event_logger() -> Element { pub fn event_logger() -> Element {
rsx_elem! { rsx! {
text { on_event: Handler::new(|txt: &mut Text, e, _| { id: "root",
txt.children.set_text(&format!("current event: {e:?}")); text { on_event: Handler::new(|_, _, document| {
}), "current event: None" } let root = document.get_element_by_id::<Div>("root").unwrap();
root.children.add_child(rsx_elem!( text { "Ok" } ));
}), id: "txt", "current event: None" }
} }
} }
+24 -2
View File
@@ -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 /// Converts a `Element` into the struct
@@ -221,6 +226,11 @@ pub enum Command {
GetElementById(String), GetElementById(String),
} }
pub enum CommandResult {
Element(*mut Element),
None
}
pub struct Document { pub struct Document {
cmd_sender: std::sync::mpsc::Sender<Command>, cmd_sender: std::sync::mpsc::Sender<Command>,
cmd_recv: *const std::ffi::c_void, cmd_recv: *const std::ffi::c_void,
@@ -235,13 +245,25 @@ impl Document {
.send(Command::GetElementById(id.to_string())) .send(Command::GetElementById(id.to_string()))
.unwrap(); .unwrap();
let rx = let rx =
unsafe { &*(self.cmd_recv as *const std::sync::mpsc::Receiver<Option<*mut Element>>) }; unsafe { &*(self.cmd_recv as *const std::sync::mpsc::Receiver<CommandResult>) };
if let Ok(Some(e)) = rx.recv() { if let Ok(CommandResult::Element(e)) = rx.recv() {
Some(convert(unsafe { &mut *e })) Some(convert(unsafe { &mut *e }))
} else { } else {
None 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<CommandResult>) };
if let Ok(CommandResult::Element(e)) = rx.recv() {
Some(e)
} else {
None
}
}
pub fn render(&self) { pub fn render(&self) {
self.cmd_sender.send(Command::Render).unwrap(); self.cmd_sender.send(Command::Render).unwrap();
} }