97 lines
3.0 KiB
TypeScript
97 lines
3.0 KiB
TypeScript
/** The window sizes the heuristic reads. */
|
|
export type WindowSizes = {
|
|
innerWidth: number;
|
|
innerHeight: number;
|
|
outerWidth: number;
|
|
outerHeight: number;
|
|
};
|
|
|
|
/**
|
|
* How much larger the outer window may be than the page before the developer
|
|
* tools are suspected, in pixels. Window frames, tab strips, and address bars
|
|
* take up to about 130 px of height, and a docked panel takes at least 200.
|
|
*/
|
|
const sizeGapPx = 160;
|
|
|
|
/**
|
|
* Guesses that a docked developer tools panel is open, because it takes room
|
|
* from the page and leaves the outer window as it was. It is a heuristic with
|
|
* known false positives (a sidebar, a zoomed page, a window that is being
|
|
* resized) and false negatives (a detached panel), so a result is only a weak
|
|
* hint. A window that reports no sizes is never suspected.
|
|
*/
|
|
export function devtoolsDockedOpen(sizes: Partial<WindowSizes>): boolean {
|
|
const { innerWidth, innerHeight, outerWidth, outerHeight } = sizes;
|
|
if (
|
|
innerWidth === undefined ||
|
|
innerHeight === undefined ||
|
|
outerWidth === undefined ||
|
|
outerHeight === undefined
|
|
) {
|
|
return false;
|
|
}
|
|
return outerWidth - innerWidth > sizeGapPx || outerHeight - innerHeight > sizeGapPx;
|
|
}
|
|
|
|
export type DevtoolsWatcherOptions = {
|
|
window: EventTarget & Partial<WindowSizes>;
|
|
now: () => number;
|
|
schedule: (callback: () => void, delayMs: number) => unknown;
|
|
cancel: (handle: unknown) => void;
|
|
/** Called once when the panel looks opened, and again only after the cooldown. */
|
|
onOpen: () => void;
|
|
intervalMs?: number;
|
|
cooldownMs?: number;
|
|
};
|
|
|
|
const defaultIntervalMs = 2_000;
|
|
const defaultCooldownMs = 60_000;
|
|
|
|
/**
|
|
* Watches the window sizes on every resize and on a slow timer, and reports a
|
|
* change from closed to open. A panel that is closed and opened again reports
|
|
* again only after the cooldown, so a candidate who toggles it cannot flood the
|
|
* batch. Returns the function that stops the watcher.
|
|
*/
|
|
export function startDevtoolsWatcher(options: DevtoolsWatcherOptions): () => void {
|
|
const interval = options.intervalMs ?? defaultIntervalMs;
|
|
const cooldown = options.cooldownMs ?? defaultCooldownMs;
|
|
let open = false;
|
|
let lastReportAt: number | null = null;
|
|
let timer: unknown = null;
|
|
let stopped = false;
|
|
|
|
function check(): void {
|
|
const nowOpen = devtoolsDockedOpen(options.window);
|
|
if (nowOpen && !open) {
|
|
const at = options.now();
|
|
if (lastReportAt === null || at - lastReportAt >= cooldown) {
|
|
lastReportAt = at;
|
|
try {
|
|
options.onOpen();
|
|
} catch {
|
|
// A watcher must never disturb the test.
|
|
}
|
|
}
|
|
}
|
|
open = nowOpen;
|
|
}
|
|
|
|
function tick(): void {
|
|
if (stopped) return;
|
|
check();
|
|
timer = options.schedule(tick, interval);
|
|
}
|
|
|
|
const onResize = () => check();
|
|
options.window.addEventListener("resize", onResize, { passive: true });
|
|
check();
|
|
timer = options.schedule(tick, interval);
|
|
|
|
return () => {
|
|
stopped = true;
|
|
options.window.removeEventListener("resize", onResize);
|
|
if (timer !== null) options.cancel(timer);
|
|
};
|
|
}
|