dom.rs 3.0 KB

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