app.rs 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110
  1. // Copyright 2019-2024 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. use crate::{
  5. image::Image,
  6. ipc::{
  7. channel::ChannelDataIpcQueue, CallbackFn, CommandArg, CommandItem, Invoke, InvokeError,
  8. InvokeHandler, InvokeResponder, InvokeResponse,
  9. },
  10. manager::{webview::UriSchemeProtocol, AppManager, Asset},
  11. plugin::{Plugin, PluginStore},
  12. resources::ResourceTable,
  13. runtime::{
  14. window::{WebviewEvent as RuntimeWebviewEvent, WindowEvent as RuntimeWindowEvent},
  15. ExitRequestedEventAction, RunEvent as RuntimeRunEvent,
  16. },
  17. sealed::{ManagerBase, RuntimeOrDispatch},
  18. utils::config::Config,
  19. utils::Env,
  20. webview::PageLoadPayload,
  21. Context, DeviceEventFilter, Emitter, EventLoopMessage, Listener, Manager, Monitor, Result,
  22. Runtime, Scopes, StateManager, Theme, Webview, WebviewWindowBuilder, Window,
  23. };
  24. #[cfg(desktop)]
  25. use crate::menu::{Menu, MenuEvent};
  26. #[cfg(all(desktop, feature = "tray-icon"))]
  27. use crate::tray::{TrayIcon, TrayIconBuilder, TrayIconEvent, TrayIconId};
  28. use raw_window_handle::HasDisplayHandle;
  29. use serialize_to_javascript::{default_template, DefaultTemplate, Template};
  30. use tauri_macros::default_runtime;
  31. #[cfg(desktop)]
  32. use tauri_runtime::EventLoopProxy;
  33. use tauri_runtime::{
  34. dpi::{PhysicalPosition, PhysicalSize},
  35. window::DragDropEvent,
  36. RuntimeInitArgs,
  37. };
  38. use tauri_utils::PackageInfo;
  39. use serde::Serialize;
  40. use std::{
  41. borrow::Cow,
  42. collections::HashMap,
  43. fmt,
  44. sync::{mpsc::Sender, Arc, MutexGuard},
  45. };
  46. use crate::{event::EventId, runtime::RuntimeHandle, Event, EventTarget};
  47. #[cfg(target_os = "macos")]
  48. use crate::ActivationPolicy;
  49. pub(crate) mod plugin;
  50. #[cfg(desktop)]
  51. pub(crate) type GlobalMenuEventListener<T> = Box<dyn Fn(&T, crate::menu::MenuEvent) + Send + Sync>;
  52. #[cfg(all(desktop, feature = "tray-icon"))]
  53. pub(crate) type GlobalTrayIconEventListener<T> =
  54. Box<dyn Fn(&T, crate::tray::TrayIconEvent) + Send + Sync>;
  55. pub(crate) type GlobalWindowEventListener<R> = Box<dyn Fn(&Window<R>, &WindowEvent) + Send + Sync>;
  56. pub(crate) type GlobalWebviewEventListener<R> =
  57. Box<dyn Fn(&Webview<R>, &WebviewEvent) + Send + Sync>;
  58. /// A closure that is run when the Tauri application is setting up.
  59. pub type SetupHook<R> =
  60. Box<dyn FnOnce(&mut App<R>) -> std::result::Result<(), Box<dyn std::error::Error>> + Send>;
  61. /// A closure that is run every time a page starts or finishes loading.
  62. pub type OnPageLoad<R> = dyn Fn(&Webview<R>, &PageLoadPayload<'_>) + Send + Sync + 'static;
  63. /// The exit code on [`RunEvent::ExitRequested`] when [`AppHandle#method.restart`] is called.
  64. pub const RESTART_EXIT_CODE: i32 = i32::MAX;
  65. /// Api exposed on the `ExitRequested` event.
  66. #[derive(Debug)]
  67. pub struct ExitRequestApi(Sender<ExitRequestedEventAction>);
  68. impl ExitRequestApi {
  69. /// Prevents the app from exiting.
  70. ///
  71. /// **Note:** This is ignored when using [`AppHandle#method.restart`].
  72. pub fn prevent_exit(&self) {
  73. self.0.send(ExitRequestedEventAction::Prevent).unwrap();
  74. }
  75. }
  76. /// Api exposed on the `CloseRequested` event.
  77. #[derive(Debug, Clone)]
  78. pub struct CloseRequestApi(Sender<bool>);
  79. impl CloseRequestApi {
  80. /// Prevents the window from being closed.
  81. pub fn prevent_close(&self) {
  82. self.0.send(true).unwrap();
  83. }
  84. }
  85. /// An event from a window.
  86. #[derive(Debug, Clone)]
  87. #[non_exhaustive]
  88. pub enum WindowEvent {
  89. /// The size of the window has changed. Contains the client area's new dimensions.
  90. Resized(PhysicalSize<u32>),
  91. /// The position of the window has changed. Contains the window's new position.
  92. Moved(PhysicalPosition<i32>),
  93. /// The window has been requested to close.
  94. #[non_exhaustive]
  95. CloseRequested {
  96. /// An API modify the behavior of the close requested event.
  97. api: CloseRequestApi,
  98. },
  99. /// The window has been destroyed.
  100. Destroyed,
  101. /// The window gained or lost focus.
  102. ///
  103. /// The parameter is true if the window has gained focus, and false if it has lost focus.
  104. Focused(bool),
  105. /// The window's scale factor has changed.
  106. ///
  107. /// The following user actions can cause DPI changes:
  108. ///
  109. /// - Changing the display's resolution.
  110. /// - Changing the display's scale factor (e.g. in Control Panel on Windows).
  111. /// - Moving the window to a display with a different scale factor.
  112. #[non_exhaustive]
  113. ScaleFactorChanged {
  114. /// The new scale factor.
  115. scale_factor: f64,
  116. /// The window inner size.
  117. new_inner_size: PhysicalSize<u32>,
  118. },
  119. /// An event associated with the drag and drop action.
  120. DragDrop(DragDropEvent),
  121. /// The system window theme has changed. Only delivered if the window [`theme`](`crate::window::WindowBuilder#method.theme`) is `None`.
  122. ///
  123. /// Applications might wish to react to this to change the theme of the content of the window when the system changes the window theme.
  124. ///
  125. /// ## Platform-specific
  126. ///
  127. /// - **Linux**: Not supported.
  128. ThemeChanged(Theme),
  129. }
  130. impl From<RuntimeWindowEvent> for WindowEvent {
  131. fn from(event: RuntimeWindowEvent) -> Self {
  132. match event {
  133. RuntimeWindowEvent::Resized(size) => Self::Resized(size),
  134. RuntimeWindowEvent::Moved(position) => Self::Moved(position),
  135. RuntimeWindowEvent::CloseRequested { signal_tx } => Self::CloseRequested {
  136. api: CloseRequestApi(signal_tx),
  137. },
  138. RuntimeWindowEvent::Destroyed => Self::Destroyed,
  139. RuntimeWindowEvent::Focused(flag) => Self::Focused(flag),
  140. RuntimeWindowEvent::ScaleFactorChanged {
  141. scale_factor,
  142. new_inner_size,
  143. } => Self::ScaleFactorChanged {
  144. scale_factor,
  145. new_inner_size,
  146. },
  147. RuntimeWindowEvent::DragDrop(event) => Self::DragDrop(event),
  148. RuntimeWindowEvent::ThemeChanged(theme) => Self::ThemeChanged(theme),
  149. }
  150. }
  151. }
  152. /// An event from a window.
  153. #[derive(Debug, Clone)]
  154. #[non_exhaustive]
  155. pub enum WebviewEvent {
  156. /// An event associated with the drag and drop action.
  157. DragDrop(DragDropEvent),
  158. }
  159. impl From<RuntimeWebviewEvent> for WebviewEvent {
  160. fn from(event: RuntimeWebviewEvent) -> Self {
  161. match event {
  162. RuntimeWebviewEvent::DragDrop(e) => Self::DragDrop(e),
  163. }
  164. }
  165. }
  166. /// An application event, triggered from the event loop.
  167. ///
  168. /// See [`App::run`](crate::App#method.run) for usage examples.
  169. #[derive(Debug)]
  170. #[non_exhaustive]
  171. pub enum RunEvent {
  172. /// Event loop is exiting.
  173. Exit,
  174. /// The app is about to exit
  175. #[non_exhaustive]
  176. ExitRequested {
  177. /// Exit code.
  178. /// [`Option::None`] when the exit is requested by user interaction,
  179. /// [`Option::Some`] when requested programmatically via [`AppHandle#method.exit`] and [`AppHandle#method.restart`].
  180. code: Option<i32>,
  181. /// Event API
  182. api: ExitRequestApi,
  183. },
  184. /// An event associated with a window.
  185. #[non_exhaustive]
  186. WindowEvent {
  187. /// The window label.
  188. label: String,
  189. /// The detailed event.
  190. event: WindowEvent,
  191. },
  192. /// An event associated with a webview.
  193. #[non_exhaustive]
  194. WebviewEvent {
  195. /// The window label.
  196. label: String,
  197. /// The detailed event.
  198. event: WebviewEvent,
  199. },
  200. /// Application ready.
  201. Ready,
  202. /// Sent if the event loop is being resumed.
  203. Resumed,
  204. /// Emitted when all of the event loop's input events have been processed and redraw processing is about to begin.
  205. ///
  206. /// 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.
  207. MainEventsCleared,
  208. /// Emitted when the user wants to open the specified resource with the app.
  209. #[cfg(any(target_os = "macos", target_os = "ios"))]
  210. #[cfg_attr(docsrs, doc(cfg(any(target_os = "macos", feature = "ios"))))]
  211. Opened {
  212. /// The URL of the resources that is being open.
  213. urls: Vec<url::Url>,
  214. },
  215. /// An event from a menu item, could be on the window menu bar, application menu bar (on macOS) or tray icon menu.
  216. #[cfg(desktop)]
  217. #[cfg_attr(docsrs, doc(cfg(desktop)))]
  218. MenuEvent(crate::menu::MenuEvent),
  219. /// An event from a tray icon.
  220. #[cfg(all(desktop, feature = "tray-icon"))]
  221. #[cfg_attr(docsrs, doc(cfg(all(desktop, feature = "tray-icon"))))]
  222. TrayIconEvent(crate::tray::TrayIconEvent),
  223. /// Emitted when the NSApplicationDelegate's applicationShouldHandleReopen gets called
  224. #[non_exhaustive]
  225. #[cfg(target_os = "macos")]
  226. #[cfg_attr(docsrs, doc(cfg(target_os = "macos")))]
  227. Reopen {
  228. /// Indicates whether the NSApplication object found any visible windows in your application.
  229. has_visible_windows: bool,
  230. },
  231. }
  232. impl From<EventLoopMessage> for RunEvent {
  233. fn from(event: EventLoopMessage) -> Self {
  234. match event {
  235. #[cfg(desktop)]
  236. EventLoopMessage::MenuEvent(e) => Self::MenuEvent(e),
  237. #[cfg(all(desktop, feature = "tray-icon"))]
  238. EventLoopMessage::TrayIconEvent(e) => Self::TrayIconEvent(e),
  239. }
  240. }
  241. }
  242. /// The asset resolver is a helper to access the [`tauri_utils::assets::Assets`] interface.
  243. #[derive(Debug, Clone)]
  244. pub struct AssetResolver<R: Runtime> {
  245. manager: Arc<AppManager<R>>,
  246. }
  247. impl<R: Runtime> AssetResolver<R> {
  248. /// Gets the app asset associated with the given path.
  249. ///
  250. /// Resolves to the embedded asset that is part of the app
  251. /// in dev when [`devPath`](https://tauri.app/v1/api/config/#buildconfig.devpath) points to a folder in your filesystem
  252. /// or in production when [`distDir`](https://tauri.app/v1/api/config/#buildconfig.distdir)
  253. /// points to your frontend assets.
  254. ///
  255. /// Fallbacks to reading the asset from the [distDir] folder so the behavior is consistent in development.
  256. /// Note that the dist directory must exist so you might need to build your frontend assets first.
  257. pub fn get(&self, path: String) -> Option<Asset> {
  258. #[cfg(dev)]
  259. {
  260. // on dev if the devPath is a path to a directory we have the embedded assets
  261. // so we can use get_asset() directly
  262. // we only fallback to reading from distDir directly if we're using an external URL (which is likely)
  263. if let (Some(_), Some(crate::utils::config::FrontendDist::Directory(dist_path))) = (
  264. &self.manager.config().build.dev_url,
  265. &self.manager.config().build.frontend_dist,
  266. ) {
  267. let asset_path = std::path::PathBuf::from(&path)
  268. .components()
  269. .filter(|c| !matches!(c, std::path::Component::RootDir))
  270. .collect::<std::path::PathBuf>();
  271. let asset_path = self
  272. .manager
  273. .config_parent()
  274. .map(|p| p.join(dist_path).join(&asset_path))
  275. .unwrap_or_else(|| dist_path.join(&asset_path));
  276. return std::fs::read(asset_path).ok().map(|bytes| {
  277. let mime_type = crate::utils::mime_type::MimeType::parse(&bytes, &path);
  278. Asset {
  279. bytes,
  280. mime_type,
  281. csp_header: None,
  282. }
  283. });
  284. }
  285. }
  286. self.manager.get_asset(path).ok()
  287. }
  288. /// Iterate on all assets.
  289. pub fn iter(&self) -> Box<dyn Iterator<Item = (&str, &[u8])> + '_> {
  290. self.manager.assets.iter()
  291. }
  292. }
  293. /// A handle to the currently running application.
  294. ///
  295. /// This type implements [`Manager`] which allows for manipulation of global application items.
  296. #[default_runtime(crate::Wry, wry)]
  297. #[derive(Debug)]
  298. pub struct AppHandle<R: Runtime> {
  299. pub(crate) runtime_handle: R::Handle,
  300. pub(crate) manager: Arc<AppManager<R>>,
  301. }
  302. /// APIs specific to the wry runtime.
  303. #[cfg(feature = "wry")]
  304. impl AppHandle<crate::Wry> {
  305. /// Create a new tao window using a callback. The event loop must be running at this point.
  306. pub fn create_tao_window<
  307. F: FnOnce() -> (String, tauri_runtime_wry::TaoWindowBuilder) + Send + 'static,
  308. >(
  309. &self,
  310. f: F,
  311. ) -> crate::Result<std::sync::Weak<tauri_runtime_wry::Window>> {
  312. self.runtime_handle.create_tao_window(f).map_err(Into::into)
  313. }
  314. /// Sends a window message to the event loop.
  315. pub fn send_tao_window_event(
  316. &self,
  317. window_id: tauri_runtime_wry::TaoWindowId,
  318. message: tauri_runtime_wry::WindowMessage,
  319. ) -> crate::Result<()> {
  320. self
  321. .runtime_handle
  322. .send_event(tauri_runtime_wry::Message::Window(
  323. self.runtime_handle.window_id(window_id),
  324. message,
  325. ))
  326. .map_err(Into::into)
  327. }
  328. }
  329. impl<R: Runtime> Clone for AppHandle<R> {
  330. fn clone(&self) -> Self {
  331. Self {
  332. runtime_handle: self.runtime_handle.clone(),
  333. manager: self.manager.clone(),
  334. }
  335. }
  336. }
  337. impl<'de, R: Runtime> CommandArg<'de, R> for AppHandle<R> {
  338. /// Grabs the [`Window`] from the [`CommandItem`] and returns the associated [`AppHandle`]. This will never fail.
  339. fn from_command(command: CommandItem<'de, R>) -> std::result::Result<Self, InvokeError> {
  340. Ok(command.message.webview().window().app_handle)
  341. }
  342. }
  343. impl<R: Runtime> AppHandle<R> {
  344. /// Runs the given closure on the main thread.
  345. pub fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> crate::Result<()> {
  346. self
  347. .runtime_handle
  348. .run_on_main_thread(f)
  349. .map_err(Into::into)
  350. }
  351. /// Adds a Tauri application plugin.
  352. /// This function can be used to register a plugin that is loaded dynamically e.g. after login.
  353. /// For plugins that are created when the app is started, prefer [`Builder::plugin`].
  354. ///
  355. /// See [`Builder::plugin`] for more information.
  356. ///
  357. /// # Examples
  358. ///
  359. /// ```
  360. /// use tauri::{plugin::{Builder as PluginBuilder, TauriPlugin}, Runtime};
  361. ///
  362. /// fn init_plugin<R: Runtime>() -> TauriPlugin<R> {
  363. /// PluginBuilder::new("dummy").build()
  364. /// }
  365. ///
  366. /// tauri::Builder::default()
  367. /// .setup(move |app| {
  368. /// let handle = app.handle().clone();
  369. /// std::thread::spawn(move || {
  370. /// handle.plugin(init_plugin());
  371. /// });
  372. ///
  373. /// Ok(())
  374. /// });
  375. /// ```
  376. #[cfg_attr(feature = "tracing", tracing::instrument(name = "app::plugin::register", skip(plugin), fields(name = plugin.name())))]
  377. pub fn plugin<P: Plugin<R> + 'static>(&self, plugin: P) -> crate::Result<()> {
  378. let mut plugin = Box::new(plugin) as Box<dyn Plugin<R>>;
  379. let mut store = self.manager().plugins.lock().unwrap();
  380. store.initialize(&mut plugin, self, &self.config().plugins)?;
  381. store.register(plugin);
  382. Ok(())
  383. }
  384. /// Removes the plugin with the given name.
  385. ///
  386. /// # Examples
  387. ///
  388. /// ```
  389. /// use tauri::{plugin::{Builder as PluginBuilder, TauriPlugin, Plugin}, Runtime};
  390. ///
  391. /// fn init_plugin<R: Runtime>() -> TauriPlugin<R> {
  392. /// PluginBuilder::new("dummy").build()
  393. /// }
  394. ///
  395. /// let plugin = init_plugin();
  396. /// // `.name()` requires the `PLugin` trait import
  397. /// let plugin_name = plugin.name();
  398. /// tauri::Builder::default()
  399. /// .plugin(plugin)
  400. /// .setup(move |app| {
  401. /// let handle = app.handle().clone();
  402. /// std::thread::spawn(move || {
  403. /// handle.remove_plugin(plugin_name);
  404. /// });
  405. ///
  406. /// Ok(())
  407. /// });
  408. /// ```
  409. pub fn remove_plugin(&self, plugin: &'static str) -> bool {
  410. self.manager().plugins.lock().unwrap().unregister(plugin)
  411. }
  412. /// Exits the app by triggering [`RunEvent::ExitRequested`] and [`RunEvent::Exit`].
  413. pub fn exit(&self, exit_code: i32) {
  414. if let Err(e) = self.runtime_handle.request_exit(exit_code) {
  415. log::error!("failed to exit: {}", e);
  416. self.cleanup_before_exit();
  417. std::process::exit(exit_code);
  418. }
  419. }
  420. /// Restarts the app by triggering [`RunEvent::ExitRequested`] with code [`RESTART_EXIT_CODE`] and [`RunEvent::Exit`]..
  421. pub fn restart(&self) -> ! {
  422. if self.runtime_handle.request_exit(RESTART_EXIT_CODE).is_err() {
  423. self.cleanup_before_exit();
  424. }
  425. crate::process::restart(&self.env());
  426. }
  427. /// Sets the activation policy for the application. It is set to `NSApplicationActivationPolicyRegular` by default.
  428. ///
  429. /// # Examples
  430. /// ```,no_run
  431. /// tauri::Builder::default()
  432. /// .setup(move |app| {
  433. /// #[cfg(target_os = "macos")]
  434. /// app.handle().set_activation_policy(tauri::ActivationPolicy::Accessory);
  435. /// Ok(())
  436. /// });
  437. /// ```
  438. #[cfg(target_os = "macos")]
  439. #[cfg_attr(docsrs, doc(cfg(target_os = "macos")))]
  440. pub fn set_activation_policy(&self, activation_policy: ActivationPolicy) -> crate::Result<()> {
  441. self
  442. .runtime_handle
  443. .set_activation_policy(activation_policy)
  444. .map_err(Into::into)
  445. }
  446. }
  447. impl<R: Runtime> Manager<R> for AppHandle<R> {
  448. fn resources_table(&self) -> MutexGuard<'_, ResourceTable> {
  449. self.manager.resources_table()
  450. }
  451. }
  452. impl<R: Runtime> ManagerBase<R> for AppHandle<R> {
  453. fn manager(&self) -> &AppManager<R> {
  454. &self.manager
  455. }
  456. fn manager_owned(&self) -> Arc<AppManager<R>> {
  457. self.manager.clone()
  458. }
  459. fn runtime(&self) -> RuntimeOrDispatch<'_, R> {
  460. RuntimeOrDispatch::RuntimeHandle(self.runtime_handle.clone())
  461. }
  462. fn managed_app_handle(&self) -> &AppHandle<R> {
  463. self
  464. }
  465. }
  466. /// The instance of the currently running application.
  467. ///
  468. /// This type implements [`Manager`] which allows for manipulation of global application items.
  469. #[default_runtime(crate::Wry, wry)]
  470. pub struct App<R: Runtime> {
  471. runtime: Option<R>,
  472. setup: Option<SetupHook<R>>,
  473. manager: Arc<AppManager<R>>,
  474. handle: AppHandle<R>,
  475. ran_setup: bool,
  476. }
  477. impl<R: Runtime> fmt::Debug for App<R> {
  478. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  479. f.debug_struct("App")
  480. .field("runtime", &self.runtime)
  481. .field("manager", &self.manager)
  482. .field("handle", &self.handle)
  483. .finish()
  484. }
  485. }
  486. impl<R: Runtime> Manager<R> for App<R> {
  487. fn resources_table(&self) -> MutexGuard<'_, ResourceTable> {
  488. self.manager.resources_table()
  489. }
  490. }
  491. impl<R: Runtime> ManagerBase<R> for App<R> {
  492. fn manager(&self) -> &AppManager<R> {
  493. &self.manager
  494. }
  495. fn manager_owned(&self) -> Arc<AppManager<R>> {
  496. self.manager.clone()
  497. }
  498. fn runtime(&self) -> RuntimeOrDispatch<'_, R> {
  499. if let Some(runtime) = self.runtime.as_ref() {
  500. RuntimeOrDispatch::Runtime(runtime)
  501. } else {
  502. self.handle.runtime()
  503. }
  504. }
  505. fn managed_app_handle(&self) -> &AppHandle<R> {
  506. self.handle()
  507. }
  508. }
  509. /// APIs specific to the wry runtime.
  510. #[cfg(feature = "wry")]
  511. impl App<crate::Wry> {
  512. /// Adds a [`tauri_runtime_wry::Plugin`] using its [`tauri_runtime_wry::PluginBuilder`].
  513. ///
  514. /// # Stability
  515. ///
  516. /// This API is unstable.
  517. pub fn wry_plugin<P: tauri_runtime_wry::PluginBuilder<EventLoopMessage> + Send + 'static>(
  518. &mut self,
  519. plugin: P,
  520. ) where
  521. <P as tauri_runtime_wry::PluginBuilder<EventLoopMessage>>::Plugin: Send,
  522. {
  523. self.handle.runtime_handle.plugin(plugin);
  524. }
  525. }
  526. macro_rules! shared_app_impl {
  527. ($app: ty) => {
  528. impl<R: Runtime> $app {
  529. /// Registers a global menu event listener.
  530. #[cfg(desktop)]
  531. pub fn on_menu_event<F: Fn(&AppHandle<R>, MenuEvent) + Send + Sync + 'static>(
  532. &self,
  533. handler: F,
  534. ) {
  535. self.manager.menu.on_menu_event(handler)
  536. }
  537. /// Registers a global tray icon menu event listener.
  538. #[cfg(all(desktop, feature = "tray-icon"))]
  539. #[cfg_attr(docsrs, doc(cfg(all(desktop, feature = "tray-icon"))))]
  540. pub fn on_tray_icon_event<F: Fn(&AppHandle<R>, TrayIconEvent) + Send + Sync + 'static>(
  541. &self,
  542. handler: F,
  543. ) {
  544. self.manager.tray.on_tray_icon_event(handler)
  545. }
  546. /// Gets a tray icon using the provided id.
  547. #[cfg(all(desktop, feature = "tray-icon"))]
  548. #[cfg_attr(docsrs, doc(cfg(all(desktop, feature = "tray-icon"))))]
  549. pub fn tray_by_id<'a, I>(&self, id: &'a I) -> Option<TrayIcon<R>>
  550. where
  551. I: ?Sized,
  552. TrayIconId: PartialEq<&'a I>,
  553. {
  554. self.manager.tray.tray_by_id(id)
  555. }
  556. /// Removes a tray icon using the provided id from tauri's internal state and returns it.
  557. ///
  558. /// Note that dropping the returned icon, may cause the tray icon to disappear
  559. /// if it wasn't cloned somewhere else or referenced by JS.
  560. #[cfg(all(desktop, feature = "tray-icon"))]
  561. #[cfg_attr(docsrs, doc(cfg(all(desktop, feature = "tray-icon"))))]
  562. pub fn remove_tray_by_id<'a, I>(&self, id: &'a I) -> Option<TrayIcon<R>>
  563. where
  564. I: ?Sized,
  565. TrayIconId: PartialEq<&'a I>,
  566. {
  567. self.manager.tray.remove_tray_by_id(id)
  568. }
  569. /// Gets the app's configuration, defined on the `tauri.conf.json` file.
  570. pub fn config(&self) -> &Config {
  571. self.manager.config()
  572. }
  573. /// Gets the app's package information.
  574. pub fn package_info(&self) -> &PackageInfo {
  575. self.manager.package_info()
  576. }
  577. /// The application's asset resolver.
  578. pub fn asset_resolver(&self) -> AssetResolver<R> {
  579. AssetResolver {
  580. manager: self.manager.clone(),
  581. }
  582. }
  583. /// Returns the primary monitor of the system.
  584. ///
  585. /// Returns None if it can't identify any monitor as a primary one.
  586. pub fn primary_monitor(&self) -> crate::Result<Option<Monitor>> {
  587. Ok(match self.runtime() {
  588. RuntimeOrDispatch::Runtime(h) => h.primary_monitor().map(Into::into),
  589. RuntimeOrDispatch::RuntimeHandle(h) => h.primary_monitor().map(Into::into),
  590. _ => unreachable!(),
  591. })
  592. }
  593. /// Returns the monitor that contains the given point.
  594. pub fn monitor_from_point(&self, x: f64, y: f64) -> crate::Result<Option<Monitor>> {
  595. Ok(match self.runtime() {
  596. RuntimeOrDispatch::Runtime(h) => h.monitor_from_point(x, y).map(Into::into),
  597. RuntimeOrDispatch::RuntimeHandle(h) => h.monitor_from_point(x, y).map(Into::into),
  598. _ => unreachable!(),
  599. })
  600. }
  601. /// Returns the list of all the monitors available on the system.
  602. pub fn available_monitors(&self) -> crate::Result<Vec<Monitor>> {
  603. Ok(match self.runtime() {
  604. RuntimeOrDispatch::Runtime(h) => {
  605. h.available_monitors().into_iter().map(Into::into).collect()
  606. }
  607. RuntimeOrDispatch::RuntimeHandle(h) => {
  608. h.available_monitors().into_iter().map(Into::into).collect()
  609. }
  610. _ => unreachable!(),
  611. })
  612. }
  613. /// Get the cursor position relative to the top-left hand corner of the desktop.
  614. ///
  615. /// Note that the top-left hand corner of the desktop is not necessarily the same as the screen.
  616. /// If the user uses a desktop with multiple monitors,
  617. /// the top-left hand corner of the desktop is the top-left hand corner of the main monitor on Windows and macOS
  618. /// or the top-left of the leftmost monitor on X11.
  619. ///
  620. /// The coordinates can be negative if the top-left hand corner of the window is outside of the visible screen region.
  621. pub fn cursor_position(&self) -> crate::Result<PhysicalPosition<f64>> {
  622. Ok(match self.runtime() {
  623. RuntimeOrDispatch::Runtime(h) => h.cursor_position()?,
  624. RuntimeOrDispatch::RuntimeHandle(h) => h.cursor_position()?,
  625. _ => unreachable!(),
  626. })
  627. }
  628. /// Returns the default window icon.
  629. pub fn default_window_icon(&self) -> Option<&Image<'_>> {
  630. self.manager.window.default_icon.as_ref()
  631. }
  632. /// Returns the app-wide menu.
  633. #[cfg(desktop)]
  634. pub fn menu(&self) -> Option<Menu<R>> {
  635. self.manager.menu.menu_lock().clone()
  636. }
  637. /// Sets the app-wide menu and returns the previous one.
  638. ///
  639. /// If a window was not created with an explicit menu or had one set explicitly,
  640. /// this menu will be assigned to it.
  641. #[cfg(desktop)]
  642. pub fn set_menu(&self, menu: Menu<R>) -> crate::Result<Option<Menu<R>>> {
  643. let prev_menu = self.remove_menu()?;
  644. self.manager.menu.insert_menu_into_stash(&menu);
  645. self.manager.menu.menu_lock().replace(menu.clone());
  646. // set it on all windows that don't have one or previously had the app-wide menu
  647. #[cfg(not(target_os = "macos"))]
  648. {
  649. for window in self.manager.windows().values() {
  650. let has_app_wide_menu = window.has_app_wide_menu() || window.menu().is_none();
  651. if has_app_wide_menu {
  652. window.set_menu(menu.clone())?;
  653. window.menu_lock().replace(crate::window::WindowMenu {
  654. is_app_wide: true,
  655. menu: menu.clone(),
  656. });
  657. }
  658. }
  659. }
  660. // set it app-wide for macos
  661. #[cfg(target_os = "macos")]
  662. {
  663. let menu_ = menu.clone();
  664. self.run_on_main_thread(move || {
  665. let _ = init_app_menu(&menu_);
  666. })?;
  667. }
  668. Ok(prev_menu)
  669. }
  670. /// Remove the app-wide menu and returns it.
  671. ///
  672. /// If a window was not created with an explicit menu or had one set explicitly,
  673. /// this will remove the menu from it.
  674. #[cfg(desktop)]
  675. pub fn remove_menu(&self) -> crate::Result<Option<Menu<R>>> {
  676. let menu = self.manager.menu.menu_lock().as_ref().cloned();
  677. #[allow(unused_variables)]
  678. if let Some(menu) = menu {
  679. // remove from windows that have the app-wide menu
  680. #[cfg(not(target_os = "macos"))]
  681. {
  682. for window in self.manager.windows().values() {
  683. let has_app_wide_menu = window.has_app_wide_menu();
  684. if has_app_wide_menu {
  685. window.remove_menu()?;
  686. *window.menu_lock() = None;
  687. }
  688. }
  689. }
  690. // remove app-wide for macos
  691. #[cfg(target_os = "macos")]
  692. {
  693. self.run_on_main_thread(move || {
  694. menu.inner().remove_for_nsapp();
  695. })?;
  696. }
  697. }
  698. let prev_menu = self.manager.menu.menu_lock().take();
  699. self
  700. .manager
  701. .remove_menu_from_stash_by_id(prev_menu.as_ref().map(|m| m.id()));
  702. Ok(prev_menu)
  703. }
  704. /// Hides the app-wide menu from windows that have it.
  705. ///
  706. /// If a window was not created with an explicit menu or had one set explicitly,
  707. /// this will hide the menu from it.
  708. #[cfg(desktop)]
  709. pub fn hide_menu(&self) -> crate::Result<()> {
  710. #[cfg(not(target_os = "macos"))]
  711. {
  712. let is_app_menu_set = self.manager.menu.menu_lock().is_some();
  713. if is_app_menu_set {
  714. for window in self.manager.windows().values() {
  715. if window.has_app_wide_menu() {
  716. window.hide_menu()?;
  717. }
  718. }
  719. }
  720. }
  721. Ok(())
  722. }
  723. /// Shows the app-wide menu for windows that have it.
  724. ///
  725. /// If a window was not created with an explicit menu or had one set explicitly,
  726. /// this will show the menu for it.
  727. #[cfg(desktop)]
  728. pub fn show_menu(&self) -> crate::Result<()> {
  729. #[cfg(not(target_os = "macos"))]
  730. {
  731. let is_app_menu_set = self.manager.menu.menu_lock().is_some();
  732. if is_app_menu_set {
  733. for window in self.manager.windows().values() {
  734. if window.has_app_wide_menu() {
  735. window.show_menu()?;
  736. }
  737. }
  738. }
  739. }
  740. Ok(())
  741. }
  742. /// Shows the application, but does not automatically focus it.
  743. #[cfg(target_os = "macos")]
  744. pub fn show(&self) -> crate::Result<()> {
  745. match self.runtime() {
  746. RuntimeOrDispatch::Runtime(r) => r.show(),
  747. RuntimeOrDispatch::RuntimeHandle(h) => h.show()?,
  748. _ => unreachable!(),
  749. }
  750. Ok(())
  751. }
  752. /// Hides the application.
  753. #[cfg(target_os = "macos")]
  754. pub fn hide(&self) -> crate::Result<()> {
  755. match self.runtime() {
  756. RuntimeOrDispatch::Runtime(r) => r.hide(),
  757. RuntimeOrDispatch::RuntimeHandle(h) => h.hide()?,
  758. _ => unreachable!(),
  759. }
  760. Ok(())
  761. }
  762. /// Runs necessary cleanup tasks before exiting the process.
  763. /// **You should always exit the tauri app immediately after this function returns and not use any tauri-related APIs.**
  764. pub fn cleanup_before_exit(&self) {
  765. #[cfg(all(desktop, feature = "tray-icon"))]
  766. self.manager.tray.icons.lock().unwrap().clear();
  767. self.manager.resources_table().clear();
  768. for (_, window) in self.manager.windows() {
  769. window.resources_table().clear();
  770. #[cfg(windows)]
  771. let _ = window.hide();
  772. }
  773. for (_, webview) in self.manager.webviews() {
  774. webview.resources_table().clear();
  775. }
  776. }
  777. }
  778. impl<R: Runtime> Listener<R> for $app {
  779. /// Listen to an event on this app.
  780. ///
  781. /// # Examples
  782. ///
  783. /// ```
  784. /// use tauri::Listener;
  785. ///
  786. /// tauri::Builder::default()
  787. /// .setup(|app| {
  788. /// app.listen("component-loaded", move |event| {
  789. /// println!("window just loaded a component");
  790. /// });
  791. ///
  792. /// Ok(())
  793. /// });
  794. /// ```
  795. fn listen<F>(&self, event: impl Into<String>, handler: F) -> EventId
  796. where
  797. F: Fn(Event) + Send + 'static,
  798. {
  799. self.manager.listen(event.into(), EventTarget::App, handler)
  800. }
  801. /// Listen to an event on this app only once.
  802. ///
  803. /// See [`Self::listen`] for more information.
  804. fn once<F>(&self, event: impl Into<String>, handler: F) -> EventId
  805. where
  806. F: FnOnce(Event) + Send + 'static,
  807. {
  808. self.manager.once(event.into(), EventTarget::App, handler)
  809. }
  810. /// Unlisten to an event on this app.
  811. ///
  812. /// # Examples
  813. ///
  814. /// ```
  815. /// use tauri::Listener;
  816. ///
  817. /// tauri::Builder::default()
  818. /// .setup(|app| {
  819. /// let handler = app.listen("component-loaded", move |event| {
  820. /// println!("app just loaded a component");
  821. /// });
  822. ///
  823. /// // stop listening to the event when you do not need it anymore
  824. /// app.unlisten(handler);
  825. ///
  826. /// Ok(())
  827. /// });
  828. /// ```
  829. fn unlisten(&self, id: EventId) {
  830. self.manager.unlisten(id)
  831. }
  832. }
  833. impl<R: Runtime> Emitter<R> for $app {
  834. /// Emits an event to all [targets](EventTarget).
  835. ///
  836. /// # Examples
  837. /// ```
  838. /// use tauri::Emitter;
  839. ///
  840. /// #[tauri::command]
  841. /// fn synchronize(app: tauri::AppHandle) {
  842. /// // emits the synchronized event to all webviews
  843. /// app.emit("synchronized", ());
  844. /// }
  845. /// ```
  846. fn emit<S: Serialize + Clone>(&self, event: &str, payload: S) -> Result<()> {
  847. self.manager.emit(event, payload)
  848. }
  849. /// Emits an event to all [targets](EventTarget) matching the given target.
  850. ///
  851. /// # Examples
  852. /// ```
  853. /// use tauri::{Emitter, EventTarget};
  854. ///
  855. /// #[tauri::command]
  856. /// fn download(app: tauri::AppHandle) {
  857. /// for i in 1..100 {
  858. /// std::thread::sleep(std::time::Duration::from_millis(150));
  859. /// // emit a download progress event to all listeners
  860. /// app.emit_to(EventTarget::any(), "download-progress", i);
  861. /// // emit an event to listeners that used App::listen or AppHandle::listen
  862. /// app.emit_to(EventTarget::app(), "download-progress", i);
  863. /// // emit an event to any webview/window/webviewWindow matching the given label
  864. /// app.emit_to("updater", "download-progress", i); // similar to using EventTarget::labeled
  865. /// app.emit_to(EventTarget::labeled("updater"), "download-progress", i);
  866. /// // emit an event to listeners that used WebviewWindow::listen
  867. /// app.emit_to(EventTarget::webview_window("updater"), "download-progress", i);
  868. /// }
  869. /// }
  870. /// ```
  871. fn emit_to<I, S>(&self, target: I, event: &str, payload: S) -> Result<()>
  872. where
  873. I: Into<EventTarget>,
  874. S: Serialize + Clone,
  875. {
  876. self.manager.emit_to(target, event, payload)
  877. }
  878. /// Emits an event to all [targets](EventTarget) based on the given filter.
  879. ///
  880. /// # Examples
  881. /// ```
  882. /// use tauri::{Emitter, EventTarget};
  883. ///
  884. /// #[tauri::command]
  885. /// fn download(app: tauri::AppHandle) {
  886. /// for i in 1..100 {
  887. /// std::thread::sleep(std::time::Duration::from_millis(150));
  888. /// // emit a download progress event to the updater window
  889. /// app.emit_filter("download-progress", i, |t| match t {
  890. /// EventTarget::WebviewWindow { label } => label == "main",
  891. /// _ => false,
  892. /// });
  893. /// }
  894. /// }
  895. /// ```
  896. fn emit_filter<S, F>(&self, event: &str, payload: S, filter: F) -> Result<()>
  897. where
  898. S: Serialize + Clone,
  899. F: Fn(&EventTarget) -> bool,
  900. {
  901. self.manager.emit_filter(event, payload, filter)
  902. }
  903. }
  904. };
  905. }
  906. shared_app_impl!(App<R>);
  907. shared_app_impl!(AppHandle<R>);
  908. impl<R: Runtime> App<R> {
  909. #[cfg_attr(
  910. feature = "tracing",
  911. tracing::instrument(name = "app::core_plugins::register")
  912. )]
  913. fn register_core_plugins(&self) -> crate::Result<()> {
  914. self.handle.plugin(crate::path::plugin::init())?;
  915. self.handle.plugin(crate::event::plugin::init())?;
  916. self.handle.plugin(crate::window::plugin::init())?;
  917. self.handle.plugin(crate::webview::plugin::init())?;
  918. self.handle.plugin(crate::app::plugin::init())?;
  919. self.handle.plugin(crate::resources::plugin::init())?;
  920. self.handle.plugin(crate::image::plugin::init())?;
  921. #[cfg(desktop)]
  922. self.handle.plugin(crate::menu::plugin::init())?;
  923. #[cfg(all(desktop, feature = "tray-icon"))]
  924. self.handle.plugin(crate::tray::plugin::init())?;
  925. Ok(())
  926. }
  927. /// Runs the given closure on the main thread.
  928. pub fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> crate::Result<()> {
  929. self.app_handle().run_on_main_thread(f)
  930. }
  931. /// Gets a handle to the application instance.
  932. pub fn handle(&self) -> &AppHandle<R> {
  933. &self.handle
  934. }
  935. /// Sets the activation policy for the application. It is set to `NSApplicationActivationPolicyRegular` by default.
  936. ///
  937. /// # Examples
  938. /// ```,no_run
  939. /// tauri::Builder::default()
  940. /// .setup(move |app| {
  941. /// #[cfg(target_os = "macos")]
  942. /// app.set_activation_policy(tauri::ActivationPolicy::Accessory);
  943. /// Ok(())
  944. /// });
  945. /// ```
  946. #[cfg(target_os = "macos")]
  947. #[cfg_attr(docsrs, doc(cfg(target_os = "macos")))]
  948. pub fn set_activation_policy(&mut self, activation_policy: ActivationPolicy) {
  949. if let Some(runtime) = self.runtime.as_mut() {
  950. runtime.set_activation_policy(activation_policy);
  951. } else {
  952. let _ = self.app_handle().set_activation_policy(activation_policy);
  953. }
  954. }
  955. /// Change the device event filter mode.
  956. ///
  957. /// Since the DeviceEvent capture can lead to high CPU usage for unfocused windows, [`tao`]
  958. /// will ignore them by default for unfocused windows on Windows. This method allows changing
  959. /// the filter to explicitly capture them again.
  960. ///
  961. /// ## Platform-specific
  962. ///
  963. /// - ** Linux / macOS / iOS / Android**: Unsupported.
  964. ///
  965. /// # Examples
  966. /// ```,no_run
  967. /// let mut app = tauri::Builder::default()
  968. /// // on an actual app, remove the string argument
  969. /// .build(tauri::generate_context!("test/fixture/src-tauri/tauri.conf.json"))
  970. /// .expect("error while building tauri application");
  971. /// app.set_device_event_filter(tauri::DeviceEventFilter::Always);
  972. /// app.run(|_app_handle, _event| {});
  973. /// ```
  974. ///
  975. /// [`tao`]: https://crates.io/crates/tao
  976. pub fn set_device_event_filter(&mut self, filter: DeviceEventFilter) {
  977. self
  978. .runtime
  979. .as_mut()
  980. .unwrap()
  981. .set_device_event_filter(filter);
  982. }
  983. /// Runs the application.
  984. ///
  985. /// # Examples
  986. /// ```,no_run
  987. /// let app = tauri::Builder::default()
  988. /// // on an actual app, remove the string argument
  989. /// .build(tauri::generate_context!("test/fixture/src-tauri/tauri.conf.json"))
  990. /// .expect("error while building tauri application");
  991. /// app.run(|_app_handle, event| match event {
  992. /// tauri::RunEvent::ExitRequested { api, .. } => {
  993. /// api.prevent_exit();
  994. /// }
  995. /// _ => {}
  996. /// });
  997. /// ```
  998. pub fn run<F: FnMut(&AppHandle<R>, RunEvent) + 'static>(mut self, mut callback: F) {
  999. let app_handle = self.handle().clone();
  1000. let manager = self.manager.clone();
  1001. self.runtime.take().unwrap().run(move |event| match event {
  1002. RuntimeRunEvent::Ready => {
  1003. if let Err(e) = setup(&mut self) {
  1004. panic!("Failed to setup app: {e}");
  1005. }
  1006. let event = on_event_loop_event(&app_handle, RuntimeRunEvent::Ready, &manager);
  1007. callback(&app_handle, event);
  1008. }
  1009. RuntimeRunEvent::Exit => {
  1010. let event = on_event_loop_event(&app_handle, RuntimeRunEvent::Exit, &manager);
  1011. callback(&app_handle, event);
  1012. app_handle.cleanup_before_exit();
  1013. }
  1014. _ => {
  1015. let event = on_event_loop_event(&app_handle, event, &manager);
  1016. callback(&app_handle, event);
  1017. }
  1018. });
  1019. }
  1020. /// Runs an iteration of the runtime event loop and immediately return.
  1021. ///
  1022. /// Note that when using this API, app cleanup is not automatically done.
  1023. /// The cleanup calls [`App::cleanup_before_exit`] so you may want to call that function before exiting the application.
  1024. ///
  1025. /// # Examples
  1026. /// ```no_run
  1027. /// use tauri::Manager;
  1028. ///
  1029. /// let mut app = tauri::Builder::default()
  1030. /// // on an actual app, remove the string argument
  1031. /// .build(tauri::generate_context!("test/fixture/src-tauri/tauri.conf.json"))
  1032. /// .expect("error while building tauri application");
  1033. ///
  1034. /// loop {
  1035. /// app.run_iteration(|_app, _event| {});
  1036. /// if app.webview_windows().is_empty() {
  1037. /// app.cleanup_before_exit();
  1038. /// break;
  1039. /// }
  1040. /// }
  1041. /// ```
  1042. #[cfg(desktop)]
  1043. pub fn run_iteration<F: FnMut(&AppHandle<R>, RunEvent) + 'static>(&mut self, mut callback: F) {
  1044. let manager = self.manager.clone();
  1045. let app_handle = self.handle().clone();
  1046. if !self.ran_setup {
  1047. if let Err(e) = setup(self) {
  1048. panic!("Failed to setup app: {e}");
  1049. }
  1050. }
  1051. self.runtime.as_mut().unwrap().run_iteration(move |event| {
  1052. let event = on_event_loop_event(&app_handle, event, &manager);
  1053. callback(&app_handle, event);
  1054. })
  1055. }
  1056. }
  1057. /// Builds a Tauri application.
  1058. ///
  1059. /// # Examples
  1060. /// ```,no_run
  1061. /// tauri::Builder::default()
  1062. /// // on an actual app, remove the string argument
  1063. /// .run(tauri::generate_context!("test/fixture/src-tauri/tauri.conf.json"))
  1064. /// .expect("error while running tauri application");
  1065. /// ```
  1066. #[allow(clippy::type_complexity)]
  1067. pub struct Builder<R: Runtime> {
  1068. /// A flag indicating that the runtime must be started on an environment that supports the event loop not on the main thread.
  1069. #[cfg(any(windows, target_os = "linux"))]
  1070. runtime_any_thread: bool,
  1071. /// The JS message handler.
  1072. invoke_handler: Box<InvokeHandler<R>>,
  1073. /// The JS message responder.
  1074. invoke_responder: Option<Arc<InvokeResponder<R>>>,
  1075. /// The script that initializes the `window.__TAURI_INTERNALS__.postMessage` function.
  1076. pub(crate) invoke_initialization_script: String,
  1077. /// The setup hook.
  1078. setup: SetupHook<R>,
  1079. /// Page load hook.
  1080. on_page_load: Option<Arc<OnPageLoad<R>>>,
  1081. /// All passed plugins
  1082. plugins: PluginStore<R>,
  1083. /// The webview protocols available to all windows.
  1084. uri_scheme_protocols: HashMap<String, Arc<UriSchemeProtocol<R>>>,
  1085. /// App state.
  1086. state: StateManager,
  1087. /// A closure that returns the menu set to all windows.
  1088. #[cfg(desktop)]
  1089. menu: Option<Box<dyn FnOnce(&AppHandle<R>) -> crate::Result<Menu<R>> + Send>>,
  1090. /// Enable macOS default menu creation.
  1091. #[allow(unused)]
  1092. enable_macos_default_menu: bool,
  1093. /// Window event handlers that listens to all windows.
  1094. window_event_listeners: Vec<GlobalWindowEventListener<R>>,
  1095. /// Webview event handlers that listens to all webviews.
  1096. webview_event_listeners: Vec<GlobalWebviewEventListener<R>>,
  1097. /// The device event filter.
  1098. device_event_filter: DeviceEventFilter,
  1099. pub(crate) invoke_key: String,
  1100. }
  1101. #[derive(Template)]
  1102. #[default_template("../scripts/ipc-protocol.js")]
  1103. pub(crate) struct InvokeInitializationScript<'a> {
  1104. /// The function that processes the IPC message.
  1105. #[raw]
  1106. pub(crate) process_ipc_message_fn: &'a str,
  1107. pub(crate) os_name: &'a str,
  1108. pub(crate) fetch_channel_data_command: &'a str,
  1109. pub(crate) linux_ipc_protocol_enabled: bool,
  1110. pub(crate) invoke_key: &'a str,
  1111. }
  1112. /// Make `Wry` the default `Runtime` for `Builder`
  1113. #[cfg(feature = "wry")]
  1114. #[cfg_attr(docsrs, doc(cfg(feature = "wry")))]
  1115. impl Default for Builder<crate::Wry> {
  1116. fn default() -> Self {
  1117. Self::new()
  1118. }
  1119. }
  1120. #[cfg(not(feature = "wry"))]
  1121. #[cfg_attr(docsrs, doc(cfg(not(feature = "wry"))))]
  1122. impl<R: Runtime> Default for Builder<R> {
  1123. fn default() -> Self {
  1124. Self::new()
  1125. }
  1126. }
  1127. impl<R: Runtime> Builder<R> {
  1128. /// Creates a new App builder.
  1129. pub fn new() -> Self {
  1130. let invoke_key = crate::generate_invoke_key().unwrap();
  1131. Self {
  1132. #[cfg(any(windows, target_os = "linux"))]
  1133. runtime_any_thread: false,
  1134. setup: Box::new(|_| Ok(())),
  1135. invoke_handler: Box::new(|_| false),
  1136. invoke_responder: None,
  1137. invoke_initialization_script: InvokeInitializationScript {
  1138. process_ipc_message_fn: crate::manager::webview::PROCESS_IPC_MESSAGE_FN,
  1139. os_name: std::env::consts::OS,
  1140. fetch_channel_data_command: crate::ipc::channel::FETCH_CHANNEL_DATA_COMMAND,
  1141. linux_ipc_protocol_enabled: cfg!(feature = "linux-ipc-protocol"),
  1142. invoke_key: &invoke_key.clone(),
  1143. }
  1144. .render_default(&Default::default())
  1145. .unwrap()
  1146. .into_string(),
  1147. on_page_load: None,
  1148. plugins: PluginStore::default(),
  1149. uri_scheme_protocols: Default::default(),
  1150. state: StateManager::new(),
  1151. #[cfg(desktop)]
  1152. menu: None,
  1153. enable_macos_default_menu: true,
  1154. window_event_listeners: Vec::new(),
  1155. webview_event_listeners: Vec::new(),
  1156. device_event_filter: Default::default(),
  1157. invoke_key,
  1158. }
  1159. }
  1160. }
  1161. impl<R: Runtime> Builder<R> {
  1162. /// Builds a new Tauri application running on any thread, bypassing the main thread requirement.
  1163. ///
  1164. /// ## Platform-specific
  1165. ///
  1166. /// - **macOS:** on macOS the application *must* be executed on the main thread, so this function is not exposed.
  1167. #[cfg(any(windows, target_os = "linux"))]
  1168. #[cfg_attr(docsrs, doc(cfg(any(windows, target_os = "linux"))))]
  1169. #[must_use]
  1170. pub fn any_thread(mut self) -> Self {
  1171. self.runtime_any_thread = true;
  1172. self
  1173. }
  1174. /// Defines the JS message handler callback.
  1175. ///
  1176. /// # Examples
  1177. /// ```
  1178. /// #[tauri::command]
  1179. /// fn command_1() -> String {
  1180. /// return "hello world".to_string();
  1181. /// }
  1182. /// tauri::Builder::default()
  1183. /// .invoke_handler(tauri::generate_handler![
  1184. /// command_1,
  1185. /// // etc...
  1186. /// ]);
  1187. /// ```
  1188. #[must_use]
  1189. pub fn invoke_handler<F>(mut self, invoke_handler: F) -> Self
  1190. where
  1191. F: Fn(Invoke<R>) -> bool + Send + Sync + 'static,
  1192. {
  1193. self.invoke_handler = Box::new(invoke_handler);
  1194. self
  1195. }
  1196. /// Defines a custom JS message system.
  1197. ///
  1198. /// The `responder` is a function that will be called when a command has been executed and must send a response to the JS layer.
  1199. ///
  1200. /// The `initialization_script` is a script that initializes `window.__TAURI_INTERNALS__.postMessage`.
  1201. /// That function must take the `(message: object, options: object)` arguments and send it to the backend.
  1202. #[must_use]
  1203. pub fn invoke_system<F>(mut self, initialization_script: String, responder: F) -> Self
  1204. where
  1205. F: Fn(&Webview<R>, &str, &InvokeResponse, CallbackFn, CallbackFn) + Send + Sync + 'static,
  1206. {
  1207. self.invoke_initialization_script = initialization_script;
  1208. self.invoke_responder.replace(Arc::new(responder));
  1209. self
  1210. }
  1211. /// Defines the setup hook.
  1212. ///
  1213. /// # Examples
  1214. #[cfg_attr(
  1215. feature = "unstable",
  1216. doc = r####"
  1217. ```
  1218. use tauri::Manager;
  1219. tauri::Builder::default()
  1220. .setup(|app| {
  1221. let main_window = app.get_window("main").unwrap();
  1222. main_window.set_title("Tauri!")?;
  1223. Ok(())
  1224. });
  1225. ```
  1226. "####
  1227. )]
  1228. #[must_use]
  1229. pub fn setup<F>(mut self, setup: F) -> Self
  1230. where
  1231. F: FnOnce(&mut App<R>) -> std::result::Result<(), Box<dyn std::error::Error>> + Send + 'static,
  1232. {
  1233. self.setup = Box::new(setup);
  1234. self
  1235. }
  1236. /// Defines the page load hook.
  1237. #[must_use]
  1238. pub fn on_page_load<F>(mut self, on_page_load: F) -> Self
  1239. where
  1240. F: Fn(&Webview<R>, &PageLoadPayload<'_>) + Send + Sync + 'static,
  1241. {
  1242. self.on_page_load.replace(Arc::new(on_page_load));
  1243. self
  1244. }
  1245. /// Adds a Tauri application plugin.
  1246. ///
  1247. /// A plugin is created using the [`crate::plugin::Builder`] struct.Check its documentation for more information.
  1248. ///
  1249. /// # Examples
  1250. ///
  1251. /// ```
  1252. /// mod plugin {
  1253. /// use tauri::{plugin::{Builder as PluginBuilder, TauriPlugin}, RunEvent, Runtime};
  1254. ///
  1255. /// // this command can be called in the frontend using `invoke('plugin:window|do_something')`.
  1256. /// #[tauri::command]
  1257. /// async fn do_something<R: Runtime>(app: tauri::AppHandle<R>, window: tauri::Window<R>) -> Result<(), String> {
  1258. /// println!("command called");
  1259. /// Ok(())
  1260. /// }
  1261. /// pub fn init<R: Runtime>() -> TauriPlugin<R> {
  1262. /// PluginBuilder::new("window")
  1263. /// .setup(|app, api| {
  1264. /// // initialize the plugin here
  1265. /// Ok(())
  1266. /// })
  1267. /// .on_event(|app, event| {
  1268. /// match event {
  1269. /// RunEvent::Ready => {
  1270. /// println!("app is ready");
  1271. /// }
  1272. /// RunEvent::WindowEvent { label, event, .. } => {
  1273. /// println!("window {} received an event: {:?}", label, event);
  1274. /// }
  1275. /// _ => (),
  1276. /// }
  1277. /// })
  1278. /// .invoke_handler(tauri::generate_handler![do_something])
  1279. /// .build()
  1280. /// }
  1281. /// }
  1282. ///
  1283. /// tauri::Builder::default()
  1284. /// .plugin(plugin::init());
  1285. /// ```
  1286. #[must_use]
  1287. pub fn plugin<P: Plugin<R> + 'static>(mut self, plugin: P) -> Self {
  1288. self.plugins.register(Box::new(plugin));
  1289. self
  1290. }
  1291. /// Add `state` to the state managed by the application.
  1292. ///
  1293. /// This method can be called any number of times as long as each call
  1294. /// refers to a different `T`.
  1295. ///
  1296. /// Managed state can be retrieved by any command handler via the
  1297. /// [`crate::State`] guard. In particular, if a value of type `T`
  1298. /// is managed by Tauri, adding `State<T>` to the list of arguments in a
  1299. /// command handler instructs Tauri to retrieve the managed value.
  1300. /// Additionally, [`state`](crate::Manager#method.state) can be used to retrieve the value manually.
  1301. ///
  1302. /// # Panics
  1303. ///
  1304. /// Panics if state of type `T` is already being managed.
  1305. ///
  1306. /// # Mutability
  1307. ///
  1308. /// Since the managed state is global and must be [`Send`] + [`Sync`], mutations can only happen through interior mutability:
  1309. ///
  1310. /// ```,no_run
  1311. /// use std::{collections::HashMap, sync::Mutex};
  1312. /// use tauri::State;
  1313. /// // here we use Mutex to achieve interior mutability
  1314. /// struct Storage {
  1315. /// store: Mutex<HashMap<u64, String>>,
  1316. /// }
  1317. /// struct Connection;
  1318. /// struct DbConnection {
  1319. /// db: Mutex<Option<Connection>>,
  1320. /// }
  1321. ///
  1322. /// #[tauri::command]
  1323. /// fn connect(connection: State<DbConnection>) {
  1324. /// // initialize the connection, mutating the state with interior mutability
  1325. /// *connection.db.lock().unwrap() = Some(Connection {});
  1326. /// }
  1327. ///
  1328. /// #[tauri::command]
  1329. /// fn storage_insert(key: u64, value: String, storage: State<Storage>) {
  1330. /// // mutate the storage behind the Mutex
  1331. /// storage.store.lock().unwrap().insert(key, value);
  1332. /// }
  1333. ///
  1334. /// tauri::Builder::default()
  1335. /// .manage(Storage { store: Default::default() })
  1336. /// .manage(DbConnection { db: Default::default() })
  1337. /// .invoke_handler(tauri::generate_handler![connect, storage_insert])
  1338. /// // on an actual app, remove the string argument
  1339. /// .run(tauri::generate_context!("test/fixture/src-tauri/tauri.conf.json"))
  1340. /// .expect("error while running tauri application");
  1341. /// ```
  1342. ///
  1343. /// # Examples
  1344. ///
  1345. /// ```,no_run
  1346. /// use tauri::State;
  1347. ///
  1348. /// struct MyInt(isize);
  1349. /// struct MyString(String);
  1350. ///
  1351. /// #[tauri::command]
  1352. /// fn int_command(state: State<MyInt>) -> String {
  1353. /// format!("The stateful int is: {}", state.0)
  1354. /// }
  1355. ///
  1356. /// #[tauri::command]
  1357. /// fn string_command<'r>(state: State<'r, MyString>) {
  1358. /// println!("state: {}", state.inner().0);
  1359. /// }
  1360. ///
  1361. /// tauri::Builder::default()
  1362. /// .manage(MyInt(10))
  1363. /// .manage(MyString("Hello, managed state!".to_string()))
  1364. /// .invoke_handler(tauri::generate_handler![int_command, string_command])
  1365. /// // on an actual app, remove the string argument
  1366. /// .run(tauri::generate_context!("test/fixture/src-tauri/tauri.conf.json"))
  1367. /// .expect("error while running tauri application");
  1368. /// ```
  1369. #[must_use]
  1370. pub fn manage<T>(self, state: T) -> Self
  1371. where
  1372. T: Send + Sync + 'static,
  1373. {
  1374. let type_name = std::any::type_name::<T>();
  1375. assert!(
  1376. self.state.set(state),
  1377. "state for type '{type_name}' is already being managed",
  1378. );
  1379. self
  1380. }
  1381. /// Sets the menu to use on all windows.
  1382. ///
  1383. /// # Examples
  1384. /// ```
  1385. /// use tauri::menu::{Menu, MenuItem, PredefinedMenuItem, Submenu};
  1386. ///
  1387. /// tauri::Builder::default()
  1388. /// .menu(|handle| Menu::with_items(handle, &[
  1389. /// &Submenu::with_items(
  1390. /// handle,
  1391. /// "File",
  1392. /// true,
  1393. /// &[
  1394. /// &PredefinedMenuItem::close_window(handle, None)?,
  1395. /// #[cfg(target_os = "macos")]
  1396. /// &MenuItem::new(handle, "Hello", true, None::<&str>)?,
  1397. /// ],
  1398. /// )?
  1399. /// ]));
  1400. /// ```
  1401. #[must_use]
  1402. #[cfg(desktop)]
  1403. pub fn menu<F: FnOnce(&AppHandle<R>) -> crate::Result<Menu<R>> + Send + 'static>(
  1404. mut self,
  1405. f: F,
  1406. ) -> Self {
  1407. self.menu.replace(Box::new(f));
  1408. self
  1409. }
  1410. /// Enable or disable the default menu on macOS. Enabled by default.
  1411. ///
  1412. /// # Examples
  1413. /// ```
  1414. /// tauri::Builder::default()
  1415. /// .enable_macos_default_menu(false);
  1416. /// ```
  1417. #[must_use]
  1418. pub fn enable_macos_default_menu(mut self, enable: bool) -> Self {
  1419. self.enable_macos_default_menu = enable;
  1420. self
  1421. }
  1422. /// Registers a window event handler for all windows.
  1423. ///
  1424. /// # Examples
  1425. /// ```
  1426. /// tauri::Builder::default()
  1427. /// .on_window_event(|window, event| match event {
  1428. /// tauri::WindowEvent::Focused(focused) => {
  1429. /// // hide window whenever it loses focus
  1430. /// if !focused {
  1431. /// window.hide().unwrap();
  1432. /// }
  1433. /// }
  1434. /// _ => {}
  1435. /// });
  1436. /// ```
  1437. #[must_use]
  1438. pub fn on_window_event<F: Fn(&Window<R>, &WindowEvent) + Send + Sync + 'static>(
  1439. mut self,
  1440. handler: F,
  1441. ) -> Self {
  1442. self.window_event_listeners.push(Box::new(handler));
  1443. self
  1444. }
  1445. /// Registers a webview event handler for all webviews.
  1446. ///
  1447. /// # Examples
  1448. /// ```
  1449. /// tauri::Builder::default()
  1450. /// .on_webview_event(|window, event| match event {
  1451. /// tauri::WebviewEvent::DragDrop(event) => {
  1452. /// println!("{:?}", event);
  1453. /// }
  1454. /// _ => {}
  1455. /// });
  1456. /// ```
  1457. #[must_use]
  1458. pub fn on_webview_event<F: Fn(&Webview<R>, &WebviewEvent) + Send + Sync + 'static>(
  1459. mut self,
  1460. handler: F,
  1461. ) -> Self {
  1462. self.webview_event_listeners.push(Box::new(handler));
  1463. self
  1464. }
  1465. /// Registers a URI scheme protocol available to all webviews.
  1466. /// Leverages [setURLSchemeHandler](https://developer.apple.com/documentation/webkit/wkwebviewconfiguration/2875766-seturlschemehandler) on macOS,
  1467. /// [AddWebResourceRequestedFilter](https://docs.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2.addwebresourcerequestedfilter?view=webview2-dotnet-1.0.774.44) on Windows
  1468. /// and [webkit-web-context-register-uri-scheme](https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebContext.html#webkit-web-context-register-uri-scheme) on Linux.
  1469. ///
  1470. /// # Arguments
  1471. ///
  1472. /// * `uri_scheme` The URI scheme to register, such as `example`.
  1473. /// * `protocol` the protocol associated with the given URI scheme. It's a function that takes a request and returns a response.
  1474. ///
  1475. /// # Examples
  1476. /// ```
  1477. /// tauri::Builder::default()
  1478. /// .register_uri_scheme_protocol("app-files", |_app, request| {
  1479. /// // skip leading `/`
  1480. /// if let Ok(data) = std::fs::read(&request.uri().path()[1..]) {
  1481. /// http::Response::builder()
  1482. /// .body(data)
  1483. /// .unwrap()
  1484. /// } else {
  1485. /// http::Response::builder()
  1486. /// .status(http::StatusCode::BAD_REQUEST)
  1487. /// .header(http::header::CONTENT_TYPE, mime::TEXT_PLAIN.essence_str())
  1488. /// .body("failed to read file".as_bytes().to_vec())
  1489. /// .unwrap()
  1490. /// }
  1491. /// });
  1492. /// ```
  1493. #[must_use]
  1494. pub fn register_uri_scheme_protocol<
  1495. N: Into<String>,
  1496. T: Into<Cow<'static, [u8]>>,
  1497. H: Fn(&AppHandle<R>, http::Request<Vec<u8>>) -> http::Response<T> + Send + Sync + 'static,
  1498. >(
  1499. mut self,
  1500. uri_scheme: N,
  1501. protocol: H,
  1502. ) -> Self {
  1503. self.uri_scheme_protocols.insert(
  1504. uri_scheme.into(),
  1505. Arc::new(UriSchemeProtocol {
  1506. protocol: Box::new(move |app, request, responder| {
  1507. responder.respond(protocol(app, request))
  1508. }),
  1509. }),
  1510. );
  1511. self
  1512. }
  1513. /// Similar to [`Self::register_uri_scheme_protocol`] but with an asynchronous responder that allows you
  1514. /// to process the request in a separate thread and respond asynchronously.
  1515. ///
  1516. /// # Arguments
  1517. ///
  1518. /// * `uri_scheme` The URI scheme to register, such as `example`.
  1519. /// * `protocol` the protocol associated with the given URI scheme. It's a function that takes an URL such as `example://localhost/asset.css`.
  1520. ///
  1521. /// # Examples
  1522. /// ```
  1523. /// tauri::Builder::default()
  1524. /// .register_asynchronous_uri_scheme_protocol("app-files", |_app, request, responder| {
  1525. /// // skip leading `/`
  1526. /// let path = request.uri().path()[1..].to_string();
  1527. /// std::thread::spawn(move || {
  1528. /// if let Ok(data) = std::fs::read(path) {
  1529. /// responder.respond(
  1530. /// http::Response::builder()
  1531. /// .body(data)
  1532. /// .unwrap()
  1533. /// );
  1534. /// } else {
  1535. /// responder.respond(
  1536. /// http::Response::builder()
  1537. /// .status(http::StatusCode::BAD_REQUEST)
  1538. /// .header(http::header::CONTENT_TYPE, mime::TEXT_PLAIN.essence_str())
  1539. /// .body("failed to read file".as_bytes().to_vec())
  1540. /// .unwrap()
  1541. /// );
  1542. /// }
  1543. /// });
  1544. /// });
  1545. /// ```
  1546. #[must_use]
  1547. pub fn register_asynchronous_uri_scheme_protocol<
  1548. N: Into<String>,
  1549. H: Fn(&AppHandle<R>, http::Request<Vec<u8>>, UriSchemeResponder) + Send + Sync + 'static,
  1550. >(
  1551. mut self,
  1552. uri_scheme: N,
  1553. protocol: H,
  1554. ) -> Self {
  1555. self.uri_scheme_protocols.insert(
  1556. uri_scheme.into(),
  1557. Arc::new(UriSchemeProtocol {
  1558. protocol: Box::new(protocol),
  1559. }),
  1560. );
  1561. self
  1562. }
  1563. /// Change the device event filter mode.
  1564. ///
  1565. /// Since the DeviceEvent capture can lead to high CPU usage for unfocused windows, [`tao`]
  1566. /// will ignore them by default for unfocused windows on Windows. This method allows changing
  1567. /// the filter to explicitly capture them again.
  1568. ///
  1569. /// ## Platform-specific
  1570. ///
  1571. /// - ** Linux / macOS / iOS / Android**: Unsupported.
  1572. ///
  1573. /// # Examples
  1574. /// ```,no_run
  1575. /// tauri::Builder::default()
  1576. /// .device_event_filter(tauri::DeviceEventFilter::Always);
  1577. /// ```
  1578. ///
  1579. /// [`tao`]: https://crates.io/crates/tao
  1580. pub fn device_event_filter(mut self, filter: DeviceEventFilter) -> Self {
  1581. self.device_event_filter = filter;
  1582. self
  1583. }
  1584. /// Builds the application.
  1585. #[allow(clippy::type_complexity, unused_mut)]
  1586. #[cfg_attr(
  1587. feature = "tracing",
  1588. tracing::instrument(name = "app::build", skip_all)
  1589. )]
  1590. pub fn build(mut self, context: Context<R>) -> crate::Result<App<R>> {
  1591. #[cfg(target_os = "macos")]
  1592. if self.menu.is_none() && self.enable_macos_default_menu {
  1593. self.menu = Some(Box::new(|app_handle| {
  1594. crate::menu::Menu::default(app_handle)
  1595. }));
  1596. }
  1597. let manager = Arc::new(AppManager::with_handlers(
  1598. context,
  1599. self.plugins,
  1600. self.invoke_handler,
  1601. self.on_page_load,
  1602. self.uri_scheme_protocols,
  1603. self.state,
  1604. self.window_event_listeners,
  1605. self.webview_event_listeners,
  1606. #[cfg(desktop)]
  1607. HashMap::new(),
  1608. (self.invoke_responder, self.invoke_initialization_script),
  1609. self.invoke_key,
  1610. ));
  1611. #[cfg(any(
  1612. target_os = "linux",
  1613. target_os = "dragonfly",
  1614. target_os = "freebsd",
  1615. target_os = "netbsd",
  1616. target_os = "openbsd"
  1617. ))]
  1618. let app_id = if manager.config.app.enable_gtk_app_id {
  1619. Some(manager.config.identifier.clone())
  1620. } else {
  1621. None
  1622. };
  1623. let runtime_args = RuntimeInitArgs {
  1624. #[cfg(any(
  1625. target_os = "linux",
  1626. target_os = "dragonfly",
  1627. target_os = "freebsd",
  1628. target_os = "netbsd",
  1629. target_os = "openbsd"
  1630. ))]
  1631. app_id,
  1632. #[cfg(windows)]
  1633. msg_hook: {
  1634. let menus = manager.menu.menus.clone();
  1635. Some(Box::new(move |msg| {
  1636. use windows::Win32::UI::WindowsAndMessaging::{TranslateAcceleratorW, HACCEL, MSG};
  1637. unsafe {
  1638. let msg = msg as *const MSG;
  1639. for menu in menus.lock().unwrap().values() {
  1640. let translated =
  1641. TranslateAcceleratorW((*msg).hwnd, HACCEL(menu.inner().haccel() as _), msg);
  1642. if translated == 1 {
  1643. return true;
  1644. }
  1645. }
  1646. false
  1647. }
  1648. }))
  1649. },
  1650. };
  1651. #[cfg(any(windows, target_os = "linux"))]
  1652. let mut runtime = if self.runtime_any_thread {
  1653. R::new_any_thread(runtime_args)?
  1654. } else {
  1655. R::new(runtime_args)?
  1656. };
  1657. #[cfg(not(any(windows, target_os = "linux")))]
  1658. let mut runtime = R::new(runtime_args)?;
  1659. #[cfg(desktop)]
  1660. {
  1661. // setup menu event handler
  1662. let proxy = runtime.create_proxy();
  1663. muda::MenuEvent::set_event_handler(Some(move |e: muda::MenuEvent| {
  1664. let _ = proxy.send_event(EventLoopMessage::MenuEvent(e.into()));
  1665. }));
  1666. // setup tray event handler
  1667. #[cfg(feature = "tray-icon")]
  1668. {
  1669. let proxy = runtime.create_proxy();
  1670. tray_icon::TrayIconEvent::set_event_handler(Some(move |e: tray_icon::TrayIconEvent| {
  1671. let _ = proxy.send_event(EventLoopMessage::TrayIconEvent(e.into()));
  1672. }));
  1673. }
  1674. }
  1675. runtime.set_device_event_filter(self.device_event_filter);
  1676. let runtime_handle = runtime.handle();
  1677. #[allow(unused_mut)]
  1678. let mut app = App {
  1679. runtime: Some(runtime),
  1680. setup: Some(self.setup),
  1681. manager: manager.clone(),
  1682. handle: AppHandle {
  1683. runtime_handle,
  1684. manager,
  1685. },
  1686. ran_setup: false,
  1687. };
  1688. #[cfg(desktop)]
  1689. if let Some(menu) = self.menu {
  1690. let menu = menu(&app.handle)?;
  1691. app
  1692. .manager
  1693. .menu
  1694. .menus_stash_lock()
  1695. .insert(menu.id().clone(), menu.clone());
  1696. #[cfg(target_os = "macos")]
  1697. init_app_menu(&menu)?;
  1698. app.manager.menu.menu_lock().replace(menu);
  1699. }
  1700. app.register_core_plugins()?;
  1701. let env = Env::default();
  1702. app.manage(env);
  1703. app.manage(Scopes {
  1704. #[cfg(feature = "protocol-asset")]
  1705. asset_protocol: crate::scope::fs::Scope::new(
  1706. &app,
  1707. &app.config().app.security.asset_protocol.scope,
  1708. )?,
  1709. });
  1710. app.manage(ChannelDataIpcQueue::default());
  1711. app.handle.plugin(crate::ipc::channel::plugin())?;
  1712. #[cfg(windows)]
  1713. {
  1714. if let crate::utils::config::WebviewInstallMode::FixedRuntime { path } =
  1715. &app.manager.config().bundle.windows.webview_install_mode
  1716. {
  1717. if let Ok(resource_dir) = app.path().resource_dir() {
  1718. std::env::set_var(
  1719. "WEBVIEW2_BROWSER_EXECUTABLE_FOLDER",
  1720. resource_dir.join(path),
  1721. );
  1722. } else {
  1723. #[cfg(debug_assertions)]
  1724. eprintln!(
  1725. "failed to resolve resource directory; fallback to the installed Webview2 runtime."
  1726. );
  1727. }
  1728. }
  1729. }
  1730. let handle = app.handle();
  1731. // initialize default tray icon if defined
  1732. #[cfg(all(desktop, feature = "tray-icon"))]
  1733. {
  1734. let config = app.config();
  1735. if let Some(tray_config) = &config.app.tray_icon {
  1736. let mut tray =
  1737. TrayIconBuilder::with_id(tray_config.id.clone().unwrap_or_else(|| "main".into()))
  1738. .icon_as_template(tray_config.icon_as_template)
  1739. .menu_on_left_click(tray_config.menu_on_left_click);
  1740. if let Some(icon) = &app.manager.tray.icon {
  1741. tray = tray.icon(icon.clone());
  1742. }
  1743. if let Some(title) = &tray_config.title {
  1744. tray = tray.title(title);
  1745. }
  1746. if let Some(tooltip) = &tray_config.tooltip {
  1747. tray = tray.tooltip(tooltip);
  1748. }
  1749. tray.build(handle)?;
  1750. }
  1751. }
  1752. app.manager.initialize_plugins(handle)?;
  1753. Ok(app)
  1754. }
  1755. /// Runs the configured Tauri application.
  1756. pub fn run(self, context: Context<R>) -> crate::Result<()> {
  1757. self.build(context)?.run(|_, _| {});
  1758. Ok(())
  1759. }
  1760. }
  1761. pub(crate) type UriSchemeResponderFn = Box<dyn FnOnce(http::Response<Cow<'static, [u8]>>) + Send>;
  1762. /// Async uri scheme protocol responder.
  1763. pub struct UriSchemeResponder(pub(crate) UriSchemeResponderFn);
  1764. impl UriSchemeResponder {
  1765. /// Resolves the request with the given response.
  1766. pub fn respond<T: Into<Cow<'static, [u8]>>>(self, response: http::Response<T>) {
  1767. let (parts, body) = response.into_parts();
  1768. (self.0)(http::Response::from_parts(parts, body.into()))
  1769. }
  1770. }
  1771. #[cfg(target_os = "macos")]
  1772. fn init_app_menu<R: Runtime>(menu: &Menu<R>) -> crate::Result<()> {
  1773. menu.inner().init_for_nsapp();
  1774. if let Some(window_menu) = menu.get(crate::menu::WINDOW_SUBMENU_ID) {
  1775. if let Some(m) = window_menu.as_submenu() {
  1776. m.set_as_windows_menu_for_nsapp()?;
  1777. }
  1778. }
  1779. if let Some(help_menu) = menu.get(crate::menu::HELP_SUBMENU_ID) {
  1780. if let Some(m) = help_menu.as_submenu() {
  1781. m.set_as_help_menu_for_nsapp()?;
  1782. }
  1783. }
  1784. Ok(())
  1785. }
  1786. impl<R: Runtime> HasDisplayHandle for AppHandle<R> {
  1787. fn display_handle(
  1788. &self,
  1789. ) -> std::result::Result<raw_window_handle::DisplayHandle<'_>, raw_window_handle::HandleError> {
  1790. self.runtime_handle.display_handle()
  1791. }
  1792. }
  1793. impl<R: Runtime> HasDisplayHandle for App<R> {
  1794. fn display_handle(
  1795. &self,
  1796. ) -> std::result::Result<raw_window_handle::DisplayHandle<'_>, raw_window_handle::HandleError> {
  1797. self.handle.display_handle()
  1798. }
  1799. }
  1800. #[cfg_attr(feature = "tracing", tracing::instrument(name = "app::setup"))]
  1801. fn setup<R: Runtime>(app: &mut App<R>) -> crate::Result<()> {
  1802. app.ran_setup = true;
  1803. for window_config in app.config().app.windows.clone() {
  1804. WebviewWindowBuilder::from_config(app.handle(), &window_config)?.build()?;
  1805. }
  1806. app.manager.assets.setup(app);
  1807. if let Some(setup) = app.setup.take() {
  1808. (setup)(app).map_err(|e| crate::Error::Setup(e.into()))?;
  1809. }
  1810. Ok(())
  1811. }
  1812. fn on_event_loop_event<R: Runtime>(
  1813. app_handle: &AppHandle<R>,
  1814. event: RuntimeRunEvent<EventLoopMessage>,
  1815. manager: &AppManager<R>,
  1816. ) -> RunEvent {
  1817. if let RuntimeRunEvent::WindowEvent {
  1818. label,
  1819. event: RuntimeWindowEvent::Destroyed,
  1820. } = &event
  1821. {
  1822. manager.on_window_close(label);
  1823. }
  1824. let event = match event {
  1825. RuntimeRunEvent::Exit => RunEvent::Exit,
  1826. RuntimeRunEvent::ExitRequested { code, tx } => RunEvent::ExitRequested {
  1827. code,
  1828. api: ExitRequestApi(tx),
  1829. },
  1830. RuntimeRunEvent::WindowEvent { label, event } => RunEvent::WindowEvent {
  1831. label,
  1832. event: event.into(),
  1833. },
  1834. RuntimeRunEvent::WebviewEvent { label, event } => RunEvent::WebviewEvent {
  1835. label,
  1836. event: event.into(),
  1837. },
  1838. RuntimeRunEvent::Ready => {
  1839. // set the app icon in development
  1840. #[cfg(all(dev, target_os = "macos"))]
  1841. unsafe {
  1842. use cocoa::{
  1843. appkit::NSImage,
  1844. base::{id, nil},
  1845. foundation::NSData,
  1846. };
  1847. use objc::*;
  1848. if let Some(icon) = app_handle.manager.app_icon.clone() {
  1849. let ns_app: id = msg_send![class!(NSApplication), sharedApplication];
  1850. let data = NSData::dataWithBytes_length_(
  1851. nil,
  1852. icon.as_ptr() as *const std::os::raw::c_void,
  1853. icon.len() as u64,
  1854. );
  1855. let app_icon = NSImage::initWithData_(NSImage::alloc(nil), data);
  1856. let _: () = msg_send![ns_app, setApplicationIconImage: app_icon];
  1857. }
  1858. }
  1859. RunEvent::Ready
  1860. }
  1861. RuntimeRunEvent::Resumed => RunEvent::Resumed,
  1862. RuntimeRunEvent::MainEventsCleared => RunEvent::MainEventsCleared,
  1863. RuntimeRunEvent::UserEvent(t) => {
  1864. match t {
  1865. #[cfg(desktop)]
  1866. EventLoopMessage::MenuEvent(ref e) => {
  1867. for listener in &*app_handle
  1868. .manager
  1869. .menu
  1870. .global_event_listeners
  1871. .lock()
  1872. .unwrap()
  1873. {
  1874. listener(app_handle, e.clone());
  1875. }
  1876. for (label, listener) in &*app_handle.manager.menu.event_listeners.lock().unwrap() {
  1877. if let Some(w) = app_handle.manager().get_window(label) {
  1878. listener(&w, e.clone());
  1879. }
  1880. }
  1881. }
  1882. #[cfg(all(desktop, feature = "tray-icon"))]
  1883. EventLoopMessage::TrayIconEvent(ref e) => {
  1884. for listener in &*app_handle
  1885. .manager
  1886. .tray
  1887. .global_event_listeners
  1888. .lock()
  1889. .unwrap()
  1890. {
  1891. listener(app_handle, e.clone());
  1892. }
  1893. for (id, listener) in &*app_handle.manager.tray.event_listeners.lock().unwrap() {
  1894. if e.id() == id {
  1895. if let Some(tray) = app_handle.tray_by_id(id) {
  1896. listener(&tray, e.clone());
  1897. }
  1898. }
  1899. }
  1900. }
  1901. }
  1902. #[allow(unreachable_code)]
  1903. t.into()
  1904. }
  1905. #[cfg(any(target_os = "macos", target_os = "ios"))]
  1906. RuntimeRunEvent::Opened { urls } => RunEvent::Opened { urls },
  1907. #[cfg(target_os = "macos")]
  1908. RuntimeRunEvent::Reopen {
  1909. has_visible_windows,
  1910. } => RunEvent::Reopen {
  1911. has_visible_windows,
  1912. },
  1913. _ => unimplemented!(),
  1914. };
  1915. manager
  1916. .plugins
  1917. .lock()
  1918. .expect("poisoned plugin store")
  1919. .on_event(app_handle, &event);
  1920. event
  1921. }
  1922. #[cfg(test)]
  1923. mod tests {
  1924. #[test]
  1925. fn is_send_sync() {
  1926. crate::test_utils::assert_send::<super::AppHandle>();
  1927. crate::test_utils::assert_sync::<super::AppHandle>();
  1928. #[cfg(feature = "wry")]
  1929. {
  1930. crate::test_utils::assert_send::<super::AssetResolver<crate::Wry>>();
  1931. crate::test_utils::assert_sync::<super::AssetResolver<crate::Wry>>();
  1932. }
  1933. }
  1934. }