[feat] tray items with kill and launch game
todo: launch option selection + seperator
This commit is contained in:
@@ -1,16 +1,30 @@
|
|||||||
use tauri::{
|
use tauri::{
|
||||||
menu::{Menu, MenuItem},
|
menu::{Menu, MenuItem},
|
||||||
tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent},
|
tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent},
|
||||||
Manager, Runtime,
|
Emitter, Manager, Runtime,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn create_tray<R: Runtime>(app: &tauri::AppHandle<R>) -> tauri::Result<()> {
|
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 show_i = MenuItem::with_id(app, "show", "显示主界面", true, None::<&str>)?;
|
||||||
let quit_i = MenuItem::with_id(app, "quit", "退出", 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")
|
let _ = TrayIconBuilder::with_id("tray")
|
||||||
.icon(app.default_window_icon().unwrap().clone())
|
.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);
|
app.exit(0);
|
||||||
}
|
}
|
||||||
"show" => {
|
"show" => {
|
||||||
if let Some(window) = app.get_webview_window("main") {
|
if let Some(window) = app.get_webview_window("main") {
|
||||||
let _ = window.show();
|
let _ = window.show();
|
||||||
let _ = window.set_focus();
|
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
|
// Add more events here
|
||||||
_ => {}
|
_ => {}
|
||||||
|
|||||||
@@ -1,21 +1,61 @@
|
|||||||
"use client"
|
"use client"
|
||||||
|
import { init } from "@/store"
|
||||||
import { useSteamStore } from "@/store/steam"
|
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 { useEffect } from "react"
|
||||||
import "./globals.css"
|
import "./globals.css"
|
||||||
import Providers from "./providers"
|
import Providers from "./providers"
|
||||||
import { init } from "@/store"
|
|
||||||
import { useDebounce } from "ahooks"
|
|
||||||
|
|
||||||
export default function RootLayout({ children }: { children: React.ReactNode }) {
|
export default function RootLayout({ children }: { children: React.ReactNode }) {
|
||||||
|
const steam = useSteamStore()
|
||||||
|
const tool = useToolStore()
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
void init()
|
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路径和游戏路径是否有效
|
// 检测steam路径和游戏路径是否有效
|
||||||
const steam = useSteamStore()
|
const debounceSteamDir = useDebounce(steam.state.steamDir, {
|
||||||
const debounceSteamDir = useDebounce(steam.state.steamDir, {wait: 500, leading: true, trailing: true, maxWait: 2500})
|
wait: 500,
|
||||||
const debounceCs2Dir = useDebounce(steam.state.cs2Dir, {wait: 500, leading: true, trailing: true, maxWait: 2500})
|
leading: true,
|
||||||
const debounceSteamDirValid = useDebounce(steam.state.steamDirValid, {wait: 500, leading: true, trailing: true, maxWait: 2500})
|
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(() => {
|
useEffect(() => {
|
||||||
void steam.checkSteamDirValid()
|
void steam.checkSteamDirValid()
|
||||||
}, [debounceSteamDir])
|
}, [debounceSteamDir])
|
||||||
|
|||||||
Reference in New Issue
Block a user