Feat: move webview logic into library

This commit is contained in:
Jonathan Kelley committed 2021-02-25 18:44:00 -05:00
1 parent 18a7a1f9c4
commit 32b45e5ba1
10 files changed
+249 -514

No files matched your search

+20 -270
View File
@@ -1,6 +1,6 @@
use dioxus_core::changelist::Edit;
use fxhash::FxHashMap;
use log::{debug};
use log::debug;
use wasm_bindgen::{closure::Closure, JsCast};
use web_sys::{window, Document, Element, Event, Node};
@@ -8,13 +8,13 @@ use web_sys::{window, Document, Element, Event, Node};
pub struct CacheId(u32);
#[derive(Debug)]
pub struct PatchMachine {
pub(crate) struct PatchMachine {
container: Element,
pub stack: Stack,
pub(crate) stack: Stack,
temporaries: FxHashMap<u32, Node>,
templates: FxHashMap<CacheId, Node>,
callback: Option<Closure<dyn FnMut(&Event)>>,
document: Document,
// templates: FxHashMap<CacheId, Node>,
}
#[derive(Debug, Default)]
@@ -65,10 +65,10 @@ impl PatchMachine {
.expect("must have access to the Document");
Self {
// templates: Default::default(),
container,
stack: Stack::with_capacity(20),
temporaries: Default::default(),
templates: Default::default(),
callback: None,
document,
}
@@ -77,7 +77,7 @@ impl PatchMachine {
pub fn unmount(&mut self) {
self.stack.clear();
self.temporaries.clear();
self.templates.clear();
// self.templates.clear();
}
pub fn start(&mut self) {
@@ -92,7 +92,8 @@ impl PatchMachine {
}
pub fn get_template(&self, id: CacheId) -> Option<&Node> {
self.templates.get(&id)
todo!()
// self.templates.get(&id)
}
pub fn init_events_trampoline(&mut self, _trampoline: ()) {
@@ -379,273 +380,22 @@ impl PatchMachine {
}
}
// // 0
// pub fn set_text(&mut self, text: &str) {
// self.stack.top().set_text_content(Some(text));
// // 24
// pub fn save_template(&mut self, id: CacheId) {
// let template = self.stack.top();
// let t = template.clone_node_with_deep(true).unwrap();
// // self.templates.insert(id, t);
// }
// // 1
// pub fn remove_self_and_next_siblings(&mut self) {
// let node = self.stack.pop();
// let mut sibling = node.next_sibling();
// while let Some(inner) = sibling {
// let temp = inner.next_sibling();
// if let Some(sibling) = inner.dyn_ref::<Element>() {
// sibling.remove();
// }
// sibling = temp;
// }
// if let Some(node) = node.dyn_ref::<Element>() {
// node.remove();
// }
// }
// // 2
// pub fn replace_with(&mut self) {
// let new_node = self.stack.pop();
// let old_node = self.stack.pop();
// if old_node.has_type::<Element>() {
// old_node
// .dyn_ref::<Element>()
// .unwrap()
// .replace_with_with_node_1(&new_node)
// .unwrap();
// } else if old_node.has_type::<web_sys::CharacterData>() {
// old_node
// .dyn_ref::<web_sys::CharacterData>()
// .unwrap()
// .replace_with_with_node_1(&new_node)
// .unwrap();
// } else if old_node.has_type::<web_sys::DocumentType>() {
// old_node
// .dyn_ref::<web_sys::DocumentType>()
// .unwrap()
// .replace_with_with_node_1(&new_node)
// .unwrap();
// } else {
// panic!("Cannot replace node: {:?}", old_node);
// }
// self.stack.push(new_node);
// }
// // 3
// pub fn set_attribute(&mut self, name: &str, value: &str) {
// let node = self.stack.top();
// if let Some(node) = node.dyn_ref::<Element>() {
// node.set_attribute(name, value).unwrap();
// // Some attributes are "volatile" and don't work through `setAttribute`.
// // TODO:
// // if name == "value" {
// // node.set_value(value);
// // }
// // if name == "checked" {
// // node.set_checked(true);
// // }
// // if name == "selected" {
// // node.set_selected(true);
// // }
// }
// }
// // 4
// pub fn remove_attribute(&mut self, name: &str) {
// let node = self.stack.top();
// if let Some(node) = node.dyn_ref::<Element>() {
// node.remove_attribute(name).unwrap();
// // Some attributes are "volatile" and don't work through `removeAttribute`.
// // TODO:
// // if name == "value" {
// // node.set_value("");
// // }
// // if name == "checked" {
// // node.set_checked(false);
// // }
// // if name == "selected" {
// // node.set_selected(false);
// // }
// }
// }
// // 5
// pub fn push_reverse_child(&mut self, n: u32) {
// let parent = self.stack.top();
// let children = parent.child_nodes();
// let child = children.get(children.length() - n - 1).unwrap();
// self.stack.push(child);
// }
// // 6
// pub fn pop_push_child(&mut self, n: u32) {
// self.stack.pop();
// let parent = self.stack.top();
// let children = parent.child_nodes();
// let child = children.get(n).unwrap();
// self.stack.push(child);
// }
// // 7
// pub fn pop(&mut self) {
// self.stack.pop();
// }
// // 8
// pub fn append_child(&mut self) {
// let child = self.stack.pop();
// self.stack.top().append_child(&child).unwrap();
// }
// // 9
// pub fn create_text_node(&mut self, text: &str) {
// self.stack.push(
// self.document
// .create_text_node(text)
// .dyn_into::<Node>()
// .unwrap(),
// );
// }
// // 10
// pub fn create_element(&mut self, tag_name: &str) {
// let el = self
// .document
// .create_element(tag_name)
// .unwrap()
// .dyn_into::<Node>()
// .unwrap();
// self.stack.push(el);
// }
// // 11
// pub fn new_event_listener(&mut self, event_type: &str, a: u32, b: u32) {
// let el = self.stack.top();
// let el = el
// .dyn_ref::<Element>()
// .expect(&format!("not an element: {:?}", el));
// el.add_event_listener_with_callback(
// event_type,
// self.callback.as_ref().unwrap().as_ref().unchecked_ref(),
// )
// .unwrap();
// debug!("adding attributes: {}, {}", a, b);
// el.set_attribute(&format!("dodrio-a-{}", event_type), &a.to_string())
// .unwrap();
// el.set_attribute(&format!("dodrio-b-{}", event_type), &b.to_string())
// .unwrap();
// }
// // 12
// pub fn update_event_listener(&mut self, event_type: &str, a: u32, b: u32) {
// if let Some(el) = self.stack.top().dyn_ref::<Element>() {
// el.set_attribute(&format!("dodrio-a-{}", event_type), &a.to_string())
// .unwrap();
// el.set_attribute(&format!("dodrio-b-{}", event_type), &b.to_string())
// .unwrap();
// }
// }
// // 13
// pub fn remove_event_listener(&mut self, event_type: &str) {
// if let Some(el) = self.stack.top().dyn_ref::<Element>() {
// el.remove_event_listener_with_callback(
// event_type,
// self.callback.as_ref().unwrap().as_ref().unchecked_ref(),
// )
// .unwrap();
// }
// }
// // 16
// pub fn create_element_ns(&mut self, tag_name: &str, ns: &str) {
// let el = self
// .document
// .create_element_ns(Some(ns), tag_name)
// .unwrap()
// .dyn_into::<Node>()
// .unwrap();
// self.stack.push(el);
// }
// // 17
// pub fn save_children_to_temporaries(&mut self, mut temp: u32, start: u32, end: u32) {
// let parent = self.stack.top();
// let children = parent.child_nodes();
// for i in start..end {
// self.temporaries.insert(temp, children.get(i).unwrap());
// temp += 1;
// }
// }
// // 18
// pub fn push_child(&mut self, n: u32) {
// let parent = self.stack.top();
// let child = parent.child_nodes().get(n).unwrap();
// self.stack.push(child);
// }
// // 19
// pub fn push_temporary(&mut self, temp: u32) {
// let t = self.temporaries.get(&temp).unwrap().clone();
// // 25
// pub fn push_template(&mut self, id: CacheId) {
// let template = self.get_template(id).unwrap();
// let t = template.clone_node_with_deep(true).unwrap();
// self.stack.push(t);
// }
// // 20
// pub fn insert_before(&mut self) {
// let before = self.stack.pop();
// let after = self.stack.pop();
// after
// .parent_node()
// .unwrap()
// .insert_before(&before, Some(&after))
// .unwrap();
// self.stack.push(before);
// pub fn has_template(&self, id: CacheId) -> bool {
// todo!()
// // self.templates.contains_key(&id)
// }
// // 21
// pub fn pop_push_reverse_child(&mut self, n: u32) {
// self.stack.pop();
// let parent = self.stack.top();
// let children = parent.child_nodes();
// let child = children.get(children.length() - n - 1).unwrap();
// self.stack.push(child);
// }
// // 22
// pub fn remove_child(&mut self, n: u32) {
// let parent = self.stack.top();
// if let Some(child) = parent.child_nodes().get(n).unwrap().dyn_ref::<Element>() {
// child.remove();
// }
// }
// // 23
// pub fn set_class(&mut self, class_name: &str) {
// if let Some(el) = self.stack.top().dyn_ref::<Element>() {
// el.set_class_name(class_name);
// }
// }
// 24
pub fn save_template(&mut self, id: CacheId) {
let template = self.stack.top();
let t = template.clone_node_with_deep(true).unwrap();
self.templates.insert(id, t);
}
// 25
pub fn push_template(&mut self, id: CacheId) {
let template = self.get_template(id).unwrap();
let t = template.clone_node_with_deep(true).unwrap();
self.stack.push(t);
}
pub fn has_template(&self, id: CacheId) -> bool {
self.templates.contains_key(&id)
}
}
+2 -8
View File
@@ -19,9 +19,6 @@ pub mod interpreter;
///
pub struct WebsysRenderer {
internal_dom: VirtualDom,
// this should be a component index
_event_map: FxHashMap<(u32, u32), u32>,
}
impl WebsysRenderer {
@@ -58,10 +55,7 @@ impl WebsysRenderer {
/// Create a new text renderer from an existing Virtual DOM.
pub fn from_vdom(dom: VirtualDom) -> Self {
// todo: initialize the event registry properly
Self {
internal_dom: dom,
_event_map: FxHashMap::default(),
}
Self { internal_dom: dom }
}
pub async fn run(&mut self) -> dioxus_core::error::Result<()> {
@@ -72,7 +66,7 @@ impl WebsysRenderer {
let root_node = body_element.first_child().unwrap();
patch_machine.stack.push(root_node);
// todo: initialize the event registry properly on the root element
// todo: initialize the event registry properly on the root
self.internal_dom
.rebuild()?