app.rs 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808
  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, CustomProtocol, WindowManager},
  11. plugin::{Plugin, PluginStore},
  12. runtime::{
  13. http::{Request as HttpRequest, Response as HttpResponse},
  14. webview::WebviewAttributes,
  15. window::{PendingWindow, WindowEvent as RuntimeWindowEvent},
  16. ExitRequestedEventAction, RunEvent as RuntimeRunEvent,
  17. },
  18. scope::IpcScope,
  19. sealed::{ManagerBase, RuntimeOrDispatch},
  20. utils::config::Config,
  21. utils::{assets::Assets, Env},
  22. Context, DeviceEventFilter, EventLoopMessage, Icon, Manager, Monitor, Runtime, Scopes,
  23. StateManager, Theme, Window,
  24. };
  25. #[cfg(feature = "protocol-asset")]
  26. use crate::scope::FsScope;
  27. #[cfg(desktop)]
  28. use crate::menu::{Menu, MenuEvent};
  29. #[cfg(all(desktop, feature = "tray-icon"))]
  30. use crate::tray::{TrayIcon, TrayIconBuilder, TrayIconEvent, TrayIconId};
  31. #[cfg(desktop)]
  32. use crate::window::WindowMenu;
  33. use raw_window_handle::HasRawDisplayHandle;
  34. use serde::Deserialize;
  35. use serialize_to_javascript::{default_template, DefaultTemplate, Template};
  36. use tauri_macros::default_runtime;
  37. #[cfg(desktop)]
  38. use tauri_runtime::EventLoopProxy;
  39. use tauri_runtime::{
  40. window::{
  41. dpi::{PhysicalPosition, PhysicalSize},
  42. FileDropEvent,
  43. },
  44. RuntimeInitArgs,
  45. };
  46. use tauri_utils::PackageInfo;
  47. use std::{
  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<CustomProtocol<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>>>>,
  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>, String, &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>> + '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 an URL such as `example://localhost/asset.css`.
  1246. #[must_use]
  1247. pub fn register_uri_scheme_protocol<
  1248. N: Into<String>,
  1249. H: Fn(&AppHandle<R>, &HttpRequest) -> Result<HttpResponse, Box<dyn std::error::Error>>
  1250. + Send
  1251. + Sync
  1252. + 'static,
  1253. >(
  1254. mut self,
  1255. uri_scheme: N,
  1256. protocol: H,
  1257. ) -> Self {
  1258. self.uri_scheme_protocols.insert(
  1259. uri_scheme.into(),
  1260. Arc::new(CustomProtocol {
  1261. protocol: Box::new(protocol),
  1262. }),
  1263. );
  1264. self
  1265. }
  1266. /// Change the device event filter mode.
  1267. ///
  1268. /// Since the DeviceEvent capture can lead to high CPU usage for unfocused windows, [`tao`]
  1269. /// will ignore them by default for unfocused windows on Windows. This method allows changing
  1270. /// the filter to explicitly capture them again.
  1271. ///
  1272. /// ## Platform-specific
  1273. ///
  1274. /// - ** Linux / macOS / iOS / Android**: Unsupported.
  1275. ///
  1276. /// # Examples
  1277. /// ```,no_run
  1278. /// tauri::Builder::default()
  1279. /// .device_event_filter(tauri::DeviceEventFilter::Always);
  1280. /// ```
  1281. ///
  1282. /// [`tao`]: https://crates.io/crates/tao
  1283. pub fn device_event_filter(mut self, filter: DeviceEventFilter) -> Self {
  1284. self.device_event_filter = filter;
  1285. self
  1286. }
  1287. /// Builds the application.
  1288. #[allow(clippy::type_complexity)]
  1289. pub fn build<A: Assets>(mut self, context: Context<A>) -> crate::Result<App<R>> {
  1290. #[cfg(target_os = "macos")]
  1291. if self.menu.is_none() && self.enable_macos_default_menu {
  1292. self.menu = Some(Box::new(|app_handle| {
  1293. crate::menu::Menu::default(app_handle)
  1294. }));
  1295. }
  1296. let manager = WindowManager::with_handlers(
  1297. context,
  1298. self.plugins,
  1299. self.invoke_handler,
  1300. self.on_page_load,
  1301. self.uri_scheme_protocols,
  1302. self.state,
  1303. self.window_event_listeners,
  1304. #[cfg(desktop)]
  1305. HashMap::new(),
  1306. (self.invoke_responder, self.invoke_initialization_script),
  1307. );
  1308. // set up all the windows defined in the config
  1309. for config in manager.config().tauri.windows.clone() {
  1310. let label = config.label.clone();
  1311. let webview_attributes = WebviewAttributes::from(&config);
  1312. self.pending_windows.push(PendingWindow::with_config(
  1313. config,
  1314. webview_attributes,
  1315. label,
  1316. )?);
  1317. }
  1318. let runtime_args = RuntimeInitArgs {
  1319. #[cfg(windows)]
  1320. msg_hook: {
  1321. let menus = manager.inner.menus.clone();
  1322. Some(Box::new(move |msg| {
  1323. use windows::Win32::UI::WindowsAndMessaging::{TranslateAcceleratorW, HACCEL, MSG};
  1324. unsafe {
  1325. let msg = msg as *const MSG;
  1326. for menu in menus.lock().unwrap().values() {
  1327. let translated =
  1328. TranslateAcceleratorW((*msg).hwnd, HACCEL(menu.inner().haccel()), msg);
  1329. if translated == 1 {
  1330. return true;
  1331. }
  1332. }
  1333. false
  1334. }
  1335. }))
  1336. },
  1337. };
  1338. #[cfg(any(windows, target_os = "linux"))]
  1339. let mut runtime = if self.runtime_any_thread {
  1340. R::new_any_thread(runtime_args)?
  1341. } else {
  1342. R::new(runtime_args)?
  1343. };
  1344. #[cfg(not(any(windows, target_os = "linux")))]
  1345. let mut runtime = R::new(runtime_args)?;
  1346. #[cfg(desktop)]
  1347. {
  1348. // setup menu event handler
  1349. let proxy = runtime.create_proxy();
  1350. crate::menu::MenuEvent::set_event_handler(Some(move |e| {
  1351. let _ = proxy.send_event(EventLoopMessage::MenuEvent(e));
  1352. }));
  1353. // setup tray event handler
  1354. #[cfg(feature = "tray-icon")]
  1355. {
  1356. let proxy = runtime.create_proxy();
  1357. crate::tray::TrayIconEvent::set_event_handler(Some(move |e| {
  1358. let _ = proxy.send_event(EventLoopMessage::TrayIconEvent(e));
  1359. }));
  1360. }
  1361. }
  1362. runtime.set_device_event_filter(self.device_event_filter);
  1363. let runtime_handle = runtime.handle();
  1364. #[allow(unused_mut)]
  1365. let mut app = App {
  1366. runtime: Some(runtime),
  1367. pending_windows: Some(self.pending_windows),
  1368. setup: Some(self.setup),
  1369. manager: manager.clone(),
  1370. handle: AppHandle {
  1371. runtime_handle,
  1372. manager,
  1373. },
  1374. };
  1375. #[cfg(desktop)]
  1376. if let Some(menu) = self.menu {
  1377. let menu = menu(&app.handle)?;
  1378. app
  1379. .manager
  1380. .menus_stash_lock()
  1381. .insert(menu.id().clone(), menu.clone());
  1382. #[cfg(target_os = "macos")]
  1383. init_app_menu(&menu)?;
  1384. app.manager.menu_lock().replace(menu);
  1385. }
  1386. app.register_core_plugins()?;
  1387. let env = Env::default();
  1388. app.manage(env);
  1389. app.manage(Scopes {
  1390. ipc: IpcScope::new(&app.config()),
  1391. #[cfg(feature = "protocol-asset")]
  1392. asset_protocol: FsScope::for_fs_api(&app, &app.config().tauri.security.asset_protocol.scope)?,
  1393. });
  1394. app.manage(ChannelDataIpcQueue::default());
  1395. app.handle.plugin(crate::ipc::channel::plugin())?;
  1396. #[cfg(windows)]
  1397. {
  1398. if let crate::utils::config::WebviewInstallMode::FixedRuntime { path } = &app
  1399. .manager
  1400. .config()
  1401. .tauri
  1402. .bundle
  1403. .windows
  1404. .webview_install_mode
  1405. {
  1406. if let Ok(resource_dir) = app.path().resource_dir() {
  1407. std::env::set_var(
  1408. "WEBVIEW2_BROWSER_EXECUTABLE_FOLDER",
  1409. resource_dir.join(path),
  1410. );
  1411. } else {
  1412. #[cfg(debug_assertions)]
  1413. eprintln!(
  1414. "failed to resolve resource directory; fallback to the installed Webview2 runtime."
  1415. );
  1416. }
  1417. }
  1418. }
  1419. let handle = app.handle();
  1420. // initialize default tray icon if defined
  1421. #[cfg(all(desktop, feature = "tray-icon"))]
  1422. {
  1423. let config = app.config();
  1424. if let Some(tray_config) = &config.tauri.tray_icon {
  1425. let mut tray = TrayIconBuilder::new()
  1426. .icon_as_template(tray_config.icon_as_template)
  1427. .menu_on_left_click(tray_config.menu_on_left_click);
  1428. if let Some(icon) = &app.manager.inner.tray_icon {
  1429. tray = tray.icon(icon.clone());
  1430. }
  1431. if let Some(title) = &tray_config.title {
  1432. tray = tray.title(title);
  1433. }
  1434. if let Some(tooltip) = &tray_config.tooltip {
  1435. tray = tray.tooltip(tooltip);
  1436. }
  1437. let tray = tray.build(handle)?;
  1438. app.manager.inner.tray_icons.lock().unwrap().push(tray);
  1439. }
  1440. }
  1441. app.manager.initialize_plugins(handle)?;
  1442. Ok(app)
  1443. }
  1444. /// Runs the configured Tauri application.
  1445. pub fn run<A: Assets>(self, context: Context<A>) -> crate::Result<()> {
  1446. self.build(context)?.run(|_, _| {});
  1447. Ok(())
  1448. }
  1449. }
  1450. #[cfg(target_os = "macos")]
  1451. fn init_app_menu<R: Runtime>(menu: &Menu<R>) -> crate::Result<()> {
  1452. menu.inner().init_for_nsapp();
  1453. if let Some(window_menu) = menu.get(crate::menu::WINDOW_SUBMENU_ID) {
  1454. if let Some(m) = window_menu.as_submenu() {
  1455. m.set_as_windows_menu_for_nsapp()?;
  1456. }
  1457. }
  1458. if let Some(help_menu) = menu.get(crate::menu::HELP_SUBMENU_ID) {
  1459. if let Some(m) = help_menu.as_submenu() {
  1460. m.set_as_help_menu_for_nsapp()?;
  1461. }
  1462. }
  1463. Ok(())
  1464. }
  1465. unsafe impl<R: Runtime> HasRawDisplayHandle for AppHandle<R> {
  1466. fn raw_display_handle(&self) -> raw_window_handle::RawDisplayHandle {
  1467. self.runtime_handle.raw_display_handle()
  1468. }
  1469. }
  1470. unsafe impl<R: Runtime> HasRawDisplayHandle for App<R> {
  1471. fn raw_display_handle(&self) -> raw_window_handle::RawDisplayHandle {
  1472. self.handle.raw_display_handle()
  1473. }
  1474. }
  1475. fn setup<R: Runtime>(app: &mut App<R>) -> crate::Result<()> {
  1476. let pending_windows = app.pending_windows.take();
  1477. if let Some(pending_windows) = pending_windows {
  1478. let window_labels = pending_windows
  1479. .iter()
  1480. .map(|p| p.label.clone())
  1481. .collect::<Vec<_>>();
  1482. let app_handle = app.handle();
  1483. let manager = app.manager();
  1484. for pending in pending_windows {
  1485. let pending = manager.prepare_window(app_handle.clone(), pending, &window_labels)?;
  1486. #[cfg(desktop)]
  1487. let window_menu = app.manager.menu_lock().as_ref().map(|m| WindowMenu {
  1488. is_app_wide: true,
  1489. menu: m.clone(),
  1490. });
  1491. #[cfg(desktop)]
  1492. let handler = manager.prepare_window_menu_creation_handler(window_menu.as_ref());
  1493. #[cfg(not(desktop))]
  1494. #[allow(clippy::type_complexity)]
  1495. let handler: Option<Box<dyn Fn(tauri_runtime::window::RawWindow<'_>) + Send>> = None;
  1496. let window_effects = pending.webview_attributes.window_effects.clone();
  1497. let detached = if let RuntimeOrDispatch::RuntimeHandle(runtime) = app_handle.runtime() {
  1498. runtime.create_window(pending, handler)?
  1499. } else {
  1500. // the AppHandle's runtime is always RuntimeOrDispatch::RuntimeHandle
  1501. unreachable!()
  1502. };
  1503. let window = manager.attach_window(
  1504. app_handle.clone(),
  1505. detached,
  1506. #[cfg(desktop)]
  1507. None,
  1508. );
  1509. if let Some(effects) = window_effects {
  1510. crate::vibrancy::set_window_effects(&window, Some(effects))?;
  1511. }
  1512. }
  1513. }
  1514. if let Some(setup) = app.setup.take() {
  1515. (setup)(app).map_err(|e| crate::Error::Setup(e.into()))?;
  1516. }
  1517. Ok(())
  1518. }
  1519. fn on_event_loop_event<R: Runtime, F: FnMut(&AppHandle<R>, RunEvent) + 'static>(
  1520. app_handle: &AppHandle<R>,
  1521. event: RuntimeRunEvent<EventLoopMessage>,
  1522. manager: &WindowManager<R>,
  1523. callback: Option<&mut F>,
  1524. ) {
  1525. if let RuntimeRunEvent::WindowEvent {
  1526. label,
  1527. event: RuntimeWindowEvent::Destroyed,
  1528. } = &event
  1529. {
  1530. manager.on_window_close(label);
  1531. }
  1532. let event = match event {
  1533. RuntimeRunEvent::Exit => RunEvent::Exit,
  1534. RuntimeRunEvent::ExitRequested { tx } => RunEvent::ExitRequested {
  1535. api: ExitRequestApi(tx),
  1536. },
  1537. RuntimeRunEvent::WindowEvent { label, event } => RunEvent::WindowEvent {
  1538. label,
  1539. event: event.into(),
  1540. },
  1541. RuntimeRunEvent::Ready => {
  1542. // set the app icon in development
  1543. #[cfg(all(dev, target_os = "macos"))]
  1544. unsafe {
  1545. use cocoa::{
  1546. appkit::NSImage,
  1547. base::{id, nil},
  1548. foundation::NSData,
  1549. };
  1550. use objc::*;
  1551. if let Some(icon) = app_handle.manager.inner.app_icon.clone() {
  1552. let ns_app: id = msg_send![class!(NSApplication), sharedApplication];
  1553. let data = NSData::dataWithBytes_length_(
  1554. nil,
  1555. icon.as_ptr() as *const std::os::raw::c_void,
  1556. icon.len() as u64,
  1557. );
  1558. let app_icon = NSImage::initWithData_(NSImage::alloc(nil), data);
  1559. let _: () = msg_send![ns_app, setApplicationIconImage: app_icon];
  1560. }
  1561. }
  1562. RunEvent::Ready
  1563. }
  1564. RuntimeRunEvent::Resumed => RunEvent::Resumed,
  1565. RuntimeRunEvent::MainEventsCleared => RunEvent::MainEventsCleared,
  1566. RuntimeRunEvent::UserEvent(t) => {
  1567. match t {
  1568. #[cfg(desktop)]
  1569. EventLoopMessage::MenuEvent(ref e) => {
  1570. for listener in &*app_handle
  1571. .manager
  1572. .inner
  1573. .menu_event_listeners
  1574. .lock()
  1575. .unwrap()
  1576. {
  1577. listener(app_handle, e.clone());
  1578. }
  1579. for (label, listener) in &*app_handle
  1580. .manager
  1581. .inner
  1582. .window_menu_event_listeners
  1583. .lock()
  1584. .unwrap()
  1585. {
  1586. if let Some(w) = app_handle.get_window(label) {
  1587. listener(&w, e.clone());
  1588. }
  1589. }
  1590. }
  1591. #[cfg(all(desktop, feature = "tray-icon"))]
  1592. EventLoopMessage::TrayIconEvent(ref e) => {
  1593. for listener in &*app_handle
  1594. .manager
  1595. .inner
  1596. .global_tray_event_listeners
  1597. .lock()
  1598. .unwrap()
  1599. {
  1600. listener(app_handle, e.clone());
  1601. }
  1602. for (id, listener) in &*app_handle
  1603. .manager
  1604. .inner
  1605. .tray_event_listeners
  1606. .lock()
  1607. .unwrap()
  1608. {
  1609. if e.id == id {
  1610. if let Some(tray) = app_handle.tray_by_id(id) {
  1611. listener(&tray, e.clone());
  1612. }
  1613. }
  1614. }
  1615. }
  1616. }
  1617. #[allow(unreachable_code)]
  1618. t.into()
  1619. }
  1620. #[cfg(any(target_os = "macos", target_os = "ios"))]
  1621. RuntimeRunEvent::Opened { urls } => RunEvent::Opened { urls },
  1622. _ => unimplemented!(),
  1623. };
  1624. manager
  1625. .inner
  1626. .plugins
  1627. .lock()
  1628. .expect("poisoned plugin store")
  1629. .on_event(app_handle, &event);
  1630. if let Some(c) = callback {
  1631. c(app_handle, event);
  1632. }
  1633. }
  1634. /// Make `Wry` the default `Runtime` for `Builder`
  1635. #[cfg(feature = "wry")]
  1636. #[cfg_attr(doc_cfg, doc(cfg(feature = "wry")))]
  1637. impl Default for Builder<crate::Wry> {
  1638. fn default() -> Self {
  1639. Self::new()
  1640. }
  1641. }
  1642. #[cfg(test)]
  1643. mod tests {
  1644. #[test]
  1645. fn is_send_sync() {
  1646. crate::test_utils::assert_send::<super::AppHandle>();
  1647. crate::test_utils::assert_sync::<super::AppHandle>();
  1648. #[cfg(feature = "wry")]
  1649. {
  1650. crate::test_utils::assert_send::<super::AssetResolver<crate::Wry>>();
  1651. crate::test_utils::assert_sync::<super::AssetResolver<crate::Wry>>();
  1652. }
  1653. }
  1654. }