Skip to content

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

fix(pet): 菜单宠物项按 itemLabel 匹配并复用 Ghost 图标

hairyf796cc9082b8b
diff --git a/packages/dsh-tauri-pet/src/client/constants/index.ts b/packages/dsh-tauri-pet/src/client/constants/index.ts
index 629ee73b..0cab0f8f 100644
--- a/packages/dsh-tauri-pet/src/client/constants/index.ts
+++ b/packages/dsh-tauri-pet/src/client/constants/index.ts
@@ -33,16 +33,17 @@ export const CMD_LIST_PRESET_PETS = 'list_preset_pets'
* 设置菜单补丁的选择器与守卫属性。
*
* 官方账号菜单(桌面载体)与壳层自有菜单(浏览器直开)都是官方 primitives 的 portal `Menu`,
- * 条目为 `button[role=menuitem]` 且由 `itemWrap` 包裹;「设置」条目是唯一锚点,其它菜单
- * (工作区、模型选择等)不含该文案。克隆官方条目以继承样式,因此不依赖动态类名哈希。
+ * 条目为 `button[role=menuitem]`,结构为 `itemIcon` + `itemLabel`(+ 可选 `shortcut` 键帽);
+ * 「设置」条目是唯一锚点,其它菜单(工作区、模型选择等)不含该文案。克隆官方条目以继承样式,
+ * 因此不依赖动态类名哈希。
*
* 桌宠的开关只存在于这个菜单里:侧栏爪按钮已删除(设置入口两种形态都是菜单)。
*/
export const SETTINGS_MENU_LABELS: readonly string[] = ['设置', 'Settings']
export const MENU_ITEM_SELECTOR = 'button[role="menuitem"]'
-export const MENU_ITEM_WRAP_SELECTOR = '[class*="itemWrap"]'
export const MENU_ITEM_LABEL_SELECTOR = '[class*="itemLabel"]'
export const MENU_ITEM_ICON_SELECTOR = '[class*="itemIcon"]'
+export const MENU_ITEM_SHORTCUT_SELECTOR = '[class*="shortcut"]'
export const PET_MENU_ITEM_ATTRIBUTE = 'data-dsh-tauri-pet-menu-item'
export const PET_MENU_PATCH_ATTRIBUTE = 'data-dsh-tauri-pet-menu-patched'
diff --git a/packages/dsh-tauri-pet/src/client/register/settings-menu.utils.test.ts b/packages/dsh-tauri-pet/src/client/register/settings-menu.utils.test.ts
index 1b3ade20..cd50e49e 100644
--- a/packages/dsh-tauri-pet/src/client/register/settings-menu.utils.test.ts
+++ b/packages/dsh-tauri-pet/src/client/register/settings-menu.utils.test.ts
@@ -1,16 +1,30 @@
-/**
- * settings-menu.utils.test.ts — 设置菜单桌宠条目的补丁契约。
- *
- * 仓库未装 jsdom:这里用最小假节点锁住三条真实契约——只认「设置」文案锚点、克隆项换文案
- * 与爪印图标并打守卫属性、缺 primitives 文案节点时中止(不插半成品条目)。
- */
-import { describe, expect, it } from 'vitest'
+import { Ghost, Icon } from 'dsh-tauri-ui/client'
+import { describe, expect, it, vi } from 'vitest'
import { decoratePetMenuItem, isSettingsMenuItem } from './settings-menu.utils'
+const { roots } = vi.hoisted(() => ({
+ roots: [] as { container: unknown, node: unknown }[],
+}))
+
+vi.mock('react-dom/client', () => ({
+ createRoot: (container: unknown) => ({
+ render: (node: unknown) => {
+ roots.push({ container, node })
+ },
+ unmount: () => {},
+ }),
+}))
+
+vi.mock('dsh-tauri-ui/client', () => ({
+ Ghost: () => null,
+ Icon: () => null,
+}))
+
interface FakeNode {
textContent?: string
label: FakeLabel | null
icon: FakeIcon | null
+ shortcut: FakeShortcut | null
attributes: Record<string, string>
}
@@ -22,22 +36,31 @@ interface FakeIcon {
innerHTML: string
}
-function fakeItem(text: string, structure = true): HTMLButtonElement {
+interface FakeShortcut {
+ removed: boolean
+ remove: () => void
+}
+
+function fakeItem(text: string, structure = true, buttonText = text): HTMLButtonElement {
const label: FakeLabel | null = structure ? { textContent: text } : null
const icon: FakeIcon | null = structure ? { innerHTML: '<svg data-official="1"></svg>' } : null
+ const shortcut: FakeShortcut | null = structure ? { removed: false, remove: () => { node.shortcut!.removed = true } } : null
const node = {
- textContent: text,
+ textContent: buttonText,
label,
icon,
+ shortcut,
attributes: {} as Record<string, string>,
cloneNode(): unknown {
- return fakeItem(text, structure)
+ return fakeItem(text, structure, buttonText)
},
querySelector(selector: string): unknown {
if (selector.includes('itemLabel'))
return node.label
if (selector.includes('itemIcon'))
return node.icon
+ if (selector.includes('shortcut'))
+ return node.shortcut
return null
},
setAttribute(name: string, value: string): void {
@@ -54,21 +77,36 @@ describe('isSettingsMenuItem', () => {
expect(isSettingsMenuItem(fakeItem('删除工作区'))).toBe(false)
expect(isSettingsMenuItem(fakeItem('设置宠物'))).toBe(false)
})
+
+ it('ignores the shortcut keycaps appended to the row text', () => {
+ expect(isSettingsMenuItem(fakeItem('设置', true, '设置Ctrl+,'))).toBe(true)
+ })
})
describe('decoratePetMenuItem', () => {
- it('rewrites the label and icon and marks the clone', () => {
- const source = fakeItem('设置')
+ it('rewrites the label, drops the keycaps and marks the clone', () => {
+ const source = fakeItem('设置', true, '设置Ctrl+,')
const item = decoratePetMenuItem(source, '启用宠物')
const clone = item as unknown as FakeNode
expect(clone).not.toBe(source)
expect(clone.label?.textContent).toBe('启用宠物')
- expect(clone.icon?.innerHTML).toContain('<path')
+ expect(clone.shortcut?.removed).toBe(true)
expect(clone.attributes['data-dsh-tauri-pet-menu-item']).toBe('1')
})
+ it('mounts the shared Ghost icon into the cloned icon node', () => {
+ roots.length = 0
+ const source = fakeItem('设置')
+ const item = decoratePetMenuItem(source, '启用宠物') as unknown as FakeNode
+
+ expect(roots).toHaveLength(1)
+ expect(roots[0].container).toBe(item.icon)
+ expect((roots[0].node as { type: unknown, props: { as: unknown } }).type).toBe(Icon)
+ expect((roots[0].node as { props: { as: unknown } }).props.as).toBe(Ghost)
+ })
+
it('refuses to decorate a non-primitives item', () => {
expect(decoratePetMenuItem(fakeItem('设置', false), '启用宠物')).toBeNull()
})
diff --git a/packages/dsh-tauri-pet/src/client/register/settings-menu.utils.ts b/packages/dsh-tauri-pet/src/client/register/settings-menu.utils.ts
index 68bb345e..df7df98b 100644
--- a/packages/dsh-tauri-pet/src/client/register/settings-menu.utils.ts
+++ b/packages/dsh-tauri-pet/src/client/register/settings-menu.utils.ts
@@ -1,36 +1,43 @@
+import type { Root } from 'react-dom/client'
+import { Ghost, Icon } from 'dsh-tauri-ui/client'
+import { createElement } from 'react'
+import { createRoot } from 'react-dom/client'
import {
MENU_ITEM_ICON_SELECTOR,
MENU_ITEM_LABEL_SELECTOR,
+ MENU_ITEM_SHORTCUT_SELECTOR,
PET_MENU_ITEM_ATTRIBUTE,
SETTINGS_MENU_LABELS,
} from '../constants'
-/** 爪印字形(@gravity-ui/icons 无对应图标,随 currentColor 变色)。 */
-const PET_PAW_PATHS = [
- 'M12 13.5c-2.7 0-5.5 2-5.5 4.3 0 1.4 1 2.2 2.3 2.2 1 0 1.9-.6 3.2-.6s2.2.6 3.2.6c1.3 0 2.3-.8 2.3-2.2 0-2.3-2.8-4.3-5.5-4.3z',
- 'M7.3 8.1c-1 .1-1.8 1.2-1.7 2.5.1 1.2 1 2.1 2 2 .9-.1 1.7-1.2 1.6-2.4-.1-1.2-1-2.2-1.9-2.1z',
- 'M12 4.5c-1.1 0-2 1.1-2 2.5s.9 2.5 2 2.5 2-1.1 2-2.5-.9-2.5-2-2.5z',
- 'M16.7 8.1c-.9-.1-1.8.9-1.9 2.1-.1 1.2.7 2.3 1.6 2.4 1 .1 1.9-.8 2-2 .1-1.3-.7-2.4-1.7-2.5z',
- 'M4.8 12.3c-.8.3-1.2 1.4-.9 2.4.3 1 1.2 1.6 2 1.3.8-.3 1.1-1.4.8-2.4-.3-1-1.1-1.6-1.9-1.3z',
- 'M19.2 12.3c-.8-.3-1.6.3-1.9 1.3-.3 1 0 2.1.8 2.4.8.3 1.7-.3 2-1.3.3-1-.1-2.1-.9-2.4z',
-]
+/** 克隆条目里的图标由 React 接管(`Ghost` 随 currentColor 变色),与官方条目同一套图标组件。 */
+const ICON_ROOTS = new WeakMap<HTMLElement, Root>()
-/** 克隆条目换图标用的标记串(不引入 react-dom/server)。 */
-const PET_PAW_SVG = `<svg viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">${PET_PAW_PATHS.map(path => `<path d="${path}"/>`).join('')}</svg>`
+function renderIcon(icon: HTMLElement): void {
+ ICON_ROOTS.get(icon)?.unmount()
+ const root = createRoot(icon)
+ root.render(createElement(Icon, { as: Ghost }))
+ ICON_ROOTS.set(icon, root)
+}
/**
* 该条目是否是设置菜单的锚点(文案为「设置」)。
*
* 官方账号菜单与壳层自有菜单都以它作为唯一稳定锚点:两者条目结构相同(primitives 的
* `button[role=menuitem]`),其它菜单(工作区、模型选择)不含该文案。
+ *
+ * 只认 `itemLabel` 节点的文案而非按钮 `textContent`:带快捷键的条目(桌面账号菜单的
+ * 「设置」由壳层传入 `settingsShortcut`)会把 `Ctrl+,` 键帽串进 `textContent`,
+ * 整串比较会把唯一锚点判否,宠物项随之静默消失。
*/
export function isSettingsMenuItem(item: HTMLElement): boolean {
- const text = item.textContent?.trim() ?? ''
- return SETTINGS_MENU_LABELS.includes(text)
+ const label = item.querySelector<HTMLElement>(MENU_ITEM_LABEL_SELECTOR)?.textContent ?? item.textContent ?? ''
+ return SETTINGS_MENU_LABELS.includes(label.trim())
}
/**
- * 克隆官方条目改成桌宠动作项:样式随克隆继承(绝不手写动态类名哈希),只换文案与图标。
+ * 克隆官方条目改成桌宠动作项:样式随克隆继承(绝不手写动态类名哈希),只换文案与图标,
+ * 并摘掉键帽——桌宠条目不响应 `Ctrl+,`。
* 文案节点缺失(非 primitives 结构)时返回 null,调用方中止插入,不追加半成品条目。
*/
export function decoratePetMenuItem(source: HTMLButtonElement, label: string): HTMLButtonElement | null {
@@ -41,7 +48,8 @@ export function decoratePetMenuItem(source: HTMLButtonElement, label: string): H
labelNode.textContent = label
const icon = item.querySelector<HTMLElement>(MENU_ITEM_ICON_SELECTOR)
if (icon !== null)
- icon.innerHTML = PET_PAW_SVG
+ renderIcon(icon)
+ item.querySelector<HTMLElement>(MENU_ITEM_SHORTCUT_SELECTOR)?.remove()
item.setAttribute(PET_MENU_ITEM_ATTRIBUTE, '1')
return item
}