fix: replace update_tray_display with update_menu for consistency across tray updates

This commit is contained in:
Tunglies
2025-11-04 10:38:59 +08:00
parent 0a03867352
commit 0306f73841
6 changed files with 77 additions and 101 deletions

View File

@@ -65,7 +65,7 @@ impl Config {
d.enable_tun_mode = Some(false); d.enable_tun_mode = Some(false);
}); });
verge.apply(); verge.apply();
let _ = tray::Tray::global().update_tray_display().await; let _ = tray::Tray::global().update_menu().await;
// 分离数据获取和异步调用避免Send问题 // 分离数据获取和异步调用避免Send问题
let verge_data = Config::verge().await.latest_arc(); let verge_data = Config::verge().await.latest_arc();

View File

@@ -532,7 +532,7 @@ impl ServiceManager {
return Err(anyhow::anyhow!("服务不可用: {}", reason)); return Err(anyhow::anyhow!("服务不可用: {}", reason));
} }
} }
let _ = tray::Tray::global().update_tray_display().await; let _ = tray::Tray::global().update_menu().await;
Ok(()) Ok(())
} }
} }

View File

@@ -161,9 +161,7 @@ impl Timer {
.set_frequency_count_down_by_seconds(3, 3) .set_frequency_count_down_by_seconds(3, 3)
.spawn_async_routine(|| async move { .spawn_async_routine(|| async move {
logging!(debug, Type::Timer, "Updating tray menu"); logging!(debug, Type::Timer, "Updating tray menu");
crate::core::tray::Tray::global() crate::core::tray::Tray::global().update_menu().await
.update_tray_display()
.await
}) })
.context("failed to create update tray menu timer task")?; .context("failed to create update tray menu timer task")?;
delay_timer delay_timer

View File

@@ -1,5 +1,4 @@
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
use tauri::Emitter;
use tauri::tray::TrayIconBuilder; use tauri::tray::TrayIconBuilder;
use tauri_plugin_mihomo::models::Proxies; use tauri_plugin_mihomo::models::Proxies;
use tokio::fs; use tokio::fs;
@@ -431,24 +430,6 @@ impl Tray {
Ok(()) Ok(())
} }
/// 更新托盘显示状态的函数
pub async fn update_tray_display(&self) -> Result<()> {
if handle::Handle::global().is_exiting() {
logging!(debug, Type::Tray, "应用正在退出,跳过托盘显示状态更新");
return Ok(());
}
let app_handle = handle::Handle::app_handle();
let _tray = app_handle
.tray_by_id("main")
.ok_or_else(|| anyhow::anyhow!("Failed to get main tray"))?;
// 更新菜单
self.update_menu().await?;
Ok(())
}
/// 更新托盘提示 /// 更新托盘提示
pub async fn update_tooltip(&self) -> Result<()> { pub async fn update_tooltip(&self) -> Result<()> {
if handle::Handle::global().is_exiting() { if handle::Handle::global().is_exiting() {
@@ -523,9 +504,7 @@ impl Tray {
logging!(debug, Type::Tray, "应用正在退出,跳过托盘局部更新"); logging!(debug, Type::Tray, "应用正在退出,跳过托盘局部更新");
return Ok(()); return Ok(());
} }
// self.update_menu().await?; self.update_menu().await?;
// 更新轻量模式显示状态
self.update_tray_display().await?;
self.update_icon().await?; self.update_icon().await?;
self.update_tooltip().await?; self.update_tooltip().await?;
Ok(()) Ok(())
@@ -620,22 +599,6 @@ impl Tray {
tray.on_menu_event(on_menu_event); tray.on_menu_event(on_menu_event);
Ok(()) Ok(())
} }
// 托盘统一的状态更新函数
pub async fn update_all_states(&self) -> Result<()> {
if handle::Handle::global().is_exiting() {
logging!(debug, Type::Tray, "应用正在退出,跳过托盘状态更新");
return Ok(());
}
// 确保所有状态更新完成
self.update_tray_display().await?;
// self.update_menu().await?;
self.update_icon().await?;
self.update_tooltip().await?;
Ok(())
}
} }
fn create_hotkeys(hotkeys: &Option<Vec<String>>) -> HashMap<String, String> { fn create_hotkeys(hotkeys: &Option<Vec<String>>) -> HashMap<String, String> {
@@ -1253,66 +1216,20 @@ fn on_menu_event(_: &AppHandle, event: MenuEvent) {
} }
id if id.starts_with("proxy_") => { id if id.starts_with("proxy_") => {
// proxy_{group_name}_{proxy_name} // proxy_{group_name}_{proxy_name}
let parts: Vec<&str> = id.splitn(3, '_').collect(); let rest = match id.strip_prefix("proxy_") {
Some(r) => r,
if parts.len() == 3 && parts[0] == "proxy" { None => return,
let group_name = parts[1]; };
let proxy_name = parts[2]; let (group_name, proxy_name) = match rest.split_once('_') {
Some((g, p)) => (g, p),
match handle::Handle::mihomo() None => return,
.await };
.select_node_for_group(group_name, proxy_name) feat::switch_proxy_node(group_name, proxy_name).await;
.await
{
Ok(_) => {
logging!(
info,
Type::Tray,
"切换代理成功: {} -> {}",
group_name,
proxy_name
);
let _ = handle::Handle::app_handle()
.emit("verge://refresh-proxy-config", ());
}
Err(e) => {
logging!(
error,
Type::Tray,
"切换代理失败: {} -> {}, 错误: {:?}",
group_name,
proxy_name,
e
);
// Fallback to IPC update
if (handle::Handle::mihomo()
.await
.select_node_for_group(group_name, proxy_name)
.await)
.is_ok()
{
logging!(
info,
Type::Tray,
"代理切换回退成功: {} -> {}",
group_name,
proxy_name
);
let app_handle = handle::Handle::app_handle();
let _ = app_handle.emit("verge://force-refresh-proxies", ());
}
}
}
}
} }
_ => {} _ => {}
} }
// Ensure tray state update is awaited and properly handled // We dont expected to refresh tray state here
if let Err(e) = Tray::global().update_all_states().await { // as the inner handle function (SHOULD) already takes care of it
logging!(warn, Type::Tray, "Failed to update tray state: {e}");
}
}); });
} }

View File

@@ -7,6 +7,7 @@ use crate::{
}; };
use anyhow::{Result, bail}; use anyhow::{Result, bail};
use smartstring::alias::String; use smartstring::alias::String;
use tauri::Emitter;
/// Toggle proxy profile /// Toggle proxy profile
pub async fn toggle_proxy_profile(profile_index: String) { pub async fn toggle_proxy_profile(profile_index: String) {
@@ -23,6 +24,66 @@ pub async fn toggle_proxy_profile(profile_index: String) {
} }
} }
pub async fn switch_proxy_node(group_name: &str, proxy_name: &str) {
match handle::Handle::mihomo()
.await
.select_node_for_group(group_name, proxy_name)
.await
{
Ok(_) => {
logging!(
info,
Type::Tray,
"切换代理成功: {} -> {}",
group_name,
proxy_name
);
let _ = handle::Handle::app_handle().emit("verge://refresh-proxy-config", ());
let _ = tray::Tray::global().update_menu().await;
return;
}
Err(err) => {
logging!(
error,
Type::Tray,
"切换代理失败: {} -> {}, 错误: {:?}",
group_name,
proxy_name,
err
);
}
}
match handle::Handle::mihomo()
.await
.select_node_for_group(group_name, proxy_name)
.await
{
Ok(_) => {
logging!(
info,
Type::Tray,
"代理切换回退成功: {} -> {}",
group_name,
proxy_name
);
let _ = handle::Handle::app_handle().emit("verge://force-refresh-proxies", ());
let _ = tray::Tray::global().update_menu().await;
}
Err(err) => {
logging!(
error,
Type::Tray,
"代理切换最终失败: {} -> {}, 错误: {:?}",
group_name,
proxy_name,
err
);
let _ = handle::Handle::app_handle().emit("verge://force-refresh-proxies", ());
}
}
}
async fn should_update_profile( async fn should_update_profile(
uid: &String, uid: &String,
ignore_auto_update: bool, ignore_auto_update: bool,

View File

@@ -79,7 +79,7 @@ pub fn is_in_lightweight_mode() -> bool {
} }
async fn refresh_lightweight_tray_state() { async fn refresh_lightweight_tray_state() {
if let Err(err) = Tray::global().update_tray_display().await { if let Err(err) = Tray::global().update_menu().await {
logging!(warn, Type::Lightweight, "更新托盘轻量模式状态失败: {err}"); logging!(warn, Type::Lightweight, "更新托盘轻量模式状态失败: {err}");
} }
} }