Skip to content

DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.

feat(pet): open main window on double-click and add context menu (#1139)

Hairy2dd4178132a6
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 27e3be1e..303689ee 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -323,8 +323,8 @@ catalogs:
specifier: ^5.101.4
version: 5.101.4
dsh-pet-component:
- specifier: ^0.2.4
- version: 0.2.4
+ specifier: ^0.2.5
+ version: 0.2.5
i18next:
specifier: ^26.3.6
version: 26.3.6
@@ -533,7 +533,7 @@ importers:
version: 2.4.4
dsh-pet-component:
specifier: catalog:runtime
expo:
specifier: catalog:native
@@ -5911,8 +5911,8 @@ packages:
typescript:
optional: true
- resolution: {integrity: sha512-q8HIQX7sx47Rb53g67YXWgNOlFFDooXNvxAAkYW6Nhzyu1mGnTjZ14cGsuD5QKumxVnG5yE8yvR2SqWnk1DEKQ==}
+ resolution: {integrity: sha512-7uaVJfEC0o9tT2+ZeUiNHUwpa7He3q1TWdCwUu/snAus4z9WDUxKiLFracUugvlsCvJru785KKXmGKjgogAAXA==}
peerDependencies:
react: ^19.0.0
@@ -14576,7 +14576,7 @@ snapshots:
- crossws
- ocache
dependencies:
'@gravity-ui/icons': 2.21.0([email protected])
'@reause/core': 0.1.7([email protected])
diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml
index d42bc2cc..2b4f7af0 100644
--- a/pnpm-workspace.yaml
+++ b/pnpm-workspace.yaml
@@ -12,7 +12,7 @@ inject-workspace-packages: true
minimumReleaseAgeExclude:
- '@reause/[email protected]'
- '@reause/[email protected]'
- '@deepseek-ai/[email protected]'
- '@deepseek-ai/[email protected]'
- '@deepseek-ai/[email protected]'
@@ -226,7 +226,7 @@ catalogs:
'@overlastic/react': ^0.9.0
'@reause/core': ^0.1.7
'@tanstack/react-query': ^5.101.4
- dsh-pet-component: ^0.2.4
+ dsh-pet-component: ^0.2.5
i18next: ^26.3.6
react: ^19.2.8
react-dom: ^19.2.8
diff --git a/src-tauri/src/bridge/pet.rs b/src-tauri/src/bridge/pet.rs
index 4ca5fa8e..dbf5d421 100644
--- a/src-tauri/src/bridge/pet.rs
+++ b/src-tauri/src/bridge/pet.rs
@@ -211,6 +211,50 @@ fn emit_pet_status(app: &AppHandle, status: &PetStatus) {
);
}
+#[tauri::command]
+pub fn pet_show_main_window(window: WebviewWindow) -> Result<(), String> {
+ if window.label() != pet_window::PET_WINDOW_LABEL {
+ return Err(
+ "PET_WINDOW_LABEL_MISMATCH: this command is restricted to the pet window".into(),
+ );
+ }
+ crate::utils::show_main_window(window.app_handle());
+ Ok(())
+}
+
+#[tauri::command]
+pub fn pet_show_context_menu(window: WebviewWindow) -> Result<(), String> {
+ use tauri::menu::{Menu, MenuItem};
+
+ if window.label() != pet_window::PET_WINDOW_LABEL {
+ return Err(
+ "PET_WINDOW_LABEL_MISMATCH: this command is restricted to the pet window".into(),
+ );
+ }
+ let app = window.app_handle();
+ let show = MenuItem::with_id(
+ app,
+ "pet-show-main-window",
+ config::i18n::t("pet.show_main_window"),
+ true,
+ None::<&str>,
+ )
+ .map_err(|error| format!("PET_MENU_CREATE_FAILED: {error}"))?;
+ let close = MenuItem::with_id(
+ app,
+ "pet-close",
+ config::i18n::t("pet.close"),
+ true,
+ None::<&str>,
+ )
+ .map_err(|error| format!("PET_MENU_CREATE_FAILED: {error}"))?;
+ let menu = Menu::with_items(app, &[&show, &close])
+ .map_err(|error| format!("PET_MENU_CREATE_FAILED: {error}"))?;
+ window
+ .popup_menu(&menu)
+ .map_err(|error| format!("PET_MENU_POPUP_FAILED: {error}"))
+}
+
/// 查询桌宠当前完整状态。
#[tauri::command]
pub fn get_pet_status(app: AppHandle) -> PetStatus {
diff --git a/src-tauri/src/config/i18n.rs b/src-tauri/src/config/i18n.rs
index 08acedd3..2097eab5 100644
--- a/src-tauri/src/config/i18n.rs
+++ b/src-tauri/src/config/i18n.rs
@@ -49,6 +49,8 @@ pub fn t(key: &str) -> String {
"menu.new_chat" => ("新聊天", "New Chat"),
"menu.open_folder" => ("打开文件夹", "Open Folder"),
"menu.open_panel" => ("打开面板", "Open Panel"),
+ "pet.show_main_window" => ("显示主界面", "Show Main Window"),
+ "pet.close" => ("关闭宠物", "Close Pet"),
"menu.close" => ("关闭", "Close"),
"menu.quit" => ("退出", "Quit"),
"menu.documentation" => ("文档", "Documentation"),
diff --git a/src-tauri/src/desktop/builder.rs b/src-tauri/src/desktop/builder.rs
index bc727fcb..c23bd57a 100644
--- a/src-tauri/src/desktop/builder.rs
+++ b/src-tauri/src/desktop/builder.rs
@@ -1655,6 +1655,8 @@ pub fn handler() -> impl Fn(Invoke<Wry>) -> bool + Send + Sync + 'static {
crate::desktop::window::toggle_devtools,
crate::bridge::log_frontend,
crate::bridge::get_pet_status,
+ crate::bridge::pet_show_main_window,
+ crate::bridge::pet_show_context_menu,
crate::bridge::get_pet_overlay_supported,
crate::bridge::get_force_xwayland,
crate::bridge::set_force_xwayland,
@@ -1739,6 +1741,12 @@ pub fn builder() -> tauri::Builder<tauri::Wry> {
Ok(())
})
.on_menu_event(|app, event| match event.id().as_ref() {
+ "pet-show-main-window" => show_main_window(app),
+ "pet-close" => {
+ if let Err(error) = crate::bridge::set_pet_enabled(app.clone(), false) {
+ log::error!("PET_CLOSE_FAILED: {error}");
+ }
+ }
"desktop-toggle-sidebar"
| "desktop-toggle-right-panel"
| "desktop-open-terminal"
diff --git a/src/i18n/locales/en-US.json b/src/i18n/locales/en-US.json
index 114857e1..3f1e9262 100644
--- a/src/i18n/locales/en-US.json
+++ b/src/i18n/locales/en-US.json
@@ -96,6 +96,8 @@
"menu.new_window": "New Window",
"menu.new_chat": "New Chat",
"menu.open_folder": "Open Folder",
+ "pet.show_main_window": "Show Main Window",
+ "pet.close": "Close Pet",
"menu.close": "Close",
"menu.quit": "Quit",
"menu.documentation": "Documentation",
diff --git a/src/i18n/locales/zh-CN.json b/src/i18n/locales/zh-CN.json
index 3880eab5..cf017fcd 100644
--- a/src/i18n/locales/zh-CN.json
+++ b/src/i18n/locales/zh-CN.json
@@ -96,6 +96,8 @@
"menu.new_window": "新建窗口",
"menu.new_chat": "新聊天",
"menu.open_folder": "打开文件夹",
+ "pet.show_main_window": "显示主界面",
+ "pet.close": "关闭宠物",
"menu.close": "关闭",
"menu.quit": "退出",
"menu.documentation": "文档",
diff --git a/src/pet/app.test.tsx b/src/pet/app.test.tsx
new file mode 100644
index 00000000..1f5f6696
--- /dev/null
+++ b/src/pet/app.test.tsx
@@ -0,0 +1,79 @@
+// @vitest-environment jsdom
+import type { PetProps } from 'dsh-pet-component'
+import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import { App } from './app'
+
+const native = vi.hoisted(() => ({ invoke: vi.fn(), report: vi.fn() }))
+vi.mock('@tauri-apps/api/core', () => ({ invoke: native.invoke }))
+vi.mock('./utils/log', () => ({ reportPetIssue: native.report }))
+vi.mock('dsh-pet-component', () => ({
+ Pet: (props: PetProps) => (
+ <div data-testid="pet-hitbox" onDoubleClick={props.onDoubleClick} onContextMenu={props.onContextMenu} />
+ ),
+ useControllablePet: () => ({}),
+}))
+vi.mock('./hooks/use-pet-status', () => ({
+ usePetStatus: () => ({ enabled: true, visible: true, active_pet: 'codex:cat', pet_size: 100 }),
+ normalizeSizePercent: (size: number) => size,
+}))
+vi.mock('./hooks/use-pet-source', () => ({
+ usePetSource: () => ({ source: { kind: 'codex', config: {}, uri: 'cat.png' }, error: null }),
+}))
+vi.mock('./hooks/use-bubble-tracker', () => ({ useBubbleTracker: vi.fn() }))
+vi.mock('./hooks/use-pet-physics', () => ({ usePetPhysics: () => ({}) }))
+vi.mock('./hooks/use-pet-window', () => ({ usePetWindowSize: vi.fn() }))
+vi.mock('./hooks/use-pet-window-clamp', () => ({ usePetWindowClamp: () => ({}) }))
+vi.mock('@/hooks/use-window-draggable', () => ({ useWindowDraggable: () => ({ dragging: false }) }))
+vi.mock('@/hooks/use-omit-ignore-cursor-events', () => ({ useOmitIgnoreCursorEvents: vi.fn() }))
+vi.mock('../hooks/use-wakelock-release', () => ({ useWakelockRelease: vi.fn() }))
+vi.mock('@/ui/pet/hint', () => ({ Hint: () => null }))
+
+beforeEach(() => {
+ vi.resetAllMocks()
+ native.invoke.mockResolvedValue(undefined)
+})
+afterEach(() => {
+ cleanup()
+ vi.restoreAllMocks()
+})
+
+describe('pet window interactions', () => {
+ it('opens the main window on the component double-click event', () => {
+ render(<App />)
+ fireEvent.doubleClick(screen.getByTestId('pet-hitbox'))
+ expect(native.invoke.mock.calls).toEqual([['pet_show_main_window']])
+ })
+
+ it('requests the native menu on the component context-menu event', () => {
+ render(<App />)
+ expect(fireEvent.contextMenu(screen.getByTestId('pet-hitbox'))).toBe(false)
+ expect(native.invoke.mock.calls).toEqual([['pet_show_context_menu']])
+ })
+
+ it('does not open the main window on a single click', () => {
+ render(<App />)
+ fireEvent.click(screen.getByTestId('pet-hitbox'))
+ expect(native.invoke).not.toHaveBeenCalled()
+ })
+
+ it('suppresses the browser menu without showing the pet menu outside the body', () => {
+ render(<App />)
+ expect(fireEvent.contextMenu(screen.getByRole('main'))).toBe(false)
+ expect(native.invoke).not.toHaveBeenCalled()
+ })
+
+ it('reports a failed main-window request', async () => {
+ native.invoke.mockRejectedValueOnce(new Error('show failed'))
+ render(<App />)
+ fireEvent.doubleClick(screen.getByTestId('pet-hitbox'))
+ await waitFor(() => expect(native.report).toHaveBeenCalledWith('show main window', new Error('show failed')))
+ })
+
+ it('reports a failed native-menu request', async () => {
+ native.invoke.mockRejectedValueOnce(new Error('menu failed'))
+ render(<App />)
+ fireEvent.contextMenu(screen.getByTestId('pet-hitbox'))
+ await waitFor(() => expect(native.report).toHaveBeenCalledWith('context menu', new Error('menu failed')))
+ })
+})
diff --git a/src/pet/app.tsx b/src/pet/app.tsx
index de888f28..870fbceb 100644
--- a/src/pet/app.tsx
+++ b/src/pet/app.tsx
@@ -1,5 +1,7 @@
import type { PetRef, PetRenderMotion } from 'dsh-pet-component'
+import type { MouseEvent } from 'react'
import { useEventListener } from '@reause/core'
+import { invoke } from '@tauri-apps/api/core'
import { Pet, useControllablePet } from 'dsh-pet-component'
import { useRef } from 'react'
import { If } from 'react-if-lite'
@@ -48,6 +50,15 @@ export function App() {
useEventListener('contextmenu', event => event.preventDefault())
useWakelockRelease()
+ function handleDoubleClick(): void {
+ void invoke('pet_show_main_window').catch(error => reportPetIssue('show main window', error))
+ }
+
+ function handleContextMenu(event: MouseEvent<HTMLDivElement>): void {
+ event.preventDefault()
+ void invoke('pet_show_context_menu').catch(error => reportPetIssue('context menu', error))
+ }
+
const motion: PetRenderMotion | undefined = draggable.dragging
? (draggable.direction === undefined ? undefined : `moving-${draggable.direction}`)
: undefined
@@ -65,6 +76,8 @@ export function App() {
size={width}
dragging={draggable.dragging}
onFling={onFling}
+ onDoubleClick={handleDoubleClick}
+ onContextMenu={handleContextMenu}
cache={source.cache}
hidden={!visible}
hitboxRef={hitboxRef}
diff --git a/test/pet-interactions.test.ts b/test/pet-interactions.test.ts
new file mode 100644
index 00000000..d21605d6
--- /dev/null
+++ b/test/pet-interactions.test.ts
@@ -0,0 +1,46 @@
+import { describe, expect, it } from 'vitest'
+import en from '../src/i18n/locales/en-US.json'
+import zh from '../src/i18n/locales/zh-CN.json'
+import { readSource } from './setup/read-source'
+
+const bridge = readSource('src-tauri/src/bridge/pet.rs')
+const builder = readSource('src-tauri/src/desktop/builder.rs')
+const translations = readSource('src-tauri/src/config/i18n.rs')
+
+describe('pet native interaction wiring', () => {
+ it.each(['pet_show_main_window', 'pet_show_context_menu'])('registers %s and restricts it to the pet window', (command) => {
+ expect(builder).toContain(`crate::bridge::${command},`)
+ const body = bridge.split(`pub fn ${command}(`)[1].split('\n}\n')[0]
+ expect(body).toContain('window.label() != pet_window::PET_WINDOW_LABEL')
+ expect(body).toContain('PET_WINDOW_LABEL_MISMATCH:')
+ })
+
+ it('uses the shared native restoration path for double-click and menu activation', () => {
+ const body = bridge.split('pub fn pet_show_main_window(')[1].split('\n}\n')[0]
+ expect(body).toContain('crate::utils::show_main_window(window.app_handle())')
+ expect(builder).toContain('"pet-show-main-window" => show_main_window(app)')
+ })
+
+ it('creates only show-main and close actions in the pet native popup', () => {
+ const body = bridge.split('pub fn pet_show_context_menu(')[1].split('\n}\n')[0]
+ expect([...body.matchAll(/"(pet-[a-z-]+)"/g)].map(match => match[1])).toEqual(['pet-show-main-window', 'pet-close'])
+ expect(body).toContain('Menu::with_items(app, &[&show, &close])')
+ expect(body).toContain('.popup_menu(&menu)')
+ })
+
+ it('closes the pet through the persistent disable path instead of hiding or quitting', () => {
+ const action = builder.split('"pet-close" => {')[1].split('"desktop-toggle-sidebar"')[0]
+ expect(action).toContain('crate::bridge::set_pet_enabled(app.clone(), false)')
+ expect(action).not.toMatch(/\.hide\(|\.exit\(/)
+ })
+
+ it.each([
+ ['pet.show_main_window', '显示主界面', 'Show Main Window'],
+ ['pet.close', '关闭宠物', 'Close Pet'],
+ ] as const)('keeps %s translated consistently in the shell and native menu', (key, chinese, english) => {
+ expect(zh[key]).toBe(chinese)
+ expect(en[key]).toBe(english)
+ expect(translations).toContain(`"${key}" => ("${chinese}", "${english}")`)
+ expect(bridge).toContain(`config::i18n::t("${key}")`)
+ })
+})