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
+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();
}