app.rs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. #[cfg(feature = "system-tray")]
  5. #[cfg_attr(doc_cfg, doc(cfg(feature = "system-tray")))]
  6. pub(crate) mod tray;
  7. use crate::{
  8. api::assets::Assets,
  9. api::config::{Config, WindowUrl},
  10. command::{CommandArg, CommandItem},
  11. hooks::{InvokeHandler, OnPageLoad, PageLoadPayload, SetupHook},
  12. manager::WindowManager,
  13. plugin::{Plugin, PluginStore},
  14. runtime::{
  15. webview::{CustomProtocol, WebviewAttributes, WindowBuilder},
  16. window::{PendingWindow, WindowEvent},
  17. Dispatch, RunEvent, Runtime,
  18. },
  19. sealed::{ManagerBase, RuntimeOrDispatch},
  20. Context, Invoke, InvokeError, Manager, StateManager, Window,
  21. };
  22. use tauri_macros::default_runtime;
  23. use tauri_utils::PackageInfo;
  24. use std::{
  25. collections::HashMap,
  26. path::PathBuf,
  27. sync::{mpsc::Sender, Arc},
  28. };
  29. #[cfg(feature = "menu")]
  30. use crate::runtime::menu::{Menu, MenuId, MenuIdRef};
  31. #[cfg(all(windows, feature = "system-tray"))]
  32. use crate::runtime::RuntimeHandle;
  33. #[cfg(feature = "system-tray")]
  34. use crate::runtime::{Icon, SystemTrayEvent as RuntimeSystemTrayEvent};
  35. #[cfg(feature = "updater")]
  36. use crate::updater;
  37. #[cfg(feature = "menu")]
  38. pub(crate) type GlobalMenuEventListener<R> = Box<dyn Fn(WindowMenuEvent<R>) + Send + Sync>;
  39. pub(crate) type GlobalWindowEventListener<R> = Box<dyn Fn(GlobalWindowEvent<R>) + Send + Sync>;
  40. #[cfg(feature = "system-tray")]
  41. type SystemTrayEventListener<R> = Box<dyn Fn(&AppHandle<R>, tray::SystemTrayEvent) + Send + Sync>;
  42. /// Api exposed on the `CloseRequested` event.
  43. pub struct CloseRequestApi(Sender<bool>);
  44. impl CloseRequestApi {
  45. /// Prevents the window from being closed.
  46. pub fn prevent_close(&self) {
  47. self.0.send(true).unwrap();
  48. }
  49. }
  50. /// An application event, triggered from the event loop.
  51. #[non_exhaustive]
  52. pub enum Event {
  53. /// Event loop is exiting.
  54. Exit,
  55. /// Window close was requested by the user.
  56. #[non_exhaustive]
  57. CloseRequested {
  58. /// The window label.
  59. label: String,
  60. /// Event API.
  61. api: CloseRequestApi,
  62. },
  63. /// Window closed.
  64. WindowClosed(String),
  65. }
  66. /// A menu event that was triggered on a window.
  67. #[cfg(feature = "menu")]
  68. #[cfg_attr(doc_cfg, doc(cfg(feature = "menu")))]
  69. #[default_runtime(crate::Wry, wry)]
  70. pub struct WindowMenuEvent<R: Runtime> {
  71. pub(crate) menu_item_id: MenuId,
  72. pub(crate) window: Window<R>,
  73. }
  74. #[cfg(feature = "menu")]
  75. impl<R: Runtime> WindowMenuEvent<R> {
  76. /// The menu item id.
  77. pub fn menu_item_id(&self) -> MenuIdRef<'_> {
  78. &self.menu_item_id
  79. }
  80. /// The window that the menu belongs to.
  81. pub fn window(&self) -> &Window<R> {
  82. &self.window
  83. }
  84. }
  85. /// A window event that was triggered on the specified window.
  86. #[default_runtime(crate::Wry, wry)]
  87. pub struct GlobalWindowEvent<R: Runtime> {
  88. pub(crate) event: WindowEvent,
  89. pub(crate) window: Window<R>,
  90. }
  91. impl<R: Runtime> GlobalWindowEvent<R> {
  92. /// The event payload.
  93. pub fn event(&self) -> &WindowEvent {
  94. &self.event
  95. }
  96. /// The window that the menu belongs to.
  97. pub fn window(&self) -> &Window<R> {
  98. &self.window
  99. }
  100. }
  101. /// The path resolver is a helper for the application-specific [`crate::api::path`] APIs.
  102. #[derive(Debug, Clone)]
  103. pub struct PathResolver {
  104. config: Arc<Config>,
  105. package_info: PackageInfo,
  106. }
  107. impl PathResolver {
  108. /// Returns the path to the resource directory of this app.
  109. pub fn resource_dir(&self) -> Option<PathBuf> {
  110. crate::api::path::resource_dir(&self.package_info)
  111. }
  112. /// Returns the path to the suggested directory for your app config files.
  113. pub fn app_dir(&self) -> Option<PathBuf> {
  114. crate::api::path::app_dir(&self.config)
  115. }
  116. }
  117. /// A handle to the currently running application.
  118. ///
  119. /// This type implements [`Manager`] which allows for manipulation of global application items.
  120. #[default_runtime(crate::Wry, wry)]
  121. pub struct AppHandle<R: Runtime> {
  122. runtime_handle: R::Handle,
  123. manager: WindowManager<R>,
  124. global_shortcut_manager: R::GlobalShortcutManager,
  125. clipboard_manager: R::ClipboardManager,
  126. #[cfg(feature = "system-tray")]
  127. tray_handle: Option<tray::SystemTrayHandle<R>>,
  128. }
  129. impl<R: Runtime> Clone for AppHandle<R> {
  130. fn clone(&self) -> Self {
  131. Self {
  132. runtime_handle: self.runtime_handle.clone(),
  133. manager: self.manager.clone(),
  134. global_shortcut_manager: self.global_shortcut_manager.clone(),
  135. clipboard_manager: self.clipboard_manager.clone(),
  136. #[cfg(feature = "system-tray")]
  137. tray_handle: self.tray_handle.clone(),
  138. }
  139. }
  140. }
  141. impl<'de, R: Runtime> CommandArg<'de, R> for AppHandle<R> {
  142. /// Grabs the [`Window`] from the [`CommandItem`] and returns the associated [`AppHandle`]. This will never fail.
  143. fn from_command(command: CommandItem<'de, R>) -> Result<Self, InvokeError> {
  144. Ok(command.message.window().app_handle)
  145. }
  146. }
  147. impl<R: Runtime> AppHandle<R> {
  148. /// Removes the system tray.
  149. #[cfg(all(windows, feature = "system-tray"))]
  150. #[cfg_attr(doc_cfg, doc(cfg(all(windows, feature = "system-tray"))))]
  151. fn remove_system_tray(&self) -> crate::Result<()> {
  152. self.runtime_handle.remove_system_tray().map_err(Into::into)
  153. }
  154. }
  155. impl<R: Runtime> Manager<R> for AppHandle<R> {}
  156. impl<R: Runtime> ManagerBase<R> for AppHandle<R> {
  157. fn manager(&self) -> &WindowManager<R> {
  158. &self.manager
  159. }
  160. fn runtime(&self) -> RuntimeOrDispatch<'_, R> {
  161. RuntimeOrDispatch::RuntimeHandle(self.runtime_handle.clone())
  162. }
  163. fn app_handle(&self) -> AppHandle<R> {
  164. self.clone()
  165. }
  166. }
  167. /// The instance of the currently running application.
  168. ///
  169. /// This type implements [`Manager`] which allows for manipulation of global application items.
  170. #[default_runtime(crate::Wry, wry)]
  171. pub struct App<R: Runtime> {
  172. runtime: Option<R>,
  173. manager: WindowManager<R>,
  174. global_shortcut_manager: R::GlobalShortcutManager,
  175. clipboard_manager: R::ClipboardManager,
  176. #[cfg(feature = "system-tray")]
  177. tray_handle: Option<tray::SystemTrayHandle<R>>,
  178. handle: AppHandle<R>,
  179. }
  180. impl<R: Runtime> Manager<R> for App<R> {}
  181. impl<R: Runtime> ManagerBase<R> for App<R> {
  182. fn manager(&self) -> &WindowManager<R> {
  183. &self.manager
  184. }
  185. fn runtime(&self) -> RuntimeOrDispatch<'_, R> {
  186. RuntimeOrDispatch::Runtime(self.runtime.as_ref().unwrap())
  187. }
  188. fn app_handle(&self) -> AppHandle<R> {
  189. self.handle()
  190. }
  191. }
  192. macro_rules! shared_app_impl {
  193. ($app: ty) => {
  194. impl<R: Runtime> $app {
  195. /// Creates a new webview window.
  196. pub fn create_window<F>(
  197. &self,
  198. label: impl Into<String>,
  199. url: WindowUrl,
  200. setup: F,
  201. ) -> crate::Result<()>
  202. where
  203. F: FnOnce(
  204. <R::Dispatcher as Dispatch>::WindowBuilder,
  205. WebviewAttributes,
  206. ) -> (
  207. <R::Dispatcher as Dispatch>::WindowBuilder,
  208. WebviewAttributes,
  209. ),
  210. {
  211. let (window_builder, webview_attributes) = setup(
  212. <R::Dispatcher as Dispatch>::WindowBuilder::new(),
  213. WebviewAttributes::new(url),
  214. );
  215. self.create_new_window(PendingWindow::new(
  216. window_builder,
  217. webview_attributes,
  218. label,
  219. ))?;
  220. Ok(())
  221. }
  222. #[cfg(feature = "system-tray")]
  223. #[cfg_attr(doc_cfg, doc(cfg(feature = "system-tray")))]
  224. /// Gets a handle handle to the system tray.
  225. pub fn tray_handle(&self) -> tray::SystemTrayHandle<R> {
  226. self
  227. .tray_handle
  228. .clone()
  229. .expect("tray not configured; use the `Builder#system_tray` API first.")
  230. }
  231. /// The path resolver for the application.
  232. pub fn path_resolver(&self) -> PathResolver {
  233. PathResolver {
  234. config: self.manager.config(),
  235. package_info: self.manager.package_info().clone(),
  236. }
  237. }
  238. /// Gets a copy of the global shortcut manager instance.
  239. pub fn global_shortcut_manager(&self) -> R::GlobalShortcutManager {
  240. self.global_shortcut_manager.clone()
  241. }
  242. /// Gets a copy of the clipboard manager instance.
  243. pub fn clipboard_manager(&self) -> R::ClipboardManager {
  244. self.clipboard_manager.clone()
  245. }
  246. /// Gets the app's configuration, defined on the `tauri.conf.json` file.
  247. pub fn config(&self) -> Arc<Config> {
  248. self.manager.config()
  249. }
  250. /// Gets the app's package information.
  251. pub fn package_info(&self) -> &PackageInfo {
  252. self.manager.package_info()
  253. }
  254. }
  255. };
  256. }
  257. shared_app_impl!(App<R>);
  258. shared_app_impl!(AppHandle<R>);
  259. impl<R: Runtime> App<R> {
  260. /// Gets a handle to the application instance.
  261. pub fn handle(&self) -> AppHandle<R> {
  262. self.handle.clone()
  263. }
  264. /// Runs the application.
  265. pub fn run<F: Fn(&AppHandle<R>, Event) + 'static>(mut self, callback: F) {
  266. let app_handle = self.handle();
  267. let manager = self.manager.clone();
  268. self.runtime.take().unwrap().run(move |event| match event {
  269. RunEvent::Exit => {
  270. #[cfg(shell_execute)]
  271. {
  272. crate::api::process::kill_children();
  273. }
  274. #[cfg(all(windows, feature = "system-tray"))]
  275. {
  276. let _ = app_handle.remove_system_tray();
  277. }
  278. callback(&app_handle, Event::Exit);
  279. }
  280. _ => {
  281. on_event_loop_event(&event, &manager);
  282. callback(
  283. &app_handle,
  284. match event {
  285. RunEvent::Exit => Event::Exit,
  286. RunEvent::CloseRequested { label, signal_tx } => Event::CloseRequested {
  287. label: label.parse().unwrap_or_else(|_| unreachable!()),
  288. api: CloseRequestApi(signal_tx),
  289. },
  290. RunEvent::WindowClose(label) => {
  291. Event::WindowClosed(label.parse().unwrap_or_else(|_| unreachable!()))
  292. }
  293. _ => unimplemented!(),
  294. },
  295. );
  296. }
  297. });
  298. }
  299. /// Runs a iteration of the runtime event loop and immediately return.
  300. ///
  301. /// Note that when using this API, app cleanup is not automatically done.
  302. /// The cleanup calls [`crate::api::process::kill_children`] so you may want to call that function before exiting the application.
  303. /// Additionally, the cleanup calls [AppHandle#remove_system_tray](`AppHandle#method.remove_system_tray`) (Windows only).
  304. ///
  305. /// # Example
  306. /// ```rust,ignore
  307. /// fn main() {
  308. /// let mut app = tauri::Builder::default()
  309. /// .build(tauri::generate_context!())
  310. /// .expect("error while building tauri application");
  311. /// loop {
  312. /// let iteration = app.run_iteration();
  313. /// if iteration.webview_count == 0 {
  314. /// break;
  315. /// }
  316. /// }
  317. /// }
  318. #[cfg(any(target_os = "windows", target_os = "macos"))]
  319. pub fn run_iteration(&mut self) -> crate::runtime::RunIteration {
  320. let manager = self.manager.clone();
  321. self
  322. .runtime
  323. .as_mut()
  324. .unwrap()
  325. .run_iteration(move |event| on_event_loop_event(&event, &manager))
  326. }
  327. }
  328. #[cfg(feature = "updater")]
  329. impl<R: Runtime> App<R> {
  330. /// Runs the updater hook with built-in dialog.
  331. fn run_updater_dialog(&self, window: Window<R>) {
  332. let updater_config = self.manager.config().tauri.updater.clone();
  333. let package_info = self.manager.package_info().clone();
  334. #[cfg(not(target_os = "linux"))]
  335. crate::async_runtime::spawn(async move {
  336. updater::check_update_with_dialog(updater_config, package_info, window).await
  337. });
  338. #[cfg(target_os = "linux")]
  339. {
  340. let context = glib::MainContext::default();
  341. context.spawn_with_priority(glib::PRIORITY_HIGH, async move {
  342. updater::check_update_with_dialog(updater_config, package_info, window).await
  343. });
  344. }
  345. }
  346. /// Listen updater events when dialog are disabled.
  347. fn listen_updater_events(&self, window: Window<R>) {
  348. let updater_config = self.manager.config().tauri.updater.clone();
  349. updater::listener(updater_config, self.manager.package_info().clone(), &window);
  350. }
  351. fn run_updater(&self, main_window: Option<Window<R>>) {
  352. if let Some(main_window) = main_window {
  353. let event_window = main_window.clone();
  354. let updater_config = self.manager.config().tauri.updater.clone();
  355. // check if updater is active or not
  356. if updater_config.dialog && updater_config.active {
  357. // if updater dialog is enabled spawn a new task
  358. self.run_updater_dialog(main_window.clone());
  359. let config = self.manager.config().tauri.updater.clone();
  360. let package_info = self.manager.package_info().clone();
  361. // When dialog is enabled, if user want to recheck
  362. // if an update is available after first start
  363. // invoke the Event `tauri://update` from JS or rust side.
  364. main_window.listen(updater::EVENT_CHECK_UPDATE, move |_msg| {
  365. let window = event_window.clone();
  366. let package_info = package_info.clone();
  367. let config = config.clone();
  368. // re-spawn task inside tokyo to launch the download
  369. // we don't need to emit anything as everything is handled
  370. // by the process (user is asked to restart at the end)
  371. // and it's handled by the updater
  372. crate::async_runtime::spawn(async move {
  373. updater::check_update_with_dialog(config, package_info, window).await
  374. });
  375. });
  376. } else if updater_config.active {
  377. // we only listen for `tauri://update`
  378. // once we receive the call, we check if an update is available or not
  379. // if there is a new update we emit `tauri://update-available` with details
  380. // this is the user responsabilities to display dialog and ask if user want to install
  381. // to install the update you need to invoke the Event `tauri://update-install`
  382. self.listen_updater_events(main_window);
  383. }
  384. }
  385. }
  386. }
  387. /// Builds a Tauri application.
  388. #[allow(clippy::type_complexity)]
  389. pub struct Builder<R: Runtime> {
  390. /// The JS message handler.
  391. invoke_handler: Box<InvokeHandler<R>>,
  392. /// The setup hook.
  393. setup: SetupHook<R>,
  394. /// Page load hook.
  395. on_page_load: Box<OnPageLoad<R>>,
  396. /// windows to create when starting up.
  397. pending_windows: Vec<PendingWindow<R>>,
  398. /// All passed plugins
  399. plugins: PluginStore<R>,
  400. /// The webview protocols available to all windows.
  401. uri_scheme_protocols: HashMap<String, Arc<CustomProtocol>>,
  402. /// App state.
  403. state: StateManager,
  404. /// The menu set to all windows.
  405. #[cfg(feature = "menu")]
  406. menu: Option<Menu>,
  407. /// Menu event handlers that listens to all windows.
  408. #[cfg(feature = "menu")]
  409. menu_event_listeners: Vec<GlobalMenuEventListener<R>>,
  410. /// Window event handlers that listens to all windows.
  411. window_event_listeners: Vec<GlobalWindowEventListener<R>>,
  412. /// The app system tray.
  413. #[cfg(feature = "system-tray")]
  414. system_tray: Option<tray::SystemTray>,
  415. /// System tray event handlers.
  416. #[cfg(feature = "system-tray")]
  417. system_tray_event_listeners: Vec<SystemTrayEventListener<R>>,
  418. }
  419. impl<R: Runtime> Builder<R> {
  420. /// Creates a new App builder.
  421. pub fn new() -> Self {
  422. Self {
  423. setup: Box::new(|_| Ok(())),
  424. invoke_handler: Box::new(|_| ()),
  425. on_page_load: Box::new(|_, _| ()),
  426. pending_windows: Default::default(),
  427. plugins: PluginStore::default(),
  428. uri_scheme_protocols: Default::default(),
  429. state: StateManager::new(),
  430. #[cfg(feature = "menu")]
  431. menu: None,
  432. #[cfg(feature = "menu")]
  433. menu_event_listeners: Vec::new(),
  434. window_event_listeners: Vec::new(),
  435. #[cfg(feature = "system-tray")]
  436. system_tray: None,
  437. #[cfg(feature = "system-tray")]
  438. system_tray_event_listeners: Vec::new(),
  439. }
  440. }
  441. /// Defines the JS message handler callback.
  442. pub fn invoke_handler<F>(mut self, invoke_handler: F) -> Self
  443. where
  444. F: Fn(Invoke<R>) + Send + Sync + 'static,
  445. {
  446. self.invoke_handler = Box::new(invoke_handler);
  447. self
  448. }
  449. /// Defines the setup hook.
  450. pub fn setup<F>(mut self, setup: F) -> Self
  451. where
  452. F: Fn(&mut App<R>) -> Result<(), Box<dyn std::error::Error + Send>> + Send + 'static,
  453. {
  454. self.setup = Box::new(setup);
  455. self
  456. }
  457. /// Defines the page load hook.
  458. pub fn on_page_load<F>(mut self, on_page_load: F) -> Self
  459. where
  460. F: Fn(Window<R>, PageLoadPayload) + Send + Sync + 'static,
  461. {
  462. self.on_page_load = Box::new(on_page_load);
  463. self
  464. }
  465. /// Adds a plugin to the runtime.
  466. pub fn plugin<P: Plugin<R> + 'static>(mut self, plugin: P) -> Self {
  467. self.plugins.register(plugin);
  468. self
  469. }
  470. /// Add `state` to the state managed by the application.
  471. ///
  472. /// This method can be called any number of times as long as each call
  473. /// refers to a different `T`.
  474. ///
  475. /// Managed state can be retrieved by any request handler via the
  476. /// [`State`](crate::State) request guard. In particular, if a value of type `T`
  477. /// is managed by Tauri, adding `State<T>` to the list of arguments in a
  478. /// request handler instructs Tauri to retrieve the managed value.
  479. ///
  480. /// # Panics
  481. ///
  482. /// Panics if state of type `T` is already being managed.
  483. ///
  484. /// # Mutability
  485. ///
  486. /// Since the managed state is global and must be [`Send`] + [`Sync`], mutations can only happen through interior mutability:
  487. ///
  488. /// ```rust,ignore
  489. /// use std::{collections::HashMap, sync::Mutex};
  490. /// use tauri::State;
  491. /// // here we use Mutex to achieve interior mutability
  492. /// struct Storage(Mutex<HashMap<u64, String>>);
  493. /// struct Connection;
  494. /// struct DbConnection(Mutex<Option<Connection>>);
  495. ///
  496. /// #[tauri::command]
  497. /// fn connect(connection: State<DbConnection>) {
  498. /// // initialize the connection, mutating the state with interior mutability
  499. /// *connection.0.lock().unwrap() = Some(Connection {});
  500. /// }
  501. ///
  502. /// #[tauri::command]
  503. /// fn storage_insert(key: u64, value: String, storage: State<Storage>) {
  504. /// // mutate the storage behind the Mutex
  505. /// storage.0.lock().unwrap().insert(key, value);
  506. /// }
  507. ///
  508. /// fn main() {
  509. /// Builder::default()
  510. /// .manage(Storage(Default::default()))
  511. /// .manage(DbConnection(Default::default()))
  512. /// .invoke_handler(tauri::generate_handler![connect, storage_insert])
  513. /// .run(tauri::generate_context!())
  514. /// .expect("error while running tauri application");
  515. /// }
  516. /// ```
  517. ///
  518. /// # Example
  519. ///
  520. /// ```rust,ignore
  521. /// use tauri::State;
  522. ///
  523. /// struct MyInt(isize);
  524. /// struct MyString(String);
  525. ///
  526. /// #[tauri::command]
  527. /// fn int_command(state: State<MyInt>) -> String {
  528. /// format!("The stateful int is: {}", state.0)
  529. /// }
  530. ///
  531. /// #[tauri::command]
  532. /// fn string_command<'r>(state: State<'r, MyString>) {
  533. /// println!("state: {}", state.inner().0);
  534. /// }
  535. ///
  536. /// fn main() {
  537. /// tauri::Builder::default()
  538. /// .manage(MyInt(10))
  539. /// .manage(MyString("Hello, managed state!".to_string()))
  540. /// .invoke_handler(tauri::generate_handler![int_command, string_command])
  541. /// .run(tauri::generate_context!())
  542. /// .expect("error while running tauri application");
  543. /// }
  544. /// ```
  545. pub fn manage<T>(self, state: T) -> Self
  546. where
  547. T: Send + Sync + 'static,
  548. {
  549. let type_name = std::any::type_name::<T>();
  550. if !self.state.set(state) {
  551. panic!("state for type '{}' is already being managed", type_name);
  552. }
  553. self
  554. }
  555. /// Creates a new webview window.
  556. pub fn create_window<F>(mut self, label: impl Into<String>, url: WindowUrl, setup: F) -> Self
  557. where
  558. F: FnOnce(
  559. <R::Dispatcher as Dispatch>::WindowBuilder,
  560. WebviewAttributes,
  561. ) -> (
  562. <R::Dispatcher as Dispatch>::WindowBuilder,
  563. WebviewAttributes,
  564. ),
  565. {
  566. let (window_builder, webview_attributes) = setup(
  567. <R::Dispatcher as Dispatch>::WindowBuilder::new(),
  568. WebviewAttributes::new(url),
  569. );
  570. self.pending_windows.push(PendingWindow::new(
  571. window_builder,
  572. webview_attributes,
  573. label,
  574. ));
  575. self
  576. }
  577. /// Adds the icon configured on `tauri.conf.json` to the system tray with the specified menu items.
  578. #[cfg(feature = "system-tray")]
  579. #[cfg_attr(doc_cfg, doc(cfg(feature = "system-tray")))]
  580. pub fn system_tray(mut self, system_tray: tray::SystemTray) -> Self {
  581. self.system_tray.replace(system_tray);
  582. self
  583. }
  584. /// Sets the menu to use on all windows.
  585. #[cfg(feature = "menu")]
  586. #[cfg_attr(doc_cfg, doc(cfg(feature = "menu")))]
  587. pub fn menu(mut self, menu: Menu) -> Self {
  588. self.menu.replace(menu);
  589. self
  590. }
  591. /// Registers a menu event handler for all windows.
  592. #[cfg(feature = "menu")]
  593. #[cfg_attr(doc_cfg, doc(cfg(feature = "menu")))]
  594. pub fn on_menu_event<F: Fn(WindowMenuEvent<R>) + Send + Sync + 'static>(
  595. mut self,
  596. handler: F,
  597. ) -> Self {
  598. self.menu_event_listeners.push(Box::new(handler));
  599. self
  600. }
  601. /// Registers a window event handler for all windows.
  602. pub fn on_window_event<F: Fn(GlobalWindowEvent<R>) + Send + Sync + 'static>(
  603. mut self,
  604. handler: F,
  605. ) -> Self {
  606. self.window_event_listeners.push(Box::new(handler));
  607. self
  608. }
  609. /// Registers a system tray event handler.
  610. #[cfg(feature = "system-tray")]
  611. #[cfg_attr(doc_cfg, doc(cfg(feature = "system-tray")))]
  612. pub fn on_system_tray_event<
  613. F: Fn(&AppHandle<R>, tray::SystemTrayEvent) + Send + Sync + 'static,
  614. >(
  615. mut self,
  616. handler: F,
  617. ) -> Self {
  618. self.system_tray_event_listeners.push(Box::new(handler));
  619. self
  620. }
  621. /// Registers a URI scheme protocol available to all webviews.
  622. /// Leverages [setURLSchemeHandler](https://developer.apple.com/documentation/webkit/wkwebviewconfiguration/2875766-seturlschemehandler) on macOS,
  623. /// [AddWebResourceRequestedFilter](https://docs.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2.addwebresourcerequestedfilter?view=webview2-dotnet-1.0.774.44) on Windows
  624. /// and [webkit-web-context-register-uri-scheme](https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebContext.html#webkit-web-context-register-uri-scheme) on Linux.
  625. ///
  626. /// # Arguments
  627. ///
  628. /// * `uri_scheme` The URI scheme to register, such as `example`.
  629. /// * `protocol` the protocol associated with the given URI scheme. It's a function that takes an URL such as `example://localhost/asset.css`.
  630. pub fn register_global_uri_scheme_protocol<
  631. N: Into<String>,
  632. H: Fn(&str) -> Result<Vec<u8>, Box<dyn std::error::Error>> + Send + Sync + 'static,
  633. >(
  634. mut self,
  635. uri_scheme: N,
  636. protocol: H,
  637. ) -> Self {
  638. self.uri_scheme_protocols.insert(
  639. uri_scheme.into(),
  640. Arc::new(CustomProtocol {
  641. protocol: Box::new(protocol),
  642. }),
  643. );
  644. self
  645. }
  646. /// Builds the application.
  647. #[allow(clippy::type_complexity)]
  648. pub fn build<A: Assets>(mut self, context: Context<A>) -> crate::Result<App<R>> {
  649. #[cfg(feature = "system-tray")]
  650. let system_tray_icon = {
  651. let icon = context.system_tray_icon.clone();
  652. // check the icon format if the system tray is configured
  653. if self.system_tray.is_some() {
  654. use std::io::{Error, ErrorKind};
  655. #[cfg(target_os = "linux")]
  656. if let Some(Icon::Raw(_)) = icon {
  657. return Err(crate::Error::InvalidIcon(Box::new(Error::new(
  658. ErrorKind::InvalidInput,
  659. "system tray icons on linux must be a file path",
  660. ))));
  661. }
  662. #[cfg(not(target_os = "linux"))]
  663. if let Some(Icon::File(_)) = icon {
  664. return Err(crate::Error::InvalidIcon(Box::new(Error::new(
  665. ErrorKind::InvalidInput,
  666. "system tray icons on non-linux platforms must be the raw bytes",
  667. ))));
  668. }
  669. }
  670. icon
  671. };
  672. let manager = WindowManager::with_handlers(
  673. context,
  674. self.plugins,
  675. self.invoke_handler,
  676. self.on_page_load,
  677. self.uri_scheme_protocols,
  678. self.state,
  679. self.window_event_listeners,
  680. #[cfg(feature = "menu")]
  681. (self.menu, self.menu_event_listeners),
  682. );
  683. // set up all the windows defined in the config
  684. for config in manager.config().tauri.windows.clone() {
  685. let url = config.url.clone();
  686. let label = config.label.clone();
  687. let file_drop_enabled = config.file_drop_enabled;
  688. let mut webview_attributes = WebviewAttributes::new(url);
  689. if !file_drop_enabled {
  690. webview_attributes = webview_attributes.disable_file_drop_handler();
  691. }
  692. self.pending_windows.push(PendingWindow::with_config(
  693. config,
  694. webview_attributes,
  695. label,
  696. ));
  697. }
  698. let runtime = R::new()?;
  699. let runtime_handle = runtime.handle();
  700. let global_shortcut_manager = runtime.global_shortcut_manager();
  701. let clipboard_manager = runtime.clipboard_manager();
  702. let mut app = App {
  703. runtime: Some(runtime),
  704. manager: manager.clone(),
  705. global_shortcut_manager: global_shortcut_manager.clone(),
  706. clipboard_manager: clipboard_manager.clone(),
  707. #[cfg(feature = "system-tray")]
  708. tray_handle: None,
  709. handle: AppHandle {
  710. runtime_handle,
  711. manager,
  712. global_shortcut_manager,
  713. clipboard_manager,
  714. #[cfg(feature = "system-tray")]
  715. tray_handle: None,
  716. },
  717. };
  718. app.manager.initialize_plugins(&app)?;
  719. let pending_labels = self
  720. .pending_windows
  721. .iter()
  722. .map(|p| p.label.clone())
  723. .collect::<Vec<_>>();
  724. #[cfg(feature = "updater")]
  725. let mut main_window = None;
  726. for pending in self.pending_windows {
  727. let pending = app
  728. .manager
  729. .prepare_window(app.handle.clone(), pending, &pending_labels)?;
  730. let detached = app.runtime.as_ref().unwrap().create_window(pending)?;
  731. let _window = app.manager.attach_window(app.handle(), detached);
  732. #[cfg(feature = "updater")]
  733. if main_window.is_none() {
  734. main_window = Some(_window);
  735. }
  736. }
  737. (self.setup)(&mut app).map_err(|e| crate::Error::Setup(e))?;
  738. #[cfg(feature = "system-tray")]
  739. if let Some(system_tray) = self.system_tray {
  740. let mut ids = HashMap::new();
  741. if let Some(menu) = system_tray.menu() {
  742. tray::get_menu_ids(&mut ids, menu);
  743. }
  744. let mut tray = tray::SystemTray::new();
  745. if let Some(menu) = system_tray.menu {
  746. tray = tray.with_menu(menu);
  747. }
  748. let tray_handler = app
  749. .runtime
  750. .as_ref()
  751. .unwrap()
  752. .system_tray(
  753. tray.with_icon(
  754. system_tray
  755. .icon
  756. .or(system_tray_icon)
  757. .expect("tray icon not found; please configure it on tauri.conf.json"),
  758. ),
  759. )
  760. .expect("failed to run tray");
  761. let tray_handle = tray::SystemTrayHandle {
  762. ids: Arc::new(ids.clone()),
  763. inner: tray_handler,
  764. };
  765. app.tray_handle.replace(tray_handle.clone());
  766. app.handle.tray_handle.replace(tray_handle);
  767. for listener in self.system_tray_event_listeners {
  768. let app_handle = app.handle();
  769. let ids = ids.clone();
  770. let listener = Arc::new(std::sync::Mutex::new(listener));
  771. app
  772. .runtime
  773. .as_mut()
  774. .unwrap()
  775. .on_system_tray_event(move |event| {
  776. let app_handle = app_handle.clone();
  777. let event = match event {
  778. RuntimeSystemTrayEvent::MenuItemClick(id) => tray::SystemTrayEvent::MenuItemClick {
  779. id: ids.get(id).unwrap().clone(),
  780. },
  781. RuntimeSystemTrayEvent::LeftClick { position, size } => {
  782. tray::SystemTrayEvent::LeftClick {
  783. position: *position,
  784. size: *size,
  785. }
  786. }
  787. RuntimeSystemTrayEvent::RightClick { position, size } => {
  788. tray::SystemTrayEvent::RightClick {
  789. position: *position,
  790. size: *size,
  791. }
  792. }
  793. RuntimeSystemTrayEvent::DoubleClick { position, size } => {
  794. tray::SystemTrayEvent::DoubleClick {
  795. position: *position,
  796. size: *size,
  797. }
  798. }
  799. };
  800. let listener = listener.clone();
  801. crate::async_runtime::spawn(async move {
  802. listener.lock().unwrap()(&app_handle, event);
  803. });
  804. });
  805. }
  806. }
  807. #[cfg(feature = "updater")]
  808. app.run_updater(main_window);
  809. Ok(app)
  810. }
  811. /// Runs the configured Tauri application.
  812. pub fn run<A: Assets>(self, context: Context<A>) -> crate::Result<()> {
  813. self.build(context)?.run(|_, _| {});
  814. Ok(())
  815. }
  816. }
  817. fn on_event_loop_event<R: Runtime>(event: &RunEvent, manager: &WindowManager<R>) {
  818. if let RunEvent::WindowClose(label) = event {
  819. manager.on_window_close(label);
  820. }
  821. }
  822. /// Make `Wry` the default `Runtime` for `Builder`
  823. #[cfg(feature = "wry")]
  824. impl Default for Builder<crate::Wry> {
  825. fn default() -> Self {
  826. Self::new()
  827. }
  828. }