DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.
feat(desktop): 新增默认关闭的强制 XWayland 设置项
fainabi38d7f01bfe77
diff --git a/src-tauri/src/bridge/pet.rs b/src-tauri/src/bridge/pet.rsindex 97bf8bd2..3a766e75 100644--- a/src-tauri/src/bridge/pet.rs+++ b/src-tauri/src/bridge/pet.rs@@ -174,6 +174,28 @@ pub fn get_pet_overlay_supported() -> bool {crate::pet_overlay_supported_env()}+/// 读取「强制 XWayland」开关(issue #649 的方案 2,默认关闭)。+///+/// 该设置作用于整个应用而非只有桌宠,命令名因此不带 `pet_` 前缀;落在本模块只因+/// 桌宠遮挡是它唯一的症状与唯一的消费方。同理不并入 [`PetStatus`]:后者经+/// `pet://status` 广播给桌宠窗口与侧栏指示点,两者都不关心这个值。+#[tauri::command]+pub fn get_force_xwayland(app: AppHandle) -> bool {+ config::get_store_dat_setting(&app).force_xwayland+}++/// 写入「强制 XWayland」开关,返回落盘后的值。+///+/// 不操作窗口、不广播 `pet://status`:`GDK_BACKEND` 只在 GTK 初始化时被读取一次,+/// 本次进程里没有任何东西能因此改变,生效要等下次启动。+#[tauri::command]+pub fn set_force_xwayland(app: AppHandle, enabled: bool) -> Result<bool, String> {+ let updated = config::update_store_dat_setting(&app, |setting| {+ setting.force_xwayland = enabled;+ });+ Ok(updated.force_xwayland)+}+/// 启用/关闭桌宠(持久化)。侧栏入口、设置页与桌宠窗口自身的关闭请求都走这里。////// 关闭即销毁窗口实例(不是 hide,见 `desktop::pet::set_pet_window_visible`:隐藏窗口里diff --git a/src-tauri/src/config/setting.rs b/src-tauri/src/config/setting.rsindex 4b65c647..be4432bb 100644--- a/src-tauri/src/config/setting.rs+++ b/src-tauri/src/config/setting.rs@@ -79,6 +79,13 @@ pub struct Setting {/// 桌宠精灵图的显示宽度(逻辑像素);`None` = 沿用窗口侧默认值。#[serde(default)]pub pet_size: Option<f64>,+ /// 强制以 XWayland 运行(默认关闭,下次启动生效)。+ ///+ /// 影响的是整个应用而非只有桌宠:原生 Wayland 下桌宠既不能置顶也不能定位+ /// (issue #649),把应用拉到 XWayland 是在 GNOME 上恢复这两项能力的唯一办法,+ /// 代价是主窗口也一并经 XWayland 渲染。默认关闭,由用户显式开启。+ #[serde(default)]+ pub force_xwayland: bool,}pub const ZOOM_FACTOR_MIN: f64 = 0.5;@@ -178,6 +185,7 @@ impl Default for Setting {pet_enabled: false,active_pet: None,pet_size: None,+ force_xwayland: false,}}}@@ -210,6 +218,50 @@ fn resolve_store_dat_file(e2e: bool, debug: bool) -> &'static str {}}+/// 启动最早期读取 `force_xwayland`,绕过 `tauri_plugin_store` 直接解析 store 文件。+///+/// `GDK_BACKEND` 必须在 GTK 初始化之前设置,那时 `AppHandle` 尚不存在,插件的+/// `StoreExt` 用不了。路径由 `logger::identifier_dir()` 与 `store_dat_file_name()` 拼出,+/// 与插件的 `BaseDirectory::AppData` + 文件名解析一致,开发 / E2E / 生产三份 store+/// 不互读。store 靠文件名区分 dev,目录不带 `dev/` 一层,与日志的做法不同。+/// 文件缺失、JSON 非法、键缺失一律按关闭处理:此处早于 `logger::init()`,+/// 无处告警,静默回落到默认行为比中断启动合适。+///+/// `migrate_app_data_dir` 在 builder 的 setup 阶段才执行,晚于这里。从旧标识符升级+/// 上来的用户,升级后的首次启动读不到设置,该次不强制,迁移完成后下次启动恢复。+pub fn force_xwayland_setting() -> bool {+ crate::logger::identifier_dir()+ .map(|dir| dir.join(store_dat_file_name()))+ .and_then(|path| std::fs::read_to_string(path).ok())+ .is_some_and(|raw| force_xwayland_in_store_json(&raw))+}++/// `force_xwayland_setting` 的纯函数内核,便于单测覆盖各种损坏输入。+fn force_xwayland_in_store_json(raw: &str) -> bool {+ let Ok(root) = serde_json::from_str::<serde_json::Value>(raw) else {+ return false;+ };+ let Some(value) = root.get(STORE_SETTING_KEY) else {+ return false;+ };+ // 与 read_store_dat_setting 同理:值可能是对象,也可能是内含对象的 JSON 字符串。+ let unwrapped;+ let object = match value.as_str() {+ Some(text) => match serde_json::from_str::<serde_json::Value>(text) {+ Ok(parsed) => {+ unwrapped = parsed;+ &unwrapped+ }+ Err(_) => return false,+ },+ None => value,+ };+ object+ .get("force_xwayland")+ .and_then(serde_json::Value::as_bool)+ .unwrap_or(false)+}+fn setting_write_lock() -> &'static Mutex<()> {static LOCK: OnceLock<Mutex<()>> = OnceLock::new();LOCK.get_or_init(|| Mutex::new(()))@@ -282,6 +334,7 @@ fn preserve_persisted_fields(mut replacement: Setting, current: &Setting) -> Setreplacement.pet_enabled = current.pet_enabled;replacement.active_pet.clone_from(¤t.active_pet);replacement.pet_size = current.pet_size;+ replacement.force_xwayland = current.force_xwayland;replacement}@@ -358,11 +411,56 @@ pub fn set_dsh_pkg_tag(app_handle: &AppHandle, tag: String) {#[cfg(test)]mod tests {use super::{- default_close_action, default_zoom_factor, normalize_close_action, normalize_zoom_factor,- preserve_persisted_fields, resolve_store_dat_file, Setting, STORE_DAT_DEV_FILE,- STORE_DAT_FILE, STORE_DAT_TEST_FILE, ZOOM_FACTOR_MAX, ZOOM_FACTOR_MIN,+ default_close_action, default_zoom_factor, force_xwayland_in_store_json,+ normalize_close_action, normalize_zoom_factor, preserve_persisted_fields,+ resolve_store_dat_file, Setting, STORE_DAT_DEV_FILE, STORE_DAT_FILE, STORE_DAT_TEST_FILE,+ STORE_SETTING_KEY, ZOOM_FACTOR_MAX, ZOOM_FACTOR_MIN,};+ /// 启动前读取跑在 `logger::init()` 之前,任何损坏输入都只能静默回落到关闭。+ #[test]+ fn force_xwayland_falls_back_to_off_on_any_unreadable_store() {+ // 正常形状:`setting` 的值是对象。+ assert!(force_xwayland_in_store_json(+ r#"{"setting":{"force_xwayland":true}}"#+ ));+ assert!(!force_xwayland_in_store_json(+ r#"{"setting":{"force_xwayland":false}}"#+ ));+ // 历史形状:`setting` 的值是一个内含对象的 JSON 字符串,read_store_dat_setting+ // 同样兼容;漏掉这一支会让部分用户的设置被静默读成关闭。+ assert!(force_xwayland_in_store_json(+ r#"{"setting":"{\"force_xwayland\":true}"}"#+ ));+ // 字段缺失(老版本写下的 store)。+ assert!(!force_xwayland_in_store_json(r#"{"setting":{"port":3080}}"#));+ // 键缺失、JSON 非法、空文件。+ assert!(!force_xwayland_in_store_json(r#"{"window_state":{}}"#));+ assert!(!force_xwayland_in_store_json("{ not json"));+ assert!(!force_xwayland_in_store_json(""));+ // 字符串包裹但内层非法。+ assert!(!force_xwayland_in_store_json(r#"{"setting":"not json"}"#));+ // 值不是 bool:不做真值推断,按关闭处理。+ assert!(!force_xwayland_in_store_json(+ r#"{"setting":{"force_xwayland":"yes"}}"#+ ));+ assert!(!force_xwayland_in_store_json(+ r#"{"setting":{"force_xwayland":1}}"#+ ));+ }++ /// 启动前读取按字符串字面量取字段,与 `Setting` 的序列化形状只靠约定对齐。+ /// 字段改名不会有编译错误,只会让读取静默失效,这里拿真实序列化结果兜住。+ #[test]+ fn force_xwayland_key_matches_the_serialized_setting() {+ let setting = Setting {+ force_xwayland: true,+ ..Setting::default()+ };+ let raw = serde_json::json!({ STORE_SETTING_KEY: setting }).to_string();+ assert!(force_xwayland_in_store_json(&raw));+ }+#[test]fn store_dat_file_name_isolates_the_three_modes() {assert_eq!(resolve_store_dat_file(true, true), STORE_DAT_TEST_FILE);@@ -419,6 +517,7 @@ mod tests {stale.pet_enabled = false;stale.active_pet = Some("chat:stale".to_string());stale.pet_size = Some(80.0);+ stale.force_xwayland = false;let mut current = Setting::default();current.zoom_factor = 1.6;@@ -426,6 +525,7 @@ mod tests {current.pet_enabled = true;current.active_pet = Some("codex:latest".to_string());current.pet_size = Some(140.0);+ current.force_xwayland = true;let merged = preserve_persisted_fields(stale, ¤t);@@ -438,6 +538,10 @@ mod tests {Some(140.0),"整对象写入不得覆盖最新桌宠字段");+ assert!(+ merged.force_xwayland,+ "整对象写入不得覆盖最新的 XWayland 开关"+ );}#[test]diff --git a/src-tauri/src/desktop/builder.rs b/src-tauri/src/desktop/builder.rsindex 5f7e6547..4fd600d3 100644--- a/src-tauri/src/desktop/builder.rs+++ b/src-tauri/src/desktop/builder.rs@@ -985,6 +985,8 @@ pub fn handler() -> impl Fn(Invoke<Wry>) -> bool + Send + Sync + 'static {crate::bridge::log_frontend,crate::bridge::get_pet_status,crate::bridge::get_pet_overlay_supported,+ crate::bridge::get_force_xwayland,+ crate::bridge::set_force_xwayland,crate::bridge::set_pet_enabled,crate::bridge::set_active_pet,crate::bridge::set_pet_size,diff --git a/src-tauri/src/desktop/pet.rs b/src-tauri/src/desktop/pet.rsindex 65990f3d..87543743 100644--- a/src-tauri/src/desktop/pet.rs+++ b/src-tauri/src/desktop/pet.rs@@ -301,7 +301,7 @@ pub fn ensure_pet_window<R: Runtime>(app: &AppHandle<R>) -> tauri::Result<Webvielog::warn!("PET_OVERLAY_UNSUPPORTED: always-on-top and absolute positioning are unavailable on \a native Wayland session; the pet window is covered by other windows and does not \- stay where it was placed. Launch with GDK_BACKEND=x11 to restore them."+ stay where it was placed. Enable the XWayland option in pet settings to restore them.");}let app_handle = app.clone();diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rsindex fdd9f9ef..587c1299 100644--- a/src-tauri/src/lib.rs+++ b/src-tauri/src/lib.rs@@ -40,6 +40,18 @@ pub fn run() {eprintln!("[wayland] set {} for WebKitGTK EGL", applied.join("="));}}+ // 强制 XWayland(默认关闭的设置项,见 `should_force_xwayland`)。必须在此处执行:+ // GDK 只在初始化时读一次 GDK_BACKEND,建窗之后再改无效。+ if should_force_xwayland(+ config::force_xwayland_setting(),+ &std::env::var("WAYLAND_DISPLAY").unwrap_or_default(),+ &std::env::var("GDK_BACKEND").unwrap_or_default(),+ &std::env::var("DISPLAY").unwrap_or_default(),+ ) {+ std::env::set_var("GDK_BACKEND", "x11");+ // 同上,logger::init() 尚未执行,只能用 eprintln+ eprintln!("[wayland] set GDK_BACKEND=x11 so the pet window can stay on top (issue #649)");+ }// 初始化日志系统logger::init();@@ -115,6 +127,34 @@ pub(crate) fn pet_overlay_supported(wayland_display: &str, gdk_backend: &str) ->wayland_display.is_empty() || gdk_backend.split(',').next() == Some("x11")}+/// 本次启动是否应把应用拉到 XWayland 上(`force_xwayland` 设置项的判定)。+///+/// 值取 `x11` 而不是 `x11,wayland`:后者在 XWayland 缺席时会回落到 Wayland,看似+/// 更安全,但 [`pet_overlay_supported`] 只看列表首项,回落之后提示反而消失,用户+/// 拿到「桌宠仍被遮挡且没有解释」这个最差结果。GDK 是否发生了回落无法从环境变量+/// 观测,判定侧补不回来。改为要求 `DISPLAY` 非空能买到同样的启动安全性——合成器+/// 运行 XWayland 时才导出该变量——同时让判定保持诚实:`DISPLAY` 为空时什么都不设,+/// 提示照常显示。+///+/// 已显式设过 `GDK_BACKEND` 的用户不被覆盖,与上方 EGL workaround 的约定一致:+/// 启动脚本或 `.desktop` 里的 `Exec=env GDK_BACKEND=…` 保持权威,显式设了+/// `wayland` 的用户是在主动接受该限制。+///+/// macOS / Windows 上 `WAYLAND_DISPLAY` 为空,[`pet_overlay_supported`] 返回 true,+/// 判定短路为 false,变量不会被设置。因此不加 `#[cfg(target_os)]`,单测在三条 CI+/// 腿上都跑得到。+pub(crate) fn should_force_xwayland(+ enabled: bool,+ wayland_display: &str,+ gdk_backend: &str,+ display: &str,+) -> bool {+ enabled+ && !pet_overlay_supported(wayland_display, gdk_backend)+ && gdk_backend.is_empty()+ && !display.is_empty()+}+/// [`pet_overlay_supported`] 的环境变量读取版本,供查询命令与建窗路径共用。pub(crate) fn pet_overlay_supported_env() -> bool {pet_overlay_supported(@@ -156,4 +196,21 @@ mod tests {assert!(pet_overlay_supported("", ""));assert!(pet_overlay_supported("", "wayland"));}++ #[test]+ fn force_xwayland_only_on_native_wayland_with_xserver() {+ // 设置项默认关闭:其余条件全部满足也不改环境。+ assert!(!should_force_xwayland(false, "wayland-0", "", ":0"));+ // 开启 + 原生 Wayland + XWayland 在跑:唯一生效的组合。+ assert!(should_force_xwayland(true, "wayland-0", "", ":0"));+ // DISPLAY 为空说明合成器没起 XWayland,设了 x11 会让应用起不来。+ assert!(!should_force_xwayland(true, "wayland-0", "", ""));+ // 用户已显式指定后端,不覆盖(无论指定的是哪一个)。+ assert!(!should_force_xwayland(true, "wayland-0", "wayland", ":0"));+ assert!(!should_force_xwayland(true, "wayland-0", "x11", ":0"));+ // X11 会话:置顶与定位本来就能用。+ assert!(!should_force_xwayland(true, "", "", ":0"));+ // macOS / Windows:三个变量都为空,GDK_BACKEND 在那里也无意义。+ assert!(!should_force_xwayland(true, "", "", ""));+ }}diff --git a/src-tauri/src/logger/mod.rs b/src-tauri/src/logger/mod.rsindex 34c38e0e..463a135f 100644--- a/src-tauri/src/logger/mod.rs+++ b/src-tauri/src/logger/mod.rs@@ -29,7 +29,10 @@ static FILE_GUARD: OnceLock<WorkerGuard> = OnceLock::new();static FRONTDESK_WRITER: OnceLock<Arc<Mutex<SizeRotatingWriter>>> = OnceLock::new();/// 平台应用数据根目录下的本应用目录(`identifier` 一层;dev 与 release 相同)。-fn identifier_dir() -> Option<PathBuf> {+///+/// 仅用 `std::env` 解析,不依赖 `AppHandle`,因此也是启动前读取 store 的路径来源+/// (`config::force_xwayland_setting`)。+pub(crate) fn identifier_dir() -> Option<PathBuf> {#[cfg(target_os = "windows")]{let appdata = std::env::var("APPDATA").ok()?;diff --git a/src/hooks/use-invoke-iframe.ts b/src/hooks/use-invoke-iframe.tsindex 861c3a4c..f12feec8 100644--- a/src/hooks/use-invoke-iframe.ts+++ b/src/hooks/use-invoke-iframe.ts@@ -40,6 +40,8 @@ const ALLOWED_INVOKE_CMDS = new Set(['open_external_url','get_pet_status','get_pet_overlay_supported',+ 'get_force_xwayland',+ 'set_force_xwayland','set_pet_enabled','set_active_pet','set_pet_size',