feat(tray): Added tray icon left-click event handling

- Implemented the function of displaying and focusing the main window when the tray icon is left-clicked
- Added handling logic for tray icon events
- Added Run Dev Server runtime configuration file
- Added Run spary Cargo command runtime configuration file
This commit is contained in:
2025-11-11 15:12:32 +08:00
parent 18ca4fd675
commit 8eaac4df96
3 changed files with 52 additions and 3 deletions

View File

@@ -1,8 +1,7 @@
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
use tauri::Manager;
use crate::spary::spary_switch;
use tauri::menu::{Menu, MenuItem};
use tauri::tray::TrayIconBuilder;
use tauri::tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent};
use tauri_plugin_sql::{Migration, MigrationKind};
mod spary;
@@ -52,6 +51,24 @@ pub fn run() {
app.exit(0);
}
})
//Linux: Unsupported. The event is not emitted even though the icon is shown and will still show a context menu on right click.
.on_tray_icon_event(|tray, event| match event {
TrayIconEvent::Click {
button: MouseButton::Left,
button_state: MouseButtonState::Up,
..
} => {
let app = tray.app_handle();
if let Some(window) = app.get_webview_window("main") {
let _ = window.unminimize();
let _ = window.show();
let _ = window.set_focus();
}
}
_ => {
println!("unhandled event {event:?}");
}
})
.build(app)?;
tray.set_menu(Some(menu))?;
Ok(())