fix store dir + feat launch option switch in tray
This commit is contained in:
@@ -1,11 +1,18 @@
|
||||
use tauri::{
|
||||
menu::{CheckMenuItem, Menu, MenuItem, PredefinedMenuItem},
|
||||
menu::{CheckMenuItem, Menu, MenuItem, PredefinedMenuItem, Submenu},
|
||||
tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent},
|
||||
Emitter, Listener, Manager, Runtime,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::tool::powerplan::PowerPlanMode;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
struct LaunchOption {
|
||||
option: String,
|
||||
name: String,
|
||||
}
|
||||
|
||||
pub fn create_tray<R: Runtime>(app: &tauri::AppHandle<R>) -> tauri::Result<()> {
|
||||
// 托盘菜单项目
|
||||
let separator = &PredefinedMenuItem::separator(app).unwrap();
|
||||
@@ -47,13 +54,8 @@ pub fn create_tray<R: Runtime>(app: &tauri::AppHandle<R>) -> tauri::Result<()> {
|
||||
)?;
|
||||
|
||||
|
||||
let current_launch_option = MenuItem::with_id(
|
||||
app,
|
||||
"current_launch_option",
|
||||
"启动项档位",
|
||||
true,
|
||||
None::<&str>,
|
||||
)?;
|
||||
// 创建启动项子菜单(初始为空,后续会动态更新)
|
||||
let launch_option_submenu = Submenu::with_id(app, "launch_option_submenu", "启动项: 游戏", true)?;
|
||||
|
||||
// 创建托盘菜单
|
||||
let menu = Menu::with_items(
|
||||
@@ -64,7 +66,7 @@ pub fn create_tray<R: Runtime>(app: &tauri::AppHandle<R>) -> tauri::Result<()> {
|
||||
&power_plan_balanced,
|
||||
&power_plan_powersave,
|
||||
separator,
|
||||
¤t_launch_option,
|
||||
&launch_option_submenu,
|
||||
launch_ww_i,
|
||||
launch_pw_i,
|
||||
separator,
|
||||
@@ -114,10 +116,58 @@ pub fn create_tray<R: Runtime>(app: &tauri::AppHandle<R>) -> tauri::Result<()> {
|
||||
}
|
||||
});
|
||||
|
||||
let _ = app.listen("tray://get_current_launch_option", move |event| {
|
||||
let payload = event.payload();
|
||||
if payload != "" {
|
||||
let _ = current_launch_option.set_text("启动项档位 ".to_string() + payload);
|
||||
// 监听启动项列表更新事件
|
||||
let launch_option_submenu_clone = launch_option_submenu.clone();
|
||||
let _ = app.listen("tray://update_launch_options", move |event| {
|
||||
if let Ok(data) = serde_json::from_str::<serde_json::Value>(event.payload()) {
|
||||
if let (Some(options), Some(current_index)) = (
|
||||
data.get("options").and_then(|v| v.as_array()),
|
||||
data.get("currentIndex").and_then(|v| v.as_u64()),
|
||||
) {
|
||||
let current_index = current_index as usize;
|
||||
// 获取当前启动项名称
|
||||
let current_name = options
|
||||
.get(current_index)
|
||||
.and_then(|opt| opt.get("name"))
|
||||
.and_then(|n| n.as_str())
|
||||
.map(|s| s.to_string())
|
||||
.unwrap_or_else(|| format!("{}", current_index + 1));
|
||||
|
||||
// 更新子菜单标题
|
||||
let _ = launch_option_submenu_clone.set_text(format!("启动项: {}", current_name));
|
||||
|
||||
// 清空现有子菜单项 - 先收集所有项目,然后移除
|
||||
if let Ok(items) = launch_option_submenu_clone.items() {
|
||||
let items_to_remove: Vec<_> = items.iter().collect();
|
||||
for item in items_to_remove {
|
||||
let _ = launch_option_submenu_clone.remove(item);
|
||||
}
|
||||
}
|
||||
|
||||
// 创建新的子菜单项
|
||||
for (index, option) in options.iter().enumerate() {
|
||||
if let Some(name) = option.get("name").and_then(|n| n.as_str()) {
|
||||
let display_name = if name.is_empty() {
|
||||
format!("{}", index + 1)
|
||||
} else {
|
||||
name.to_string()
|
||||
};
|
||||
|
||||
let item_id = format!("launch_option_{}", index);
|
||||
let app_handle = launch_option_submenu_clone.app_handle();
|
||||
if let Ok(item) = CheckMenuItem::with_id(
|
||||
app_handle,
|
||||
&item_id,
|
||||
&display_name,
|
||||
true,
|
||||
index == current_index,
|
||||
None::<&str>,
|
||||
) {
|
||||
let _ = launch_option_submenu_clone.append(&item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -166,6 +216,12 @@ pub fn create_tray<R: Runtime>(app: &tauri::AppHandle<R>) -> tauri::Result<()> {
|
||||
let _ = app.emit("tray://set_powerplan", PowerPlanMode::PowerSaving as i32);
|
||||
// let _ = power_plan_powersave.set_checked(true);
|
||||
}
|
||||
id if id.starts_with("launch_option_") => {
|
||||
// 提取索引
|
||||
if let Ok(index) = id.replace("launch_option_", "").parse::<usize>() {
|
||||
let _ = app.emit("tray://set_launch_index", index);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
})
|
||||
.on_tray_icon_event(|tray, event| {
|
||||
|
||||
Reference in New Issue
Block a user