[feat] tray items with kill and launch game

todo: launch option selection + seperator
This commit is contained in:
Purp1e
2025-03-26 03:49:03 +08:00
parent ee03bf0160
commit 8f885a5412
2 changed files with 78 additions and 12 deletions

View File

@@ -1,16 +1,30 @@
use tauri::{
menu::{Menu, MenuItem},
tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent},
Manager, Runtime,
Emitter, Manager, Runtime,
};
pub fn create_tray<R: Runtime>(app: &tauri::AppHandle<R>) -> tauri::Result<()> {
// 托盘菜单项目
let launch_ww_i = MenuItem::with_id(app, "launch_ww", "启动国际服", true, None::<&str>)?;
let launch_pw_i = MenuItem::with_id(app, "launch_pw", "启动国服", true, None::<&str>)?;
let kill_game_i = MenuItem::with_id(app, "kill_game", "关闭CS2", true, None::<&str>)?;
let kill_steam_i = MenuItem::with_id(app, "kill_steam", "关闭Steam", true, None::<&str>)?;
let show_i = MenuItem::with_id(app, "show", "显示主界面", true, None::<&str>)?;
let quit_i = MenuItem::with_id(app, "quit", "退出", true, None::<&str>)?;
// 创建托盘菜单
let menu = Menu::with_items(app, &[&show_i, &quit_i])?;
let menu = Menu::with_items(
app,
&[
&launch_ww_i,
&launch_pw_i,
&kill_game_i,
&kill_steam_i,
&show_i,
&quit_i,
],
)?;
let _ = TrayIconBuilder::with_id("tray")
.icon(app.default_window_icon().unwrap().clone())
@@ -21,10 +35,22 @@ pub fn create_tray<R: Runtime>(app: &tauri::AppHandle<R>) -> tauri::Result<()> {
app.exit(0);
}
"show" => {
if let Some(window) = app.get_webview_window("main") {
let _ = window.show();
let _ = window.set_focus();
}
if let Some(window) = app.get_webview_window("main") {
let _ = window.show();
let _ = window.set_focus();
}
}
"launch_ww" => {
let _ = app.emit("tray://launch_game", "worldwide");
}
"launch_pw" => {
let _ = app.emit("tray://launch_game", "perfectworld");
}
"kill_game" => {
let _ = app.emit("tray://kill_game", None::<()>);
}
"kill_steam" => {
let _ = app.emit("tray://kill_steam", None::<()>);
}
// Add more events here
_ => {}