feat(sidebar): 桌面兼容三方案——自动检测(WCO 标准几何)/壳预设/自定义 CSS (#284)
* feat(sidebar): 桌面兼容三方案——自动检测(WCO 标准几何)/壳预设/自定义 CSS 核心只保留 Web 标准机制,壳专属适配全部下沉到用户空间(KISS): - prefs 新增 titleBarScheme(默认 auto)/titleBarPresetId/customCss,旧 titleBarCompat 布尔自动迁移(true→custom 保留 stripPx,false/缺失→auto) - wco.ts:navigator.windowControlsOverlay 反应式几何(geometrychange 实时更新; visible=false 的幽灵 API 视为缺失),实现 #257 - titlebar-strip.ts 唯一 strip 取值链:WCO → dsh-desktop-titlebar-inset 契约参数 → 预设 stripFor → 手动 px → 0;desktop-env.ts 移除 win32 32px 硬编码 - shell-presets.ts 数据驱动预设(准入:issue/PR 提及 + 100+ star;v1 仅 anywhere-labs DSH Desktop:darwin 20px / win32 无 WCO 时 32px),opt-in 手动 - 预设/自定义 CSS 注入(data-dsh-preset-css / data-dsh-custom-css 标签), chrome 补稳定寻址面 data-dsh-toggle-cluster / data-dsh-panel - 交互 chrome 统一 -webkit-app-region: no-drag(吸收 #111/#153) - layout.css 底栏推挤复合锚点([data-pane] 与 :has(> [data-slot]) 双保险, rc.8 实测前者失效、后者命中,吸收 #226 思路) - 设置 UI:位置兼容模式齿轮弹窗改三方案(自动检测/壳兼容方案/自定义方案) - 测试:wco/titlebar-strip/shell-presets 单测 + prefs 迁移 + e2e 5 场景 (保守 auto/WCO 反应式/预设/自定义 CSS/复合锚点),挂载门禁 12/12 全绿 核心 grep 无壳专属类名;为某壳做的兼容在另一壳会再坏——预设与自定义 CSS 是唯一出路(AGENTS.md §8 契约已更新)。 * refine(sidebar): 位置兼容模式改主行下拉四选项——自动检测/DSH官方Web/壳预设/自定义 按用户反馈定稿 UI: - 主行下拉:自动检测(默认)/ DSH官方Web(显式零适配,连 WCO 也强制 0)/ 各壳预设(命中环境带「已检测」后缀,仅提示)/ 自定义方案(保留齿轮弹窗: 下移距离 + 自定义 CSS) - 旧文档迁移:titleBarCompat=true 或 titleBarStripPx 非默认值(此前已有配置) → 默认落到 custom 方案保留用户数值;干净文档 → auto - prefs 新增 'web' 方案;strip 取值链最优先处理 web→0 - 测试:prefs 迁移(stripPx 48→custom / 40→auto)、web 强制 0、下拉渲染断言 - 单测 693 通过,挂载 e2e 12/12 全绿 * refine(sidebar): 方案下拉复用 SelectMenu 组件,不用原生 <select> - 抽出可复用 SelectMenu(primitives Menu:锚点按钮 + 下拉选项卡,支持 icon/desc/多选),SelectRow 改为其薄壳 - 位置兼容模式方案下拉改用 SelectMenu;选项带描述(自动检测/DSH官方Web/ 各壳预设(命中带「已检测」后缀)/自定义方案) - 移除原生 select 样式;单测 693 通过,挂载 e2e 12/12 全绿
This commit is contained in:
+38
-24
@@ -1,21 +1,18 @@
|
||||
/**
|
||||
* Desktop-shell detection for the sidebar. The official DSH Desktop shell
|
||||
* (Electron) stamps every render URL with `dsh-desktop-mode` and
|
||||
* `dsh-desktop-platform` and exposes `window.__DSH_DESKTOP_FILE_PATH__`
|
||||
* through its preload; community Tauri shells keep the native system frame
|
||||
* (no stamps — plain-browser semantics). Parsed once per page and memoized
|
||||
* (the URL never changes mid-session); `resetDesktopEnvForTests` clears the
|
||||
* memo for unit tests.
|
||||
* Desktop-shell detection for the sidebar. Shells may stamp the render URL
|
||||
* with `dsh-desktop-mode` / `dsh-desktop-platform` (the official Electron
|
||||
* shell does) and expose `window.__DSH_DESKTOP_FILE_PATH__` through a
|
||||
* preload. Parsed once per page and memoized (the URL never changes
|
||||
* mid-session); `resetDesktopEnvForTests` clears the memo for unit tests.
|
||||
*
|
||||
* Geometry facts the sidebar adapts to (from the Electron runtime):
|
||||
* - win32 `advanced` mode draws the window controls in a 32px overlay strip
|
||||
* at the top-right of the web content (`titleBarOverlay: { height: 32 }`)
|
||||
* — exactly where the toggle cluster sits, so it must drop below the
|
||||
* strip and the right panel's content must clear it.
|
||||
* - darwin `advanced` mode keeps the traffic lights at (16,16) in the
|
||||
* top-left; nothing on the plugin's side touches that corner today, but
|
||||
* the platform is still reported so left-edge controls can avoid it.
|
||||
* - `compatibility` mode keeps the native frame — no adaptation needed.
|
||||
* GEOMETRY POLICY: this module only REPORTS shell facts — it never decides
|
||||
* how to adapt. The strip height comes from standard signals first (the
|
||||
* Window Controls Overlay API, see wco.ts), then the documented contract
|
||||
* parameter `dsh-desktop-titlebar-inset` (a shell may stamp the real pixels
|
||||
* it reserves at the top), then the user's chosen scheme (preset / custom).
|
||||
* The legacy win32-advanced 32px constant is gone from the core: it lives
|
||||
* in the opt-in shell preset (shell-presets.ts) as a fallback for shells
|
||||
* without the WCO API.
|
||||
*/
|
||||
export interface DesktopEnv {
|
||||
/** Running inside a desktop shell (any URL stamp or preload marker). */
|
||||
@@ -24,34 +21,51 @@ export interface DesktopEnv {
|
||||
readonly mode: 'compatibility' | 'advanced' | null
|
||||
/** Shell platform stamp ('darwin' | 'win32' | …), lowercased, or null. */
|
||||
readonly platform: string | null
|
||||
/** Pixels the win32 advanced shell reserves at the top-right for window
|
||||
* controls (0 elsewhere). */
|
||||
readonly win32OverlayTop: number
|
||||
/**
|
||||
* Contract parameter `dsh-desktop-titlebar-inset`: pixels the shell
|
||||
* reserves at the top of the web content for its own chrome (0–120,
|
||||
* clamped; 0 when absent). Standard WCO geometry takes precedence over
|
||||
* this whenever the API is available.
|
||||
*/
|
||||
readonly titlebarInset: number
|
||||
}
|
||||
|
||||
let cached: DesktopEnv | undefined
|
||||
|
||||
/** Read the shell's desktop stamps (memoized per page). */
|
||||
/** Read the shell's desktop stamps (memoized per page; SSR-safe). */
|
||||
export function parseDesktopEnv(): DesktopEnv {
|
||||
if (cached !== undefined) return cached
|
||||
const hasWindow = typeof window !== 'undefined'
|
||||
const hasPreloadMarker = hasWindow
|
||||
&& typeof (window as { __DSH_DESKTOP_FILE_PATH__?: unknown }).__DSH_DESKTOP_FILE_PATH__ !== 'undefined'
|
||||
// location.search includes the leading '?', which URLSearchParams does NOT
|
||||
// strip (it would become part of the first key) — drop it explicitly.
|
||||
const params = new URLSearchParams(window.location.search.replace(/^\?/, ''))
|
||||
// SSR (no window) resolves the empty environment: nothing desktop.
|
||||
const params = hasWindow
|
||||
? new URLSearchParams(window.location.search.replace(/^\?/, ''))
|
||||
: new URLSearchParams()
|
||||
const modeParam = params.get('dsh-desktop-mode')
|
||||
const mode = modeParam === 'compatibility' || modeParam === 'advanced' ? modeParam : null
|
||||
const platformParam = params.get('dsh-desktop-platform')
|
||||
const platform = platformParam !== null && platformParam !== '' ? platformParam.toLowerCase() : null
|
||||
const desktop = mode !== null
|
||||
|| typeof (window as { __DSH_DESKTOP_FILE_PATH__?: unknown }).__DSH_DESKTOP_FILE_PATH__ !== 'undefined'
|
||||
const desktop = mode !== null || hasPreloadMarker
|
||||
cached = {
|
||||
desktop,
|
||||
mode,
|
||||
platform,
|
||||
win32OverlayTop: desktop && mode === 'advanced' && platform === 'win32' ? 32 : 0,
|
||||
titlebarInset: parseTitlebarInset(params.get('dsh-desktop-titlebar-inset')),
|
||||
}
|
||||
return cached
|
||||
}
|
||||
|
||||
/** Clamp the contract inset parameter into 0–120 (invalid/absent → 0). */
|
||||
function parseTitlebarInset(raw: string | null): number {
|
||||
if (raw === null) return 0
|
||||
const parsed = Number(raw)
|
||||
if (!Number.isFinite(parsed)) return 0
|
||||
return Math.min(120, Math.max(0, Math.round(parsed)))
|
||||
}
|
||||
|
||||
/** Test hook: drop the memo so the next parse re-reads the URL/globals. */
|
||||
export function resetDesktopEnvForTests(): void {
|
||||
cached = undefined
|
||||
|
||||
Reference in New Issue
Block a user