app.rs 60 KB

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