@@ -15,8 +15,8 @@ use std::path::PathBuf;
15
15
16
16
use serde:: Serialize ;
17
17
use tauri:: {
18
- api:: dialog:: ask, CustomMenuItem , Event , Manager , SystemTray , SystemTrayEvent , SystemTrayMenu ,
19
- WindowBuilder , WindowUrl ,
18
+ api:: dialog:: ask, async_runtime , CustomMenuItem , Event , GlobalShortcutManager , Manager ,
19
+ SystemTray , SystemTrayEvent , SystemTrayMenu , WindowBuilder , WindowUrl ,
20
20
} ;
21
21
22
22
#[ derive( Serialize ) ]
@@ -55,7 +55,8 @@ fn main() {
55
55
. add_item ( CustomMenuItem :: new ( "toggle" , "Toggle" ) )
56
56
. add_item ( CustomMenuItem :: new ( "new" , "New window" ) )
57
57
. add_item ( CustomMenuItem :: new ( "icon_1" , "Tray Icon 1" ) )
58
- . add_item ( CustomMenuItem :: new ( "icon_2" , "Tray Icon 2" ) ) ,
58
+ . add_item ( CustomMenuItem :: new ( "icon_2" , "Tray Icon 2" ) )
59
+ . add_item ( CustomMenuItem :: new ( "exit_app" , "Quit" ) ) ,
59
60
) ,
60
61
)
61
62
. on_system_tray_event ( |app, event| match event {
@@ -71,6 +72,10 @@ fn main() {
71
72
SystemTrayEvent :: MenuItemClick { id, .. } => {
72
73
let item_handle = app. tray_handle ( ) . get_item ( & id) ;
73
74
match id. as_str ( ) {
75
+ "exit_app" => {
76
+ // exit the app
77
+ app. exit ( 0 ) ;
78
+ }
74
79
"toggle" => {
75
80
let window = app. get_window ( "main" ) . unwrap ( ) ;
76
81
let new_title = if window. is_visible ( ) . unwrap ( ) {
@@ -157,15 +162,42 @@ fn main() {
157
162
#[ cfg( target_os = "macos" ) ]
158
163
app. set_activation_policy ( tauri:: ActivationPolicy :: Regular ) ;
159
164
160
- app. run ( |app_handle, e| {
161
- if let Event :: CloseRequested { label , api , .. } = e {
162
- api . prevent_close ( ) ;
165
+ app. run ( |app_handle, e| match e {
166
+ // Application is ready (triggered only once)
167
+ Event :: Ready => {
163
168
let app_handle = app_handle. clone ( ) ;
169
+ // launch a new thread so it doesnt block any channel
170
+ async_runtime:: spawn ( async move {
171
+ let app_handle = app_handle. clone ( ) ;
172
+ app_handle
173
+ . global_shortcut_manager ( )
174
+ . register ( "CmdOrCtrl+1" , move || {
175
+ let app_handle = app_handle. clone ( ) ;
176
+ let window = app_handle. get_window ( "main" ) . unwrap ( ) ;
177
+ window. set_title ( "New title!" ) . unwrap ( ) ;
178
+ } )
179
+ . unwrap ( ) ;
180
+ } ) ;
181
+ }
182
+
183
+ // Triggered when a window is trying to close
184
+ Event :: CloseRequested { label, api, .. } => {
185
+ let app_handle = app_handle. clone ( ) ;
186
+ // use the exposed close api, and prevent the event loop to close
187
+ api. prevent_close ( ) ;
188
+ // ask the user if he wants to quit
164
189
ask ( "Tauri API" , "Are you sure?" , move |answer| {
165
190
if answer {
166
191
app_handle. get_window ( & label) . unwrap ( ) . close ( ) . unwrap ( ) ;
167
192
}
168
193
} ) ;
169
194
}
195
+
196
+ // Keep the event loop running even if all windows are closed
197
+ // This allow us to catch system tray events when there is no window
198
+ Event :: ExitRequested { api, .. } => {
199
+ api. prevent_exit ( ) ;
200
+ }
201
+ _ => { }
170
202
} )
171
203
}
0 commit comments