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>,
// for returning command output
pub rtx: mpsc::Sender<Option<*mut Element>>,
pub rrx: mpsc::Receiver<Option<*mut Element>>,
pub rtx: mpsc::Sender<crate::CommandResult>,
pub rrx: mpsc::Receiver<crate::CommandResult>,
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<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();
@@ -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();
}
+7 -5
View File
@@ -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::<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
@@ -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<Command>,
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<Option<*mut Element>>) };
if let Ok(Some(e)) = rx.recv() {
unsafe { &*(self.cmd_recv as *const std::sync::mpsc::Receiver<CommandResult>) };
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<CommandResult>) };
if let Ok(CommandResult::Element(e)) = rx.recv() {
Some(e)
} else {
None
}
}
pub fn render(&self) {
self.cmd_sender.send(Command::Render).unwrap();
}