Skip to content

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

fix(settings): 手机端不挂载自有设置侧栏,改用官方弹出层 (#785)

Mao Mr2658504b8889
diff --git a/packages/dsh-tauri-ui/src/client/register/settings.register.test.ts b/packages/dsh-tauri-ui/src/client/register/settings.register.test.ts
index 8f8d7957..951b51c1 100644
--- a/packages/dsh-tauri-ui/src/client/register/settings.register.test.ts
+++ b/packages/dsh-tauri-ui/src/client/register/settings.register.test.ts
@@ -26,7 +26,34 @@ vi.mock('dsh-tauri/client', () => ({
},
}))
+interface MatchMediaHost {
+ window?: { matchMedia?: (query: string) => { matches: boolean } }
+}
+
+const host = globalThis as unknown as MatchMediaHost
+const originalWindow = host.window
+
+/** 手机端才有三条设备查询全部成立;触屏为主但另接鼠标的设备缺 `(any-hover: none)`。 */
+const MOBILE_DEVICE = {
+ '(hover: none)': true,
+ '(any-pointer: coarse)': true,
+ '(any-hover: none)': true,
+}
+const TOUCHSCREEN_WITH_MOUSE = {
+ '(hover: none)': true,
+ '(any-pointer: coarse)': true,
+ '(any-hover: none)': false,
+}
+
+function stubDevice(answers: Record<string, boolean>): void {
+ host.window = {
+ ...host.window,
+ matchMedia: (query: string) => ({ matches: answers[query] ?? false }),
+ }
+}
+
afterEach(() => {
+ host.window = originalWindow
mocks.settings.launcherAvailable = false
mocks.settings.launcherShortcut = undefined
})
@@ -74,6 +101,42 @@ describe('registerSettings launcher seat', () => {
expect(registered).toContain('sidebar.settings')
})
+ /** 手机端把 `sidebar.settings` 交还官方:自有侧栏与触发器都不许抢座位。 */
+ it('leaves every settings seat to the official dialog on mobile', () => {
+ stubDevice(MOBILE_DEVICE)
+
+ const { injected, registered } = activate()
+
+ expect(injected).toEqual([])
+ expect(registered).toEqual([])
+ })
+
+ /** 触屏为主但接了鼠标的设备仍有鼠标可用:不能被手机端判据误伤。 */
+ it('keeps the own sidebar on a touchscreen with a secondary mouse', () => {
+ stubDevice(TOUCHSCREEN_WITH_MOUSE)
+
+ const { injected } = activate()
+
+ expect(injected.map(entry => entry.key)).toEqual([
+ 'shell.overlay',
+ 'sidebar.settings',
+ 'settings.launcher',
+ ])
+ })
+
+ /** 桌面端仍走自有侧栏,手机端判据不误伤。 */
+ it('keeps the own sidebar on a touchscreen desktop', () => {
+ stubDevice({ '(hover: none)': false, '(any-pointer: coarse)': true, '(any-hover: none)': false })
+
+ const { injected } = activate()
+
+ expect(injected.map(entry => entry.key)).toEqual([
+ 'shell.overlay',
+ 'sidebar.settings',
+ 'settings.launcher',
+ ])
+ })
+
/** 官方账号菜单落在 `settings.launcher`:声明前必须退回自有触发器,声明后由官方条目渲染。 */
it('hosts the official account launcher only while it is declared', () => {
const { injected } = activate()
diff --git a/packages/dsh-tauri-ui/src/client/register/settings.ts b/packages/dsh-tauri-ui/src/client/register/settings.ts
index faf2855f..919e3410 100644
--- a/packages/dsh-tauri-ui/src/client/register/settings.ts
+++ b/packages/dsh-tauri-ui/src/client/register/settings.ts
@@ -12,6 +12,7 @@ import {
import { store } from '../store'
import { SettingsSidebar } from '../ui/settings-sidebar'
import { SettingsTrigger } from '../ui/settings-trigger'
+import { detectMobileDevice } from './settings.utils'
const SETTINGS_SHORTCUT_EFFECT = 'dsh-tauri-ui: settings launcher shortcut (Ctrl+, hint)'
@@ -23,6 +24,9 @@ export const registerSettings = defineRegister<ClientContext>((controller, ctx)
return
}
+ if (detectMobileDevice())
+ return
+
controller.add(
ctx.slots.inject(SETTINGS_SHELL_OVERLAY_SLOT, () =>
ctx.slots.register(
diff --git a/packages/dsh-tauri-ui/src/client/register/settings.utils.test.ts b/packages/dsh-tauri-ui/src/client/register/settings.utils.test.ts
new file mode 100644
index 00000000..a5f7ecb3
--- /dev/null
+++ b/packages/dsh-tauri-ui/src/client/register/settings.utils.test.ts
@@ -0,0 +1,95 @@
+import { afterEach, describe, expect, it, vi } from 'vitest'
+import { detectMobileDevice, isMobileDevice } from './settings.utils'
+
+const MOBILE_QUERIES = ['(hover: none)', '(any-pointer: coarse)', '(any-hover: none)']
+
+/** 只记查询串、按给定的答案表作答:被测函数不碰真实浏览器。 */
+function matcher(answers: Record<string, boolean>) {
+ const queries: string[] = []
+ const matches = (query: string): boolean => {
+ queries.push(query)
+ return answers[query] ?? false
+ }
+ return {
+ queries,
+ matches,
+ matchMedia: (query: string) => ({ matches: matches(query) }),
+ }
+}
+
+function stubWindow(matchMedia?: (query: string) => { matches: boolean }): void {
+ vi.stubGlobal('window', matchMedia === undefined ? undefined : { matchMedia })
+}
+
+afterEach(() => {
+ vi.unstubAllGlobals()
+})
+
+describe('isMobileDevice', () => {
+ it('treats a touch device with a coarse pointer as mobile', () => {
+ const probe = matcher({
+ '(hover: none)': true,
+ '(any-pointer: coarse)': true,
+ '(any-hover: none)': true,
+ })
+
+ expect(isMobileDevice(probe.matches)).toBe(true)
+ expect(probe.queries).toEqual(MOBILE_QUERIES)
+ })
+
+ /** 触屏笔记本两个都成立、纯触屏平板与鼠标桌面各只成立一个,都不能进手机端分支。 */
+ it('keeps every partially matching device on the desktop branch', () => {
+ const mouseDesktop = matcher({ '(hover: none)': false, '(any-pointer: coarse)': false })
+ const touchscreenLaptop = matcher({ '(hover: none)': false, '(any-pointer: coarse)': true })
+ const hoverTouch = matcher({ '(hover: none)': true, '(any-pointer: coarse)': false })
+
+ expect(isMobileDevice(mouseDesktop.matches)).toBe(false)
+ expect(isMobileDevice(touchscreenLaptop.matches)).toBe(false)
+ expect(isMobileDevice(hoverTouch.matches)).toBe(false)
+ })
+
+ /** 触屏为主、另接鼠标的设备(`any-hover: hover`)有鼠标可用,仍按桌面端处理。 */
+ it('keeps a touchscreen with a secondary mouse on the desktop branch', () => {
+ const tabletWithMouse = matcher({
+ '(hover: none)': true,
+ '(any-pointer: coarse)': true,
+ '(any-hover: hover)': true,
+ })
+
+ expect(isMobileDevice(tabletWithMouse.matches)).toBe(false)
+ expect(tabletWithMouse.queries).toEqual(MOBILE_QUERIES)
+ })
+
+ /** 判据只看设备能力:窄窗口但带鼠标仍是桌面端,横竖屏切换不会翻案。 */
+ it('ignores viewport width', () => {
+ const narrowDesktop = matcher({
+ '(max-width: 480px)': true,
+ '(hover: none)': false,
+ '(any-pointer: coarse)': false,
+ })
+
+ expect(isMobileDevice(narrowDesktop.matches)).toBe(false)
+ expect(narrowDesktop.queries).not.toContain('(max-width: 480px)')
+ })
+})
+
+describe('detectMobileDevice', () => {
+ it('probes the device capabilities through the kernel matchMedia', () => {
+ const probe = matcher({
+ '(hover: none)': true,
+ '(any-pointer: coarse)': true,
+ '(any-hover: none)': true,
+ })
+ stubWindow(probe.matchMedia)
+
+ expect(detectMobileDevice()).toBe(true)
+ expect(probe.queries).toEqual(MOBILE_QUERIES)
+ })
+
+ /** 老内核没有 matchMedia(node 单测同样如此):按桌面端处理,保住自有侧栏。 */
+ it('falls back to the desktop branch without a kernel matchMedia', () => {
+ stubWindow()
+
+ expect(detectMobileDevice()).toBe(false)
+ })
+})
diff --git a/packages/dsh-tauri-ui/src/client/register/settings.utils.ts b/packages/dsh-tauri-ui/src/client/register/settings.utils.ts
new file mode 100644
index 00000000..bdf3001a
--- /dev/null
+++ b/packages/dsh-tauri-ui/src/client/register/settings.utils.ts
@@ -0,0 +1,14 @@
+/** 手机端判据只看设备能力:触屏(无悬停)+ 主指针粗糙 + 没有任何可悬停指针,窗口缩放与旋转都不会改判。 */
+const MOBILE_MEDIA_QUERIES = ['(hover: none)', '(any-pointer: coarse)', '(any-hover: none)'] as const
+
+/** 三条必须同时成立:触屏设备一旦接上鼠标(`any-hover: hover`)仍按桌面端处理。 */
+export function isMobileDevice(match: (query: string) => boolean): boolean {
+ return MOBILE_MEDIA_QUERIES.every(query => match(query))
+}
+
+/** 没有 `window`(Node 单测)或内核缺 `matchMedia` 时一律按桌面端处理。 */
+export function detectMobileDevice(): boolean {
+ if (typeof window === 'undefined' || typeof window.matchMedia !== 'function')
+ return false
+ return isMobileDevice(query => window.matchMedia(query).matches)
+}