From 100884781f8961ad039bbece7e1590cfc4193535 Mon Sep 17 00:00:00 2001 From: coso Date: Tue, 23 Dec 2025 02:29:49 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20Windows=20=E6=9C=80=E5=B0=8F=E5=8C=96?= =?UTF-8?q?=E5=88=B0=E6=89=98=E7=9B=98=E6=97=A0=E6=95=88=20(#30)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加 minimize_to_tray 配置字段到 Config 结构体 - 添加 on_window_event 处理器拦截窗口关闭事件 - 前端设置页面支持保存最小化到托盘选项 - 默认启用最小化到托盘功能 Closes #30 --- package.json | 2 +- src-tauri/Cargo.lock | 2 +- src-tauri/Cargo.toml | 2 +- src-tauri/src/config/types.rs | 8 +++++++ src-tauri/src/lib.rs | 25 +++++++++++++++++++++ src-tauri/tauri.conf.json | 2 +- src/components/settings/GeneralSettings.tsx | 17 +++++++++++++- src/hooks/useTauri.ts | 2 ++ 8 files changed, 55 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index e023effeb..93df14877 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "proxycast", "private": true, - "version": "0.17.2", + "version": "0.17.3", "type": "module", "repository": { "type": "git", diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 2999bad60..bbfa19f1b 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -3377,7 +3377,7 @@ dependencies = [ [[package]] name = "proxycast" -version = "0.17.2" +version = "0.17.3" dependencies = [ "anyhow", "async-stream", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index ade297e3d..0daa46c0d 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "proxycast" -version = "0.17.2" +version = "0.17.3" description = "AI API Proxy Desktop App" authors = ["you"] edition = "2021" diff --git a/src-tauri/src/config/types.rs b/src-tauri/src/config/types.rs index 92c8a1a4c..3b79c9942 100644 --- a/src-tauri/src/config/types.rs +++ b/src-tauri/src/config/types.rs @@ -308,6 +308,13 @@ pub struct Config { /// 允许为不同的客户端端点(CC/Codex)配置不同的 Provider #[serde(default)] pub endpoint_providers: EndpointProvidersConfig, + /// 关闭时最小化到托盘(而不是退出应用) + #[serde(default = "default_minimize_to_tray")] + pub minimize_to_tray: bool, +} + +fn default_minimize_to_tray() -> bool { + true } /// 服务器配置 @@ -771,6 +778,7 @@ impl Default for Config { proxy_url: None, ampcode: AmpConfig::default(), endpoint_providers: EndpointProvidersConfig::default(), + minimize_to_tray: default_minimize_to_tray(), } } } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 769bdbfa2..d0254a3bc 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1687,6 +1687,31 @@ pub fn run() { .manage(bookmark_manager_state) .manage(enhanced_stats_service_state) .manage(batch_operations_state) + .on_window_event(move |window, event| { + // 处理窗口关闭事件 + if let tauri::WindowEvent::CloseRequested { api, .. } = event { + // 获取配置,检查是否启用最小化到托盘 + let app_handle = window.app_handle(); + if let Some(app_state) = app_handle.try_state::() { + // 使用 block_on 同步获取配置 + let minimize_to_tray = tauri::async_runtime::block_on(async { + let state = app_state.read().await; + state.config.minimize_to_tray + }); + + if minimize_to_tray { + // 阻止默认关闭行为 + api.prevent_close(); + // 隐藏窗口而不是关闭 + if let Err(e) = window.hide() { + tracing::error!("[窗口] 隐藏窗口失败: {}", e); + } else { + tracing::info!("[窗口] 窗口已最小化到托盘"); + } + } + } + } + }) .setup(move |app| { // 初始化托盘管理器 // Requirements 1.4: 应用启动时显示停止状态图标 diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index d1c472304..932fe3163 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -1,7 +1,7 @@ { "$schema": "https://schema.tauri.app/config/2", "productName": "ProxyCast", - "version": "0.17.2", + "version": "0.17.3", "identifier": "com.proxycast.app", "build": { "beforeDevCommand": "npm run dev", diff --git a/src/components/settings/GeneralSettings.tsx b/src/components/settings/GeneralSettings.tsx index 77484df6f..d6cf8edb2 100644 --- a/src/components/settings/GeneralSettings.tsx +++ b/src/components/settings/GeneralSettings.tsx @@ -38,6 +38,8 @@ export function GeneralSettings() { const c = await getConfig(); setConfig(c); setProxyUrl(c.proxy_url || ""); + // 加载最小化到托盘设置 + setMinimizeToTray(c.minimize_to_tray ?? true); } catch (e) { console.error("加载配置失败:", e); } finally { @@ -248,7 +250,20 @@ export function GeneralSettings() { setMinimizeToTray(e.target.checked)} + onChange={async (e) => { + const newValue = e.target.checked; + setMinimizeToTray(newValue); + if (config) { + try { + await saveConfig({ ...config, minimize_to_tray: newValue }); + setConfig({ ...config, minimize_to_tray: newValue }); + } catch (err) { + console.error("保存最小化到托盘设置失败:", err); + // 恢复原值 + setMinimizeToTray(!newValue); + } + } + }} className="w-4 h-4 rounded border-gray-300" /> diff --git a/src/hooks/useTauri.ts b/src/hooks/useTauri.ts index e205fece5..478d0ce94 100644 --- a/src/hooks/useTauri.ts +++ b/src/hooks/useTauri.ts @@ -146,6 +146,8 @@ export interface Config { ampcode: AmpConfig; credential_pool: CredentialPoolConfig; proxy_url: string | null; + /** 关闭时最小化到托盘(而不是退出应用) */ + minimize_to_tray: boolean; } export interface LogEntry {