DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.
fix(tray): Linux 托盘改用 tray-icon 0.25 KSNI 后端,单击可唤起主窗
hairyf344bf42163be
diff --git a/docs/AGENTS.desktop.md b/docs/AGENTS.desktop.mdindex 6380a3ef..9e3c71b5 100644--- a/docs/AGENTS.desktop.md+++ b/docs/AGENTS.desktop.md@@ -351,6 +351,8 @@ export function FooComponent(props: FooProps) {- macOS 麦克风/摄像头权限(issue #214):内嵌 WKWebView 里的 `getUserMedia` 要同时满足两处,缺任何一处都拿不到流——`src-tauri/Info.plist` 的 `NSMicrophoneUsageDescription` / `NSCameraUsageDescription`(**缺用途说明时 TCC 直接终止进程**,连授权框都不弹,日志里只有 `attempted to access privacy-sensitive data without a usage description`)与 `src-tauri/Entitlements.plist` 的 `com.apple.security.device.audio-input` / `com.apple.security.device.camera`(`bundle.macOS.hardenedRuntime` 默认 true,Hardened Runtime 会把资源访问全部关上,没有对应 entitlement 即使有用途说明也无效)。两处都在 `bundle.macOS` 里显式声明(`infoPlist` / `entitlements`),**相对路径按 bundle 时的 CWD 解析**——`crates/tauri-cli/src/bundle.rs` 会先 `set_current_dir(dirs.tauri)`,所以写 `Info.plist` 而不是 `src-tauri/Info.plist`;CLI 另外还会自动合并 `tauri.conf.json` 同目录下的 `Info.plist`。WKWebView 的 `requestMediaCapturePermissionForOrigin` 不用自己接:wry 0.55.1 的 `WryWebViewUIDelegate` 无条件回 `WKPermissionDecision::Grant`(`src/wkwebview/class/wry_web_view_ui_delegate.rs`)。回归测试见 `src-tauri/src/desktop/builder.rs` 的 `macos_bundle_tests`(macOS 上额外用 `plutil -lint` 校验 plist 语法;`tauri dev` 不产 `.app`,走终端的 TCC 授权,不在覆盖范围内)。+- Linux 托盘点击(issue #386 / #438):`tauri::tray` 固定依赖的 tray-icon 0.24 在 Linux 上只有 libappindicator/GTK 后端,该后端**不上报任何 `TrayIconEvent`**(上游 tauri-apps/tray-icon#104),单击/双击都无法唤起主窗,只能走托盘菜单。上游 0.25.0 的 KSNI 后端把 SNI 的 `Activate` 映射成 `TrayIconEvent::Click { button: Left }`,但 tauri 2.x 的菜单类型来自 muda 0.19,而 tray-icon 0.25 换成了 muda 0.20(`TrayIconBuilder::menu` 的 `ContextMenu` 不是同一个 trait,编译期即不兼容),tauri 2.11.5 的 `tray-icon = "^0.24"` 既不能 `cargo update` 也不能 `[patch.crates-io]` 顶到 0.25,tauri 3 目前只有 alpha。因此 `src-tauri/Cargo.toml` 仅在 `cfg(target_os = "linux")` 直接依赖 `tray-icon = { version = "0.25", default-features = false, features = ["ksni"] }`(`ksni` 会启用 muda 的 `snapshot`,菜单经 D-Bus 导出;同时不再需要 GTK/libappindicator),Linux 托盘改由 `src-tauri/src/desktop/linux_tray.rs` 自建,`desktop::builder::tray` 在 Linux 分支只调它。要点:`TrayIcon` 必须 `Box::leak` 保活(析构即注销 SNI 项);`TrayIconEvent::receiver()` / `MenuEvent::receiver()` 是全局通道且由 KSNI 自己的线程投递(tauri 只给**它自己依赖的** 0.19 / 0.24 装全局处理器,与本模块用的 0.20 / 0.25 是各自独立的静态通道),各起一个线程消费再 `run_on_main_thread` 回主线程执行 `show_main_window` / `app.exit`;没有 SNI watcher(未装 AppIndicator 扩展的 GNOME)或会话总线不可用时只记 `LINUX_TRAY_FAILED`,不让启动失败。macOS / Windows 继续走 `tauri::tray`(避免两套 muda ObjC 菜单实现并存)。tauri 2.x 跟进 bump tray-icon 之后,删掉这份依赖与 `linux_tray.rs` 即可。+## Summary- **API Import**: 直接 `import { invoke } from '@tauri-apps/api/core'`(本仓库没有 `@/apis` 层);类型就近定义在 hooks/组件文件中diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lockindex 460cb7e5..738e0906 100644--- a/src-tauri/Cargo.lock+++ b/src-tauri/Cargo.lock@@ -998,6 +998,7 @@ dependencies = ["tracing-appender","tracing-log","tracing-subscriber",+ "tray-icon 0.25.0","webview2-com","windows-core 0.61.2","windows-sys 0.61.2",@@ -2507,6 +2508,34 @@ dependencies = ["unicode-segmentation",]+[[package]]+name = "keyboard-types"+version = "0.8.3"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "0fbe853b403ae61a04233030ae8a79d94975281ed9770a1f9e246732b534b28d"+dependencies = [+ "bitflags 2.13.1",+ "serde",+]++[[package]]+name = "ksni"+version = "0.3.6"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "814b44c24cd2cb236c3b8a41c7f08237b452a8e76ecaa81f1cec40b5b678215b"+dependencies = [+ "async-executor",+ "async-io",+ "async-lock",+ "futures-channel",+ "futures-lite",+ "futures-util",+ "pastey",+ "serde",+ "task-local",+ "zbus",+]+[[package]]name = "lazy_static"version = "1.5.0"@@ -2714,7 +2743,7 @@ dependencies = ["crossbeam-channel","dpi","gtk",- "keyboard-types",+ "keyboard-types 0.7.0","objc2","objc2-app-kit","objc2-core-foundation",@@ -2726,6 +2755,25 @@ dependencies = ["windows-sys 0.61.2",]+[[package]]+name = "muda"+version = "0.20.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "cca139e57da4383727f189e43e66e84fa372347027fc996f9f1a007ec0a37996"+dependencies = [+ "crossbeam-channel",+ "dpi",+ "keyboard-types 0.8.3",+ "objc2",+ "objc2-app-kit",+ "objc2-core-foundation",+ "objc2-foundation",+ "once_cell",+ "png 0.18.1",+ "thiserror 2.0.20",+ "windows-sys 0.61.2",+]+[[package]]name = "native-tls"version = "0.2.18"@@ -3227,6 +3275,12 @@ dependencies = ["subtle",]+[[package]]+name = "pastey"+version = "0.2.3"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "2ee67f1008b1ba2321834326597b8e186293b049a023cdef258527550b9935b4"+[[package]]name = "pbkdf2"version = "0.11.0"@@ -4659,6 +4713,15 @@ version = "0.12.16"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1"+[[package]]+name = "task-local"+version = "0.1.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "2972044a9e5e448a506a7ff6f0d03b566d8ef4cd6918a58fc59835a0f8666626"+dependencies = [+ "pin-project-lite",+]+[[package]]name = "tauri"version = "2.11.5"@@ -4681,7 +4744,7 @@ dependencies = ["libc","log","mime",- "muda",+ "muda 0.19.3","objc2","objc2-app-kit","objc2-foundation",@@ -4703,7 +4766,7 @@ dependencies = ["tauri-utils","thiserror 2.0.20","tokio",- "tray-icon",+ "tray-icon 0.24.2","url","webkit2gtk","webview2-com",@@ -5504,7 +5567,7 @@ dependencies = ["crossbeam-channel","dirs 6.0.0","libappindicator",- "muda",+ "muda 0.19.3","objc2","objc2-app-kit","objc2-core-foundation",@@ -5517,6 +5580,26 @@ dependencies = ["windows-sys 0.61.2",]+[[package]]+name = "tray-icon"+version = "0.25.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "b1d7d8cfccf1ae7ae9cba25a60daac8355bbaaf0fd07ff1c3af8f9872c4e4cb8"+dependencies = [+ "crossbeam-channel",+ "ksni",+ "muda 0.20.0",+ "objc2",+ "objc2-app-kit",+ "objc2-core-foundation",+ "objc2-core-graphics",+ "objc2-foundation",+ "once_cell",+ "png 0.18.1",+ "thiserror 2.0.20",+ "windows-sys 0.61.2",+]+[[package]]name = "tree_magic_mini"version = "3.2.2"diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.tomlindex 7ccd6ee6..7fa61a5d 100644--- a/src-tauri/Cargo.toml+++ b/src-tauri/Cargo.toml@@ -93,6 +93,20 @@ core-foundation = "0.9"[target.'cfg(unix)'.dependencies]libc = "0.2"+# Linux 托盘直接使用上游 tray-icon 0.25 的 KSNI 后端(实现见 src/desktop/linux_tray.rs,+# 背景见 issue #386 / #438):+# - tauri 2.11.5 仍固定 `tray-icon = "^0.24"`,而 0.24 在 Linux 上只有 libappindicator/GTK+# 后端,该后端不上报任何托盘事件,单击/双击都无法唤起主窗口;+# - tray-icon 0.25.0 新增的 KSNI 后端会把 SNI 的 Activate 转成+# `TrayIconEvent::Click { button: Left }`;+# - 但 tauri 2.x 的菜单类型来自 muda 0.19,与 tray-icon 0.25 的 muda 0.20 不是同一套类型+# (`TrayIconBuilder::menu` 的 `ContextMenu` trait 不同),既无法 `cargo update` 也无法+# `[patch.crates-io]` 把 tauri 的 tray-icon 顶到 0.25,tauri 3 目前只有 alpha。+# 因此 Linux 上绕过 `tauri::tray` 自建托盘;tauri 2.x 跟进 bump 后可删除本依赖与+# src/desktop/linux_tray.rs。`ksni` 特性同时避免再引入 GTK/libappindicator 后端。+[target.'cfg(target_os = "linux")'.dependencies]+tray-icon = { version = "0.25", default-features = false, features = [ "ksni" ] }+[target.'cfg(windows)'.dependencies]rfd = "0.16"webview2-com = "0.38"diff --git a/src-tauri/src/desktop/builder.rs b/src-tauri/src/desktop/builder.rsindex 3cb89a05..73ac7b19 100644--- a/src-tauri/src/desktop/builder.rs+++ b/src-tauri/src/desktop/builder.rs@@ -8,11 +8,16 @@ use std::sync::{Mutex, OnceLock};use tauri::{ipc::Invoke,- menu::{Menu, MenuEvent, MenuItem},- tray::{MouseButton, TrayIconBuilder, TrayIconEvent},Emitter, Manager, Runtime, WebviewUrl, WebviewWindowBuilder, Wry,};+// 托盘相关的 tauri 类型只在非 Linux 路径使用:Linux 走 desktop::linux_tray 的+// KSNI 托盘(见该文件的背景说明),届时这些导入会变成未使用。+#[cfg(not(target_os = "linux"))]+use tauri::menu::{Menu, MenuEvent, MenuItem};+#[cfg(not(target_os = "linux"))]+use tauri::tray::{MouseButton, TrayIconBuilder, TrayIconEvent};+#[cfg(target_os = "macos")]use tauri::menu::{PredefinedMenuItem, Submenu};@@ -23,6 +28,9 @@ static MACOS_FULLSCREEN_MENU_ITEM: OnceLock<Mutex<Option<PredefinedMenuItem<Wry>#[cfg(windows)]use crate::desktop::window::on_page_load;use crate::desktop::window::{on_download, on_new_window};+// 只在 tauri::tray 路径(Windows / macOS)里按短名使用;Linux 的 KSNI 托盘在+// desktop::linux_tray 内自行引用,且下面单例回调用的是完整路径。+#[cfg(not(target_os = "linux"))]use crate::utils::show_main_window;/// 壳层(`Navbar`)导航栏高度,单位 CSS px。@@ -147,7 +155,19 @@ pub fn setup(app_handle: tauri::AppHandle) {});}-/// setup tray+/// 构建系统托盘。+///+/// Linux 用上游 tray-icon 的 KSNI 后端自建(`tauri::tray` 固定依赖的 tray-icon 0.24+/// 在 Linux 上不上报任何托盘事件,见 `desktop::linux_tray` 的平台说明与 issue #386);+/// Windows / macOS 沿用 `tauri::tray`。+#[cfg(target_os = "linux")]+pub fn tray<R: Runtime>(app: &tauri::AppHandle<R>) -> tauri::Result<()> {+ crate::desktop::linux_tray::build(app);+ Ok(())+}++/// 构建系统托盘(Windows / macOS:`tauri::tray`)。+#[cfg(not(target_os = "linux"))]pub fn tray<R: Runtime>(app: &tauri::AppHandle<R>) -> tauri::Result<()> {// 平台差异的托盘图标策略:// - macOS:使用 scoped template 透明图标(NSImage template),由系统按菜单栏diff --git a/src-tauri/src/desktop/linux_tray.rs b/src-tauri/src/desktop/linux_tray.rsnew file mode 100644index 00000000..29e83116--- /dev/null+++ b/src-tauri/src/desktop/linux_tray.rs@@ -0,0 +1,131 @@+//! Linux 系统托盘(KSNI / StatusNotifierItem)。+//!+//! 背景(issue #386 / #438):本应用此前在 Linux 上走 `tauri::tray`,而 tauri 2.11.5+//! 固定依赖的 tray-icon 0.24.2 在 Linux 上只有 libappindicator/GTK 后端,该后端**完全+//! 不上报托盘事件**(上游 tauri-apps/tray-icon#104),因此单击/双击托盘都无法唤起主+//! 窗口,只能走托盘菜单里的「打开面板」。+//!+//! 上游 tray-icon 0.25.0 合并了 KSNI 后端:SNI 的 `Activate`(左键单击)会被映射成+//! `TrayIconEvent::Click { button: Left, .. }`,即 Linux 上终于能收到单击事件。但+//! tauri 2.x 的菜单类型来自 muda 0.19,而 tray-icon 0.25 换成了 muda 0.20+//! (`TrayIconBuilder::menu` 的 `ContextMenu` 不是同一个 trait),tauri 2.11.5 的+//! `tray-icon = "^0.24"` 既无法 `cargo update` 也无法 `[patch.crates-io]` 顶到 0.25;+//! tauri 3 目前只有 alpha。+//!+//! 因此 Linux 上绕过 `tauri::tray`,直接用同一份上游 crate(`tray-icon` 0.25 的 `ksni`+//! 后端,见 Cargo.toml 里的 `cfg(target_os = "linux")` 依赖)自建托盘;Windows / macOS+//! 继续走 `tauri::tray`(两边事件上报本就正常,也避免在 macOS 上让 muda 0.19 / 0.20+//! 两套 ObjC 菜单实现并存)。tauri 2.x 跟进 bump tray-icon 后,本文件与那份依赖即可删除。+//!+//! 事件为什么不会被 tauri 抢走:tauri 在 `App::run` 里会给**它自己依赖的** muda 0.19 /+//! tray-icon 0.24 装全局处理器(`muda::MenuEvent::set_event_handler` /+//! `tray_icon::TrayIconEvent::set_event_handler`),而同一 crate 的不同 semver 大版本在+//! 二进制里是两份独立实例、各自带静态通道,所以这里用的 0.20 / 0.25 仍能从 `receiver()`+//! 拿到事件。反过来说,只要还在这条“自建托盘”路径上,就不要混用 tauri 的菜单/托盘类型。++use tauri::{AppHandle, Runtime};++use crate::utils::show_main_window;++/// 构建 Linux 托盘。+///+/// 托盘是可选壳层能力:Linux 桌面没有 SNI watcher(例如未装 AppIndicator 扩展的+/// GNOME)或会话总线不可用时,只记日志,不让应用启动失败——与 `tauri::tray` 在+/// macOS/Windows 上的失败语义差异在此显式收口。+pub fn build<R: Runtime>(app: &AppHandle<R>) {+ if let Err(error) = build_inner(app) {+ log::warn!("[tray] LINUX_TRAY_FAILED: {error}");+ }+}++fn build_inner<R: Runtime>(app: &AppHandle<R>) -> Result<(), String> {+ use tray_icon::menu::{Menu, MenuItem};+ use tray_icon::{Icon, TrayIconBuilder};++ // 菜单文案与 `tauri::tray` 版本保持一致("打开面板" / "退出")。+ let menu = Menu::new();+ menu.append_items(&[+ &MenuItem::with_id("open", "打开面板", true, None),+ &MenuItem::with_id("quit", "退出", true, None),+ ])+ .map_err(|error| format!("LINUX_TRAY_MENU_FAILED: {error}"))?;++ let mut builder = TrayIconBuilder::new()+ .with_menu(Box::new(menu))+ .with_tooltip("Deepseek Harness Desktop")+ .with_menu_on_left_click(false);+ if let Some(icon) = app.default_window_icon() {+ match Icon::from_rgba(icon.rgba().to_vec(), icon.width(), icon.height()) {+ Ok(icon) => builder = builder.with_icon(icon),+ // 图标无效不该连累托盘本身:没有图标仍能收到单击/菜单事件。+ Err(error) => log::warn!("[tray] LINUX_TRAY_ICON_INVALID: {error}"),+ }+ }++ let tray = builder+ .build()+ .map_err(|error| format!("LINUX_TRAY_BUILD_FAILED: {error}"))?;+ // 托盘必须活到进程结束:`TrayIcon` 析构会注销 SNI 项(图标从托盘消失)。它与进程+ // 同寿命,且没有 Send/Sync 约束,直接泄漏一份句柄比引入包装类型更直接。+ let _ = Box::leak(Box::new(tray));++ spawn_event_forwarders(app)+}++/// KSNI 后端在它自己的线程上投递事件,这里各起一个消费线程把事件送回主线程执行。+fn spawn_event_forwarders<R: Runtime>(app: &AppHandle<R>) -> Result<(), String> {+ use tray_icon::menu::MenuEvent;+ use tray_icon::{MouseButton, TrayIconEvent};++ // 左键单击(SNI Activate)→ 打开主窗口,这正是 issue #386 要修的行为。+ // KSNI 后端只在 Activate 时发一次 Click/Up,不区分物理上的单击与双击(双击会+ // 收到两次),因此这里不必额外匹配 DoubleClick。+ let click_app = app.clone();+ std::thread::Builder::new()+ .name("linux-tray-click".into())+ .spawn(move || {+ for event in TrayIconEvent::receiver() {+ if let TrayIconEvent::Click {+ button: MouseButton::Left,+ ..+ } = event+ {+ on_main_thread(&click_app, |app| show_main_window(app));+ }+ }+ })+ .map_err(|error| format!("LINUX_TRAY_CLICK_THREAD_FAILED: {error}"))?;++ // 托盘菜单:KSNI 把菜单项的 activate 回调(muda 快照里携带的)直接接到 D-Bus 菜单上,+ // 点击后经 muda 的 MenuEvent 通道出来,语义与 `tauri::tray` 的 "open" / "quit" 对齐。+ let menu_app = app.clone();+ std::thread::Builder::new()+ .name("linux-tray-menu".into())+ .spawn(move || {+ for event in MenuEvent::receiver() {+ match event.id().as_ref() {+ "open" => on_main_thread(&menu_app, |app| show_main_window(app)),+ "quit" => on_main_thread(&menu_app, |app| app.exit(0)),+ _ => {}+ }+ }+ })+ .map_err(|error| format!("LINUX_TRAY_MENU_THREAD_FAILED: {error}"))?;++ Ok(())+}++/// 把动作送回主线程执行。+///+/// `run_on_main_thread` 会借用 `app`,而闭包里还要用同一份句柄,所以先克隆出两份:+/// 一份用于投递,一份交给主线程上的动作。+fn on_main_thread<R: Runtime>(+ app: &AppHandle<R>,+ action: impl FnOnce(&AppHandle<R>) + Send + 'static,+) {+ let task_app = app.clone();+ let main_app = app.clone();+ if let Err(error) = task_app.run_on_main_thread(move || action(&main_app)) {+ log::warn!("[tray] LINUX_TRAY_MAIN_THREAD_FAILED: {error}");+ }+}diff --git a/src-tauri/src/desktop/mod.rs b/src-tauri/src/desktop/mod.rsindex d6f0c821..934705da 100644--- a/src-tauri/src/desktop/mod.rs+++ b/src-tauri/src/desktop/mod.rs@@ -2,6 +2,8 @@ pub mod activation;pub mod autostart;pub mod builder;pub mod compat;+#[cfg(target_os = "linux")]+pub mod linux_tray;pub mod notification;pub mod paste;pub mod payload;