1
0

edits.rs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. use crate::query::QueryEngine;
  2. use crate::shortcut::{HotKey, ShortcutId, ShortcutRegistry, ShortcutRegistryError};
  3. use crate::AssetHandler;
  4. use crate::Config;
  5. use crate::{assets::AssetFuture, DesktopContext};
  6. use crate::{assets::AssetHandlerRegistry, DesktopService};
  7. use crate::{events::IpcMessage, webview::WebviewHandler};
  8. use dioxus_core::{BorrowedAttributeValue, Template, TemplateAttribute, TemplateNode, VirtualDom};
  9. use dioxus_core::{Mutations, ScopeState};
  10. use dioxus_html::event_bubbles;
  11. use dioxus_interpreter_js::binary_protocol::Channel;
  12. use rustc_hash::FxHashMap;
  13. use std::{
  14. cell::RefCell,
  15. rc::Rc,
  16. sync::atomic::AtomicU16,
  17. sync::Arc,
  18. sync::{atomic::Ordering, Mutex},
  19. };
  20. use wry::{RequestAsyncResponder, WebView};
  21. pub(crate) type WebviewQueue = Rc<RefCell<Vec<WebviewHandler>>>;
  22. /// This handles communication between the requests that the webview makes and the interpreter. The interpreter constantly makes long running requests to the webview to get any edits that should be made to the DOM almost like server side events.
  23. /// It will hold onto the requests until the interpreter is ready to handle them and hold onto any pending edits until a new request is made.
  24. #[derive(Default, Clone)]
  25. pub(crate) struct EditQueue {
  26. queue: Arc<Mutex<Vec<Vec<u8>>>>,
  27. responder: Arc<Mutex<Option<RequestAsyncResponder>>>,
  28. }
  29. impl EditQueue {
  30. pub fn handle_request(&self, responder: RequestAsyncResponder) {
  31. let mut queue = self.queue.lock().unwrap();
  32. if let Some(bytes) = queue.pop() {
  33. responder.respond(wry::http::Response::new(bytes));
  34. } else {
  35. *self.responder.lock().unwrap() = Some(responder);
  36. }
  37. }
  38. pub fn add_edits(&self, edits: Vec<u8>) {
  39. let mut responder = self.responder.lock().unwrap();
  40. if let Some(responder) = responder.take() {
  41. responder.respond(wry::http::Response::new(edits));
  42. } else {
  43. self.queue.lock().unwrap().push(edits);
  44. }
  45. }
  46. }
  47. pub(crate) fn apply_edits(
  48. mutations: Mutations,
  49. channel: &mut Channel,
  50. templates: &mut FxHashMap<String, u16>,
  51. max_template_count: &AtomicU16,
  52. ) -> Option<Vec<u8>> {
  53. if mutations.templates.is_empty() && mutations.edits.is_empty() {
  54. return None;
  55. }
  56. for template in mutations.templates {
  57. add_template(&template, channel, templates, max_template_count);
  58. }
  59. use dioxus_core::Mutation::*;
  60. for edit in mutations.edits {
  61. match edit {
  62. AppendChildren { id, m } => channel.append_children(id.0 as u32, m as u16),
  63. AssignId { path, id } => channel.assign_id(path, id.0 as u32),
  64. CreatePlaceholder { id } => channel.create_placeholder(id.0 as u32),
  65. CreateTextNode { value, id } => channel.create_text_node(value, id.0 as u32),
  66. HydrateText { path, value, id } => channel.hydrate_text(path, value, id.0 as u32),
  67. LoadTemplate { name, index, id } => {
  68. if let Some(tmpl_id) = templates.get(name) {
  69. channel.load_template(*tmpl_id, index as u16, id.0 as u32)
  70. }
  71. }
  72. ReplaceWith { id, m } => channel.replace_with(id.0 as u32, m as u16),
  73. ReplacePlaceholder { path, m } => channel.replace_placeholder(path, m as u16),
  74. InsertAfter { id, m } => channel.insert_after(id.0 as u32, m as u16),
  75. InsertBefore { id, m } => channel.insert_before(id.0 as u32, m as u16),
  76. SetAttribute {
  77. name,
  78. value,
  79. id,
  80. ns,
  81. } => match value {
  82. BorrowedAttributeValue::Text(txt) => {
  83. channel.set_attribute(id.0 as u32, name, txt, ns.unwrap_or_default())
  84. }
  85. BorrowedAttributeValue::Float(f) => {
  86. channel.set_attribute(id.0 as u32, name, &f.to_string(), ns.unwrap_or_default())
  87. }
  88. BorrowedAttributeValue::Int(n) => {
  89. channel.set_attribute(id.0 as u32, name, &n.to_string(), ns.unwrap_or_default())
  90. }
  91. BorrowedAttributeValue::Bool(b) => channel.set_attribute(
  92. id.0 as u32,
  93. name,
  94. if b { "true" } else { "false" },
  95. ns.unwrap_or_default(),
  96. ),
  97. BorrowedAttributeValue::None => {
  98. channel.remove_attribute(id.0 as u32, name, ns.unwrap_or_default())
  99. }
  100. _ => unreachable!(),
  101. },
  102. SetText { value, id } => channel.set_text(id.0 as u32, value),
  103. NewEventListener { name, id, .. } => {
  104. channel.new_event_listener(name, id.0 as u32, event_bubbles(name) as u8)
  105. }
  106. RemoveEventListener { name, id } => {
  107. channel.remove_event_listener(name, id.0 as u32, event_bubbles(name) as u8)
  108. }
  109. Remove { id } => channel.remove(id.0 as u32),
  110. PushRoot { id } => channel.push_root(id.0 as u32),
  111. }
  112. }
  113. let bytes: Vec<_> = channel.export_memory().collect();
  114. channel.reset();
  115. Some(bytes)
  116. }
  117. pub fn add_template(
  118. template: &Template<'static>,
  119. channel: &mut Channel,
  120. templates: &mut FxHashMap<String, u16>,
  121. max_template_count: &AtomicU16,
  122. ) {
  123. let current_max_template_count = max_template_count.load(Ordering::Relaxed);
  124. for root in template.roots.iter() {
  125. create_template_node(channel, root);
  126. templates.insert(template.name.to_owned(), current_max_template_count);
  127. }
  128. channel.add_templates(current_max_template_count, template.roots.len() as u16);
  129. max_template_count.fetch_add(1, Ordering::Relaxed);
  130. }
  131. pub fn create_template_node(channel: &mut Channel, node: &'static TemplateNode<'static>) {
  132. use TemplateNode::*;
  133. match node {
  134. Element {
  135. tag,
  136. namespace,
  137. attrs,
  138. children,
  139. ..
  140. } => {
  141. // Push the current node onto the stack
  142. match namespace {
  143. Some(ns) => channel.create_element_ns(tag, ns),
  144. None => channel.create_element(tag),
  145. }
  146. // Set attributes on the current node
  147. for attr in *attrs {
  148. if let TemplateAttribute::Static {
  149. name,
  150. value,
  151. namespace,
  152. } = attr
  153. {
  154. channel.set_top_attribute(name, value, namespace.unwrap_or_default())
  155. }
  156. }
  157. // Add each child to the stack
  158. for child in *children {
  159. create_template_node(channel, child);
  160. }
  161. // Add all children to the parent
  162. channel.append_children_to_top(children.len() as u16);
  163. }
  164. Text { text } => channel.create_raw_text(text),
  165. DynamicText { .. } => channel.create_raw_text("p"),
  166. Dynamic { .. } => channel.add_placeholder(),
  167. }
  168. }