fix(floating): 修正控件带通知与无带子时的布局偏差

viewport-inset 的快照原本是 { top } 对象,重读时每次新建,引用比较永远不等:
带子存在时每次 resize / geometrychange 都会广播,三个浮层各重渲染一次。改为
直接存数字,值不变就不通知。

AnchoredPopover 新增的让位原本无条件执行,普通浏览器里锚点贴到视口最顶时
弹层会从 6px 变 8px,与「无带子时逐字等价」的承诺不符。改为只在带子存在时
才抬高。

顺带删除 mermaid 工具栏里恒被内联样式覆盖的 CSS top(不跑 JS 时该弹层不存在,
不是降级),并补上测试:同值不广播、resize 兜底、visible 运行时翻转、浮窗
高度让位、AnchoredPopover 三个位置用例。

文档:接入指南的 WCO 例外改标「尚未发布」(v0.29.0 从未发布到 npm),计划
文档修正 #430 对照表与测试清单。
This commit is contained in:
unsc9 committed 2026-11-05 13:16:39 +08:00
1 parent 353a234cf4
commit a9c2ff0fce
10 files changed
+164 -57

No files matched your search

+1 -1
View File
@@ -8,7 +8,7 @@
- 🧪 测试:`tests/settings-nav-icon.spec.tsx` 钉住图标按宿主给的 `size` 缩放、只用 `currentColor`;真实宿主上用无头浏览器确认导航行只剩一个 `svg`、无残留标记。
- 🪟 **浮层不再把自己的标题栏摆进桌面壳的原生窗口控件带**([#805](https://github.com/omdsh-dev/DSH-better-sidebar/issues/805) 同根因):Windows 桌面壳用 `titleBarOverlay` 在视口顶部画原生「最小化/最大化/关闭」,那条带子里的指针事件到不了网页。任务详情浮窗(`FloatingWindow`)的最小顶部距离写死 8px、自身标题栏 34px,× 正好落在带子里点不动;节点详情弹层(`AnchoredPopover`)与 mermaid 放大图工具栏同理。新增 `src/client/viewport-inset.ts` 读标准 `navigator.windowControlsOverlay`(`visible` + `getTitlebarAreaRect()`,订阅 `geometrychange`,`resize` 兜底)求出带高,三处消费者把可交互区压在带子下方;拖拽/重排的纵向下限同步,带子出现时把已摆好的浮层抬起、消失时不主动下拉(用户摆的位置不被最大化/还原弹走)。**只读探测、零配置**:不写 CSS 变量、不写 DOM 属性、不加偏好键、不复活 #430 删掉的 `--dsh-title-bar-strip`;API 缺失 / `visible:false` / 读取抛错一律退化为 0,普通浏览器与网页版的位置与今天逐字一致。
- 🧪 测试:新增 `tests/viewport-inset.spec.ts`(9 例:API 缺失 / 高度 0 / `visible:false` / 抛错 / 小数取整 / `y+height` 取值 / `geometrychange` 增删 / 换源清理监听);`tests/floating-window.spec.tsx` 新增 5 例(锚点在带内抬起 / 拖拽不越界 / 带子出现抬起 / 带子消失不下拉 / 幽灵接口保持原 8px);挂载 lane 注入假 WCO 断言放大图工具栏 16→52→16 的真实布局。
- 🧪 测试:新增 `tests/viewport-inset.spec.ts`(11 例:API 缺失 / 读到带高 / 高度 0 / `visible:false` / 抛错 / 小数取整 / `y+height` 取值 / 只在值真变时通知 / `resize` 兜底 / `visible` 运行时翻转 / 换源清理监听);`tests/floating-window.spec.tsx` 新增 6 例(锚点在带内抬起 / 拖拽不越界 / 带子出现抬起 / 带子消失不下拉 / 高度上限让出带子 / 幽灵接口保持原 8px);新增 `tests/anchored-popover.spec.tsx`(3 例:带内抬起 / 无带子保持原位 / 视口最顶保持历史位置);挂载 lane 注入假 WCO 断言放大图工具栏 16→52→16 的真实布局。
### v0.30.0
+1 -1
View File
@@ -10,7 +10,7 @@
- 🧪 `tests/settings-nav-icon.spec.tsx` pins the glyph to the slot-provided `size` and `currentColor`; a headless pass against a real host confirms one `svg` in the row and no leftover marker.
- 🪟 **Floating layers no longer place their own title bar under the desktop shell's native window-control band** (same root cause as [#805](https://github.com/omdsh-dev/DSH-better-sidebar/issues/805)): on Windows the shell paints its native minimize/maximize/close buttons over the top band of the viewport with `titleBarOverlay`, and pointer events in that band never reach the page. The task-detail window (`FloatingWindow`) had a hard-coded 8px top floor with a 34px title bar, so its × landed inside the band and could not be clicked; the node-detail popover (`AnchoredPopover`) and the mermaid zoom toolbar had the same problem. New `src/client/viewport-inset.ts` reads the standard `navigator.windowControlsOverlay` (`visible` + `getTitlebarAreaRect()`, `geometrychange` with a `resize` fallback) and all three consumers keep their interactive area below the band; vertical drag/re-layout floors follow it, an appearing band lifts an already-placed layer, and a disappearing band never pulls it back up (maximize/restore does not move what the reader placed). **Read-only probe, zero configuration**: no CSS variable, no DOM attribute, no preference key, no revival of the `--dsh-title-bar-strip` mechanism removed in #430; a missing API, `visible:false`, or a throwing read degrade to 0, so plain browsers and the web build behave exactly as before.
- 🧪 Tests: new `tests/viewport-inset.spec.ts` (9 cases); 5 new cases in `tests/floating-window.spec.tsx`; the mount lane injects a fake WCO and asserts the zoom toolbar's real layout moves 16→52→16.
- 🧪 Tests: new `tests/viewport-inset.spec.ts` (11 cases); 6 new cases in `tests/floating-window.spec.tsx`; new `tests/anchored-popover.spec.tsx` (3 cases); the mount lane injects a fake WCO and asserts the zoom toolbar's real layout moves 16→52→16.
### v0.30.0
+1 -1
View File
@@ -1248,7 +1248,7 @@ interface SettingsDescriptor {
- **用户空间 CSS(v0.14.1+;titleBar 让位机制移除后只剩它)**:`SidebarPrefs.customCss`(设置页「自定义 CSS」分组)注入 `<style data-dsh-custom-css="custom">` 到 head 末尾(后写胜出;覆盖 JS 内联需 `!important`),fiber 卸载即移除,**非空即生效**(不再有方案门控)。稳定寻址面:`[data-dsh-toggle-cluster]` / `[data-dsh-panel]` / `[data-dsh-bottom-panel]`。
- **已移除:位置兼容模式 / titleBar 让位机制**(v0.14.1 引入,本次整体删除,见 [docs/plans/2026-10-05-remove-titlebar-compat-strip.md](plans/2026-10-05-remove-titlebar-compat-strip.md))。历史模型是四方案 `titleBarScheme`(`auto` / `web` / 壳预设 / `custom`),由 `src/client/titlebar-strip.ts` 的纯函数按「壳自带 `ctx.desktopWindow` 契约 → `navigator.windowControlsOverlay` 真实几何 → URL `dsh-desktop-titlebar-inset` → 壳预设 → 手动 px」求出顶栏让位高度,写 `body[data-dsh-title-bar-compat]` + `--dsh-title-bar-strip`。删除原因:插件自 v0.19.0 起不再自绘顶部 chrome,**CSS 里没有任何规则读这两个值**,宿主也不读这条契约(用户想移动的侧边栏按钮是宿主原生控件,插件没有它的位置 API),于是改设置只写变量、什么都不动;而设置项描述与现实不符。
- **对消费插件的影响**:不要引用 `SidebarPrefs.titleBarScheme` / `titleBarPresetId` / `titleBarCompat` / `titleBarStripPx`(`toggles` 里声明它们会被当成未知键),也不要写 `--dsh-title-bar-strip` / `body[data-dsh-title-bar-compat]`(无人消费)。桌面壳仍会戳 URL(`dsh-desktop-mode` / `-platform` / `-titlebar-inset`)并暴露 `ctx.desktopWindow` / `navigator.windowControlsOverlay`;**URL 戳记与 `ctx.desktopWindow` 本插件仍不读**(注意后者是可选客户端服务,**绝不能写进 `dsh.client.inject`**——普通浏览器没有它,注入会让插件永远 pending)。
- **例外:标准 WCO 几何(v0.29.0+,见 [docs/plans/2026-11-04-floating-window-native-titlebar-band.md](plans/2026-11-04-floating-window-native-titlebar-band.md))**:`src/client/viewport-inset.ts` 读 `navigator.windowControlsOverlay`(`visible` + `getTitlebarAreaRect()`,并订阅 `geometrychange`)求出壳的原生窗口控件带高度,供 `FloatingWindow` / `AnchoredPopover` / mermaid 放大图工具栏把自己的可交互标题区压在带子下方——带子里的指针事件到不了网页,标题栏落进去就读作「点不动」(issue #805,任务详情浮窗的 × 是同一根因)。这是**只读探测、零配置**:不写 CSS 变量、不写 DOM 属性、不进 `PrefsSchema`、不做预设检测;API 缺失 / `visible:false` / 读取抛错一律退化成 0,于是普通浏览器行为与加这条之前逐字一致。
- **例外:标准 WCO 几何(尚未发布,见 [docs/plans/2026-11-04-floating-window-native-titlebar-band.md](plans/2026-11-04-floating-window-native-titlebar-band.md))**:`src/client/viewport-inset.ts` 读 `navigator.windowControlsOverlay`(`visible` + `getTitlebarAreaRect()`,并订阅 `geometrychange`)求出壳的原生窗口控件带高度,供 `FloatingWindow` / `AnchoredPopover` / mermaid 放大图工具栏把自己的可交互标题区压在带子下方——带子里的指针事件到不了网页,标题栏落进去就读作「点不动」(issue #805,任务详情浮窗的 × 是同一根因)。这是**只读探测、零配置**:不写 CSS 变量、不写 DOM 属性、不进 `PrefsSchema`、不做预设检测;API 缺失 / `visible:false` / 读取抛错一律退化成 0,于是普通浏览器行为与加这条之前逐字一致。
- 旧偏好数据安全:已删的四个键在宿主 schema 里是未知键(OPEN),解析不报错、不写回;客户端 `parsePrefs` 只按声明字段构造,它们不会进入 UI(`tests/prefs.spec.ts` / `tests/plugin-shape.spec.ts` 守护)。
- **拖拽区退出**:交互 chrome(`.toggleCluster` / `.toggleButton` / `.tabBar`)统一 `-webkit-app-region: no-drag`(无边框壳拖拽带吞点击,#103/#111)。
- **拖拽区退出(视口层)**:宿主把每个**直挂 body 的子元素**都设成 `no-drag`(`html[data-platform=darwin] body > :not(#root)`,选择器含 id,只能靠 `!important` 压过),而 app-region **无视 `pointer-events`**——插件宿主就是这样一个 body 子元素,在 Electron 里该值还会传到它内部的**铺满视口的面板层**(#772 的 CDP 实测;挂载 lane 用的浏览器引擎不传播该属性,所以面板层自己也声明了同一条规则),于是它把下面每条 `[data-window-drag]` 拖拽带一起抵消(窗口拖一次就失效,同时丢掉 macOS 双击标题栏缩放,issue #772)。契约:装饰性的视口层用**中性值** `-webkit-app-region: initial !important` 退出计算(`initial` 的计算值 `none` 不扣减拖拽区;注意**字面量 `none` 不是中性值**——它计算成 `no-drag`,是扣减值),层内的面板/控件再声明 `no-drag` 保住点击;当前覆盖 `[data-dsh-better-sidebar]`、`[data-dsh-panel-host]`(`> *` 保持 `no-drag`)与 `.mermaidModal`(放大视图是第二个**持久**铺满视口的 body 直挂层)。**交互弹层不要加 `initial`**(`.selectionPopup` 是 `<button>`,`FloatingWindow` / `AnchoredPopover` 自带指针拖拽、不是遮罩层):宿主 `:is(button, a, input, …)` 已给它们 `no-drag`,反过来声明 `initial !important` 会让按下变成拖窗(重演 #103/#111)。放大视图是**遮罩层例外**——它是模态,只有其中的控件必须保持 `no-drag`;代价是它覆盖在顶部拖拽带上时,点背景会变成拖窗而不是关闭(为「放大图时窗口仍可拖」付的账)。**未纳入**:`FileTree` 的 `.uploadDropChatHint` 同样是 body 直挂的装饰层,但它只在 OS 文件拖拽悬停期间存在且 `pointer-events: none`,收益极小、暂不处理(`FloatingWindow` / `AnchoredPopover` 是交互层,按上面的判据本就该保持 `no-drag`)。形状由 `tests/panel-host-css.spec.ts` 在 Linux 的 `pnpm test` 里守护(无 macOS runner;`tests/theme.spec.ts` 只守颜色,故本轮不涉及),真实级联由挂载 lane 的探针按宿主规则断言计算值(面板宿主探针复现宿主两条 darwin 规则,放大视图探针直接用页面里宿主自己的规则)。
@@ -26,7 +26,7 @@
| --- | --- |
| `FloatingWindow.tsx` | `initialBox` 有/无锚点两条路径都用 `topFloor`;高度上限扣掉 `topFloor`;拖拽与 resize 的纵向夹取同步;几何变化时把已摆好的窗抬出带子,**不主动下拉** |
| `AnchoredPopover.tsx` | 布局 effect 补 `top = Math.max(topFloor, top)`(锚点自身在带子里时也要退让);拖拽纵向下限同步 |
| `mermaid.tsx` | 工具栏 `style={{ top: 16 + insetTop }}` + `data-mermaid-toolbar`(稳定寻址,供挂载 lane 量真实布局);CSS 的 `top: 16px` 保留为无 JS 时的降级 |
| `mermaid.tsx` | 工具栏 `style={{ top: 16 + insetTop }}` + `data-mermaid-toolbar`(稳定寻址,供挂载 lane 量真实布局) |
`insetTop === 0` 时三者与改动前逐字等价。
@@ -40,9 +40,9 @@
| 配置 | 4 个偏好键 + 设置界面 + 壳预设表 | 零配置、零设置项 |
| 输出 | 写 `body[data-dsh-title-bar-compat]` + `--dsh-title-bar-strip` | 不写任何全局值,只经 hook 返回一个数 |
| 消费者 | **零**(CSS 无人读、宿主不读) | 三个真实浮层(本次修复的直接对象) |
| 删掉后的症状 | 改设置什么都不动 | 浮层标题栏压在原生按钮下,点不动 |
| 删掉后的症状 | 无——顶部行为不变,删掉的只是一条没人读的写路径 | 浮层标题栏压在原生按钮下,点不动 |
共同点只有「都读 WCO 求顶部高度」;旧机制失败的原因是**没有消费者**,不是读取方式错。
共同点只有「都读 WCO 求顶部高度」;旧机制失败的原因是**没有消费者**,不是读取方式错——它还在位时的表现正是「改设置什么都不动」。
## 降级表
@@ -57,8 +57,9 @@
## 测试证据
- `tests/viewport-inset.spec.ts`(9 例):API 缺失 / 高度 0 / `visible:false` / 读取抛错不炸 / 小数四舍五入(35.6→36)/ `y:4,height:32`→36 / `geometrychange` 0→36→0 且订阅数正确增减 / 换源清理旧监听。
- `tests/floating-window.spec.tsx` 新增 5 例:锚点在带内时抬到 52(8+44)/ 拖到最上仍停 52 / 带子出现把已摆好的窗抬起 / 带子消失不下拉 / 幽灵接口保持原 8px。
- `tests/viewport-inset.spec.ts`(11 例):API 缺失 / 读到带高 36 / 高度 0 / `visible:false` / 读取抛错不炸 / 小数四舍五入(35.6→36)/ `y:4,height:32`→36 / 只在值真变时通知(同值重发不唤醒订阅者)/ `resize` 兜底重读 / `visible` 运行时翻转 / 换源清理旧监听。
- `tests/floating-window.spec.tsx` 新增 6 例:锚点在带内时抬到 52(8+44)/ 拖到最上仍停 52 / 带子出现把已摆好的窗抬起 / 带子消失不下拉 / 高度上限让出带子(400 高视口 + 44 带子 → 340)/ 幽灵接口保持原 8px。
- `tests/anchored-popover.spec.tsx`(3 例):锚点在带内时弹层落到 52 / 无带子时仍是锚点下方 26px / 视口最顶的锚点保持历史位置 6px(不因本次改动被抬到 8)。
- `tests/e2e/mount.e2e.ts`:`addInitScript` 注入假 WCO(初始 0,保证其余断言不受影响),在放大图流程里量真实布局——工具栏 `top` 16 → 设带高 36 → 52 → 归零 → 16。
## 风险
+2 -1
View File
@@ -80,7 +80,8 @@ export function AnchoredPopover(props: AnchoredPopoverProps): ReactNode {
}
// The anchor itself may sit in the band (a row near the top edge): opening
// just below it still leaves our own header under the native controls.
top = Math.max(topFloor, top)
// With no band, keep the historical placement byte-for-byte.
if (topFloor > VIEWPORT_MARGIN) top = Math.max(topFloor, top)
const left = Math.min(
Math.max(VIEWPORT_MARGIN, rect.left),
Math.max(VIEWPORT_MARGIN, window.innerWidth - cardWidth - VIEWPORT_MARGIN),
+2 -4
View File
@@ -2055,12 +2055,10 @@ body[data-dsh-sidebar-dragging] .bottomPanel {
backdrop-filter: blur(2px);
}
/* `top` is written inline by mermaid.tsx (it has to clear the shell's native
window-control band, #805). */
.mermaidModalToolbar {
position: absolute;
/* Fallback only: mermaid.tsx sets an inline `top` that also clears the
shell's native window-control band, which swallows pointer events (#805).
Kept here so the toolbar still lands somewhere sane if that never runs. */
top: 16px;
right: 16px;
display: flex;
gap: 8px;
+15 -29
View File
@@ -31,13 +31,6 @@ export interface WindowControlsOverlayLike {
removeEventListener(type: 'geometrychange', listener: () => void): void
}
/** The part of the viewport a floating surface must not cover. */
export interface ViewportInset {
readonly top: number
}
export const NO_VIEWPORT_INSET: ViewportInset = Object.freeze({ top: 0 })
function overlayOf(navigatorLike: Navigator | undefined): WindowControlsOverlayLike | undefined {
const candidate = (navigatorLike as
| (Navigator & { windowControlsOverlay?: WindowControlsOverlayLike })
@@ -54,23 +47,25 @@ function overlayOf(navigatorLike: Navigator | undefined): WindowControlsOverlayL
*
* Everything unusable collapses to 0 — no API, `visible: false` (headless
* Chromium and some macOS builds expose the interface with no overlay), a
* zero-height band (full screen), or a throwing call. 0 keeps plain browsers
* and web profiles exactly as they were.
* zero-height band, or a throwing call. 0 keeps plain browsers and web
* profiles exactly as they were.
*/
function computeInset(candidate: WindowControlsOverlayLike | undefined): ViewportInset {
if (candidate === undefined) return NO_VIEWPORT_INSET
function computeInset(candidate: WindowControlsOverlayLike | undefined): number {
if (candidate === undefined) return 0
try {
if (candidate.visible !== true) return NO_VIEWPORT_INSET
if (candidate.visible !== true) return 0
const rect = candidate.getTitlebarAreaRect()
const top = Math.round(rect.y + rect.height)
return Number.isFinite(top) && top > 0 ? { top } : NO_VIEWPORT_INSET
return Number.isFinite(top) && top > 0 ? top : 0
} catch {
return NO_VIEWPORT_INSET
return 0
}
}
let source: WindowControlsOverlayLike | undefined
let snapshot: ViewportInset = NO_VIEWPORT_INSET
// A number, not an object: the change check below is then a value comparison,
// so an unchanged band never wakes the consumers.
let snapshot = 0
const listeners = new Set<() => void>()
function emit(): void {
@@ -102,8 +97,8 @@ if (typeof window !== 'undefined') {
window.addEventListener('resize', reread)
}
/** The current inset. Stable reference until the geometry actually changes. */
export function getViewportInset(): ViewportInset {
/** The current inset in CSS pixels (0 when there is none). */
export function getViewportInset(): number {
return snapshot
}
@@ -119,20 +114,11 @@ export function setViewportInsetSourceForTests(
): void {
source?.removeEventListener('geometrychange', reread)
source = next
const computed = computeInset(next)
if (computed !== snapshot) {
snapshot = computed
emit()
}
reread()
next?.addEventListener('geometrychange', reread)
}
/** React consumer: the inset, re-rendering when the shell band changes. */
export function useViewportInset(): ViewportInset {
/** React consumer: the inset, re-rendering only when the shell band changes. */
export function useViewportInsetTop(): number {
return useSyncExternalStore(subscribeViewportInset, getViewportInset, getViewportInset)
}
/** Convenience for callers that only need the number. */
export function useViewportInsetTop(): number {
return useViewportInset().top
}
+82
View File
@@ -0,0 +1,82 @@
/**
* The anchored popover (AnchoredPopover.tsx): the card opens below its anchor
* but never inside the shell's native window-control band (issue #805's
* mechanism; the Tasks detail window is the same bug one component over).
*
* jsdom performs no layout, so the card measures 0 tall and its placement is
* read back from the inline style the component writes.
*/
// @vitest-environment jsdom
import { afterEach, describe, expect, it } from 'vitest'
import { createElement } from 'react'
import { renderRoot, setupReactAct } from './test-utils.ts'
import { AnchoredPopover } from '../src/client/AnchoredPopover.tsx'
import {
setViewportInsetSourceForTests,
type WindowControlsOverlayLike,
} from '../src/client/viewport-inset.ts'
setupReactAct()
afterEach(() => {
setViewportInsetSourceForTests(undefined)
for (const el of document.querySelectorAll('body > div')) el.remove()
})
function overlayBand(height: number): WindowControlsOverlayLike {
return {
visible: true,
getTitlebarAreaRect: () => ({ x: 0, y: 0, width: 1_000, height }),
addEventListener: () => {},
removeEventListener: () => {},
}
}
/** A real button in the DOM, positioned so its bottom edge sits at `bottom`. */
function anchorAt(bottom: number): HTMLElement {
const anchor = document.createElement('button')
document.body.append(anchor)
anchor.getBoundingClientRect = () => ({
left: 100, top: bottom - 20, right: 200, bottom, width: 100, height: 20,
x: 100, y: bottom - 20, toJSON: () => ({}),
}) as DOMRect
return anchor
}
function render(anchor: HTMLElement) {
return renderRoot(createElement(AnchoredPopover, {
anchor,
onClose: () => {},
children: createElement('div', null, 'node detail'),
}))
}
/** The card's inline top (jsdom has no layout). */
function cardTop(): number {
const card = document.querySelector('[role="dialog"]')
if (card === null) throw new Error('no popover rendered')
return Number.parseFloat((card as HTMLElement).style.top)
}
describe('AnchoredPopover and the native window-control band', () => {
it('lifts the card below the band when the anchor sits inside it', () => {
setViewportInsetSourceForTests(overlayBand(44))
const { unmount } = render(anchorAt(20))
// Without the band the card would open at 26, inside the native controls.
expect(cardTop()).toBe(52)
unmount()
})
it('keeps opening just below the anchor when there is no band', () => {
const { unmount } = render(anchorAt(20))
expect(cardTop()).toBe(26)
unmount()
})
it('keeps the historical placement for an anchor at the very top', () => {
// No band means no extra floor: 8 + rect.bottom, as before the band work.
const { unmount } = render(anchorAt(0))
expect(cardTop()).toBe(6)
unmount()
})
})
+14
View File
@@ -72,12 +72,14 @@ function renderWindow(props: {
bodyRef?: RefObject<HTMLDivElement>
footer?: boolean
actions?: boolean
initialSize?: { width: number; height: number }
} = {}) {
const rendered = renderRoot(createElement(FloatingWindow, {
title: 'bash-1',
onClose: () => { closed += 1 },
...(props.anchor === undefined ? {} : { anchor: props.anchor }),
...(props.bodyRef === undefined ? {} : { bodyRef: props.bodyRef }),
...(props.initialSize === undefined ? {} : { initialSize: props.initialSize }),
...(props.footer === true ? { footer: createElement('button', { type: 'button' }, 'stop') } : {}),
...(props.actions === true ? { actions: createElement('button', { type: 'button' }, 'copy') } : {}),
children: createElement('div', null, 'line 1'),
@@ -331,6 +333,18 @@ describe('FloatingWindow keeps its title bar out of the native control band', ()
unmount()
})
it('caps the height so a tall window still clears the band', () => {
Object.defineProperty(window, 'innerHeight', { configurable: true, value: 400 })
setViewportInsetSourceForTests(overlayBand(44).source)
const { unmount } = renderWindow({ initialSize: { width: 600, height: 500 } })
const current = box()
// Asked for 500 but only 400 - 52 (band) - 8 (margin) fits.
expect(current.height).toBe(340)
expect(current.top).toBe(52)
expect(current.top + current.height).toBe(392)
unmount()
})
it('leaves plain browsers untouched when the overlay is a ghost', () => {
// Headless Chromium and some macOS builds expose the interface with
// `visible: false`: no band, so the old 8px floor must stay exact.
+40 -15
View File
@@ -5,7 +5,7 @@
*
* Every unusable shape collapses to 0 so plain browsers keep their exact
* behaviour: no API, a `visible: false` ghost interface (headless Chromium,
* some macOS builds), a zero-height band (full screen), and a throwing read.
* some macOS builds), a zero-height band, and a throwing read.
*/
// @vitest-environment jsdom
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
@@ -59,61 +59,65 @@ afterEach(() => {
describe('viewport inset: the shape of the native control band', () => {
it('is zero without the Window Controls Overlay API', () => {
expect(getViewportInset().top).toBe(0)
expect(getViewportInset()).toBe(0)
})
it('reads the band height', () => {
setViewportInsetSourceForTests(overlay({ x: 0, y: 0, width: 1_000, height: 36 }).source)
expect(getViewportInset().top).toBe(36)
expect(getViewportInset()).toBe(36)
})
it('rounds a fractional height', () => {
setViewportInsetSourceForTests(overlay({ x: 0, y: 0, width: 1_000, height: 35.6 }).source)
expect(getViewportInset().top).toBe(36)
expect(getViewportInset()).toBe(36)
})
it('uses the rect bottom edge, not the raw height', () => {
// A shell that insets the band: the page may start at y + height.
setViewportInsetSourceForTests(overlay({ x: 0, y: 4, width: 1_000, height: 32 }).source)
expect(getViewportInset().top).toBe(36)
expect(getViewportInset()).toBe(36)
})
it('treats a non-visible overlay as absent (ghost interface)', () => {
setViewportInsetSourceForTests(overlay({ x: 0, y: 0, width: 1_000, height: 44 }, false).source)
expect(getViewportInset().top).toBe(0)
expect(getViewportInset()).toBe(0)
})
it('treats a zero-height band as no band (full screen)', () => {
it('treats a zero-height band as no band', () => {
setViewportInsetSourceForTests(overlay({ x: 0, y: 0, width: 1_000, height: 0 }).source)
expect(getViewportInset().top).toBe(0)
expect(getViewportInset()).toBe(0)
})
it('does not throw when the geometry read races the layout', () => {
const fake = overlay({ x: 0, y: 0, width: 1_000, height: 44 })
setViewportInsetSourceForTests(fake.source)
expect(getViewportInset().top).toBe(44)
expect(getViewportInset()).toBe(44)
fake.boom()
expect(() => { fake.emit() }).not.toThrow()
expect(getViewportInset().top).toBe(0)
expect(getViewportInset()).toBe(0)
})
})
describe('viewport inset: live geometry', () => {
it('notifies subscribers when the band changes', () => {
it('notifies subscribers only when the band actually changes', () => {
const fake = overlay({ x: 0, y: 0, width: 1_000, height: 0 })
setViewportInsetSourceForTests(fake.source)
let calls = 0
const dispose = subscribeViewportInset(() => { calls += 1 })
expect(getViewportInset().top).toBe(0)
expect(getViewportInset()).toBe(0)
fake.set({ height: 36 })
fake.emit()
expect(getViewportInset().top).toBe(36)
expect(getViewportInset()).toBe(36)
expect(calls).toBe(1)
// A later event with the same geometry must not wake React again.
fake.emit()
expect(calls).toBe(1)
fake.set({ height: 0 })
fake.emit()
expect(getViewportInset().top).toBe(0)
expect(getViewportInset()).toBe(0)
expect(calls).toBe(2)
dispose()
@@ -122,6 +126,27 @@ describe('viewport inset: live geometry', () => {
expect(calls).toBe(2)
})
it('re-reads on window resize (the maximize / restore fallback)', () => {
// Some Electron builds expose the overlay without firing geometrychange.
const fake = overlay({ x: 0, y: 0, width: 1_000, height: 0 })
setViewportInsetSourceForTests(fake.source)
fake.set({ height: 36 })
window.dispatchEvent(new Event('resize'))
expect(getViewportInset()).toBe(36)
})
it('drops and restores the inset when visibility flips at runtime', () => {
const fake = overlay({ x: 0, y: 0, width: 1_000, height: 36 })
setViewportInsetSourceForTests(fake.source)
expect(getViewportInset()).toBe(36)
fake.setVisible(false)
fake.emit()
expect(getViewportInset()).toBe(0)
fake.setVisible(true)
fake.emit()
expect(getViewportInset()).toBe(36)
})
it('detaches the previous source when tests swap it', () => {
const first = overlay({ x: 0, y: 0, width: 1_000, height: 36 })
setViewportInsetSourceForTests(first.source)
@@ -129,6 +154,6 @@ describe('viewport inset: live geometry', () => {
const second = overlay({ x: 0, y: 0, width: 1_000, height: 44 })
setViewportInsetSourceForTests(second.source)
expect(first.listenerCount()).toBe(0)
expect(getViewportInset().top).toBe(44)
expect(getViewportInset()).toBe(44)
})
})