@@ -15,8 +15,8 @@ use std::path::PathBuf;
1515
1616use serde:: Serialize ;
1717use 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 ,
2020} ;
2121
2222#[ derive( Serialize ) ]
@@ -55,7 +55,8 @@ fn main() {
5555 . add_item ( CustomMenuItem :: new ( "toggle" , "Toggle" ) )
5656 . add_item ( CustomMenuItem :: new ( "new" , "New window" ) )
5757 . 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" ) ) ,
5960 ) ,
6061 )
6162 . on_system_tray_event ( |app, event| match event {
@@ -71,6 +72,10 @@ fn main() {
7172 SystemTrayEvent :: MenuItemClick { id, .. } => {
7273 let item_handle = app. tray_handle ( ) . get_item ( & id) ;
7374 match id. as_str ( ) {
75+ "exit_app" => {
76+ // exit the app
77+ app. exit ( 0 ) ;
78+ }
7479 "toggle" => {
7580 let window = app. get_window ( "main" ) . unwrap ( ) ;
7681 let new_title = if window. is_visible ( ) . unwrap ( ) {
@@ -157,15 +162,42 @@ fn main() {
157162 #[ cfg( target_os = "macos" ) ]
158163 app. set_activation_policy ( tauri:: ActivationPolicy :: Regular ) ;
159164
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 => {
163168 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
164189 ask ( "Tauri API" , "Are you sure?" , move |answer| {
165190 if answer {
166191 app_handle. get_window ( & label) . unwrap ( ) . close ( ) . unwrap ( ) ;
167192 }
168193 } ) ;
169194 }
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+ _ => { }
170202 } )
171203}
0 commit comments