From ff5a2c6ca4ce9b379b6b34e792622a3bc89c40c0 Mon Sep 17 00:00:00 2001 From: wonfen Date: Sun, 4 May 2025 13:28:08 +0800 Subject: [PATCH] feat: add backend config change listener to update frontend state --- UPDATELOG.md | 2 ++ src-tauri/src/cmd/profile.rs | 11 +++++++++++ src/pages/profiles.tsx | 18 ++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/UPDATELOG.md b/UPDATELOG.md index 04ac0738..613690b0 100644 --- a/UPDATELOG.md +++ b/UPDATELOG.md @@ -17,6 +17,7 @@ - Vless URL 解码时网络类型错误 - 切换自定义代理地址导致系统代理状态异常 - Macos TUN 默认无效网卡名称 + - 托盘更改订阅后 UI 不同步的问题 #### 新增了: - 允许代理主机地址设置为非 127.0.0.1 对 WSL 代理友好 @@ -43,6 +44,7 @@ - 改进核心功能防止主进程阻塞、改进MihomoManager实现,以及优化窗口创建流程。减少应用程序可能出现的主进程卡死情况 - 优化系统代理设置更新逻辑 - 重构前端通知系统分离通知线程防止前端卡死 + - 优化网络请求和错误处理 ## v2.2.3 diff --git a/src-tauri/src/cmd/profile.rs b/src-tauri/src/cmd/profile.rs index e6f25910..ec1197f3 100644 --- a/src-tauri/src/cmd/profile.rs +++ b/src-tauri/src/cmd/profile.rs @@ -146,6 +146,9 @@ pub async fn patch_profiles_config(profiles: IProfiles) -> CmdResult { // 更新profiles配置 logging!(info, Type::Cmd, true, "正在更新配置草稿"); + + let current_value = profiles.current.clone(); + let _ = Config::profiles().draft().patch_config(profiles); // 更新配置并进行验证 @@ -156,6 +159,14 @@ pub async fn patch_profiles_config(profiles: IProfiles) -> CmdResult { let _ = Tray::global().update_tooltip(); Config::profiles().apply(); wrap_err!(Config::profiles().data().save_file())?; + + if let Some(window) = handle::Handle::global().get_window() { + if let Some(current) = ¤t_value { + logging!(info, Type::Cmd, true, "向前端发送配置变更事件: {}", current); + let _ = window.emit("profile-changed", current.clone()); + } + } + Ok(true) } Ok((false, error_msg)) => { diff --git a/src/pages/profiles.tsx b/src/pages/profiles.tsx index bd1c9cd9..d0ede361 100644 --- a/src/pages/profiles.tsx +++ b/src/pages/profiles.tsx @@ -295,6 +295,24 @@ const ProfilePage = () => { ? "rgba(0, 0, 0, 0.06)" : "rgba(255, 255, 255, 0.06)"; + // 监听后端配置变更 + useEffect(() => { + let unlistenPromise: Promise<() => void> | undefined; + + const setupListener = async () => { + unlistenPromise = listen('profile-changed', (event) => { + console.log('Profile changed event received:', event.payload); + mutateProfiles(); + }); + }; + + setupListener(); + + return () => { + unlistenPromise?.then(unlisten => unlisten()); + }; + }, [mutateProfiles, t]); + return (