@@ -41,7 +41,7 @@ use tauri_runtime::{
4141 } ,
4242 RuntimeInitArgs ,
4343} ;
44- use tauri_utils:: PackageInfo ;
44+ use tauri_utils:: { debug_eprintln , PackageInfo } ;
4545
4646use std:: {
4747 borrow:: Cow ,
@@ -69,12 +69,17 @@ pub type SetupHook<R> =
6969/// A closure that is run every time a page starts or finishes loading.
7070pub type OnPageLoad < R > = dyn Fn ( & Webview < R > , & PageLoadPayload < ' _ > ) + Send + Sync + ' static ;
7171
72+ /// The exit code on [`RunEvent::ExitRequested`] when [`AppHandle#method.restart`] is called.
73+ pub const RESTART_EXIT_CODE : i32 = i32:: MAX ;
74+
7275/// Api exposed on the `ExitRequested` event.
7376#[ derive( Debug ) ]
7477pub struct ExitRequestApi ( Sender < ExitRequestedEventAction > ) ;
7578
7679impl ExitRequestApi {
77- /// Prevents the app from exiting
80+ /// Prevents the app from exiting.
81+ ///
82+ /// **Note:** This is ignored when using [`AppHandle#method.restart`].
7883 pub fn prevent_exit ( & self ) {
7984 self . 0 . send ( ExitRequestedEventAction :: Prevent ) . unwrap ( ) ;
8085 }
@@ -171,6 +176,10 @@ pub enum RunEvent {
171176 /// The app is about to exit
172177 #[ non_exhaustive]
173178 ExitRequested {
179+ /// Exit code.
180+ /// [`Option::None`] when the exit is requested by user interaction,
181+ /// [`Option::Some`] when requested programatically via [`AppHandle#method.exit`] and [`AppHandle#method.restart`].
182+ code : Option < i32 > ,
174183 /// Event API
175184 api : ExitRequestApi ,
176185 } ,
@@ -365,15 +374,20 @@ impl<R: Runtime> AppHandle<R> {
365374 self . manager ( ) . plugins . lock ( ) . unwrap ( ) . unregister ( plugin)
366375 }
367376
368- /// Exits the app. This is the same as [`std::process::exit`], but it performs cleanup on this application .
377+ /// Exits the app by triggering [`RunEvent::ExitRequested`] and [`RunEvent::Exit`] .
369378 pub fn exit ( & self , exit_code : i32 ) {
370- self . cleanup_before_exit ( ) ;
371- std:: process:: exit ( exit_code) ;
379+ if let Err ( e) = self . runtime_handle . request_exit ( exit_code) {
380+ debug_eprintln ! ( "failed to exit: {}" , e) ;
381+ self . cleanup_before_exit ( ) ;
382+ std:: process:: exit ( exit_code) ;
383+ }
372384 }
373385
374- /// Restarts the app. This is the same as [`crate::process::restart`], but it performs cleanup on this application .
386+ /// Restarts the app by triggering [`RunEvent::ExitRequested`] with code [`RESTART_EXIT_CODE`] and [`RunEvent::Exit`]. .
375387 pub fn restart ( & self ) {
376- self . cleanup_before_exit ( ) ;
388+ if self . runtime_handle . request_exit ( RESTART_EXIT_CODE ) . is_err ( ) {
389+ self . cleanup_before_exit ( ) ;
390+ }
377391 crate :: process:: restart ( & self . env ( ) ) ;
378392 }
379393}
@@ -1718,7 +1732,8 @@ fn on_event_loop_event<R: Runtime>(
17181732
17191733 let event = match event {
17201734 RuntimeRunEvent :: Exit => RunEvent :: Exit ,
1721- RuntimeRunEvent :: ExitRequested { tx } => RunEvent :: ExitRequested {
1735+ RuntimeRunEvent :: ExitRequested { code, tx } => RunEvent :: ExitRequested {
1736+ code,
17221737 api : ExitRequestApi ( tx) ,
17231738 } ,
17241739 RuntimeRunEvent :: WindowEvent { label, event } => RunEvent :: WindowEvent {
0 commit comments