@@ -128,8 +128,21 @@ crate::manager::default_args! {
128128 ///
129129 /// This type implements [`Manager`] which allows for manipulation of global application items.
130130 pub struct App <P : Params > {
131- runtime: P :: Runtime ,
131+ runtime: Option < P :: Runtime > ,
132132 manager: WindowManager <P >,
133+ #[ cfg( shell_execute) ]
134+ cleanup_on_drop: bool ,
135+ }
136+ }
137+
138+ impl < P : Params > Drop for App < P > {
139+ fn drop ( & mut self ) {
140+ #[ cfg( shell_execute) ]
141+ {
142+ if self . cleanup_on_drop {
143+ crate :: api:: process:: kill_children ( ) ;
144+ }
145+ }
133146 }
134147}
135148
@@ -140,7 +153,7 @@ impl<P: Params> ManagerBase<P> for App<P> {
140153 }
141154
142155 fn runtime ( & self ) -> RuntimeOrDispatch < ' _ , P > {
143- RuntimeOrDispatch :: Runtime ( & self . runtime )
156+ RuntimeOrDispatch :: Runtime ( self . runtime . as_ref ( ) . unwrap ( ) )
144157 }
145158}
146159
@@ -180,7 +193,7 @@ impl<P: Params> App<P> {
180193 /// Gets a handle to the application instance.
181194 pub fn handle ( & self ) -> AppHandle < P > {
182195 AppHandle {
183- runtime_handle : self . runtime . handle ( ) ,
196+ runtime_handle : self . runtime . as_ref ( ) . unwrap ( ) . handle ( ) ,
184197 manager : self . manager . clone ( ) ,
185198 }
186199 }
@@ -202,7 +215,7 @@ impl<P: Params> App<P> {
202215 /// }
203216 #[ cfg( any( target_os = "windows" , target_os = "macos" ) ) ]
204217 pub fn run_iteration ( & mut self ) -> crate :: runtime:: RunIteration {
205- self . runtime . run_iteration ( )
218+ self . runtime . as_mut ( ) . unwrap ( ) . run_iteration ( )
206219 }
207220}
208221
@@ -315,6 +328,9 @@ where
315328 /// System tray event handlers.
316329 #[ cfg( feature = "system-tray" ) ]
317330 system_tray_event_listeners : Vec < SystemTrayEventListener < Args < E , L , MID , TID , A , R > > > ,
331+
332+ #[ cfg( shell_execute) ]
333+ cleanup_on_drop : bool ,
318334}
319335
320336impl < E , L , MID , TID , A , R > Builder < E , L , MID , TID , A , R >
@@ -345,6 +361,8 @@ where
345361 system_tray : Vec :: new ( ) ,
346362 #[ cfg( feature = "system-tray" ) ]
347363 system_tray_event_listeners : Vec :: new ( ) ,
364+ #[ cfg( shell_execute) ]
365+ cleanup_on_drop : true ,
348366 }
349367 }
350368
@@ -572,6 +590,15 @@ where
572590 self
573591 }
574592
593+ /// Skips Tauri cleanup on [`App`] drop. Useful if your application has multiple [`App`] instances.
594+ ///
595+ /// The cleanup calls [`crate::api::process::kill_children`] so you may want to call that function before exiting the application.
596+ #[ cfg( shell_execute) ]
597+ pub fn skip_cleanup_on_drop ( mut self ) -> Self {
598+ self . cleanup_on_drop = false ;
599+ self
600+ }
601+
575602 /// Builds the application.
576603 #[ allow( clippy:: type_complexity) ]
577604 pub fn build ( mut self , context : Context < A > ) -> crate :: Result < App < Args < E , L , MID , TID , A , R > > > {
@@ -630,8 +657,10 @@ where
630657 }
631658
632659 let mut app = App {
633- runtime : R :: new ( ) ?,
660+ runtime : Some ( R :: new ( ) ?) ,
634661 manager,
662+ #[ cfg( shell_execute) ]
663+ cleanup_on_drop : self . cleanup_on_drop ,
635664 } ;
636665
637666 app. manager . initialize_plugins ( & app) ?;
@@ -647,7 +676,7 @@ where
647676
648677 for pending in self . pending_windows {
649678 let pending = app. manager . prepare_window ( pending, & pending_labels) ?;
650- let detached = app. runtime . create_window ( pending) ?;
679+ let detached = app. runtime . as_ref ( ) . unwrap ( ) . create_window ( pending) ?;
651680 let _window = app. manager . attach_window ( detached) ;
652681 #[ cfg( feature = "updater" ) ]
653682 if main_window. is_none ( ) {
@@ -665,6 +694,8 @@ where
665694 let ids = get_menu_ids ( & self . system_tray ) ;
666695 app
667696 . runtime
697+ . as_ref ( )
698+ . unwrap ( )
668699 . system_tray (
669700 system_tray_icon. expect ( "tray icon not found; please configure it on tauri.conf.json" ) ,
670701 self . system_tray ,
@@ -674,14 +705,18 @@ where
674705 let app_handle = app. handle ( ) ;
675706 let ids = ids. clone ( ) ;
676707 let listener = Arc :: new ( std:: sync:: Mutex :: new ( listener) ) ;
677- app. runtime . on_system_tray_event ( move |event| {
678- let app_handle = app_handle. clone ( ) ;
679- let menu_item_id = ids. get ( & event. menu_item_id ) . unwrap ( ) . clone ( ) ;
680- let listener = listener. clone ( ) ;
681- crate :: async_runtime:: spawn ( async move {
682- listener. lock ( ) . unwrap ( ) ( & app_handle, SystemTrayEvent { menu_item_id } ) ;
708+ app
709+ . runtime
710+ . as_mut ( )
711+ . unwrap ( )
712+ . on_system_tray_event ( move |event| {
713+ let app_handle = app_handle. clone ( ) ;
714+ let menu_item_id = ids. get ( & event. menu_item_id ) . unwrap ( ) . clone ( ) ;
715+ let listener = listener. clone ( ) ;
716+ crate :: async_runtime:: spawn ( async move {
717+ listener. lock ( ) . unwrap ( ) ( & app_handle, SystemTrayEvent { menu_item_id } ) ;
718+ } ) ;
683719 } ) ;
684- } ) ;
685720 }
686721 }
687722
@@ -690,7 +725,8 @@ where
690725
691726 /// Runs the configured Tauri application.
692727 pub fn run ( self , context : Context < A > ) -> crate :: Result < ( ) > {
693- self . build ( context) ?. runtime . run ( ) ;
728+ let mut app = self . build ( context) ?;
729+ app. runtime . take ( ) . unwrap ( ) . run ( ) ;
694730 Ok ( ( ) )
695731 }
696732}
0 commit comments