app.rs 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429
  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. pub(crate) mod tray;
  6. use crate::{
  7. api::ipc::CallbackFn,
  8. command::{CommandArg, CommandItem},
  9. hooks::{
  10. window_invoke_responder, InvokeHandler, InvokeResponder, OnPageLoad, PageLoadPayload, SetupHook,
  11. },
  12. manager::{Asset, CustomProtocol, WindowManager},
  13. plugin::{Plugin, PluginStore},
  14. runtime::{
  15. http::{Request as HttpRequest, Response as HttpResponse},
  16. webview::{WebviewAttributes, WindowBuilder as _},
  17. window::{PendingWindow, WindowEvent},
  18. Dispatch, ExitRequestedEventAction, RunEvent as RuntimeRunEvent,
  19. },
  20. scope::FsScope,
  21. sealed::{ManagerBase, RuntimeOrDispatch},
  22. utils::config::{Config, WindowUrl},
  23. utils::{assets::Assets, Env},
  24. window::WindowBuilder,
  25. Context, EventLoopMessage, Invoke, InvokeError, InvokeResponse, Manager, Runtime, Scopes,
  26. StateManager, Window,
  27. };
  28. #[cfg(shell_scope)]
  29. use crate::scope::ShellScope;
  30. use tauri_macros::default_runtime;
  31. use tauri_utils::PackageInfo;
  32. use std::{
  33. collections::HashMap,
  34. path::PathBuf,
  35. sync::{mpsc::Sender, Arc, Weak},
  36. };
  37. use crate::runtime::menu::{Menu, MenuId, MenuIdRef};
  38. use crate::runtime::RuntimeHandle;
  39. #[cfg(feature = "system-tray")]
  40. use crate::runtime::{SystemTrayEvent as RuntimeSystemTrayEvent, TrayIcon};
  41. #[cfg(feature = "updater")]
  42. use crate::updater;
  43. #[cfg(target_os = "macos")]
  44. use crate::ActivationPolicy;
  45. pub(crate) type GlobalMenuEventListener<R> = Box<dyn Fn(WindowMenuEvent<R>) + Send + Sync>;
  46. pub(crate) type GlobalWindowEventListener<R> = Box<dyn Fn(GlobalWindowEvent<R>) + Send + Sync>;
  47. #[cfg(feature = "system-tray")]
  48. type SystemTrayEventListener<R> = Box<dyn Fn(&AppHandle<R>, tray::SystemTrayEvent) + Send + Sync>;
  49. /// Api exposed on the `ExitRequested` event.
  50. #[derive(Debug)]
  51. pub struct ExitRequestApi(Sender<ExitRequestedEventAction>);
  52. impl ExitRequestApi {
  53. /// Prevents the app from exiting
  54. pub fn prevent_exit(&self) {
  55. self.0.send(ExitRequestedEventAction::Prevent).unwrap();
  56. }
  57. }
  58. /// Api exposed on the `CloseRequested` event.
  59. #[derive(Debug)]
  60. pub struct CloseRequestApi(Sender<bool>);
  61. impl CloseRequestApi {
  62. /// Prevents the window from being closed.
  63. pub fn prevent_close(&self) {
  64. self.0.send(true).unwrap();
  65. }
  66. }
  67. /// An application event, triggered from the event loop.
  68. #[derive(Debug)]
  69. #[non_exhaustive]
  70. pub enum RunEvent {
  71. /// Event loop is exiting.
  72. Exit,
  73. /// The app is about to exit
  74. #[non_exhaustive]
  75. ExitRequested {
  76. /// The label of the window that requested the exit.
  77. /// It is the last window managed by tauri.
  78. window_label: String,
  79. /// Event API
  80. api: ExitRequestApi,
  81. },
  82. /// Window close was requested by the user.
  83. #[non_exhaustive]
  84. CloseRequested {
  85. /// The window label.
  86. label: String,
  87. /// Event API.
  88. api: CloseRequestApi,
  89. },
  90. /// Window closed.
  91. WindowClosed(String),
  92. /// Application ready.
  93. Ready,
  94. /// Sent if the event loop is being resumed.
  95. Resumed,
  96. /// Emitted when all of the event loop’s input events have been processed and redraw processing is about to begin.
  97. ///
  98. /// This event is useful as a place to put your code that should be run after all state-changing events have been handled and you want to do stuff (updating state, performing calculations, etc) that happens as the “main body” of your event loop.
  99. MainEventsCleared,
  100. /// Updater event.
  101. #[cfg(feature = "updater")]
  102. #[cfg_attr(doc_cfg, doc(cfg(feature = "updater")))]
  103. Updater(crate::UpdaterEvent),
  104. }
  105. impl From<EventLoopMessage> for RunEvent {
  106. fn from(event: EventLoopMessage) -> Self {
  107. match event {
  108. #[cfg(feature = "updater")]
  109. EventLoopMessage::Updater(event) => RunEvent::Updater(event),
  110. }
  111. }
  112. }
  113. /// A menu event that was triggered on a window.
  114. #[default_runtime(crate::Wry, wry)]
  115. #[derive(Debug)]
  116. pub struct WindowMenuEvent<R: Runtime> {
  117. pub(crate) menu_item_id: MenuId,
  118. pub(crate) window: Window<R>,
  119. }
  120. impl<R: Runtime> WindowMenuEvent<R> {
  121. /// The menu item id.
  122. pub fn menu_item_id(&self) -> MenuIdRef<'_> {
  123. &self.menu_item_id
  124. }
  125. /// The window that the menu belongs to.
  126. pub fn window(&self) -> &Window<R> {
  127. &self.window
  128. }
  129. }
  130. /// A window event that was triggered on the specified window.
  131. #[default_runtime(crate::Wry, wry)]
  132. #[derive(Debug)]
  133. pub struct GlobalWindowEvent<R: Runtime> {
  134. pub(crate) event: WindowEvent,
  135. pub(crate) window: Window<R>,
  136. }
  137. impl<R: Runtime> GlobalWindowEvent<R> {
  138. /// The event payload.
  139. pub fn event(&self) -> &WindowEvent {
  140. &self.event
  141. }
  142. /// The window that the menu belongs to.
  143. pub fn window(&self) -> &Window<R> {
  144. &self.window
  145. }
  146. }
  147. /// The path resolver is a helper for the application-specific [`crate::api::path`] APIs.
  148. #[derive(Debug, Clone)]
  149. pub struct PathResolver {
  150. env: Env,
  151. config: Arc<Config>,
  152. package_info: PackageInfo,
  153. }
  154. impl PathResolver {
  155. /// Returns the path to the resource directory of this app.
  156. pub fn resource_dir(&self) -> Option<PathBuf> {
  157. crate::api::path::resource_dir(&self.package_info, &self.env)
  158. }
  159. /// Returns the path to the suggested directory for your app config files.
  160. pub fn app_dir(&self) -> Option<PathBuf> {
  161. crate::api::path::app_dir(&self.config)
  162. }
  163. /// Returns the path to the suggested log directory.
  164. pub fn log_dir(&self) -> Option<PathBuf> {
  165. crate::api::path::log_dir(&self.config)
  166. }
  167. }
  168. /// The asset resolver is a helper to access the [`tauri_utils::assets::Assets`] interface.
  169. #[derive(Debug, Clone)]
  170. pub struct AssetResolver<R: Runtime> {
  171. manager: WindowManager<R>,
  172. }
  173. impl<R: Runtime> AssetResolver<R> {
  174. /// Gets the app asset associated with the given path.
  175. pub fn get(&self, path: String) -> Option<Asset> {
  176. self.manager.get_asset(path).ok()
  177. }
  178. }
  179. /// A handle to the currently running application.
  180. ///
  181. /// This type implements [`Manager`] which allows for manipulation of global application items.
  182. #[default_runtime(crate::Wry, wry)]
  183. #[derive(Debug)]
  184. pub struct AppHandle<R: Runtime> {
  185. runtime_handle: R::Handle,
  186. manager: WindowManager<R>,
  187. global_shortcut_manager: R::GlobalShortcutManager,
  188. clipboard_manager: R::ClipboardManager,
  189. #[cfg(feature = "system-tray")]
  190. tray_handle: Option<tray::SystemTrayHandle<R>>,
  191. }
  192. impl<R: Runtime> AppHandle<R> {
  193. // currently only used on the updater
  194. #[allow(dead_code)]
  195. pub(crate) fn create_proxy(&self) -> R::EventLoopProxy {
  196. self.runtime_handle.create_proxy()
  197. }
  198. }
  199. #[cfg(feature = "wry")]
  200. impl AppHandle<crate::Wry> {
  201. /// Create a new tao window using a callback. The event loop must be running at this point.
  202. pub fn create_tao_window<
  203. F: FnOnce() -> (String, tauri_runtime_wry::WryWindowBuilder) + Send + 'static,
  204. >(
  205. &self,
  206. f: F,
  207. ) -> crate::Result<Weak<tauri_runtime_wry::Window>> {
  208. self.runtime_handle.create_tao_window(f).map_err(Into::into)
  209. }
  210. /// Sends a window message to the event loop.
  211. pub fn send_tao_window_event(
  212. &self,
  213. window_id: tauri_runtime_wry::WindowId,
  214. message: tauri_runtime_wry::WindowMessage,
  215. ) -> crate::Result<()> {
  216. self
  217. .runtime_handle
  218. .send_event(tauri_runtime_wry::Message::Window(
  219. self.runtime_handle.window_id(window_id),
  220. message,
  221. ))
  222. .map_err(Into::into)
  223. }
  224. }
  225. impl<R: Runtime> Clone for AppHandle<R> {
  226. fn clone(&self) -> Self {
  227. Self {
  228. runtime_handle: self.runtime_handle.clone(),
  229. manager: self.manager.clone(),
  230. global_shortcut_manager: self.global_shortcut_manager.clone(),
  231. clipboard_manager: self.clipboard_manager.clone(),
  232. #[cfg(feature = "system-tray")]
  233. tray_handle: self.tray_handle.clone(),
  234. }
  235. }
  236. }
  237. impl<'de, R: Runtime> CommandArg<'de, R> for AppHandle<R> {
  238. /// Grabs the [`Window`] from the [`CommandItem`] and returns the associated [`AppHandle`]. This will never fail.
  239. fn from_command(command: CommandItem<'de, R>) -> Result<Self, InvokeError> {
  240. Ok(command.message.window().app_handle)
  241. }
  242. }
  243. impl<R: Runtime> AppHandle<R> {
  244. /// Runs the given closure on the main thread.
  245. pub fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> crate::Result<()> {
  246. self
  247. .runtime_handle
  248. .run_on_main_thread(f)
  249. .map_err(Into::into)
  250. }
  251. /// Removes the system tray.
  252. #[cfg(all(windows, feature = "system-tray"))]
  253. #[cfg_attr(doc_cfg, doc(cfg(all(windows, feature = "system-tray"))))]
  254. fn remove_system_tray(&self) -> crate::Result<()> {
  255. self.runtime_handle.remove_system_tray().map_err(Into::into)
  256. }
  257. /// Adds a plugin to the runtime.
  258. pub fn plugin<P: Plugin<R> + 'static>(&self, mut plugin: P) -> crate::Result<()> {
  259. plugin
  260. .initialize(
  261. self,
  262. self
  263. .config()
  264. .plugins
  265. .0
  266. .get(plugin.name())
  267. .cloned()
  268. .unwrap_or_default(),
  269. )
  270. .map_err(|e| crate::Error::PluginInitialization(plugin.name().to_string(), e.to_string()))?;
  271. self
  272. .manager()
  273. .inner
  274. .plugins
  275. .lock()
  276. .unwrap()
  277. .register(plugin);
  278. Ok(())
  279. }
  280. /// Exits the app. This is the same as [`std::process::exit`], but it performs cleanup on this application.
  281. pub fn exit(&self, exit_code: i32) {
  282. self.cleanup_before_exit();
  283. std::process::exit(exit_code);
  284. }
  285. /// Restarts the app. This is the same as [`crate::api::process::restart`], but it performs cleanup on this application.
  286. pub fn restart(&self) {
  287. self.cleanup_before_exit();
  288. crate::api::process::restart(&self.env());
  289. }
  290. /// Runs necessary cleanup tasks before exiting the process
  291. fn cleanup_before_exit(&self) {
  292. #[cfg(shell_execute)]
  293. {
  294. crate::api::process::kill_children();
  295. }
  296. #[cfg(all(windows, feature = "system-tray"))]
  297. {
  298. let _ = self.remove_system_tray();
  299. }
  300. }
  301. }
  302. impl<R: Runtime> Manager<R> for AppHandle<R> {}
  303. impl<R: Runtime> ManagerBase<R> for AppHandle<R> {
  304. fn manager(&self) -> &WindowManager<R> {
  305. &self.manager
  306. }
  307. fn runtime(&self) -> RuntimeOrDispatch<'_, R> {
  308. RuntimeOrDispatch::RuntimeHandle(self.runtime_handle.clone())
  309. }
  310. fn managed_app_handle(&self) -> AppHandle<R> {
  311. self.clone()
  312. }
  313. }
  314. /// The instance of the currently running application.
  315. ///
  316. /// This type implements [`Manager`] which allows for manipulation of global application items.
  317. #[default_runtime(crate::Wry, wry)]
  318. #[derive(Debug)]
  319. pub struct App<R: Runtime> {
  320. runtime: Option<R>,
  321. manager: WindowManager<R>,
  322. global_shortcut_manager: R::GlobalShortcutManager,
  323. clipboard_manager: R::ClipboardManager,
  324. #[cfg(feature = "system-tray")]
  325. tray_handle: Option<tray::SystemTrayHandle<R>>,
  326. handle: AppHandle<R>,
  327. }
  328. impl<R: Runtime> Manager<R> for App<R> {}
  329. impl<R: Runtime> ManagerBase<R> for App<R> {
  330. fn manager(&self) -> &WindowManager<R> {
  331. &self.manager
  332. }
  333. fn runtime(&self) -> RuntimeOrDispatch<'_, R> {
  334. RuntimeOrDispatch::Runtime(self.runtime.as_ref().unwrap())
  335. }
  336. fn managed_app_handle(&self) -> AppHandle<R> {
  337. self.handle()
  338. }
  339. }
  340. macro_rules! shared_app_impl {
  341. ($app: ty) => {
  342. impl<R: Runtime> $app {
  343. #[cfg(feature = "updater")]
  344. #[cfg_attr(doc_cfg, doc(cfg(feature = "updater")))]
  345. /// Runs the updater to check if there is a new app version.
  346. /// It is the same as triggering the `tauri://update` event.
  347. pub async fn check_for_updates(&self) -> updater::Result<updater::UpdateResponse<R>> {
  348. updater::check(self.app_handle()).await
  349. }
  350. /// Creates a new webview window.
  351. ///
  352. /// Data URLs are only supported with the `window-data-url` feature flag.
  353. ///
  354. /// See [`crate::window::WindowBuilder::new`] for an API with extended functionality.
  355. #[deprecated(
  356. since = "1.0.0-rc.4",
  357. note = "The `window_builder` function offers an easier API with extended functionality"
  358. )]
  359. pub fn create_window<F>(
  360. &self,
  361. label: impl Into<String>,
  362. url: WindowUrl,
  363. setup: F,
  364. ) -> crate::Result<Window<R>>
  365. where
  366. F: FnOnce(
  367. <R::Dispatcher as Dispatch<EventLoopMessage>>::WindowBuilder,
  368. WebviewAttributes,
  369. ) -> (
  370. <R::Dispatcher as Dispatch<EventLoopMessage>>::WindowBuilder,
  371. WebviewAttributes,
  372. ),
  373. {
  374. let mut builder = WindowBuilder::<R>::new(self, label, url);
  375. let (window_builder, webview_attributes) =
  376. setup(builder.window_builder, builder.webview_attributes);
  377. builder.window_builder = window_builder;
  378. builder.webview_attributes = webview_attributes;
  379. builder.build()
  380. }
  381. #[cfg(feature = "system-tray")]
  382. #[cfg_attr(doc_cfg, doc(cfg(feature = "system-tray")))]
  383. /// Gets a handle handle to the system tray.
  384. pub fn tray_handle(&self) -> tray::SystemTrayHandle<R> {
  385. self
  386. .tray_handle
  387. .clone()
  388. .expect("tray not configured; use the `Builder#system_tray` API first.")
  389. }
  390. /// The path resolver for the application.
  391. pub fn path_resolver(&self) -> PathResolver {
  392. PathResolver {
  393. env: self.state::<Env>().inner().clone(),
  394. config: self.manager.config(),
  395. package_info: self.manager.package_info().clone(),
  396. }
  397. }
  398. /// Gets a copy of the global shortcut manager instance.
  399. pub fn global_shortcut_manager(&self) -> R::GlobalShortcutManager {
  400. self.global_shortcut_manager.clone()
  401. }
  402. /// Gets a copy of the clipboard manager instance.
  403. pub fn clipboard_manager(&self) -> R::ClipboardManager {
  404. self.clipboard_manager.clone()
  405. }
  406. /// Gets the app's configuration, defined on the `tauri.conf.json` file.
  407. pub fn config(&self) -> Arc<Config> {
  408. self.manager.config()
  409. }
  410. /// Gets the app's package information.
  411. pub fn package_info(&self) -> &PackageInfo {
  412. self.manager.package_info()
  413. }
  414. /// The application's asset resolver.
  415. pub fn asset_resolver(&self) -> AssetResolver<R> {
  416. AssetResolver {
  417. manager: self.manager.clone(),
  418. }
  419. }
  420. }
  421. };
  422. }
  423. shared_app_impl!(App<R>);
  424. shared_app_impl!(AppHandle<R>);
  425. impl<R: Runtime> App<R> {
  426. /// Gets a handle to the application instance.
  427. pub fn handle(&self) -> AppHandle<R> {
  428. self.handle.clone()
  429. }
  430. /// Sets the activation policy for the application. It is set to `NSApplicationActivationPolicyRegular` by default.
  431. ///
  432. /// # Examples
  433. /// ```rust,no_run
  434. /// let mut app = tauri::Builder::default()
  435. /// // on an actual app, remove the string argument
  436. /// .build(tauri::generate_context!("test/fixture/src-tauri/tauri.conf.json"))
  437. /// .expect("error while building tauri application");
  438. /// #[cfg(target_os = "macos")]
  439. /// app.set_activation_policy(tauri::ActivationPolicy::Accessory);
  440. /// app.run(|_app_handle, _event| {});
  441. /// ```
  442. #[cfg(target_os = "macos")]
  443. #[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
  444. pub fn set_activation_policy(&mut self, activation_policy: ActivationPolicy) {
  445. self
  446. .runtime
  447. .as_mut()
  448. .unwrap()
  449. .set_activation_policy(activation_policy);
  450. }
  451. /// Runs the application.
  452. ///
  453. /// # Examples
  454. /// ```rust,no_run
  455. /// let app = tauri::Builder::default()
  456. /// // on an actual app, remove the string argument
  457. /// .build(tauri::generate_context!("test/fixture/src-tauri/tauri.conf.json"))
  458. /// .expect("error while building tauri application");
  459. /// app.run(|_app_handle, event| match event {
  460. /// tauri::RunEvent::ExitRequested { api, .. } => {
  461. /// api.prevent_exit();
  462. /// }
  463. /// _ => {}
  464. /// });
  465. /// ```
  466. pub fn run<F: FnMut(&AppHandle<R>, RunEvent) + 'static>(mut self, mut callback: F) {
  467. let app_handle = self.handle();
  468. let manager = self.manager.clone();
  469. self.runtime.take().unwrap().run(move |event| match event {
  470. RuntimeRunEvent::Exit => {
  471. app_handle.cleanup_before_exit();
  472. on_event_loop_event(
  473. &app_handle,
  474. RuntimeRunEvent::Exit,
  475. &manager,
  476. Some(&mut callback),
  477. );
  478. }
  479. _ => {
  480. on_event_loop_event(&app_handle, event, &manager, Some(&mut callback));
  481. }
  482. });
  483. }
  484. /// Runs a iteration of the runtime event loop and immediately return.
  485. ///
  486. /// Note that when using this API, app cleanup is not automatically done.
  487. /// The cleanup calls [`crate::api::process::kill_children`] so you may want to call that function before exiting the application.
  488. /// Additionally, the cleanup calls [AppHandle#remove_system_tray](`AppHandle#method.remove_system_tray`) (Windows only).
  489. ///
  490. /// # Examples
  491. /// ```rust,no_run
  492. /// let mut app = tauri::Builder::default()
  493. /// // on an actual app, remove the string argument
  494. /// .build(tauri::generate_context!("test/fixture/src-tauri/tauri.conf.json"))
  495. /// .expect("error while building tauri application");
  496. /// loop {
  497. /// let iteration = app.run_iteration();
  498. /// if iteration.window_count == 0 {
  499. /// break;
  500. /// }
  501. /// }
  502. /// ```
  503. pub fn run_iteration(&mut self) -> crate::runtime::RunIteration {
  504. let manager = self.manager.clone();
  505. let app_handle = self.handle();
  506. self.runtime.as_mut().unwrap().run_iteration(move |event| {
  507. on_event_loop_event(
  508. &app_handle,
  509. event,
  510. &manager,
  511. Option::<&mut Box<dyn FnMut(&AppHandle<R>, RunEvent)>>::None,
  512. )
  513. })
  514. }
  515. }
  516. #[cfg(feature = "updater")]
  517. impl<R: Runtime> App<R> {
  518. /// Runs the updater hook with built-in dialog.
  519. fn run_updater_dialog(&self) {
  520. let updater_config = self.manager.config().tauri.updater.clone();
  521. let package_info = self.manager.package_info().clone();
  522. let handle = self.handle();
  523. crate::async_runtime::spawn(async move {
  524. updater::check_update_with_dialog(updater_config, package_info, handle).await
  525. });
  526. }
  527. fn run_updater(&self) {
  528. let handle = self.handle();
  529. let handle_ = handle.clone();
  530. let updater_config = self.manager.config().tauri.updater.clone();
  531. // check if updater is active or not
  532. if updater_config.active {
  533. if updater_config.dialog {
  534. // if updater dialog is enabled spawn a new task
  535. self.run_updater_dialog();
  536. let config = self.manager.config().tauri.updater.clone();
  537. let package_info = self.manager.package_info().clone();
  538. // When dialog is enabled, if user want to recheck
  539. // if an update is available after first start
  540. // invoke the Event `tauri://update` from JS or rust side.
  541. handle.listen_global(updater::EVENT_CHECK_UPDATE, move |_msg| {
  542. let handle = handle_.clone();
  543. let package_info = package_info.clone();
  544. let config = config.clone();
  545. // re-spawn task inside tokyo to launch the download
  546. // we don't need to emit anything as everything is handled
  547. // by the process (user is asked to restart at the end)
  548. // and it's handled by the updater
  549. crate::async_runtime::spawn(async move {
  550. updater::check_update_with_dialog(config, package_info, handle).await
  551. });
  552. });
  553. } else {
  554. // we only listen for `tauri://update`
  555. // once we receive the call, we check if an update is available or not
  556. // if there is a new update we emit `tauri://update-available` with details
  557. // this is the user responsabilities to display dialog and ask if user want to install
  558. // to install the update you need to invoke the Event `tauri://update-install`
  559. updater::listener(handle);
  560. }
  561. }
  562. }
  563. }
  564. /// Builds a Tauri application.
  565. ///
  566. /// # Examples
  567. /// ```rust,no_run
  568. /// tauri::Builder::default()
  569. /// // on an actual app, remove the string argument
  570. /// .run(tauri::generate_context!("test/fixture/src-tauri/tauri.conf.json"))
  571. /// .expect("error while running tauri application");
  572. /// ```
  573. #[allow(clippy::type_complexity)]
  574. pub struct Builder<R: Runtime> {
  575. /// A flag indicating that the runtime must be started on an environment that supports the event loop not on the main thread.
  576. #[cfg(any(windows, target_os = "linux"))]
  577. #[cfg_attr(doc_cfg, doc(any(windows, target_os = "linux")))]
  578. runtime_any_thread: bool,
  579. /// The JS message handler.
  580. invoke_handler: Box<InvokeHandler<R>>,
  581. /// The JS message responder.
  582. invoke_responder: Arc<InvokeResponder<R>>,
  583. /// The script that initializes the `window.__TAURI_POST_MESSAGE__` function.
  584. invoke_initialization_script: String,
  585. /// The setup hook.
  586. setup: SetupHook<R>,
  587. /// Page load hook.
  588. on_page_load: Box<OnPageLoad<R>>,
  589. /// windows to create when starting up.
  590. pending_windows: Vec<PendingWindow<EventLoopMessage, R>>,
  591. /// All passed plugins
  592. plugins: PluginStore<R>,
  593. /// The webview protocols available to all windows.
  594. uri_scheme_protocols: HashMap<String, Arc<CustomProtocol<R>>>,
  595. /// App state.
  596. state: StateManager,
  597. /// The menu set to all windows.
  598. menu: Option<Menu>,
  599. /// Menu event handlers that listens to all windows.
  600. menu_event_listeners: Vec<GlobalMenuEventListener<R>>,
  601. /// Window event handlers that listens to all windows.
  602. window_event_listeners: Vec<GlobalWindowEventListener<R>>,
  603. /// The app system tray.
  604. #[cfg(feature = "system-tray")]
  605. system_tray: Option<tray::SystemTray>,
  606. /// System tray event handlers.
  607. #[cfg(feature = "system-tray")]
  608. system_tray_event_listeners: Vec<SystemTrayEventListener<R>>,
  609. }
  610. impl<R: Runtime> Builder<R> {
  611. /// Creates a new App builder.
  612. pub fn new() -> Self {
  613. Self {
  614. #[cfg(any(windows, target_os = "linux"))]
  615. runtime_any_thread: false,
  616. setup: Box::new(|_| Ok(())),
  617. invoke_handler: Box::new(|_| ()),
  618. invoke_responder: Arc::new(window_invoke_responder),
  619. invoke_initialization_script:
  620. "Object.defineProperty(window, '__TAURI_POST_MESSAGE__', { value: (message) => window.ipc.postMessage(JSON.stringify(message)) })".into(),
  621. on_page_load: Box::new(|_, _| ()),
  622. pending_windows: Default::default(),
  623. plugins: PluginStore::default(),
  624. uri_scheme_protocols: Default::default(),
  625. state: StateManager::new(),
  626. menu: None,
  627. menu_event_listeners: Vec::new(),
  628. window_event_listeners: Vec::new(),
  629. #[cfg(feature = "system-tray")]
  630. system_tray: None,
  631. #[cfg(feature = "system-tray")]
  632. system_tray_event_listeners: Vec::new(),
  633. }
  634. }
  635. /// Builds a new Tauri application running on any thread, bypassing the main thread requirement.
  636. ///
  637. /// ## Platform-specific
  638. ///
  639. /// - **macOS**: on macOS the application *must* be executed on the main thread, so this function is not exposed.
  640. #[cfg(any(windows, target_os = "linux"))]
  641. #[cfg_attr(doc_cfg, doc(any(windows, target_os = "linux")))]
  642. #[must_use]
  643. pub fn any_thread(mut self) -> Self {
  644. self.runtime_any_thread = true;
  645. self
  646. }
  647. /// Defines the JS message handler callback.
  648. ///
  649. /// # Examples
  650. /// ```rust,no_run
  651. /// #[tauri::command]
  652. /// fn command_1() -> String {
  653. /// return "hello world".to_string();
  654. /// }
  655. /// tauri::Builder::default()
  656. /// .invoke_handler(tauri::generate_handler![
  657. /// command_1,
  658. /// // etc...
  659. /// ]);
  660. /// ```
  661. #[must_use]
  662. pub fn invoke_handler<F>(mut self, invoke_handler: F) -> Self
  663. where
  664. F: Fn(Invoke<R>) + Send + Sync + 'static,
  665. {
  666. self.invoke_handler = Box::new(invoke_handler);
  667. self
  668. }
  669. /// Defines a custom JS message system.
  670. ///
  671. /// The `responder` is a function that will be called when a command has been executed and must send a response to the JS layer.
  672. ///
  673. /// The `initialization_script` is a script that initializes `window.__TAURI_POST_MESSAGE__`.
  674. /// That function must take the `message: object` argument and send it to the backend.
  675. #[must_use]
  676. pub fn invoke_system<F>(mut self, initialization_script: String, responder: F) -> Self
  677. where
  678. F: Fn(Window<R>, InvokeResponse, CallbackFn, CallbackFn) + Send + Sync + 'static,
  679. {
  680. self.invoke_initialization_script = initialization_script;
  681. self.invoke_responder = Arc::new(responder);
  682. self
  683. }
  684. /// Defines the setup hook.
  685. ///
  686. /// # Examples
  687. /// ```rust,no_run
  688. /// use tauri::Manager;
  689. /// tauri::Builder::default()
  690. /// .setup(|app| {
  691. /// let main_window = app.get_window("main").unwrap();
  692. #[cfg_attr(
  693. feature = "dialog",
  694. doc = r#" tauri::api::dialog::blocking::message(Some(&main_window), "Hello", "Welcome back!");"#
  695. )]
  696. /// Ok(())
  697. /// });
  698. /// ```
  699. #[must_use]
  700. pub fn setup<F>(mut self, setup: F) -> Self
  701. where
  702. F: FnOnce(&mut App<R>) -> Result<(), Box<dyn std::error::Error + Send>> + Send + 'static,
  703. {
  704. self.setup = Box::new(setup);
  705. self
  706. }
  707. /// Defines the page load hook.
  708. #[must_use]
  709. pub fn on_page_load<F>(mut self, on_page_load: F) -> Self
  710. where
  711. F: Fn(Window<R>, PageLoadPayload) + Send + Sync + 'static,
  712. {
  713. self.on_page_load = Box::new(on_page_load);
  714. self
  715. }
  716. /// Adds a plugin to the runtime.
  717. #[must_use]
  718. pub fn plugin<P: Plugin<R> + 'static>(mut self, plugin: P) -> Self {
  719. self.plugins.register(plugin);
  720. self
  721. }
  722. /// Add `state` to the state managed by the application.
  723. ///
  724. /// This method can be called any number of times as long as each call
  725. /// refers to a different `T`.
  726. ///
  727. /// Managed state can be retrieved by any command handler via the
  728. /// [`State`](crate::State) guard. In particular, if a value of type `T`
  729. /// is managed by Tauri, adding `State<T>` to the list of arguments in a
  730. /// command handler instructs Tauri to retrieve the managed value.
  731. ///
  732. /// # Panics
  733. ///
  734. /// Panics if state of type `T` is already being managed.
  735. ///
  736. /// # Mutability
  737. ///
  738. /// Since the managed state is global and must be [`Send`] + [`Sync`], mutations can only happen through interior mutability:
  739. ///
  740. /// ```rust,no_run
  741. /// use std::{collections::HashMap, sync::Mutex};
  742. /// use tauri::State;
  743. /// // here we use Mutex to achieve interior mutability
  744. /// struct Storage {
  745. /// store: Mutex<HashMap<u64, String>>,
  746. /// }
  747. /// struct Connection;
  748. /// struct DbConnection {
  749. /// db: Mutex<Option<Connection>>,
  750. /// }
  751. ///
  752. /// #[tauri::command]
  753. /// fn connect(connection: State<DbConnection>) {
  754. /// // initialize the connection, mutating the state with interior mutability
  755. /// *connection.db.lock().unwrap() = Some(Connection {});
  756. /// }
  757. ///
  758. /// #[tauri::command]
  759. /// fn storage_insert(key: u64, value: String, storage: State<Storage>) {
  760. /// // mutate the storage behind the Mutex
  761. /// storage.store.lock().unwrap().insert(key, value);
  762. /// }
  763. ///
  764. /// tauri::Builder::default()
  765. /// .manage(Storage { store: Default::default() })
  766. /// .manage(DbConnection { db: Default::default() })
  767. /// .invoke_handler(tauri::generate_handler![connect, storage_insert])
  768. /// // on an actual app, remove the string argument
  769. /// .run(tauri::generate_context!("test/fixture/src-tauri/tauri.conf.json"))
  770. /// .expect("error while running tauri application");
  771. /// ```
  772. ///
  773. /// # Examples
  774. ///
  775. /// ```rust,no_run
  776. /// use tauri::State;
  777. ///
  778. /// struct MyInt(isize);
  779. /// struct MyString(String);
  780. ///
  781. /// #[tauri::command]
  782. /// fn int_command(state: State<MyInt>) -> String {
  783. /// format!("The stateful int is: {}", state.0)
  784. /// }
  785. ///
  786. /// #[tauri::command]
  787. /// fn string_command<'r>(state: State<'r, MyString>) {
  788. /// println!("state: {}", state.inner().0);
  789. /// }
  790. ///
  791. /// tauri::Builder::default()
  792. /// .manage(MyInt(10))
  793. /// .manage(MyString("Hello, managed state!".to_string()))
  794. /// .invoke_handler(tauri::generate_handler![int_command, string_command])
  795. /// // on an actual app, remove the string argument
  796. /// .run(tauri::generate_context!("test/fixture/src-tauri/tauri.conf.json"))
  797. /// .expect("error while running tauri application");
  798. /// ```
  799. #[must_use]
  800. pub fn manage<T>(self, state: T) -> Self
  801. where
  802. T: Send + Sync + 'static,
  803. {
  804. let type_name = std::any::type_name::<T>();
  805. assert!(
  806. self.state.set(state),
  807. "state for type '{}' is already being managed",
  808. type_name
  809. );
  810. self
  811. }
  812. /// Creates a new webview window.
  813. ///
  814. /// # Examples
  815. /// ```rust,no_run
  816. /// use tauri::WindowBuilder;
  817. /// tauri::Builder::default()
  818. /// .create_window("main", tauri::WindowUrl::default(), |win, webview| {
  819. /// let win = win
  820. /// .title("My Main Window")
  821. /// .resizable(true)
  822. /// .inner_size(800.0, 550.0)
  823. /// .min_inner_size(400.0, 200.0);
  824. /// return (win, webview);
  825. /// });
  826. /// ```
  827. pub fn create_window<F>(
  828. mut self,
  829. label: impl Into<String>,
  830. url: WindowUrl,
  831. setup: F,
  832. ) -> crate::Result<Self>
  833. where
  834. F: FnOnce(
  835. <R::Dispatcher as Dispatch<EventLoopMessage>>::WindowBuilder,
  836. WebviewAttributes,
  837. ) -> (
  838. <R::Dispatcher as Dispatch<EventLoopMessage>>::WindowBuilder,
  839. WebviewAttributes,
  840. ),
  841. {
  842. let (window_builder, webview_attributes) = setup(
  843. <R::Dispatcher as Dispatch<EventLoopMessage>>::WindowBuilder::new(),
  844. WebviewAttributes::new(url),
  845. );
  846. self.pending_windows.push(PendingWindow::new(
  847. window_builder,
  848. webview_attributes,
  849. label,
  850. )?);
  851. Ok(self)
  852. }
  853. /// Adds the icon configured on `tauri.conf.json` to the system tray with the specified menu items.
  854. #[cfg(feature = "system-tray")]
  855. #[cfg_attr(doc_cfg, doc(cfg(feature = "system-tray")))]
  856. #[must_use]
  857. pub fn system_tray(mut self, system_tray: tray::SystemTray) -> Self {
  858. self.system_tray.replace(system_tray);
  859. self
  860. }
  861. /// Sets the menu to use on all windows.
  862. ///
  863. /// # Examples
  864. /// ```rust,no_run
  865. /// use tauri::{MenuEntry, Submenu, MenuItem, Menu, CustomMenuItem};
  866. ///
  867. /// tauri::Builder::default()
  868. /// .menu(Menu::with_items([
  869. /// MenuEntry::Submenu(Submenu::new(
  870. /// "File",
  871. /// Menu::with_items([
  872. /// MenuItem::CloseWindow.into(),
  873. /// #[cfg(target_os = "macos")]
  874. /// CustomMenuItem::new("hello", "Hello").into(),
  875. /// ]),
  876. /// )),
  877. /// ]));
  878. /// ```
  879. #[must_use]
  880. pub fn menu(mut self, menu: Menu) -> Self {
  881. self.menu.replace(menu);
  882. self
  883. }
  884. /// Registers a menu event handler for all windows.
  885. ///
  886. /// # Examples
  887. /// ```rust,no_run
  888. /// use tauri::{Menu, MenuEntry, Submenu, CustomMenuItem, api, Manager};
  889. /// tauri::Builder::default()
  890. /// .menu(Menu::with_items([
  891. /// MenuEntry::Submenu(Submenu::new(
  892. /// "File",
  893. /// Menu::with_items([
  894. /// CustomMenuItem::new("New", "New").into(),
  895. /// CustomMenuItem::new("Learn More", "Learn More").into(),
  896. /// ]),
  897. /// )),
  898. /// ]))
  899. /// .on_menu_event(|event| {
  900. /// match event.menu_item_id() {
  901. /// "Learn More" => {
  902. /// // open in browser (requires the `shell-open-api` feature)
  903. #[cfg_attr(
  904. feature = "shell-open-api",
  905. doc = r#" api::shell::open(&event.window().shell_scope(), "https://github.com/tauri-apps/tauri".to_string(), None).unwrap();"#
  906. )]
  907. /// }
  908. /// id => {
  909. /// // do something with other events
  910. /// println!("got menu event: {}", id);
  911. /// }
  912. /// }
  913. /// });
  914. /// ```
  915. #[must_use]
  916. pub fn on_menu_event<F: Fn(WindowMenuEvent<R>) + Send + Sync + 'static>(
  917. mut self,
  918. handler: F,
  919. ) -> Self {
  920. self.menu_event_listeners.push(Box::new(handler));
  921. self
  922. }
  923. /// Registers a window event handler for all windows.
  924. ///
  925. /// # Examples
  926. /// ```rust,no_run
  927. /// tauri::Builder::default()
  928. /// .on_window_event(|event| match event.event() {
  929. /// tauri::WindowEvent::Focused(focused) => {
  930. /// // hide window whenever it loses focus
  931. /// if !focused {
  932. /// event.window().hide().unwrap();
  933. /// }
  934. /// }
  935. /// _ => {}
  936. /// });
  937. /// ```
  938. #[must_use]
  939. pub fn on_window_event<F: Fn(GlobalWindowEvent<R>) + Send + Sync + 'static>(
  940. mut self,
  941. handler: F,
  942. ) -> Self {
  943. self.window_event_listeners.push(Box::new(handler));
  944. self
  945. }
  946. /// Registers a system tray event handler.
  947. ///
  948. /// # Examples
  949. /// ```rust,no_run
  950. /// use tauri::Manager;
  951. /// tauri::Builder::default()
  952. /// .on_system_tray_event(|app, event| match event {
  953. /// // show window with id "main" when the tray is left clicked
  954. /// tauri::SystemTrayEvent::LeftClick { .. } => {
  955. /// let window = app.get_window("main").unwrap();
  956. /// window.show().unwrap();
  957. /// window.set_focus().unwrap();
  958. /// }
  959. /// _ => {}
  960. /// });
  961. /// ```
  962. #[cfg(feature = "system-tray")]
  963. #[cfg_attr(doc_cfg, doc(cfg(feature = "system-tray")))]
  964. #[must_use]
  965. pub fn on_system_tray_event<
  966. F: Fn(&AppHandle<R>, tray::SystemTrayEvent) + Send + Sync + 'static,
  967. >(
  968. mut self,
  969. handler: F,
  970. ) -> Self {
  971. self.system_tray_event_listeners.push(Box::new(handler));
  972. self
  973. }
  974. /// Registers a URI scheme protocol available to all webviews.
  975. /// Leverages [setURLSchemeHandler](https://developer.apple.com/documentation/webkit/wkwebviewconfiguration/2875766-seturlschemehandler) on macOS,
  976. /// [AddWebResourceRequestedFilter](https://docs.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2.addwebresourcerequestedfilter?view=webview2-dotnet-1.0.774.44) on Windows
  977. /// and [webkit-web-context-register-uri-scheme](https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebContext.html#webkit-web-context-register-uri-scheme) on Linux.
  978. ///
  979. /// # Arguments
  980. ///
  981. /// * `uri_scheme` The URI scheme to register, such as `example`.
  982. /// * `protocol` the protocol associated with the given URI scheme. It's a function that takes an URL such as `example://localhost/asset.css`.
  983. #[must_use]
  984. pub fn register_uri_scheme_protocol<
  985. N: Into<String>,
  986. H: Fn(&AppHandle<R>, &HttpRequest) -> Result<HttpResponse, Box<dyn std::error::Error>>
  987. + Send
  988. + Sync
  989. + 'static,
  990. >(
  991. mut self,
  992. uri_scheme: N,
  993. protocol: H,
  994. ) -> Self {
  995. self.uri_scheme_protocols.insert(
  996. uri_scheme.into(),
  997. Arc::new(CustomProtocol {
  998. protocol: Box::new(protocol),
  999. }),
  1000. );
  1001. self
  1002. }
  1003. /// Builds the application.
  1004. #[allow(clippy::type_complexity)]
  1005. pub fn build<A: Assets>(mut self, context: Context<A>) -> crate::Result<App<R>> {
  1006. #[cfg(feature = "system-tray")]
  1007. let system_tray_icon = {
  1008. let icon = context.system_tray_icon.clone();
  1009. // check the icon format if the system tray is configured
  1010. if self.system_tray.is_some() {
  1011. use std::io::{Error, ErrorKind};
  1012. #[cfg(target_os = "linux")]
  1013. if let Some(TrayIcon::Raw(..)) = icon {
  1014. return Err(crate::Error::InvalidIcon(Box::new(Error::new(
  1015. ErrorKind::InvalidInput,
  1016. "system tray icons on linux must be a file path",
  1017. ))));
  1018. }
  1019. #[cfg(not(target_os = "linux"))]
  1020. if let Some(TrayIcon::File(_)) = icon {
  1021. return Err(crate::Error::InvalidIcon(Box::new(Error::new(
  1022. ErrorKind::InvalidInput,
  1023. "system tray icons on non-linux platforms must be the raw bytes",
  1024. ))));
  1025. }
  1026. }
  1027. icon
  1028. };
  1029. #[cfg(all(feature = "system-tray", target_os = "macos"))]
  1030. let system_tray_icon_as_template = context
  1031. .config
  1032. .tauri
  1033. .system_tray
  1034. .as_ref()
  1035. .map(|t| t.icon_as_template)
  1036. .unwrap_or_default();
  1037. #[cfg(shell_scope)]
  1038. let shell_scope = context.shell_scope.clone();
  1039. let manager = WindowManager::with_handlers(
  1040. context,
  1041. self.plugins,
  1042. self.invoke_handler,
  1043. self.on_page_load,
  1044. self.uri_scheme_protocols,
  1045. self.state,
  1046. self.window_event_listeners,
  1047. (self.menu, self.menu_event_listeners),
  1048. (self.invoke_responder, self.invoke_initialization_script),
  1049. );
  1050. // set up all the windows defined in the config
  1051. for config in manager.config().tauri.windows.clone() {
  1052. let url = config.url.clone();
  1053. let label = config.label.clone();
  1054. let file_drop_enabled = config.file_drop_enabled;
  1055. let mut webview_attributes = WebviewAttributes::new(url);
  1056. if !file_drop_enabled {
  1057. webview_attributes = webview_attributes.disable_file_drop_handler();
  1058. }
  1059. self.pending_windows.push(PendingWindow::with_config(
  1060. config,
  1061. webview_attributes,
  1062. label,
  1063. )?);
  1064. }
  1065. #[cfg(any(windows, target_os = "linux"))]
  1066. let runtime = if self.runtime_any_thread {
  1067. R::new_any_thread()?
  1068. } else {
  1069. R::new()?
  1070. };
  1071. #[cfg(not(any(windows, target_os = "linux")))]
  1072. let runtime = R::new()?;
  1073. let runtime_handle = runtime.handle();
  1074. let global_shortcut_manager = runtime.global_shortcut_manager();
  1075. let clipboard_manager = runtime.clipboard_manager();
  1076. let mut app = App {
  1077. runtime: Some(runtime),
  1078. manager: manager.clone(),
  1079. global_shortcut_manager: global_shortcut_manager.clone(),
  1080. clipboard_manager: clipboard_manager.clone(),
  1081. #[cfg(feature = "system-tray")]
  1082. tray_handle: None,
  1083. handle: AppHandle {
  1084. runtime_handle,
  1085. manager,
  1086. global_shortcut_manager,
  1087. clipboard_manager,
  1088. #[cfg(feature = "system-tray")]
  1089. tray_handle: None,
  1090. },
  1091. };
  1092. let env = Env::default();
  1093. app.manage(Scopes {
  1094. fs: FsScope::for_fs_api(
  1095. &app.manager.config(),
  1096. app.package_info(),
  1097. &env,
  1098. &app.config().tauri.allowlist.fs.scope,
  1099. )?,
  1100. #[cfg(protocol_asset)]
  1101. asset_protocol: FsScope::for_fs_api(
  1102. &app.manager.config(),
  1103. app.package_info(),
  1104. &env,
  1105. &app.config().tauri.allowlist.protocol.asset_scope,
  1106. )?,
  1107. #[cfg(http_request)]
  1108. http: crate::scope::HttpScope::for_http_api(&app.config().tauri.allowlist.http.scope),
  1109. #[cfg(shell_scope)]
  1110. shell: ShellScope::new(shell_scope),
  1111. });
  1112. app.manage(env);
  1113. #[cfg(windows)]
  1114. {
  1115. if let Some(w) = &app
  1116. .manager
  1117. .config()
  1118. .tauri
  1119. .bundle
  1120. .windows
  1121. .webview_fixed_runtime_path
  1122. {
  1123. if let Some(resource_dir) = app.path_resolver().resource_dir() {
  1124. std::env::set_var("WEBVIEW2_BROWSER_EXECUTABLE_FOLDER", resource_dir.join(w));
  1125. } else {
  1126. #[cfg(debug_assertions)]
  1127. eprintln!(
  1128. "failed to resolve resource directory; fallback to the installed Webview2 runtime."
  1129. );
  1130. }
  1131. }
  1132. }
  1133. #[cfg(feature = "system-tray")]
  1134. if let Some(system_tray) = self.system_tray {
  1135. let mut ids = HashMap::new();
  1136. if let Some(menu) = system_tray.menu() {
  1137. tray::get_menu_ids(&mut ids, menu);
  1138. }
  1139. let mut tray = tray::SystemTray::new();
  1140. if let Some(menu) = system_tray.menu {
  1141. tray = tray.with_menu(menu);
  1142. }
  1143. #[cfg(not(target_os = "macos"))]
  1144. let tray_handler = app
  1145. .runtime
  1146. .as_ref()
  1147. .unwrap()
  1148. .system_tray(
  1149. tray.with_icon(
  1150. system_tray
  1151. .icon
  1152. .or(system_tray_icon)
  1153. .expect("tray icon not found; please configure it on tauri.conf.json"),
  1154. ),
  1155. )
  1156. .expect("failed to run tray");
  1157. #[cfg(target_os = "macos")]
  1158. let tray_handler = app
  1159. .runtime
  1160. .as_ref()
  1161. .unwrap()
  1162. .system_tray(
  1163. tray
  1164. .with_icon(
  1165. system_tray
  1166. .icon
  1167. .or(system_tray_icon)
  1168. .expect("tray icon not found; please configure it on tauri.conf.json"),
  1169. )
  1170. .with_icon_as_template(system_tray_icon_as_template),
  1171. )
  1172. .expect("failed to run tray");
  1173. let tray_handle = tray::SystemTrayHandle {
  1174. ids: Arc::new(std::sync::Mutex::new(ids)),
  1175. inner: tray_handler,
  1176. };
  1177. let ids = tray_handle.ids.clone();
  1178. app.tray_handle.replace(tray_handle.clone());
  1179. app.handle.tray_handle.replace(tray_handle);
  1180. for listener in self.system_tray_event_listeners {
  1181. let app_handle = app.handle();
  1182. let ids = ids.clone();
  1183. let listener = Arc::new(std::sync::Mutex::new(listener));
  1184. app
  1185. .runtime
  1186. .as_mut()
  1187. .unwrap()
  1188. .on_system_tray_event(move |event| {
  1189. let app_handle = app_handle.clone();
  1190. let event = match event {
  1191. RuntimeSystemTrayEvent::MenuItemClick(id) => tray::SystemTrayEvent::MenuItemClick {
  1192. id: ids.lock().unwrap().get(id).unwrap().clone(),
  1193. },
  1194. RuntimeSystemTrayEvent::LeftClick { position, size } => {
  1195. tray::SystemTrayEvent::LeftClick {
  1196. position: *position,
  1197. size: *size,
  1198. }
  1199. }
  1200. RuntimeSystemTrayEvent::RightClick { position, size } => {
  1201. tray::SystemTrayEvent::RightClick {
  1202. position: *position,
  1203. size: *size,
  1204. }
  1205. }
  1206. RuntimeSystemTrayEvent::DoubleClick { position, size } => {
  1207. tray::SystemTrayEvent::DoubleClick {
  1208. position: *position,
  1209. size: *size,
  1210. }
  1211. }
  1212. };
  1213. let listener = listener.clone();
  1214. listener.lock().unwrap()(&app_handle, event);
  1215. });
  1216. }
  1217. }
  1218. app.manager.initialize_plugins(&app.handle())?;
  1219. let window_labels = self
  1220. .pending_windows
  1221. .iter()
  1222. .map(|p| p.label.clone())
  1223. .collect::<Vec<_>>();
  1224. for pending in self.pending_windows {
  1225. let pending =
  1226. app
  1227. .manager
  1228. .prepare_window(app.handle.clone(), pending, &window_labels, None)?;
  1229. let detached = app.runtime.as_ref().unwrap().create_window(pending)?;
  1230. let _window = app.manager.attach_window(app.handle(), detached);
  1231. }
  1232. (self.setup)(&mut app).map_err(|e| crate::Error::Setup(e))?;
  1233. #[cfg(feature = "updater")]
  1234. app.run_updater();
  1235. Ok(app)
  1236. }
  1237. /// Runs the configured Tauri application.
  1238. pub fn run<A: Assets>(self, context: Context<A>) -> crate::Result<()> {
  1239. self.build(context)?.run(|_, _| {});
  1240. Ok(())
  1241. }
  1242. }
  1243. fn on_event_loop_event<R: Runtime, F: FnMut(&AppHandle<R>, RunEvent) + 'static>(
  1244. app_handle: &AppHandle<R>,
  1245. event: RuntimeRunEvent<EventLoopMessage>,
  1246. manager: &WindowManager<R>,
  1247. callback: Option<&mut F>,
  1248. ) {
  1249. if let RuntimeRunEvent::WindowClose(label) = &event {
  1250. manager.on_window_close(label);
  1251. }
  1252. let event = match event {
  1253. RuntimeRunEvent::Exit => RunEvent::Exit,
  1254. RuntimeRunEvent::ExitRequested { window_label, tx } => RunEvent::ExitRequested {
  1255. window_label,
  1256. api: ExitRequestApi(tx),
  1257. },
  1258. RuntimeRunEvent::CloseRequested { label, signal_tx } => RunEvent::CloseRequested {
  1259. label,
  1260. api: CloseRequestApi(signal_tx),
  1261. },
  1262. RuntimeRunEvent::WindowClose(label) => RunEvent::WindowClosed(label),
  1263. RuntimeRunEvent::Ready => RunEvent::Ready,
  1264. RuntimeRunEvent::Resumed => RunEvent::Resumed,
  1265. RuntimeRunEvent::MainEventsCleared => RunEvent::MainEventsCleared,
  1266. RuntimeRunEvent::UserEvent(t) => t.into(),
  1267. _ => unimplemented!(),
  1268. };
  1269. manager
  1270. .inner
  1271. .plugins
  1272. .lock()
  1273. .expect("poisoned plugin store")
  1274. .on_event(app_handle, &event);
  1275. if let Some(c) = callback {
  1276. c(app_handle, event);
  1277. }
  1278. }
  1279. /// Make `Wry` the default `Runtime` for `Builder`
  1280. #[cfg(feature = "wry")]
  1281. #[cfg_attr(doc_cfg, doc(cfg(feature = "wry")))]
  1282. impl Default for Builder<crate::Wry> {
  1283. fn default() -> Self {
  1284. Self::new()
  1285. }
  1286. }
  1287. #[cfg(test)]
  1288. mod tests {
  1289. #[test]
  1290. fn is_send_sync() {
  1291. crate::test_utils::assert_send::<super::AppHandle>();
  1292. crate::test_utils::assert_sync::<super::AppHandle>();
  1293. #[cfg(feature = "wry")]
  1294. {
  1295. crate::test_utils::assert_send::<super::AssetResolver<crate::Wry>>();
  1296. crate::test_utils::assert_sync::<super::AssetResolver<crate::Wry>>();
  1297. }
  1298. crate::test_utils::assert_send::<super::PathResolver>();
  1299. crate::test_utils::assert_sync::<super::PathResolver>();
  1300. }
  1301. }