app.rs 57 KB

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