feat(floating): 新增壳的原生窗口控件带几何
桌面壳用 Electron titleBarOverlay 在页面顶部画最小化/最大化/关闭,那条带子 的鼠标事件到不了网页(#805 已实测)。这里只读标准 WCO 几何作为唯一来源: 无 API、visible=false(幽灵接口)、0 高(全屏)、读取抛错一律退化为 0, 普通浏览器与网页版行为不变。 不写偏好、不加设置项、不发布 CSS 变量、不复活 #430 删掉的 --dsh-title-bar-strip / body[data-dsh-title-bar-compat]。
This commit is contained in:
1 parent
11e6c8e63a
commit
4108e1544c
2 files changed
+272
No files matched your search
@@ -0,0 +1,138 @@
|
||||
/**
|
||||
* How many CSS pixels at the top of the viewport the DESKTOP SHELL paints its
|
||||
* native window controls over (minimize / maximize / close).
|
||||
*
|
||||
* Why this exists: on Windows the shell enables Electron's `titleBarOverlay`,
|
||||
* which draws those buttons OVER the page and takes every pointer event in
|
||||
* that band before it can reach the DOM. A floating surface pinned to the
|
||||
* viewport top therefore loses its whole title bar: the close button looks
|
||||
* dead while `document.elementFromPoint` still answers it (the two-point
|
||||
* bisection in issue #805 measured the same mechanism for the mermaid zoom
|
||||
* toolbar; the Tasks detail window is the same bug one component over).
|
||||
*
|
||||
* The ONE source is the standard Window Controls Overlay geometry. No
|
||||
* preference, no settings row, no URL stamp, no shell-supplied service, and
|
||||
* nothing is published to the DOM — the previous configurable "title-bar
|
||||
* strip" was removed in #430 because it had grown five sources and zero
|
||||
* consumers; this has one source and three real consumers.
|
||||
*/
|
||||
import { useSyncExternalStore } from 'react'
|
||||
|
||||
/** The slice of `navigator.windowControlsOverlay` this module needs. */
|
||||
export interface WindowControlsOverlayLike {
|
||||
readonly visible: boolean
|
||||
getTitlebarAreaRect(): {
|
||||
readonly x: number
|
||||
readonly y: number
|
||||
readonly width: number
|
||||
readonly height: number
|
||||
}
|
||||
addEventListener(type: 'geometrychange', listener: () => void): void
|
||||
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 })
|
||||
| undefined)?.windowControlsOverlay
|
||||
return candidate ?? undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* The safe gap below the native control band.
|
||||
*
|
||||
* `y + height` rather than `height` alone: the rect names the title-bar AREA,
|
||||
* so its bottom edge is the first pixel the page can own (the two coincide on
|
||||
* Windows, where `y` is 0, but a shell may inset the band).
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
function computeInset(candidate: WindowControlsOverlayLike | undefined): ViewportInset {
|
||||
if (candidate === undefined) return NO_VIEWPORT_INSET
|
||||
try {
|
||||
if (candidate.visible !== true) return NO_VIEWPORT_INSET
|
||||
const rect = candidate.getTitlebarAreaRect()
|
||||
const top = Math.round(rect.y + rect.height)
|
||||
return Number.isFinite(top) && top > 0 ? { top } : NO_VIEWPORT_INSET
|
||||
} catch {
|
||||
return NO_VIEWPORT_INSET
|
||||
}
|
||||
}
|
||||
|
||||
let source: WindowControlsOverlayLike | undefined
|
||||
let snapshot: ViewportInset = NO_VIEWPORT_INSET
|
||||
const listeners = new Set<() => void>()
|
||||
|
||||
function emit(): void {
|
||||
for (const listener of listeners) listener()
|
||||
}
|
||||
|
||||
function reread(): void {
|
||||
const next = computeInset(source)
|
||||
if (next === snapshot) return
|
||||
snapshot = next
|
||||
emit()
|
||||
}
|
||||
|
||||
// Probe at module evaluation, not on the first subscriber: `useSyncExternalStore`
|
||||
// reads the snapshot before it subscribes, so a lazy mount would paint the
|
||||
// first frame at the unsafe position and then jump the window down.
|
||||
if (typeof navigator !== 'undefined') {
|
||||
source = overlayOf(navigator)
|
||||
if (source !== undefined) {
|
||||
snapshot = computeInset(source)
|
||||
source.addEventListener('geometrychange', reread)
|
||||
}
|
||||
}
|
||||
|
||||
// Some Electron builds expose the overlay without firing `geometrychange` on
|
||||
// maximize / restore; a resize is the same event in practice. Event-driven, so
|
||||
// it costs nothing while idle (unlike the polling the steady-state rules guard).
|
||||
if (typeof window !== 'undefined') {
|
||||
window.addEventListener('resize', reread)
|
||||
}
|
||||
|
||||
/** The current inset. Stable reference until the geometry actually changes. */
|
||||
export function getViewportInset(): ViewportInset {
|
||||
return snapshot
|
||||
}
|
||||
|
||||
/** Subscribe to geometry changes; returns the disposer. */
|
||||
export function subscribeViewportInset(listener: () => void): () => void {
|
||||
listeners.add(listener)
|
||||
return () => { listeners.delete(listener) }
|
||||
}
|
||||
|
||||
/** Swap the overlay source (tests). `undefined` = no overlay API. */
|
||||
export function setViewportInsetSourceForTests(
|
||||
next: WindowControlsOverlayLike | undefined,
|
||||
): void {
|
||||
source?.removeEventListener('geometrychange', reread)
|
||||
source = next
|
||||
const computed = computeInset(next)
|
||||
if (computed !== snapshot) {
|
||||
snapshot = computed
|
||||
emit()
|
||||
}
|
||||
next?.addEventListener('geometrychange', reread)
|
||||
}
|
||||
|
||||
/** React consumer: the inset, re-rendering when the shell band changes. */
|
||||
export function useViewportInset(): ViewportInset {
|
||||
return useSyncExternalStore(subscribeViewportInset, getViewportInset, getViewportInset)
|
||||
}
|
||||
|
||||
/** Convenience for callers that only need the number. */
|
||||
export function useViewportInsetTop(): number {
|
||||
return useViewportInset().top
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
/**
|
||||
* The viewport's top inset (viewport-inset.ts): the band the desktop shell
|
||||
* paints its native window controls over, which floating surfaces must stay
|
||||
* out of (issue #805; the Tasks detail window's close button is the same bug).
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
// @vitest-environment jsdom
|
||||
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
|
||||
import {
|
||||
getViewportInset,
|
||||
setViewportInsetSourceForTests,
|
||||
subscribeViewportInset,
|
||||
type WindowControlsOverlayLike,
|
||||
} from '../src/client/viewport-inset.ts'
|
||||
|
||||
interface Rect { x: number; y: number; width: number; height: number }
|
||||
|
||||
/** A controllable WCO source: `emit()` plays the browser's geometrychange. */
|
||||
function overlay(initial: Rect, visible = true): {
|
||||
source: WindowControlsOverlayLike
|
||||
set(next: Partial<Rect>): void
|
||||
setVisible(next: boolean): void
|
||||
emit(): void
|
||||
listenerCount(): number
|
||||
boom(): void
|
||||
} {
|
||||
let rect = { ...initial }
|
||||
let isVisible = visible
|
||||
let throws = false
|
||||
const listeners = new Set<() => void>()
|
||||
return {
|
||||
source: {
|
||||
get visible() { return isVisible },
|
||||
getTitlebarAreaRect() {
|
||||
if (throws) throw new Error('geometry raced the layout')
|
||||
return { ...rect }
|
||||
},
|
||||
addEventListener(_type, listener) { listeners.add(listener) },
|
||||
removeEventListener(_type, listener) { listeners.delete(listener) },
|
||||
},
|
||||
set(next) { rect = { ...rect, ...next } },
|
||||
setVisible(next) { isVisible = next },
|
||||
emit() { for (const listener of listeners) listener() },
|
||||
listenerCount: () => listeners.size,
|
||||
boom() { throws = true },
|
||||
}
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
setViewportInsetSourceForTests(undefined)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
setViewportInsetSourceForTests(undefined)
|
||||
})
|
||||
|
||||
describe('viewport inset: the shape of the native control band', () => {
|
||||
it('is zero without the Window Controls Overlay API', () => {
|
||||
expect(getViewportInset().top).toBe(0)
|
||||
})
|
||||
|
||||
it('reads the band height', () => {
|
||||
setViewportInsetSourceForTests(overlay({ x: 0, y: 0, width: 1_000, height: 36 }).source)
|
||||
expect(getViewportInset().top).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)
|
||||
})
|
||||
|
||||
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)
|
||||
})
|
||||
|
||||
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)
|
||||
})
|
||||
|
||||
it('treats a zero-height band as no band (full screen)', () => {
|
||||
setViewportInsetSourceForTests(overlay({ x: 0, y: 0, width: 1_000, height: 0 }).source)
|
||||
expect(getViewportInset().top).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)
|
||||
fake.boom()
|
||||
expect(() => { fake.emit() }).not.toThrow()
|
||||
expect(getViewportInset().top).toBe(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe('viewport inset: live geometry', () => {
|
||||
it('notifies subscribers when the band 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)
|
||||
|
||||
fake.set({ height: 36 })
|
||||
fake.emit()
|
||||
expect(getViewportInset().top).toBe(36)
|
||||
|
||||
// A later event with the same geometry must not wake React again.
|
||||
fake.set({ height: 0 })
|
||||
fake.emit()
|
||||
expect(getViewportInset().top).toBe(0)
|
||||
expect(calls).toBe(2)
|
||||
|
||||
dispose()
|
||||
fake.set({ height: 36 })
|
||||
fake.emit()
|
||||
expect(calls).toBe(2)
|
||||
})
|
||||
|
||||
it('detaches the previous source when tests swap it', () => {
|
||||
const first = overlay({ x: 0, y: 0, width: 1_000, height: 36 })
|
||||
setViewportInsetSourceForTests(first.source)
|
||||
expect(first.listenerCount()).toBe(1)
|
||||
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)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user