DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.
fix(pet): Linux 下用输入形状取代整窗穿透,修复 XWayland 下桌宠点不到、拖不动 (#1231)
FairyBand47f03f4117bb
diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lockindex 3ee78cff..d59c4862 100644--- a/src-tauri/Cargo.lock+++ b/src-tauri/Cargo.lock@@ -1043,6 +1043,7 @@ dependencies = ["fix-path-env","flate2","futures-util",+ "gtk","image","json-patch 3.0.1","libc",diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.tomlindex 689d9c02..d9e31aa1 100644--- a/src-tauri/Cargo.toml+++ b/src-tauri/Cargo.toml@@ -156,6 +156,8 @@ libc = "0.2"# tray-icon 0.25 / muda 0.20,托盘本身走 `tauri::tray`。[target.'cfg(target_os = "linux")'.dependencies]tray-icon = { version = "0.25", default-features = false, features = [ "ksni" ] }+# 桌宠输入区域(issue #649):直接调 GTK 设置窗口输入形状,版本与 tauri 所用 gtk 0.18 对齐。+gtk = "0.18"# E2E 基础设施:上游 `tauri-plugin-wdio-webdriver` 的帧上下文靠 JS 模拟# (`frame.contentWindow.eval`)。壳层是 `tauri://localhost`,内嵌 dsh 是diff --git a/src-tauri/src/bridge/pet.rs b/src-tauri/src/bridge/pet.rsindex dbf5d421..f811130c 100644--- a/src-tauri/src/bridge/pet.rs+++ b/src-tauri/src/bridge/pet.rs@@ -770,6 +770,71 @@ pub fn set_pet_ignore_cursor_events(window: WebviewWindow, ignore: bool) -> ResuOk(ignore)}+/// 桌宠可交互区域(窗口内 CSS px,与 GTK 逻辑坐标一致)。+#[derive(Debug, Clone, Copy, Deserialize)]+pub struct PetInputRect {+ pub x: f64,+ pub y: f64,+ pub width: f64,+ pub height: f64,+}++/// Linux 下把 pet 窗口的输入形状收缩到宠物本体,取代整窗穿透切换(issue #649)。+///+/// 整窗穿透依赖全局鼠标流判定光标是否回到宠物上;Wayland 会话里 XWayland 只在光标+/// 落在 X11 窗口上时才收到指针事件,窗口一旦穿透,XRecord 就再也拿不到坐标,桌宠永远+/// 点不到。输入形状由 X server / 合成器直接命中测试,不需要任何鼠标流。tao 的+/// `set_ignore_cursor_events` 本身也是改写同一份输入形状,因此两条路径不能混用。+/// `gtk_widget_input_shape_combine_region` 在未 realize 的窗口上只暂存、realize 时生效,+/// 不受 issue #437 的 unwrap 影响。+#[tauri::command]+pub fn set_pet_input_region(window: WebviewWindow, rect: PetInputRect) -> Result<(), String> {+ if window.label() != pet_window::PET_WINDOW_LABEL {+ return Err(+ "PET_WINDOW_LABEL_MISMATCH: this command is restricted to the pet window".to_string(),+ );+ }+ if !(rect.x.is_finite()+ && rect.y.is_finite()+ && rect.width.is_finite()+ && rect.height.is_finite())+ {+ return Err("PET_INPUT_REGION_INVALID: rect must be finite".to_string());+ }+ #[cfg(target_os = "linux")]+ {+ let target = window.clone();+ window+ .run_on_main_thread(move || apply_pet_input_region(&target, rect))+ .map_err(|error| format!("PET_INPUT_REGION_FAILED: {error}"))+ }+ #[cfg(not(target_os = "linux"))]+ {+ Err("PET_INPUT_REGION_UNSUPPORTED: input region is only used on Linux".to_string())+ }+}++#[cfg(target_os = "linux")]+fn apply_pet_input_region(window: &WebviewWindow, rect: PetInputRect) {+ use gtk::prelude::WidgetExt;++ let gtk_window = match window.gtk_window() {+ Ok(gtk_window) => gtk_window,+ Err(error) => {+ log::warn!("PET_INPUT_REGION_FAILED: {error}");+ return;+ }+ };+ let left = rect.x.floor() as i32;+ let top = rect.y.floor() as i32;+ let width = ((rect.x + rect.width).ceil() as i32 - left).max(0);+ let height = ((rect.y + rect.height).ceil() as i32 - top).max(0);+ let region = gtk::cairo::Region::create_rectangle(>k::cairo::RectangleInt::new(+ left, top, width, height,+ ));+ gtk_window.input_shape_combine_region(Some(®ion));+}+/// 返回来源对应的真实目录;chat 直接使用 `$DSH_HOME/pets`,codex 直接使用/// 用户主目录下的 `.codex/pets`,均不经过应用 AppData。fn pets_dir(app: &AppHandle, source: PetSource) -> Result<PathBuf, String> {diff --git a/src-tauri/src/desktop/builder.rs b/src-tauri/src/desktop/builder.rsindex 2035bf93..c27a1712 100644--- a/src-tauri/src/desktop/builder.rs+++ b/src-tauri/src/desktop/builder.rs@@ -1685,6 +1685,7 @@ pub fn handler() -> impl Fn(Invoke<Wry>) -> bool + Send + Sync + 'static {crate::bridge::move_pet_window,crate::bridge::persist_pet_window_position,crate::bridge::set_pet_ignore_cursor_events,+ crate::bridge::set_pet_input_region,crate::bridge::list_pets,crate::bridge::import_pet,crate::bridge::delete_pet,diff --git a/src/hooks/use-omit-ignore-cursor-events.test.ts b/src/hooks/use-omit-ignore-cursor-events.test.tsindex 01d194b3..b0afb8cb 100644--- a/src/hooks/use-omit-ignore-cursor-events.test.ts+++ b/src/hooks/use-omit-ignore-cursor-events.test.ts@@ -11,7 +11,9 @@ const native = vi.hoisted(() => ({moved: vi.fn(),resized: vi.fn(),label: 'pet',+ os: 'windows',}))+vi.mock('@tauri-apps/plugin-os', () => ({ type: () => native.os }))vi.mock('@tauri-apps/api/core', () => ({ invoke: native.invoke }))vi.mock('@tauri-apps/api/event', () => ({ listen: native.listen }))vi.mock('@tauri-apps/api/window', () => ({@@ -71,6 +73,7 @@ beforeEach(() => {handlers.clear()disposers.length = 0native.label = 'pet'+ native.os = 'windows'native.position.mockResolvedValue({ x: 100, y: 100 })native.visible.mockResolvedValue(true)native.moved.mockImplementation(handler => subscribe('moved', handler))@@ -89,6 +92,22 @@ afterEach(() => {})describe('pet cursor input lifecycle', () => {+ it('shapes the linux input region to the hitbox instead of toggling click-through', async () => {+ native.os = 'linux'+ vi.stubGlobal('requestAnimationFrame', (callback: FrameRequestCallback) => {+ callback(0)+ return 1+ })+ renderHook(() => useOmitIgnoreCursorEvents({ current: element }))+ await flush()+ await emit('device-mouse-move', { x: 500, y: 500 })+ await emit('device-mouse-move', { x: 120, y: 120 })+ expect(requests()).toEqual([])+ const regions = native.invoke.mock.calls.filter(([command]) => command === 'set_pet_input_region')+ expect(regions).toEqual([['set_pet_input_region', { rect: { x: 10, y: 10, width: 100, height: 100 } }]])+ expect(native.invoke).toHaveBeenCalledWith('start_pet_mouse_stream')+ })+it('applies outside and inside hit tests without duplicate native writes', async () => {renderHook(() => useOmitIgnoreCursorEvents({ current: element }))await flush()diff --git a/src/hooks/use-omit-ignore-cursor-events.ts b/src/hooks/use-omit-ignore-cursor-events.tsindex 907154ab..bd02b5ee 100644--- a/src/hooks/use-omit-ignore-cursor-events.ts+++ b/src/hooks/use-omit-ignore-cursor-events.ts@@ -3,6 +3,7 @@ import { useWatch } from '@reause/core'import { invoke } from '@tauri-apps/api/core'import { listen } from '@tauri-apps/api/event'import { getCurrentWindow } from '@tauri-apps/api/window'+import { type } from '@tauri-apps/plugin-os'import { useEffect, useRef } from 'react'interface DeviceMousePosition {@@ -10,6 +11,13 @@ interface DeviceMousePosition {y: number}+interface PetInputRect {+ x: number+ y: number+ width: number+ height: number+}+interface RustPetStatus {enabled?: booleanvisible?: boolean | null@@ -27,6 +35,10 @@ export function useOmitIgnoreCursorEvents(elementRef: RefObject<HTMLElement | nuconst appWindow = getCurrentWindow()if (appWindow.label !== 'pet')return+ // Linux 走输入形状而非整窗穿透:XWayland 下穿透后收不到全局鼠标流,桌宠再也点不到(issue #649)。+ const inputRegion = type() === 'linux'+ let appliedRegion: PetInputRect | undefined+ let regionFrame: number | undefinedlet disposed = falselet appliedIgnore: boolean | undefined = falselet desiredIgnore = false@@ -56,8 +68,33 @@ export function useOmitIgnoreCursorEvents(elementRef: RefObject<HTMLElement | nu})}+ function applyInputRegion(): void {+ regionFrame = undefined+ const element = elementRef.current+ if (disposed || element === null)+ return+ const { x, y, width, height } = element.getBoundingClientRect()+ const rect = { x, y, width, height }+ if (appliedRegion !== undefined && appliedRegion.x === x && appliedRegion.y === y && appliedRegion.width === width && appliedRegion.height === height)+ return+ appliedRegion = rect+ void invoke('set_pet_input_region', { rect }).catch((error) => {+ appliedRegion = undefined+ console.warn('[pet] PET_INPUT_UPDATE_FAILED:', error)+ })+ }++ function scheduleInputRegion(): void {+ if (regionFrame === undefined)+ regionFrame = requestAnimationFrame(applyInputRegion)+ }+function handleVisibility(visible: boolean): void {windowVisible = visible+ if (inputRegion) {+ scheduleInputRegion()+ return+ }if (visible)refreshFromState()else@@ -95,6 +132,10 @@ export function useOmitIgnoreCursorEvents(elementRef: RefObject<HTMLElement | nufunction refreshFromState(): void {if (disposed)return+ if (inputRegion) {+ scheduleInputRegion()+ return+ }if (draggingRef.current) {setIgnoreCursorEvents(false)return@@ -108,6 +149,8 @@ export function useOmitIgnoreCursorEvents(elementRef: RefObject<HTMLElement | nu}refreshRef.current = refreshFromState+ const regionObserver = inputRegion ? new MutationObserver(scheduleInputRegion) : undefined+ regionObserver?.observe(document.body, { childList: true, subtree: true, attributes: true })void refreshWindowPosition()const initialVisibilityRevision = visibilityRevisionvoid appWindow.isVisible().then((visible) => {@@ -147,6 +190,9 @@ export function useOmitIgnoreCursorEvents(elementRef: RefObject<HTMLElement | nureturn () => {disposed = true+ regionObserver?.disconnect()+ if (regionFrame !== undefined)+ cancelAnimationFrame(regionFrame)refreshRef.current = () => {}geometryRevision++for (const unlisten of unlisteners)