Skip to main content

afia_component/dom/
text.rs

1//! Types related to the DOM text nodes.
2
3use std::sync::Arc;
4
5use crate::ComponentImports;
6
7/// A DOM text node.
8#[derive(Clone)]
9pub struct DomText {
10    inner: Arc<DomTextInner>,
11}
12struct DomTextInner {
13    text: i64,
14    component_imports: ComponentImports,
15}
16impl DomText {
17    fn maybe_new(text_handle: i64, imports: &ComponentImports) -> Option<Self> {
18        if text_handle == 0 {
19            return None;
20        }
21        Some(DomText {
22            inner: Arc::new(DomTextInner {
23                text: text_handle,
24                component_imports: imports.clone(),
25            }),
26        })
27    }
28
29    pub(crate) fn component_imports(&self) -> &ComponentImports {
30        &self.inner.component_imports
31    }
32
33    /// Will be deleted once we stop using it.
34    pub fn temporary_way_to_get_i64(&self) -> i64 {
35        self.to_i64()
36    }
37
38    pub(crate) fn to_i64(&self) -> i64 {
39        self.inner.text
40    }
41}
42impl Drop for DomTextInner {
43    fn drop(&mut self) {
44        // TODO: Call a function to free the `self.text`
45    }
46}
47
48impl ComponentImports {
49    /// Create a new text node in the DOM.
50    pub fn create_text(&self, text: &str) -> Option<DomText> {
51        let text = unsafe {
52            afia_component_sys::create_text(self.component_imports_ptr, text.as_ptr(), text.len())
53        };
54
55        if text == 0 {
56            return None;
57        }
58
59        DomText::maybe_new(text, self)
60    }
61}