fix(desktop): 顶部保留区优先读壳的 desktopWindow 契约(#864)

现象:dsh-desktop 的 compatibility / extended 模式下,壳已经把整个页面下移 36px(页面在独立
WebContentsView / 壳自有 root 里从 y=36 开始),却仍按契约在 URL 上盖
`dsh-desktop-titlebar-inset=36`;插件把这枚戳当成「页面还要再让位 36px」,于是面板顶部多一条空白。

修法:
① `desktop-env.ts`:mode 接受第三种取值 `extended`(与 compatibility 同几何的独立 36px
   frame),并新增 `probeDesktopWindow(ctx)` —— 结构化探测客户端服务 `ctx.desktopWindow`
   (镜像类型 `SidebarDesktopWindowService` 落在 `context-types.ts`,照 sidebar-file.ts 的
   probe 约定)。**不写进 `dsh.client.inject`**:普通浏览器没有该服务,注入它会让整个插件
   pending;探测按调用读(服务由壳自己的 client fiber 提供,可能后到),`get` 抛错按缺失处理。
② `computeTitleBarStrip`:取值链在 ⓪ `web` 强制 0 之后、① WCO 之前插入壳契约 —— 服务存在时
   以 `safeAreaInsets.top` 为权威(compatibility / extended 报 0 → 不再预留;advanced 报它画进
   内容里的 caption 行,darwin 与 win32 同为 32,以壳上报值为准)。URL 戳因此在有服务时被忽略;服务不存在时旧链路
   (WCO → URL → preset/custom)逐字不变。
③ `shell-presets.ts`:复核 `env.mode !== 'advanced'` —— extended 不套用 advanced 的 preset
   顶部值(guard 原样成立,补注释与用例把它钉住)。

文档:指南 §12 的壳信号与 strip 取值链、`docs/plans/2026-08-20-desktop-scheme-settings-design.md`
的实施偏差记录同步。

判别性(`git stash push -- src` 临时还原源码、保留测试后重跑):
- titlebar-strip.spec.ts:(a)/(d) 由 0 变 **36**、(b) 由 32 变 0 → 3 例红((c)「无服务 + 戳 36
  → 36」在旧代码上照旧绿,正是本次刻意不动的那段);
- desktop-window-strip.spec.tsx(挂真 Sidebar、断言页面实际状态):期望
  `{compat:false, px:''}` 实得 `{compat:true, px:'36px'}` —— issue 的可见症状本身;advanced 实得
  无 strip → 3 例红;
- desktop-env.spec.ts:extended 解析成 null、`probeDesktopWindow is not a function` → 4 例红
  (typecheck 同时报 12 条:链只收 5 参 / `'extended'` 不在联合里);
- 还原后 34/34 绿,src diff 与还原前逐字节一致(`git diff -- src | diff - /tmp/430-src.patch`)。

全量:174 files / 1977 passed | 13 skipped(新增 14 例),typecheck 0,eslint 0。

本改动**不**解决 #430(那是「preset / custom 档位不改变位置」的取值链优先级问题),两者不要互相关闭。
This commit is contained in:
Menghuan1918 committed 2026-10-05 21:59:14 +08:00
1 parent 38ceecb051
commit a67779acdc
11 files changed
+443 -41

No files matched your search

+62 -6
View File
@@ -1,14 +1,17 @@
/**
* Desktop-shell detection tests: URL stamps (dsh-desktop-mode/platform)
* from the official Electron shell, the preload marker
* (__DSH_DESKTOP_FILE_PATH__), and the `dsh-desktop-titlebar-inset`
* contract parameter. Geometry ADAPTATION is deliberately NOT here — the
* strip resolution chain lives in titlebar-strip.ts (standard WCO first,
* see wco.spec.ts), keeping this module a pure environment reporter.
* (__DSH_DESKTOP_FILE_PATH__), the `dsh-desktop-titlebar-inset` contract
* parameter, and the shell's own window service probe
* (`ctx.desktopWindow` — see titlebar-strip.spec.ts for how the chain
* consumes it). Geometry ADAPTATION is deliberately NOT here — the strip
* resolution chain lives in titlebar-strip.ts (standard WCO first, see
* wco.spec.ts), keeping this module a pure environment reporter.
*/
import { beforeEach, describe, expect, it } from 'vitest'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import './browser-globals.ts'
import { parseDesktopEnv, resetDesktopEnvForTests } from '../src/client/desktop-env.ts'
import { parseDesktopEnv, probeDesktopWindow, resetDesktopEnvForTests } from '../src/client/desktop-env.ts'
import type { Context, SidebarDesktopWindowService } from '../src/context-types.ts'
function setSearch(search: string): void {
;(window.location as { search: string }).search = search
@@ -50,6 +53,21 @@ describe('parseDesktopEnv', () => {
expect(env.titlebarInset).toBe(0)
})
it('parses extended mode — the shell-owned 36px frame with compatibility geometry', () => {
// Extended window (DSH Desktop 2.x): the shell hosts the upstream
// surfaces below its OWN frame, exactly like compatibility — the mode is
// not an unknown value, and must not be read as `advanced` (whose preset
// strip would reserve the caption row a second time).
setSearch('?dsh-desktop-mode=extended&dsh-desktop-platform=darwin&dsh-desktop-titlebar-inset=36')
const env = parseDesktopEnv()
expect(env.desktop).toBe(true)
expect(env.mode).toBe('extended')
expect(env.platform).toBe('darwin')
// The stamp is still REPORTED verbatim: it is the shell's own reserve
// (the strip chain ignores it only because the window service exists).
expect(env.titlebarInset).toBe(36)
})
it('detects the desktop preload marker even without URL stamps', () => {
;(window as unknown as Record<string, unknown>).__DSH_DESKTOP_FILE_PATH__ = { getPathForFile: () => '' }
const env = parseDesktopEnv()
@@ -87,3 +105,41 @@ describe('parseDesktopEnv', () => {
expect(parseDesktopEnv().desktop).toBe(false)
})
})
describe('probeDesktopWindow', () => {
const service: SidebarDesktopWindowService = {
mode: 'extended',
platform: 'win32',
material: 'off',
safeAreaInsets: { top: 0, right: 0, bottom: 0, left: 0 },
dragRegion: { height: 0, leftInset: 0, rightInset: 0 },
}
/** A client context whose `get` answers one service name. */
const ctxWith = (name: string, value: unknown): Context => ({
get: (asked: string): unknown => asked === name ? value : undefined,
}) as unknown as Context
it('returns the shell service the client context publishes', () => {
expect(probeDesktopWindow(ctxWith('desktopWindow', service))).toBe(service)
})
it('returns undefined on a plain browser page (the service is never injected)', () => {
expect(probeDesktopWindow(ctxWith('desktopWindow', undefined))).toBeUndefined()
expect(probeDesktopWindow({} as Context)).toBeUndefined()
})
it('stays best-effort when the context getter throws', () => {
const ctx: Context = {
get: (): never => { throw new Error('cannot get property without inject') },
} as unknown as Context
const logged = vi.spyOn(console, 'error').mockImplementation(() => {})
try {
expect(probeDesktopWindow(ctx)).toBeUndefined()
// Assert inside the spy's lifetime: mockRestore also clears the calls.
expect(logged).toHaveBeenCalledTimes(1)
} finally {
logged.mockRestore()
}
})
})
+139
View File
@@ -0,0 +1,139 @@
/**
* The desktop shell's window contract must reach the DOM, not just the pure
* chain (issue #430).
*
* `computeTitleBarStrip` is covered field by field in titlebar-strip.spec.ts;
* what this file adds is the WIRING, which is where the regression actually
* lived: the shell places the whole page below its own 36px frame and
* publishes `safeAreaInsets.top = 0` through `ctx.desktopWindow`, yet still
* stamps the render URL with `dsh-desktop-titlebar-inset=36`. If the Sidebar
* never hands the service to the chain, that stamp wins and every panel gets
* a second 36px blank strip at the top (`body[data-dsh-title-bar-compat]` +
* `--dsh-title-bar-strip: 36px`).
*
* Rendered with the REAL Sidebar + real store/service against a minimal fake
* context (the repo's jsdom mount pattern), so the assertion is on the
* document state the CSS actually consumes.
*/
// @vitest-environment jsdom
import { afterEach, describe, expect, it, vi } from 'vitest'
import { createElement } from 'react'
import { createRoot, type Root } from 'react-dom/client'
import { act } from 'react-dom/test-utils'
import { setupReactAct } from './test-utils.ts'
setupReactAct()
import { Sidebar } from '../src/client/Sidebar.tsx'
import { createSidebarStore } from '../src/client/state.ts'
import { createBetterSidebarService } from '../src/client/service.ts'
import { resetDesktopEnvForTests } from '../src/client/desktop-env.ts'
import type { Context, SidebarDesktopWindowService } from '../src/context-types.ts'
const shell = (mode: SidebarDesktopWindowService['mode'], top: number): SidebarDesktopWindowService => ({
mode,
platform: 'win32',
material: 'off',
safeAreaInsets: { top, right: 0, bottom: 0, left: 0 },
dragRegion: { height: top, leftInset: 0, rightInset: 0 },
})
/**
* Mount the real sidebar on a desktop-shell URL.
* @param search - the render URL's query (the shell's own stamps).
* @param desktopWindow - the service `ctx.get('desktopWindow')` answers, or
* undefined for a shell that does not publish one.
*/
function mount(search: string, desktopWindow: SidebarDesktopWindowService | undefined): { unmount: () => void } {
window.history.replaceState({}, '', `/${search}`)
resetDesktopEnvForTests()
// jsdom has no WebSocket; the panel's host feeds construct one on mount.
vi.stubGlobal('WebSocket', class { close(): void {} })
const store = createSidebarStore()
store.setSession('s1')
const service = createBetterSidebarService(store)
// useSyncExternalStore requires STABLE snapshots across calls (the real DSH
// services return stable objects) — a fresh object per call loops forever.
const locale = { active: 'en' }
const sessions = { byId: { s1: { id: 's1', cwd: '/ws' } } }
const ctx = {
locale: { subscribe: () => () => {}, getSnapshot: () => locale },
sessions: { list: { subscribe: () => () => {}, getSnapshot: () => sessions } },
get: (name: string) => name === 'betterSidebar' ? service : name === 'desktopWindow' ? desktopWindow : undefined,
}
const container = document.createElement('div')
document.body.append(container)
const root: Root = createRoot(container)
act(() => { root.render(createElement(Sidebar, { ctx: ctx as unknown as Context, store })) })
return {
unmount: () => {
act(() => { root.unmount() })
container.remove()
},
}
}
/** The strip the plugin applied to the page, as the CSS reads it. */
function appliedStrip(): { compat: boolean; px: string } {
return {
compat: document.body.hasAttribute('data-dsh-title-bar-compat'),
px: document.documentElement.style.getPropertyValue('--dsh-title-bar-strip'),
}
}
afterEach(() => {
document.body.innerHTML = ''
document.body.removeAttribute('data-dsh-title-bar-compat')
document.documentElement.style.removeProperty('--dsh-title-bar-strip')
vi.unstubAllGlobals()
resetDesktopEnvForTests()
})
describe('the shell window contract drives the applied strip', () => {
it('compatibility + URL inset 36 + contract top 0 → no strip at all', () => {
const view = mount(
'?dsh-desktop-mode=compatibility&dsh-desktop-platform=win32&dsh-desktop-titlebar-inset=36',
shell('compatibility', 0),
)
try {
expect(appliedStrip()).toEqual({ compat: false, px: '' })
} finally {
view.unmount()
}
})
it('extended + URL inset 36 + contract top 0 → no strip at all', () => {
const view = mount(
'?dsh-desktop-mode=extended&dsh-desktop-platform=darwin&dsh-desktop-titlebar-inset=36',
shell('extended', 0),
)
try {
expect(appliedStrip()).toEqual({ compat: false, px: '' })
} finally {
view.unmount()
}
})
it('advanced + contract top 32 → the caption row is yielded', () => {
const view = mount(
'?dsh-desktop-mode=advanced&dsh-desktop-platform=win32',
shell('advanced', 32),
)
try {
expect(appliedStrip()).toEqual({ compat: true, px: '32px' })
} finally {
view.unmount()
}
})
it('no service (plain browser / older shell) → the URL stamp chain is unchanged', () => {
const view = mount(
'?dsh-desktop-mode=compatibility&dsh-desktop-platform=win32&dsh-desktop-titlebar-inset=36',
undefined,
)
try {
expect(appliedStrip()).toEqual({ compat: true, px: '36px' })
} finally {
view.unmount()
}
})
})
+14 -2
View File
@@ -2,8 +2,8 @@
* Built-in shell-preset data tests: registry integrity (unique ids, non-empty
* titles, non-empty localized descs, pure strip/detect functions) and the
* anywhere-labs DSH Desktop entry's strip values (darwin advanced 20px,
* win32 advanced 32px as the no-WCO fallback, compatibility/plain browser
* nothing).
* win32 advanced 32px as the no-WCO fallback, compatibility / extended /
* plain browser nothing).
*/
import { describe, expect, it } from 'vitest'
import { getShellPreset, getShellPresets, presetStripFor } from '../src/client/shell-presets.ts'
@@ -65,6 +65,18 @@ describe('shell presets', () => {
expect(presetStripFor(preset, env({ desktop: false, mode: null, platform: null }))).toBeUndefined()
})
it('reserves nothing in EXTENDED mode either — the caption row is advanced-only', () => {
// Extended is a third presentation mode (DSH Desktop 2.x): the shell
// hosts the surfaces below its own 36px frame, exactly like
// compatibility, and reports a zero content inset through
// `ctx.desktopWindow`. Inheriting advanced's caption-row values here
// would be the very double reservation issue #430 is about.
expect(presetStripFor(preset, env({ desktop: true, mode: 'extended', platform: 'darwin' }))).toBeUndefined()
expect(presetStripFor(preset, env({ desktop: true, mode: 'extended', platform: 'win32' }))).toBeUndefined()
// ... and the settings badge must not SUGGEST the preset there either.
expect(preset.detect?.(env({ desktop: true, mode: 'extended', platform: 'win32' }))).toBe(false)
})
it('detects advanced shells only as a SUGGESTION signal (never auto-applies)', () => {
expect(preset.detect?.(env({ desktop: true, mode: 'advanced', platform: 'darwin' }))).toBe(true)
expect(preset.detect?.(env({ desktop: true, mode: 'advanced', platform: 'win32' }))).toBe(true)
+72 -4
View File
@@ -1,14 +1,17 @@
/**
* The title-bar strip resolution chain: standard WCO geometry first
* (authoritative even when 0), then the documented URL inset contract
* parameter, then the opt-in preset / custom scheme — and 0 (plain web,
* nothing modified) otherwise. No per-shell branch lives in the core.
* The title-bar strip resolution chain: the desktop shell's own window
* contract first (present → its `safeAreaInsets.top` is authoritative),
* then standard WCO geometry (authoritative even when 0), then the
* documented URL inset contract parameter, then the opt-in preset / custom
* scheme — and 0 (plain web, nothing modified) otherwise. No per-shell
* branch lives in the core.
*/
import { describe, expect, it } from 'vitest'
import { computeTitleBarStrip } from '../src/client/titlebar-strip.ts'
import type { DesktopEnv } from '../src/client/desktop-env.ts'
import type { WcoSnapshot } from '../src/client/wco.ts'
import type { ShellPreset } from '../src/client/shell-presets.ts'
import type { SidebarDesktopWindowService } from '../src/context-types.ts'
const env = (partial: Partial<DesktopEnv>): DesktopEnv => ({
desktop: false,
@@ -22,6 +25,18 @@ const wco = (present: boolean, height = 0): WcoSnapshot => ({ present, height })
const noPreset: ShellPreset | undefined = undefined
/** One desktop-shell window contract snapshot (only the read field varies). */
const shell = (
mode: SidebarDesktopWindowService['mode'],
top: number,
): SidebarDesktopWindowService => ({
mode,
platform: mode === 'advanced' ? 'win32' : 'darwin',
material: 'off',
safeAreaInsets: { top, right: 0, bottom: 0, left: 0 },
dragRegion: { height: top, leftInset: 0, rightInset: 0 },
})
describe('computeTitleBarStrip', () => {
it('keeps plain web untouched: no WCO, no inset, no scheme → 0', () => {
expect(computeTitleBarStrip(env({}), wco(false), 'auto', noPreset, 40)).toBe(0)
@@ -37,6 +52,8 @@ describe('computeTitleBarStrip', () => {
expect(computeTitleBarStrip(desktop, wco(false), 'web', {
id: 't', title: 't', desc: '', stripFor: () => 20,
}, 40)).toBe(0)
// ... and the shell contract does not override that explicit choice.
expect(computeTitleBarStrip(desktop, wco(false), 'web', noPreset, 40, shell('advanced', 32))).toBe(0)
})
it('trusts the standard WCO geometry first in every scheme (even 0 — e.g. maximized)', () => {
@@ -72,4 +89,55 @@ describe('computeTitleBarStrip', () => {
expect(computeTitleBarStrip(env({}), wco(false), 'auto', noPreset, 56)).toBe(0)
expect(computeTitleBarStrip(env({}), wco(false), 'preset', noPreset, 56)).toBe(0)
})
// The desktop shell's own window contract (`ctx.desktopWindow`) — issue
// #430. The shell reserves its title bar by placing the WHOLE page below it
// (compatibility / extended) yet still publishes the
// `dsh-desktop-titlebar-inset=36` URL stamp describing that same reserve;
// reading the stamp as "pixels the page must additionally yield" left a
// second 36px blank strip at the top of every panel. `safeAreaInsets.top`
// is that reserve ALREADY APPLIED, so it outranks the stamp.
describe('the shell window contract (ctx.desktopWindow)', () => {
it('(a) compatibility / extended report a zero inset → nothing is reserved', () => {
const stamped = env({ desktop: true, mode: 'compatibility', platform: 'win32', titlebarInset: 36 })
expect(computeTitleBarStrip(stamped, wco(false), 'auto', noPreset, 0, shell('compatibility', 0))).toBe(0)
const extended = env({ desktop: true, mode: 'extended', platform: 'win32', titlebarInset: 36 })
expect(computeTitleBarStrip(extended, wco(false), 'auto', noPreset, 0, shell('extended', 0))).toBe(0)
})
it('(b) advanced reports the caption row it draws INTO the content → that height', () => {
const advanced = env({ desktop: true, mode: 'advanced', platform: 'win32' })
expect(computeTitleBarStrip(advanced, wco(false), 'auto', noPreset, 0, shell('advanced', 32))).toBe(32)
// The value is the SHELL's, not a constant: macOS advanced reports 20.
expect(computeTitleBarStrip(advanced, wco(false), 'auto', noPreset, 0, shell('advanced', 20))).toBe(20)
})
it('(c) without the service the existing chain is untouched (older shells, plain pages)', () => {
const stamped = env({ desktop: true, mode: 'compatibility', platform: 'win32', titlebarInset: 36 })
expect(computeTitleBarStrip(stamped, wco(false), 'auto', noPreset, 0)).toBe(36)
expect(computeTitleBarStrip(stamped, wco(false), 'preset', noPreset, 0)).toBe(36)
})
it('(d) with the service the URL stamp is IGNORED, whatever it says', () => {
const stamped = env({ desktop: true, mode: 'extended', platform: 'darwin', titlebarInset: 36 })
expect(computeTitleBarStrip(stamped, wco(false), 'auto', noPreset, 40, shell('extended', 0))).toBe(0)
// ... including the custom scheme's manual px, which the shell's real
// geometry outranks exactly like WCO always did.
expect(computeTitleBarStrip(stamped, wco(false), 'custom', noPreset, 40, shell('extended', 0))).toBe(0)
// Even a present WCO overlay does not outrank the shell's own contract.
expect(computeTitleBarStrip(stamped, wco(true, 36), 'auto', noPreset, 0, shell('extended', 0))).toBe(0)
})
it('falls through to the standard chain for a service without a usable top', () => {
const stamped = env({ desktop: true, mode: 'compatibility', platform: 'win32', titlebarInset: 36 })
const broken = {
...shell('compatibility', 0),
safeAreaInsets: undefined,
} as unknown as SidebarDesktopWindowService
expect(computeTitleBarStrip(stamped, wco(false), 'auto', noPreset, 0, broken)).toBe(36)
const nan = { ...shell('compatibility', 0), safeAreaInsets: { top: Number.NaN, right: 0, bottom: 0, left: 0 } }
expect(computeTitleBarStrip(stamped, wco(true, 24), 'auto', noPreset, 0, nan)).toBe(24)
})
})
})