1
0

lib.rs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. //! Dioxus Desktop Renderer
  2. //!
  3. //! Render the Dioxus VirtualDom using the platform's native WebView implementation.
  4. //!
  5. //! # Desktop
  6. //!
  7. //! One of Dioxus' killer features is the ability to quickly build a native desktop app that looks and feels the same across platforms. Apps built with Dioxus are typically <5mb in size and use existing system resources, so they won't hog extreme amounts of RAM or memory.
  8. //!
  9. //! Dioxus Desktop is built off Tauri. Right now there aren't any Dioxus abstractions over keyboard shortcuts, menubar, handling, etc, so you'll want to leverage Tauri - mostly [Wry](http://github.com/tauri-apps/wry/) and [Tao](http://github.com/tauri-apps/tao)) directly. The next major release of Dioxus-Desktop will include components and hooks for notifications, global shortcuts, menubar, etc.
  10. //!
  11. //!
  12. //! ## Getting Set up
  13. //!
  14. //! Getting Set up with Dioxus-Desktop is quite easy. Make sure you have Rust and Cargo installed, and then create a new project:
  15. //!
  16. //! ```shell
  17. //! $ cargo new --bin demo
  18. //! $ cd app
  19. //! ```
  20. //!
  21. //! Add Dioxus with the `desktop` feature:
  22. //!
  23. //! ```shell
  24. //! $ cargo add dioxus --features desktop
  25. //! ```
  26. //!
  27. //! Edit your `main.rs`:
  28. //!
  29. //! ```rust
  30. //! // main.rs
  31. //! use dioxus::prelude::*;
  32. //!
  33. //! fn main() {
  34. //! dioxus::desktop::launch(app);
  35. //! }
  36. //!
  37. //! fn app(cx: Scope) -> Element {
  38. //! cx.render(rsx!{
  39. //! div {
  40. //! "hello world!"
  41. //! }
  42. //! })
  43. //! }
  44. //! ```
  45. //!
  46. //!
  47. //! To configure the webview, menubar, and other important desktop-specific features, checkout out some of the launch configuration in the [API reference](https://docs.rs/dioxus-desktop/).
  48. //!
  49. //! ## Future Steps
  50. //!
  51. //! Make sure to read the [Dioxus Guide](https://dioxuslabs.com/guide) if you already haven't!
  52. pub mod cfg;
  53. pub mod escape;
  54. pub mod events;
  55. use cfg::DesktopConfig;
  56. use dioxus_core::*;
  57. use std::{
  58. collections::{HashMap, VecDeque},
  59. sync::atomic::AtomicBool,
  60. sync::{Arc, RwLock},
  61. };
  62. use tao::{
  63. event::{Event, StartCause, WindowEvent},
  64. event_loop::{ControlFlow, EventLoop},
  65. window::{Window, WindowId},
  66. };
  67. pub use wry;
  68. pub use wry::application as tao;
  69. use wry::{
  70. application::event_loop::EventLoopProxy,
  71. webview::RpcRequest,
  72. webview::{WebView, WebViewBuilder},
  73. };
  74. /// Launch the WebView and run the event loop.
  75. ///
  76. /// This function will start a multithreaded Tokio runtime as well the WebView event loop.
  77. ///
  78. /// ```rust
  79. /// use dioxus::prelude::*;
  80. ///
  81. /// fn main() {
  82. /// dioxus::desktop::launch(app);
  83. /// }
  84. ///
  85. /// fn app(cx: Scope) -> Element {
  86. /// cx.render(rsx!{
  87. /// h1 {"hello world!"}
  88. /// })
  89. /// }
  90. /// ```
  91. pub fn launch(root: Component) {
  92. launch_with_props(root, (), |c| c)
  93. }
  94. /// Launch the WebView and run the event loop, with configuration.
  95. ///
  96. /// This function will start a multithreaded Tokio runtime as well the WebView event loop.
  97. ///
  98. /// You can configure the WebView window with a configuration closure
  99. ///
  100. /// ```rust
  101. /// use dioxus::prelude::*;
  102. ///
  103. /// fn main() {
  104. /// dioxus::desktop::launch_cfg(app, |c| c.with_window(|w| w.with_title("My App")));
  105. /// }
  106. ///
  107. /// fn app(cx: Scope) -> Element {
  108. /// cx.render(rsx!{
  109. /// h1 {"hello world!"}
  110. /// })
  111. /// }
  112. /// ```
  113. pub fn launch_cfg(
  114. root: Component,
  115. config_builder: impl FnOnce(&mut DesktopConfig) -> &mut DesktopConfig,
  116. ) {
  117. launch_with_props(root, (), config_builder)
  118. }
  119. /// Launch the WebView and run the event loop, with configuration and root props.
  120. ///
  121. /// This function will start a multithreaded Tokio runtime as well the WebView event loop.
  122. ///
  123. /// You can configure the WebView window with a configuration closure
  124. ///
  125. /// ```rust
  126. /// use dioxus::prelude::*;
  127. ///
  128. /// fn main() {
  129. /// dioxus::desktop::launch_cfg(app, AppProps { name: "asd" }, |c| c);
  130. /// }
  131. ///
  132. /// struct AppProps {
  133. /// name: &'static str
  134. /// }
  135. ///
  136. /// fn app(cx: Scope<AppProps>) -> Element {
  137. /// cx.render(rsx!{
  138. /// h1 {"hello {cx.props.name}!"}
  139. /// })
  140. /// }
  141. /// ```
  142. pub fn launch_with_props<P: 'static + Send>(
  143. root: Component<P>,
  144. props: P,
  145. builder: impl FnOnce(&mut DesktopConfig) -> &mut DesktopConfig,
  146. ) {
  147. let mut cfg = DesktopConfig::default();
  148. builder(&mut cfg);
  149. let event_loop = EventLoop::with_user_event();
  150. let mut desktop = DesktopController::new_on_tokio(root, props, event_loop.create_proxy());
  151. let proxy = event_loop.create_proxy();
  152. event_loop.run(move |window_event, event_loop, control_flow| {
  153. *control_flow = ControlFlow::Wait;
  154. match window_event {
  155. Event::NewEvents(StartCause::Init) => {
  156. let builder = cfg.window.clone();
  157. let window = builder.build(event_loop).unwrap();
  158. let window_id = window.id();
  159. let (is_ready, sender) = (desktop.is_ready.clone(), desktop.sender.clone());
  160. let proxy = proxy.clone();
  161. let file_handler = cfg.file_drop_handler.take();
  162. let mut webview = WebViewBuilder::new(window)
  163. .unwrap()
  164. .with_url("dioxus://index.html/")
  165. .unwrap()
  166. .with_rpc_handler(move |_window: &Window, req: RpcRequest| {
  167. match req.method.as_str() {
  168. "user_event" => {
  169. let event = events::trigger_from_serialized(req.params.unwrap());
  170. log::trace!("User event: {:?}", event);
  171. sender.unbounded_send(SchedulerMsg::Event(event)).unwrap();
  172. }
  173. "initialize" => {
  174. is_ready.store(true, std::sync::atomic::Ordering::Relaxed);
  175. let _ = proxy.send_event(UserWindowEvent::Update);
  176. }
  177. "browser_open" => {
  178. let data = req.params.unwrap();
  179. log::trace!("Open browser: {:?}", data);
  180. if let Some(arr) = data.as_array() {
  181. if let Some(temp) = arr[0].as_object() {
  182. if temp.contains_key("href") {
  183. let url = temp.get("href").unwrap().as_str().unwrap();
  184. if let Err(e) = webbrowser::open(url) {
  185. log::error!("Open Browser error: {:?}", e);
  186. }
  187. }
  188. }
  189. }
  190. }
  191. _ => {}
  192. }
  193. None
  194. })
  195. .with_custom_protocol(String::from("dioxus"), move |request| {
  196. // Any content that that uses the `dioxus://` scheme will be shuttled through this handler as a "special case"
  197. // For now, we only serve two pieces of content which get included as bytes into the final binary.
  198. let path = request.uri().replace("dioxus://", "");
  199. // all assets shouldbe called from index.html
  200. let trimmed = path.trim_start_matches("index.html/");
  201. if trimmed.is_empty() {
  202. wry::http::ResponseBuilder::new()
  203. .mimetype("text/html")
  204. .body(include_bytes!("./index.html").to_vec())
  205. } else if trimmed == "index.js" {
  206. wry::http::ResponseBuilder::new()
  207. .mimetype("text/javascript")
  208. .body(include_bytes!("./interpreter.js").to_vec())
  209. } else {
  210. // Read the file content from file path
  211. use std::fs::read;
  212. let path_buf = std::path::Path::new(trimmed).canonicalize()?;
  213. let cur_path = std::path::Path::new(".").canonicalize()?;
  214. if !path_buf.starts_with(cur_path) {
  215. return wry::http::ResponseBuilder::new()
  216. .status(wry::http::status::StatusCode::FORBIDDEN)
  217. .body(String::from("Forbidden").into_bytes());
  218. }
  219. if !path_buf.exists() {
  220. return wry::http::ResponseBuilder::new()
  221. .status(wry::http::status::StatusCode::NOT_FOUND)
  222. .body(String::from("Not Found").into_bytes());
  223. }
  224. let mime = mime_guess::from_path(&path_buf).first_or_octet_stream();
  225. // do not let path searching to go two layers beyond the caller level
  226. let data = read(path_buf)?;
  227. let meta = format!("{mime}");
  228. wry::http::ResponseBuilder::new().mimetype(&meta).body(data)
  229. }
  230. })
  231. .with_file_drop_handler(move |window, evet| {
  232. file_handler
  233. .as_ref()
  234. .map(|handler| handler(window, evet))
  235. .unwrap_or_default()
  236. });
  237. for (name, handler) in cfg.protocos.drain(..) {
  238. webview = webview.with_custom_protocol(name, handler)
  239. }
  240. desktop.webviews.insert(window_id, webview.build().unwrap());
  241. }
  242. Event::WindowEvent {
  243. event, window_id, ..
  244. } => match event {
  245. WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
  246. WindowEvent::Destroyed { .. } => desktop.close_window(window_id, control_flow),
  247. WindowEvent::Resized(_) | WindowEvent::Moved(_) => {
  248. if let Some(view) = desktop.webviews.get_mut(&window_id) {
  249. let _ = view.resize();
  250. }
  251. }
  252. _ => {}
  253. },
  254. Event::UserEvent(_evt) => {
  255. //
  256. match _evt {
  257. UserWindowEvent::Update => desktop.try_load_ready_webviews(),
  258. }
  259. }
  260. Event::MainEventsCleared => {}
  261. Event::Resumed => {}
  262. Event::Suspended => {}
  263. Event::LoopDestroyed => {}
  264. Event::RedrawRequested(_id) => {}
  265. _ => {}
  266. }
  267. })
  268. }
  269. pub enum UserWindowEvent {
  270. Update,
  271. }
  272. pub struct DesktopController {
  273. pub proxy: EventLoopProxy<UserWindowEvent>,
  274. pub webviews: HashMap<WindowId, WebView>,
  275. pub sender: futures_channel::mpsc::UnboundedSender<SchedulerMsg>,
  276. pub pending_edits: Arc<RwLock<VecDeque<String>>>,
  277. pub quit_app_on_close: bool,
  278. pub is_ready: Arc<AtomicBool>,
  279. }
  280. impl DesktopController {
  281. // Launch the virtualdom on its own thread managed by tokio
  282. // returns the desktop state
  283. pub fn new_on_tokio<P: Send + 'static>(
  284. root: Component<P>,
  285. props: P,
  286. evt: EventLoopProxy<UserWindowEvent>,
  287. ) -> Self {
  288. let edit_queue = Arc::new(RwLock::new(VecDeque::new()));
  289. let pending_edits = edit_queue.clone();
  290. let (sender, receiver) = futures_channel::mpsc::unbounded::<SchedulerMsg>();
  291. let return_sender = sender.clone();
  292. let proxy = evt.clone();
  293. std::thread::spawn(move || {
  294. // We create the runtime as multithreaded, so you can still "spawn" onto multiple threads
  295. let runtime = tokio::runtime::Builder::new_multi_thread()
  296. .enable_all()
  297. .build()
  298. .unwrap();
  299. runtime.block_on(async move {
  300. let mut dom =
  301. VirtualDom::new_with_props_and_scheduler(root, props, (sender, receiver));
  302. let edits = dom.rebuild();
  303. edit_queue
  304. .write()
  305. .unwrap()
  306. .push_front(serde_json::to_string(&edits.edits).unwrap());
  307. loop {
  308. dom.wait_for_work().await;
  309. let mut muts = dom.work_with_deadline(|| false);
  310. while let Some(edit) = muts.pop() {
  311. edit_queue
  312. .write()
  313. .unwrap()
  314. .push_front(serde_json::to_string(&edit.edits).unwrap());
  315. }
  316. let _ = evt.send_event(UserWindowEvent::Update);
  317. }
  318. })
  319. });
  320. Self {
  321. pending_edits,
  322. sender: return_sender,
  323. proxy,
  324. webviews: HashMap::new(),
  325. is_ready: Arc::new(AtomicBool::new(false)),
  326. quit_app_on_close: true,
  327. }
  328. }
  329. pub fn close_window(&mut self, window_id: WindowId, control_flow: &mut ControlFlow) {
  330. self.webviews.remove(&window_id);
  331. if self.webviews.is_empty() && self.quit_app_on_close {
  332. *control_flow = ControlFlow::Exit;
  333. }
  334. }
  335. pub fn try_load_ready_webviews(&mut self) {
  336. if self.is_ready.load(std::sync::atomic::Ordering::Relaxed) {
  337. let mut queue = self.pending_edits.write().unwrap();
  338. let (_id, view) = self.webviews.iter_mut().next().unwrap();
  339. while let Some(edit) = queue.pop_back() {
  340. view.evaluate_script(&format!("window.interpreter.handleEdits({})", edit))
  341. .unwrap();
  342. }
  343. } else {
  344. println!("waiting for ready");
  345. }
  346. }
  347. }