@@ -15,7 +15,7 @@ use crate::{
15
15
runtime:: {
16
16
webview:: { CustomProtocol , WebviewAttributes , WindowBuilder } ,
17
17
window:: { PendingWindow , WindowEvent } ,
18
- Dispatch , RunEvent , Runtime ,
18
+ Dispatch , ExitRequestedEventAction , RunEvent , Runtime ,
19
19
} ,
20
20
sealed:: { ManagerBase , RuntimeOrDispatch } ,
21
21
Context , Invoke , InvokeError , Manager , StateManager , Window ,
@@ -47,7 +47,19 @@ pub(crate) type GlobalWindowEventListener<R> = Box<dyn Fn(GlobalWindowEvent<R>)
47
47
#[ cfg( feature = "system-tray" ) ]
48
48
type SystemTrayEventListener < R > = Box < dyn Fn ( & AppHandle < R > , tray:: SystemTrayEvent ) + Send + Sync > ;
49
49
50
+ /// Api exposed on the `ExitRequested` event.
51
+ #[ derive( Debug ) ]
52
+ pub struct ExitRequestApi ( Sender < ExitRequestedEventAction > ) ;
53
+
54
+ impl ExitRequestApi {
55
+ /// Prevents the app from exiting
56
+ pub fn prevent_exit ( & self ) {
57
+ self . 0 . send ( ExitRequestedEventAction :: Prevent ) . unwrap ( ) ;
58
+ }
59
+ }
60
+
50
61
/// Api exposed on the `CloseRequested` event.
62
+ #[ derive( Debug ) ]
51
63
pub struct CloseRequestApi ( Sender < bool > ) ;
52
64
53
65
impl CloseRequestApi {
@@ -58,10 +70,17 @@ impl CloseRequestApi {
58
70
}
59
71
60
72
/// An application event, triggered from the event loop.
73
+ #[ derive( Debug ) ]
61
74
#[ non_exhaustive]
62
75
pub enum Event {
63
76
/// Event loop is exiting.
64
77
Exit ,
78
+ /// The app is about to exit
79
+ #[ non_exhaustive]
80
+ ExitRequested {
81
+ /// Event API
82
+ api : ExitRequestApi ,
83
+ } ,
65
84
/// Window close was requested by the user.
66
85
#[ non_exhaustive]
67
86
CloseRequested {
@@ -223,6 +242,23 @@ impl<R: Runtime> AppHandle<R> {
223
242
. register ( plugin) ;
224
243
Ok ( ( ) )
225
244
}
245
+
246
+ /// Exits the app
247
+ pub fn exit ( & self , exit_code : i32 ) {
248
+ std:: process:: exit ( exit_code) ;
249
+ }
250
+
251
+ /// Runs necessary cleanup tasks before exiting the process
252
+ fn cleanup_before_exit ( & self ) {
253
+ #[ cfg( shell_execute) ]
254
+ {
255
+ crate :: api:: process:: kill_children ( ) ;
256
+ }
257
+ #[ cfg( all( windows, feature = "system-tray" ) ) ]
258
+ {
259
+ let _ = self . remove_system_tray ( ) ;
260
+ }
261
+ }
226
262
}
227
263
228
264
impl < R : Runtime > Manager < R > for AppHandle < R > { }
@@ -356,14 +392,7 @@ impl<R: Runtime> App<R> {
356
392
let manager = self . manager . clone ( ) ;
357
393
self . runtime . take ( ) . unwrap ( ) . run ( move |event| match event {
358
394
RunEvent :: Exit => {
359
- #[ cfg( shell_execute) ]
360
- {
361
- crate :: api:: process:: kill_children ( ) ;
362
- }
363
- #[ cfg( all( windows, feature = "system-tray" ) ) ]
364
- {
365
- let _ = app_handle. remove_system_tray ( ) ;
366
- }
395
+ app_handle. cleanup_before_exit ( ) ;
367
396
callback ( & app_handle, Event :: Exit ) ;
368
397
}
369
398
_ => {
@@ -372,6 +401,9 @@ impl<R: Runtime> App<R> {
372
401
& app_handle,
373
402
match event {
374
403
RunEvent :: Exit => Event :: Exit ,
404
+ RunEvent :: ExitRequested { tx } => Event :: ExitRequested {
405
+ api : ExitRequestApi ( tx) ,
406
+ } ,
375
407
RunEvent :: CloseRequested { label, signal_tx } => Event :: CloseRequested {
376
408
label : label. parse ( ) . unwrap_or_else ( |_| unreachable ! ( ) ) ,
377
409
api : CloseRequestApi ( signal_tx) ,
0 commit comments