lib.rs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. //! Dioxus Desktop Renderer
  2. //!
  3. //! Render the Dioxus VirtualDom using the platform's native WebView implementation.
  4. //!
  5. use std::borrow::BorrowMut;
  6. use std::cell::{Cell, RefCell};
  7. use std::collections::HashMap;
  8. use std::ops::{Deref, DerefMut};
  9. use std::rc::Rc;
  10. use std::sync::atomic::AtomicBool;
  11. use std::sync::mpsc::channel;
  12. use std::sync::{Arc, RwLock};
  13. use cfg::DesktopConfig;
  14. use dioxus_core::*;
  15. use serde::{Deserialize, Serialize};
  16. pub use wry;
  17. use wry::application::accelerator::{Accelerator, SysMods};
  18. use wry::application::event::{ElementState, Event, StartCause, WindowEvent};
  19. use wry::application::event_loop::{self, ControlFlow, EventLoop};
  20. use wry::application::keyboard::{Key, KeyCode, ModifiersState};
  21. use wry::application::menu::{MenuBar, MenuItem, MenuItemAttributes};
  22. use wry::application::window::Fullscreen;
  23. use wry::webview::{WebView, WebViewBuilder};
  24. use wry::{
  25. application::menu,
  26. application::window::{Window, WindowBuilder},
  27. webview::{RpcRequest, RpcResponse},
  28. };
  29. mod cfg;
  30. mod desktop_context;
  31. mod dom;
  32. mod escape;
  33. mod events;
  34. static HTML_CONTENT: &'static str = include_str!("./index.html");
  35. pub fn launch(
  36. root: FC<()>,
  37. config_builder: impl for<'a, 'b> FnOnce(&'b mut DesktopConfig<'a>) -> &'b mut DesktopConfig<'a>,
  38. ) {
  39. launch_with_props(root, (), config_builder)
  40. }
  41. pub fn launch_with_props<P: Properties + 'static + Send + Sync>(
  42. root: FC<P>,
  43. props: P,
  44. builder: impl for<'a, 'b> FnOnce(&'b mut DesktopConfig<'a>) -> &'b mut DesktopConfig<'a>,
  45. ) {
  46. run(root, props, builder)
  47. }
  48. #[derive(Serialize)]
  49. enum RpcEvent<'a> {
  50. Initialize { edits: Vec<DomEdit<'a>> },
  51. }
  52. #[derive(Debug)]
  53. enum BridgeEvent {
  54. Initialize(serde_json::Value),
  55. Update(serde_json::Value),
  56. }
  57. #[derive(Serialize)]
  58. struct Response<'a> {
  59. pre_rendered: Option<String>,
  60. edits: Vec<DomEdit<'a>>,
  61. }
  62. pub fn run<T: 'static + Send + Sync>(
  63. root: FC<T>,
  64. props: T,
  65. user_builder: impl for<'a, 'b> FnOnce(&'b mut DesktopConfig<'a>) -> &'b mut DesktopConfig<'a>,
  66. ) {
  67. // Generate the config
  68. let mut cfg = DesktopConfig::new();
  69. user_builder(&mut cfg);
  70. let DesktopConfig {
  71. window,
  72. manual_edits,
  73. pre_rendered,
  74. ..
  75. } = cfg;
  76. // All of our webview windows are stored in a way that we can look them up later
  77. // The "DesktopContext" will provide functionality for spawning these windows
  78. let mut webviews = HashMap::new();
  79. let event_loop = EventLoop::new();
  80. let props_shared = Cell::new(Some(props));
  81. // create local modifier state
  82. let mut modifiers = ModifiersState::default();
  83. let quit_hotkey = Accelerator::new(SysMods::Cmd, KeyCode::KeyQ);
  84. event_loop.run(move |event, event_loop, control_flow| {
  85. *control_flow = ControlFlow::Wait;
  86. match event {
  87. Event::NewEvents(StartCause::Init) => {
  88. // create main menubar menu
  89. let mut menu_bar_menu = MenuBar::new();
  90. // create `first_menu`
  91. let mut first_menu = MenuBar::new();
  92. first_menu.add_native_item(MenuItem::About("Todos".to_string()));
  93. first_menu.add_native_item(MenuItem::Services);
  94. first_menu.add_native_item(MenuItem::Separator);
  95. first_menu.add_native_item(MenuItem::Hide);
  96. first_menu.add_native_item(MenuItem::HideOthers);
  97. first_menu.add_native_item(MenuItem::ShowAll);
  98. first_menu.add_native_item(MenuItem::Quit);
  99. first_menu.add_native_item(MenuItem::CloseWindow);
  100. // create second menu
  101. let mut second_menu = MenuBar::new();
  102. // second_menu.add_submenu("Sub menu", true, my_sub_menu);
  103. second_menu.add_native_item(MenuItem::Copy);
  104. second_menu.add_native_item(MenuItem::Paste);
  105. second_menu.add_native_item(MenuItem::SelectAll);
  106. menu_bar_menu.add_submenu("First menu", true, first_menu);
  107. menu_bar_menu.add_submenu("Second menu", true, second_menu);
  108. let window = WindowBuilder::new()
  109. .with_maximized(true)
  110. .with_menu(menu_bar_menu)
  111. .with_title("Dioxus App")
  112. .build(event_loop)
  113. .unwrap();
  114. let window_id = window.id();
  115. let (event_tx, event_rx) = tokio::sync::mpsc::unbounded_channel();
  116. let my_props = props_shared.take().unwrap();
  117. let sender = launch_vdom_with_tokio(root, my_props, event_tx);
  118. let locked_receiver = Rc::new(RefCell::new(event_rx));
  119. let webview = WebViewBuilder::new(window)
  120. .unwrap()
  121. .with_url("wry://index.html")
  122. .unwrap()
  123. .with_rpc_handler(move |_window: &Window, mut req: RpcRequest| {
  124. let mut rx = (*locked_receiver).borrow_mut();
  125. match req.method.as_str() {
  126. "initiate" => {
  127. if let Ok(BridgeEvent::Initialize(edits)) = rx.try_recv() {
  128. Some(RpcResponse::new_result(req.id.take(), Some(edits)))
  129. } else {
  130. None
  131. }
  132. }
  133. "user_event" => {
  134. let event = events::trigger_from_serialized(req.params.unwrap());
  135. log::debug!("User event: {:?}", event);
  136. sender.unbounded_send(SchedulerMsg::UiEvent(event)).unwrap();
  137. if let Some(BridgeEvent::Update(edits)) = rx.blocking_recv() {
  138. log::info!("bridge received message {:?}", edits);
  139. Some(RpcResponse::new_result(req.id.take(), Some(edits)))
  140. } else {
  141. log::info!("none received message");
  142. None
  143. }
  144. }
  145. _ => None,
  146. }
  147. })
  148. // Any content that that uses the `wry://` scheme will be shuttled through this handler as a "special case"
  149. // For now, we only serve two pieces of content which get included as bytes into the final binary.
  150. .with_custom_protocol("wry".into(), move |request| {
  151. let path = request.uri().replace("wry://", "");
  152. let (data, meta) = match path.as_str() {
  153. "index.html" => (include_bytes!("./index.html").to_vec(), "text/html"),
  154. "index.html/index.js" => {
  155. (include_bytes!("./index.js").to_vec(), "text/javascript")
  156. }
  157. _ => unimplemented!("path {}", path),
  158. };
  159. wry::http::ResponseBuilder::new().mimetype(meta).body(data)
  160. })
  161. .build()
  162. .unwrap();
  163. webviews.insert(window_id, webview);
  164. }
  165. Event::WindowEvent {
  166. event, window_id, ..
  167. } => match event {
  168. WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
  169. WindowEvent::Destroyed { .. } => {
  170. webviews.remove(&window_id);
  171. if webviews.is_empty() {
  172. *control_flow = ControlFlow::Exit;
  173. }
  174. }
  175. // catch only pressed event
  176. WindowEvent::KeyboardInput { event, .. } => {
  177. log::debug!("keybowrd input");
  178. if quit_hotkey.matches(&modifiers, &event.physical_key) {
  179. log::debug!("quitting");
  180. webviews.remove(&window_id);
  181. if webviews.is_empty() {
  182. *control_flow = ControlFlow::Exit;
  183. }
  184. }
  185. // println!(
  186. // "KeyEvent: `Shift` + `1` | logical_key: {:?}",
  187. // &event.logical_key
  188. // );
  189. // we can match manually without `Accelerator`
  190. // else if event.key_without_modifiers() == Key::Character("1")
  191. // && modifiers.is_empty()
  192. // {
  193. // println!("KeyEvent: `1`");
  194. // }
  195. }
  196. WindowEvent::Resized(_) | WindowEvent::Moved(_) => {
  197. if let Some(view) = webviews.get_mut(&window_id) {
  198. let _ = view.resize();
  199. }
  200. }
  201. // TODO: we want to shuttle all of these events into the user's app
  202. _ => {}
  203. },
  204. Event::MainEventsCleared => {}
  205. Event::Resumed => {}
  206. Event::Suspended => {}
  207. Event::LoopDestroyed => {}
  208. _ => {}
  209. }
  210. })
  211. }
  212. pub fn start<P: 'static + Send>(
  213. root: FC<P>,
  214. config_builder: impl for<'a, 'b> FnOnce(&'b mut DesktopConfig<'a>) -> &'b mut DesktopConfig<'a>,
  215. ) -> ((), ()) {
  216. //
  217. ((), ())
  218. }
  219. // Create a new tokio runtime on a dedicated thread and then launch the apps VirtualDom.
  220. pub(crate) fn launch_vdom_with_tokio<P: Send + 'static>(
  221. root: FC<P>,
  222. props: P,
  223. event_tx: tokio::sync::mpsc::UnboundedSender<BridgeEvent>,
  224. ) -> futures_channel::mpsc::UnboundedSender<SchedulerMsg> {
  225. let (sender, receiver) = futures_channel::mpsc::unbounded::<SchedulerMsg>();
  226. let return_sender = sender.clone();
  227. std::thread::spawn(move || {
  228. // We create the runtim as multithreaded, so you can still "spawn" onto multiple threads
  229. let runtime = tokio::runtime::Builder::new_multi_thread()
  230. .enable_all()
  231. .build()
  232. .unwrap();
  233. runtime.block_on(async move {
  234. let mut vir = VirtualDom::new_with_props_and_scheduler(root, props, sender, receiver);
  235. let _ = vir.get_event_sender();
  236. let edits = vir.rebuild();
  237. // the receiving end expects something along these lines
  238. #[derive(Serialize)]
  239. struct Evt<'a> {
  240. edits: Vec<DomEdit<'a>>,
  241. }
  242. let edit_string = serde_json::to_value(Evt { edits: edits.edits }).unwrap();
  243. event_tx
  244. .send(BridgeEvent::Initialize(edit_string))
  245. .expect("Sending should not fail");
  246. loop {
  247. vir.wait_for_work().await;
  248. // we're running on our own thread, so we don't need to worry about blocking anything
  249. // todo: maybe we want to schedule ourselves in
  250. // on average though, the virtualdom running natively is stupid fast
  251. let mut muts = vir.run_with_deadline(|| false);
  252. log::debug!("finished running with deadline");
  253. let mut edits = vec![];
  254. while let Some(edit) = muts.pop() {
  255. log::debug!("sending message on channel with edit {:?}", edit);
  256. let edit_string = serde_json::to_value(Evt { edits: edit.edits })
  257. .expect("serializing edits should never fail");
  258. edits.push(edit_string);
  259. }
  260. event_tx
  261. .send(BridgeEvent::Update(serde_json::Value::Array(edits)))
  262. .expect("Sending should not fail");
  263. }
  264. })
  265. });
  266. return_sender
  267. }