[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
_ => {}

View File

@@ -1,21 +1,61 @@
"use client"
import { init } from "@/store"
import { useSteamStore } from "@/store/steam"
import { useToolStore } from "@/store/tool"
import { addToast } from "@heroui/react"
import { invoke } from "@tauri-apps/api/core"
import { listen } from "@tauri-apps/api/event"
import { useDebounce } from "ahooks"
import { useEffect } from "react"
import "./globals.css"
import Providers from "./providers"
import { init } from "@/store"
import { useDebounce } from "ahooks"
export default function RootLayout({ children }: { children: React.ReactNode }) {
const steam = useSteamStore()
const tool = useToolStore()
useEffect(() => {
void init()
void listen<string>("tray://launch_game", async (event) => {
await invoke("launch_game", {
steamPath: `${steam.state.steamDir}/steam.exe`,
launchOption: tool.state.launchOptions[tool.state.launchIndex].option || "",
server: event.payload || "worldwide",
})
addToast({ title: "启动国服成功" })
})
void listen<string>("tray://kill_steam", async () => {
await invoke("kill_steam")
addToast({ title: "已关闭Steam" })
})
void listen<string>("tray://kill_game", async () => {
await invoke("kill_game")
addToast({ title: "已关闭CS2" })
})
})
// 检测steam路径和游戏路径是否有效
const steam = useSteamStore()
const debounceSteamDir = useDebounce(steam.state.steamDir, {wait: 500, leading: true, trailing: true, maxWait: 2500})
const debounceCs2Dir = useDebounce(steam.state.cs2Dir, {wait: 500, leading: true, trailing: true, maxWait: 2500})
const debounceSteamDirValid = useDebounce(steam.state.steamDirValid, {wait: 500, leading: true, trailing: true, maxWait: 2500})
const debounceSteamDir = useDebounce(steam.state.steamDir, {
wait: 500,
leading: true,
trailing: true,
maxWait: 2500,
})
const debounceCs2Dir = useDebounce(steam.state.cs2Dir, {
wait: 500,
leading: true,
trailing: true,
maxWait: 2500,
})
const debounceSteamDirValid = useDebounce(steam.state.steamDirValid, {
wait: 500,
leading: true,
trailing: true,
maxWait: 2500,
})
useEffect(() => {
void steam.checkSteamDirValid()
}, [debounceSteamDir])