app.rs 70 KB

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