dom.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //! webview dom
  2. use dioxus_core as dioxus;
  3. use dioxus_core::prelude::*;
  4. use dioxus_core::{
  5. diff::RealDom,
  6. serialize::DomEdits,
  7. virtual_dom::{RealDomNode, VirtualDom},
  8. };
  9. use DomEdits::*;
  10. pub struct WebviewDom<'bump> {
  11. pub edits: Vec<DomEdits<'bump>>,
  12. pub node_counter: u64,
  13. }
  14. impl WebviewDom<'_> {
  15. pub fn new() -> Self {
  16. Self {
  17. edits: Vec::new(),
  18. node_counter: 0,
  19. }
  20. }
  21. }
  22. impl<'bump> RealDom<'bump> for WebviewDom<'bump> {
  23. fn push_root(&mut self, root: dioxus_core::virtual_dom::RealDomNode) {
  24. self.edits.push(PushRoot { root: root.0 });
  25. }
  26. fn append_child(&mut self) {
  27. self.edits.push(AppendChild);
  28. }
  29. fn replace_with(&mut self) {
  30. self.edits.push(ReplaceWith);
  31. }
  32. fn remove(&mut self) {
  33. self.edits.push(Remove);
  34. }
  35. fn remove_all_children(&mut self) {
  36. self.edits.push(RemoveAllChildren);
  37. }
  38. fn create_text_node(&mut self, text: &'bump str) -> dioxus_core::virtual_dom::RealDomNode {
  39. self.node_counter += 1;
  40. let id = RealDomNode::new(self.node_counter);
  41. self.edits.push(CreateTextNode { text, id: id.0 });
  42. id
  43. }
  44. fn create_element(
  45. &mut self,
  46. tag: &'bump str,
  47. ns: Option<&'bump str>,
  48. ) -> dioxus_core::virtual_dom::RealDomNode {
  49. self.node_counter += 1;
  50. let id = RealDomNode::new(self.node_counter);
  51. match ns {
  52. Some(ns) => self.edits.push(CreateElementNs { id: id.0, ns, tag }),
  53. None => self.edits.push(CreateElement { id: id.0, tag }),
  54. }
  55. id
  56. }
  57. fn create_placeholder(&mut self) -> dioxus_core::virtual_dom::RealDomNode {
  58. self.node_counter += 1;
  59. let id = RealDomNode::new(self.node_counter);
  60. self.edits.push(CreatePlaceholder { id: id.0 });
  61. id
  62. }
  63. fn new_event_listener(
  64. &mut self,
  65. event: &'static str,
  66. scope: dioxus_core::prelude::ScopeIdx,
  67. element_id: usize,
  68. realnode: dioxus_core::virtual_dom::RealDomNode,
  69. ) {
  70. self.edits.push(NewEventListener {
  71. scope,
  72. event,
  73. idx: element_id,
  74. node: realnode.0,
  75. });
  76. }
  77. fn remove_event_listener(&mut self, event: &'static str) {
  78. self.edits.push(RemoveEventListener { event });
  79. }
  80. fn set_text(&mut self, text: &'bump str) {
  81. self.edits.push(SetText { text });
  82. }
  83. fn set_attribute(&mut self, field: &'static str, value: &'bump str, ns: Option<&'bump str>) {
  84. self.edits.push(SetAttribute { field, value, ns });
  85. }
  86. fn remove_attribute(&mut self, name: &'static str) {
  87. self.edits.push(RemoveAttribute { name });
  88. }
  89. fn raw_node_as_any_mut(&self) -> &mut dyn std::any::Any {
  90. todo!()
  91. // self.edits.push(PushRoot { root });
  92. }
  93. }