controller.rs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. use crate::desktop_context::{DesktopContext, UserWindowEvent};
  2. use dioxus_core::*;
  3. use dioxus_html::HtmlEvent;
  4. use futures_channel::mpsc::{unbounded, UnboundedSender};
  5. use futures_util::StreamExt;
  6. #[cfg(target_os = "ios")]
  7. use objc::runtime::Object;
  8. use serde_json::Value;
  9. use std::{
  10. collections::HashMap,
  11. sync::Arc,
  12. sync::{atomic::AtomicBool, Mutex},
  13. time::Duration,
  14. };
  15. use wry::{
  16. self,
  17. application::{event_loop::ControlFlow, event_loop::EventLoopProxy, window::WindowId},
  18. webview::WebView,
  19. };
  20. pub(super) struct DesktopController {
  21. pub(super) webviews: HashMap<WindowId, WebView>,
  22. pub(super) eval_sender: tokio::sync::mpsc::UnboundedSender<Value>,
  23. pub(super) pending_edits: Arc<Mutex<Vec<String>>>,
  24. pub(super) quit_app_on_close: bool,
  25. pub(super) is_ready: Arc<AtomicBool>,
  26. pub(super) proxy: EventLoopProxy<UserWindowEvent>,
  27. pub(super) event_tx: UnboundedSender<serde_json::Value>,
  28. #[cfg(target_os = "ios")]
  29. pub(super) views: Vec<*mut Object>,
  30. }
  31. impl DesktopController {
  32. // Launch the virtualdom on its own thread managed by tokio
  33. // returns the desktop state
  34. pub(super) fn new_on_tokio<P: Send + 'static>(
  35. root: Component<P>,
  36. props: P,
  37. proxy: EventLoopProxy<UserWindowEvent>,
  38. ) -> Self {
  39. let edit_queue = Arc::new(Mutex::new(Vec::new()));
  40. let (event_tx, mut event_rx) = unbounded();
  41. let proxy2 = proxy.clone();
  42. let pending_edits = edit_queue.clone();
  43. let desktop_context_proxy = proxy.clone();
  44. let (eval_sender, eval_reciever) = tokio::sync::mpsc::unbounded_channel::<Value>();
  45. std::thread::spawn(move || {
  46. // We create the runtime as multithreaded, so you can still "tokio::spawn" onto multiple threads
  47. // I'd personally not require tokio to be built-in to Dioxus-Desktop, but the DX is worse without it
  48. let runtime = tokio::runtime::Builder::new_multi_thread()
  49. .enable_all()
  50. .build()
  51. .unwrap();
  52. runtime.block_on(async move {
  53. let mut dom = VirtualDom::new_with_props(root, props)
  54. .with_root_context(DesktopContext::new(desktop_context_proxy, eval_reciever));
  55. {
  56. let edits = dom.rebuild();
  57. let mut queue = edit_queue.lock().unwrap();
  58. queue.push(serde_json::to_string(&edits).unwrap());
  59. proxy.send_event(UserWindowEvent::EditsReady).unwrap();
  60. }
  61. loop {
  62. tokio::select! {
  63. _ = dom.wait_for_work() => {}
  64. Some(json_value) = event_rx.next() => {
  65. if let Ok(value) = serde_json::from_value::<HtmlEvent>(json_value) {
  66. let HtmlEvent {
  67. name,
  68. element,
  69. bubbles,
  70. data
  71. } = value;
  72. dom.handle_event(&name, data.into_any(), element, bubbles);
  73. }
  74. }
  75. }
  76. let muts = dom
  77. .render_with_deadline(tokio::time::sleep(Duration::from_millis(16)))
  78. .await;
  79. edit_queue
  80. .lock()
  81. .unwrap()
  82. .push(serde_json::to_string(&muts).unwrap());
  83. let _ = proxy.send_event(UserWindowEvent::EditsReady);
  84. }
  85. })
  86. });
  87. Self {
  88. pending_edits,
  89. eval_sender,
  90. webviews: HashMap::new(),
  91. is_ready: Arc::new(AtomicBool::new(false)),
  92. quit_app_on_close: true,
  93. proxy: proxy2,
  94. event_tx,
  95. #[cfg(target_os = "ios")]
  96. views: vec![],
  97. }
  98. }
  99. pub(super) fn close_window(&mut self, window_id: WindowId, control_flow: &mut ControlFlow) {
  100. self.webviews.remove(&window_id);
  101. if self.webviews.is_empty() && self.quit_app_on_close {
  102. *control_flow = ControlFlow::Exit;
  103. }
  104. }
  105. pub(super) fn try_load_ready_webviews(&mut self) {
  106. if self.is_ready.load(std::sync::atomic::Ordering::Relaxed) {
  107. let mut new_queue = Vec::new();
  108. {
  109. let mut queue = self.pending_edits.lock().unwrap();
  110. std::mem::swap(&mut new_queue, &mut *queue);
  111. }
  112. let (_id, view) = self.webviews.iter_mut().next().unwrap();
  113. for edit in new_queue.drain(..) {
  114. view.evaluate_script(&format!("window.interpreter.handleEdits({})", edit))
  115. .unwrap();
  116. }
  117. }
  118. }
  119. pub(crate) fn set_template(&self, _serialized_template: String) {
  120. todo!("hot reloading currently WIP")
  121. }
  122. }