lib.rs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. accelerator::{Accelerator, SysMods},
  64. event::{Event, StartCause, WindowEvent},
  65. event_loop::{ControlFlow, EventLoop, EventLoopWindowTarget},
  66. keyboard::{KeyCode, ModifiersState},
  67. menu::{MenuBar, MenuItem},
  68. window::{Window, WindowId},
  69. };
  70. pub use wry;
  71. pub use wry::application as tao;
  72. use wry::{
  73. application::event_loop::EventLoopProxy,
  74. webview::RpcRequest,
  75. webview::{WebView, WebViewBuilder},
  76. };
  77. /// Launch the WebView and run the event loop.
  78. ///
  79. /// This function will start a multithreaded Tokio runtime as well the WebView event loop.
  80. ///
  81. /// ```rust
  82. /// use dioxus::prelude::*;
  83. ///
  84. /// fn main() {
  85. /// dioxus::desktop::launch(app);
  86. /// }
  87. ///
  88. /// fn app(cx: Scope) -> Element {
  89. /// cx.render(rsx!{
  90. /// h1 {"hello world!"}
  91. /// })
  92. /// }
  93. /// ```
  94. pub fn launch(root: Component) {
  95. launch_with_props(root, (), |c| c)
  96. }
  97. /// Launch the WebView and run the event loop, with configuration.
  98. ///
  99. /// This function will start a multithreaded Tokio runtime as well the WebView event loop.
  100. ///
  101. /// You can configure the WebView window with a configuration closure
  102. ///
  103. /// ```rust
  104. /// use dioxus::prelude::*;
  105. ///
  106. /// fn main() {
  107. /// dioxus::desktop::launch_cfg(app, |c| c.with_window(|w| w.with_title("My App")));
  108. /// }
  109. ///
  110. /// fn app(cx: Scope) -> Element {
  111. /// cx.render(rsx!{
  112. /// h1 {"hello world!"}
  113. /// })
  114. /// }
  115. /// ```
  116. pub fn launch_cfg(
  117. root: Component,
  118. config_builder: impl for<'a, 'b> FnOnce(&'b mut DesktopConfig<'a>) -> &'b mut DesktopConfig<'a>,
  119. ) {
  120. launch_with_props(root, (), config_builder)
  121. }
  122. /// Launch the WebView and run the event loop, with configuration and root props.
  123. ///
  124. /// This function will start a multithreaded Tokio runtime as well the WebView event loop.
  125. ///
  126. /// You can configure the WebView window with a configuration closure
  127. ///
  128. /// ```rust
  129. /// use dioxus::prelude::*;
  130. ///
  131. /// fn main() {
  132. /// dioxus::desktop::launch_cfg(app, AppProps { name: "asd" }, |c| c);
  133. /// }
  134. ///
  135. /// struct AppProps {
  136. /// name: &'static str
  137. /// }
  138. ///
  139. /// fn app(cx: Scope<AppProps>) -> Element {
  140. /// cx.render(rsx!{
  141. /// h1 {"hello {cx.props.name}!"}
  142. /// })
  143. /// }
  144. /// ```
  145. pub fn launch_with_props<P: 'static + Send>(
  146. root: Component<P>,
  147. props: P,
  148. builder: impl for<'a, 'b> FnOnce(&'b mut DesktopConfig<'a>) -> &'b mut DesktopConfig<'a>,
  149. ) {
  150. let mut desktop_cfg = DesktopConfig::new();
  151. builder(&mut desktop_cfg);
  152. let event_loop = EventLoop::with_user_event();
  153. let mut desktop = DesktopController::new_on_tokio(root, props, event_loop.create_proxy());
  154. let quit_hotkey = Accelerator::new(SysMods::Cmd, KeyCode::KeyQ);
  155. let modifiers = ModifiersState::default();
  156. event_loop.run(move |window_event, event_loop, control_flow| {
  157. *control_flow = ControlFlow::Wait;
  158. match window_event {
  159. Event::NewEvents(StartCause::Init) => desktop.new_window(&desktop_cfg, event_loop),
  160. Event::WindowEvent {
  161. event, window_id, ..
  162. } => {
  163. match event {
  164. WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
  165. WindowEvent::Destroyed { .. } => desktop.close_window(window_id, control_flow),
  166. WindowEvent::KeyboardInput { event, .. } => {
  167. if quit_hotkey.matches(&modifiers, &event.physical_key) {
  168. desktop.close_window(window_id, control_flow);
  169. }
  170. }
  171. WindowEvent::Resized(_) | WindowEvent::Moved(_) => {
  172. if let Some(view) = desktop.webviews.get_mut(&window_id) {
  173. let _ = view.resize();
  174. }
  175. }
  176. // TODO: we want to shuttle all of these events into the user's app or provide some handler
  177. _ => {}
  178. }
  179. }
  180. Event::UserEvent(_evt) => {
  181. desktop.try_load_ready_webviews();
  182. }
  183. Event::MainEventsCleared => {
  184. desktop.try_load_ready_webviews();
  185. }
  186. Event::Resumed => {}
  187. Event::Suspended => {}
  188. Event::LoopDestroyed => {}
  189. Event::RedrawRequested(_id) => {}
  190. _ => {}
  191. }
  192. })
  193. }
  194. pub enum UserWindowEvent {
  195. Start,
  196. Update,
  197. }
  198. pub struct DesktopController {
  199. pub webviews: HashMap<WindowId, WebView>,
  200. pub sender: futures_channel::mpsc::UnboundedSender<SchedulerMsg>,
  201. pub pending_edits: Arc<RwLock<VecDeque<String>>>,
  202. pub quit_app_on_close: bool,
  203. pub is_ready: Arc<AtomicBool>,
  204. }
  205. impl DesktopController {
  206. // Launch the virtualdom on its own thread managed by tokio
  207. // returns the desktop state
  208. pub fn new_on_tokio<P: Send + 'static>(
  209. root: Component<P>,
  210. props: P,
  211. evt: EventLoopProxy<UserWindowEvent>,
  212. ) -> Self {
  213. let edit_queue = Arc::new(RwLock::new(VecDeque::new()));
  214. let pending_edits = edit_queue.clone();
  215. let (sender, receiver) = futures_channel::mpsc::unbounded::<SchedulerMsg>();
  216. let return_sender = sender.clone();
  217. std::thread::spawn(move || {
  218. // We create the runtim as multithreaded, so you can still "spawn" onto multiple threads
  219. let runtime = tokio::runtime::Builder::new_multi_thread()
  220. .enable_all()
  221. .build()
  222. .unwrap();
  223. runtime.block_on(async move {
  224. let mut dom =
  225. VirtualDom::new_with_props_and_scheduler(root, props, (sender, receiver));
  226. let edits = dom.rebuild();
  227. edit_queue
  228. .write()
  229. .unwrap()
  230. .push_front(serde_json::to_string(&edits.edits).unwrap());
  231. loop {
  232. dom.wait_for_work().await;
  233. let mut muts = dom.work_with_deadline(|| false);
  234. while let Some(edit) = muts.pop() {
  235. edit_queue
  236. .write()
  237. .unwrap()
  238. .push_front(serde_json::to_string(&edit.edits).unwrap());
  239. }
  240. let _ = evt.send_event(UserWindowEvent::Update);
  241. }
  242. })
  243. });
  244. Self {
  245. pending_edits,
  246. sender: return_sender,
  247. webviews: HashMap::new(),
  248. is_ready: Arc::new(AtomicBool::new(false)),
  249. quit_app_on_close: true,
  250. }
  251. }
  252. pub fn new_window(
  253. &mut self,
  254. cfg: &DesktopConfig,
  255. event_loop: &EventLoopWindowTarget<UserWindowEvent>,
  256. ) {
  257. let builder = cfg.window.clone().with_menu({
  258. // create main menubar menu
  259. let mut menu_bar_menu = MenuBar::new();
  260. // create `first_menu`
  261. let mut first_menu = MenuBar::new();
  262. first_menu.add_native_item(MenuItem::About("App".to_string()));
  263. first_menu.add_native_item(MenuItem::Services);
  264. first_menu.add_native_item(MenuItem::Separator);
  265. first_menu.add_native_item(MenuItem::Hide);
  266. first_menu.add_native_item(MenuItem::HideOthers);
  267. first_menu.add_native_item(MenuItem::ShowAll);
  268. first_menu.add_native_item(MenuItem::Quit);
  269. first_menu.add_native_item(MenuItem::CloseWindow);
  270. // create second menu
  271. let mut second_menu = MenuBar::new();
  272. // second_menu.add_submenu("Sub menu", true, my_sub_menu);
  273. second_menu.add_native_item(MenuItem::Copy);
  274. second_menu.add_native_item(MenuItem::Paste);
  275. second_menu.add_native_item(MenuItem::SelectAll);
  276. menu_bar_menu.add_submenu("First menu", true, first_menu);
  277. menu_bar_menu.add_submenu("Second menu", true, second_menu);
  278. menu_bar_menu
  279. });
  280. let window = builder.build(event_loop).unwrap();
  281. let window_id = window.id();
  282. let (is_ready, sender) = (self.is_ready.clone(), self.sender.clone());
  283. let webview = WebViewBuilder::new(window)
  284. .unwrap()
  285. .with_url("wry://index.html")
  286. .unwrap()
  287. .with_rpc_handler(move |_window: &Window, req: RpcRequest| {
  288. match req.method.as_str() {
  289. "user_event" => {
  290. let event = events::trigger_from_serialized(req.params.unwrap());
  291. log::debug!("User event: {:?}", event);
  292. sender.unbounded_send(SchedulerMsg::Event(event)).unwrap();
  293. }
  294. "initialize" => {
  295. is_ready.store(true, std::sync::atomic::Ordering::Relaxed);
  296. }
  297. _ => {}
  298. }
  299. // response always driven through eval.
  300. // unfortunately, it seems to be pretty slow, so we might want to look into an RPC form
  301. None
  302. })
  303. // Any content that that uses the `wry://` scheme will be shuttled through this handler as a "special case"
  304. // For now, we only serve two pieces of content which get included as bytes into the final binary.
  305. .with_custom_protocol("wry".into(), move |request| {
  306. let path = request.uri().replace("wry://", "");
  307. let (data, meta) = match path.as_str() {
  308. "index.html" | "index.html/" | "/index.html" => {
  309. (include_bytes!("./index.html").to_vec(), "text/html")
  310. }
  311. "index.html/index.js" => {
  312. (include_bytes!("./index.js").to_vec(), "text/javascript")
  313. }
  314. _ => (include_bytes!("./index.html").to_vec(), "text/html"),
  315. };
  316. wry::http::ResponseBuilder::new().mimetype(meta).body(data)
  317. })
  318. .build()
  319. .unwrap();
  320. self.webviews.insert(window_id, webview);
  321. }
  322. pub fn close_window(&mut self, window_id: WindowId, control_flow: &mut ControlFlow) {
  323. self.webviews.remove(&window_id);
  324. if self.webviews.is_empty() && self.quit_app_on_close {
  325. *control_flow = ControlFlow::Exit;
  326. }
  327. }
  328. pub fn try_load_ready_webviews(&mut self) {
  329. if self.is_ready.load(std::sync::atomic::Ordering::Relaxed) {
  330. let mut queue = self.pending_edits.write().unwrap();
  331. let (_id, view) = self.webviews.iter_mut().next().unwrap();
  332. while let Some(edit) = queue.pop_back() {
  333. view.evaluate_script(&format!("window.interpreter.handleEdits({})", edit))
  334. .unwrap();
  335. }
  336. }
  337. }
  338. }