mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-11-22 04:46:07 -08:00
fix(overlay): identify Windows mpv windows through IPC PID (#330)
This commit is contained in:
1 parent
0360270e81
commit
6fd55053d4
19 files changed
+1038
-269
No files matched your search
@@ -0,0 +1,176 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import {
|
||||
createOverlayVisibilityDiagnostics,
|
||||
readOverlayVisibilitySnapshot,
|
||||
type OverlayVisibilitySnapshot,
|
||||
} from './overlay-visibility-diagnostics';
|
||||
|
||||
type SnapshotArgs = Parameters<typeof readOverlayVisibilitySnapshot>[0];
|
||||
|
||||
function snapshotFor(
|
||||
options: {
|
||||
tracking?: boolean;
|
||||
targetPid?: number | null;
|
||||
minimized?: boolean;
|
||||
window?: Record<string, unknown>;
|
||||
noTracker?: boolean;
|
||||
} & Pick<SnapshotArgs, 'modalActive' | 'suspendVisibleOverlay'> & {
|
||||
visibleOverlayVisible?: boolean;
|
||||
} = {},
|
||||
) {
|
||||
const tracker = {
|
||||
isTracking: () => options.tracking ?? false,
|
||||
getGeometry: () => null,
|
||||
isTargetWindowFocused: () => false,
|
||||
isTargetWindowMinimized: () => options.minimized ?? false,
|
||||
getDiagnosticState: () => ({ targetPid: options.targetPid ?? null, socketPath: '/tmp/mpv' }),
|
||||
};
|
||||
return readOverlayVisibilitySnapshot({
|
||||
mainWindow: {
|
||||
isDestroyed: () => false,
|
||||
isVisible: () => false,
|
||||
getOpacity: () => 0,
|
||||
...options.window,
|
||||
} as unknown as SnapshotArgs['mainWindow'],
|
||||
windowTracker: options.noTracker ? null : (tracker as unknown as SnapshotArgs['windowTracker']),
|
||||
visibleOverlayVisible: options.visibleOverlayVisible ?? true,
|
||||
modalActive: options.modalActive,
|
||||
suspendVisibleOverlay: options.suspendVisibleOverlay,
|
||||
});
|
||||
}
|
||||
|
||||
function harness() {
|
||||
let now = 0;
|
||||
let callback: (() => void) | null = null;
|
||||
let delay = 0;
|
||||
const warnings: Array<{ message: string; details: Record<string, unknown> }> = [];
|
||||
let snapshot: OverlayVisibilitySnapshot | null = {
|
||||
expectedVisible: true,
|
||||
visible: false,
|
||||
opacity: 0,
|
||||
contentReady: false,
|
||||
tracking: false,
|
||||
targetPid: 4242,
|
||||
pidLookupPending: false,
|
||||
};
|
||||
const diagnostics = createOverlayVisibilityDiagnostics({
|
||||
now: () => now,
|
||||
warn: (message, details) => warnings.push({ message, details }),
|
||||
schedule: (fn, ms) => {
|
||||
callback = fn;
|
||||
delay = ms;
|
||||
return { unref() {} } as ReturnType<typeof setTimeout>;
|
||||
},
|
||||
cancel: () => {
|
||||
callback = null;
|
||||
},
|
||||
});
|
||||
const update = () => diagnostics.update(() => snapshot);
|
||||
return {
|
||||
warnings,
|
||||
update,
|
||||
setSnapshot: (value: typeof snapshot) => {
|
||||
snapshot = value;
|
||||
},
|
||||
tick: () => {
|
||||
assert.ok(callback, 'watchdog must be scheduled');
|
||||
now += delay;
|
||||
callback();
|
||||
},
|
||||
scheduled: () => callback !== null,
|
||||
};
|
||||
}
|
||||
|
||||
test('visibility watchdog warns without more events and rate limits repeated polls', () => {
|
||||
const h = harness();
|
||||
h.update();
|
||||
assert.equal(h.warnings.length, 0);
|
||||
for (let i = 0; i < 100; i += 1) h.update();
|
||||
h.tick();
|
||||
assert.equal(h.warnings.length, 1);
|
||||
assert.equal(h.warnings[0]?.details.reason, 'mpv-window-not-tracked');
|
||||
assert.equal(h.warnings[0]?.details.targetPid, 4242);
|
||||
assert.equal(h.warnings[0]?.details.elapsedMs, 5_000);
|
||||
for (let i = 0; i < 100; i += 1) h.update();
|
||||
assert.equal(h.warnings.length, 1);
|
||||
h.tick();
|
||||
assert.equal(h.warnings[1]?.details.elapsedMs, 35_000);
|
||||
});
|
||||
|
||||
test('visibility watchdog distinguishes renderer readiness, hidden windows and opacity', () => {
|
||||
for (const [contentReady, visible, opacity, reason] of [
|
||||
[false, false, 0, 'renderer-not-ready'],
|
||||
[true, false, 0, 'window-hidden'],
|
||||
[true, true, 0, 'window-transparent'],
|
||||
] as const) {
|
||||
const h = harness();
|
||||
h.setSnapshot({ expectedVisible: true, tracking: true, contentReady, visible, opacity });
|
||||
h.update();
|
||||
h.tick();
|
||||
assert.equal(h.warnings[0]?.details.reason, reason);
|
||||
}
|
||||
});
|
||||
|
||||
test('visibility watchdog reports recovery only after warning and stops', () => {
|
||||
const h = harness();
|
||||
h.update();
|
||||
h.tick();
|
||||
h.setSnapshot({
|
||||
expectedVisible: true,
|
||||
visible: true,
|
||||
opacity: 1,
|
||||
contentReady: true,
|
||||
tracking: true,
|
||||
});
|
||||
h.tick();
|
||||
assert.match(h.warnings[1]!.message, /recovered/);
|
||||
assert.equal(h.scheduled(), false);
|
||||
h.update();
|
||||
assert.equal(h.warnings.length, 2);
|
||||
});
|
||||
|
||||
test('visibility watchdog stays quiet after fast startup, intentional hide or window destruction', () => {
|
||||
for (const snapshot of [
|
||||
{ expectedVisible: true, visible: true, opacity: 1, contentReady: true, tracking: true },
|
||||
{ expectedVisible: false, visible: false, opacity: 0, contentReady: false, tracking: false },
|
||||
null,
|
||||
]) {
|
||||
const h = harness();
|
||||
h.update();
|
||||
h.setSnapshot(snapshot);
|
||||
h.tick();
|
||||
assert.deepEqual(h.warnings, []);
|
||||
assert.equal(h.scheduled(), false);
|
||||
}
|
||||
});
|
||||
|
||||
test('overlay snapshot expects visibility only once an mpv has been identified', () => {
|
||||
// Requested before any mpv exists: auto_start_overlay, tray or --show-visible-overlay.
|
||||
assert.equal(snapshotFor()?.expectedVisible, false);
|
||||
assert.equal(snapshotFor({ noTracker: true })?.expectedVisible, false);
|
||||
// mpv answered on the socket but its window never matched, so tracking never started.
|
||||
assert.equal(snapshotFor({ targetPid: 4242 })?.expectedVisible, true);
|
||||
// A tracker with no target socket matches windows without ever learning a PID.
|
||||
assert.equal(snapshotFor({ tracking: true })?.expectedVisible, true);
|
||||
});
|
||||
|
||||
test('overlay snapshot does not expect an intentionally hidden overlay', () => {
|
||||
const identified = { targetPid: 4242, tracking: true };
|
||||
assert.equal(snapshotFor(identified)?.expectedVisible, true);
|
||||
assert.equal(snapshotFor({ ...identified, modalActive: true })?.expectedVisible, false);
|
||||
assert.equal(snapshotFor({ ...identified, suspendVisibleOverlay: true })?.expectedVisible, false);
|
||||
assert.equal(snapshotFor({ ...identified, minimized: true })?.expectedVisible, false);
|
||||
assert.equal(
|
||||
snapshotFor({ ...identified, visibleOverlayVisible: false })?.expectedVisible,
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
test('overlay snapshot carries tracker state, defaults opacity and ends with the window', () => {
|
||||
const snapshot = snapshotFor({ targetPid: 4242, window: { getOpacity: undefined } });
|
||||
assert.equal(snapshot?.targetPid, 4242);
|
||||
assert.equal(snapshot?.socketPath, '/tmp/mpv');
|
||||
assert.equal(snapshot?.opacity, 1);
|
||||
assert.equal(snapshotFor({ window: { isDestroyed: () => true } }), null);
|
||||
});
|
||||
@@ -0,0 +1,153 @@
|
||||
import type { BrowserWindow } from 'electron';
|
||||
import type { BaseWindowTracker } from '../../window-trackers';
|
||||
import { createLogger } from '../../logger';
|
||||
import {
|
||||
OVERLAY_WINDOW_CONTENT_READY_FLAG,
|
||||
OVERLAY_WINDOW_DOCUMENT_LOADED_FLAG,
|
||||
} from './overlay-window-flags';
|
||||
|
||||
const log = createLogger('overlay:visibility');
|
||||
|
||||
export type OverlayVisibilitySnapshot = {
|
||||
expectedVisible: boolean;
|
||||
visible: boolean;
|
||||
opacity: number;
|
||||
contentReady: boolean;
|
||||
tracking: boolean;
|
||||
[key: string]: unknown;
|
||||
};
|
||||
|
||||
// One watchdog per window, independent of further visibility events: a renderer
|
||||
// that never becomes ready must still leave evidence at the default warn level.
|
||||
export function createOverlayVisibilityDiagnostics(deps: {
|
||||
warn: (message: string, details: Record<string, unknown>) => void;
|
||||
now: () => number;
|
||||
schedule: (callback: () => void, delayMs: number) => ReturnType<typeof setTimeout>;
|
||||
cancel: (timer: ReturnType<typeof setTimeout>) => void;
|
||||
}) {
|
||||
let timer: ReturnType<typeof setTimeout> | null = null;
|
||||
let readSnapshot: () => OverlayVisibilitySnapshot | null = () => null;
|
||||
let stalledAtMs: number | null = null;
|
||||
let warned = false;
|
||||
let lastWarningAtMs = 0;
|
||||
|
||||
const check = () => {
|
||||
if (timer !== null) deps.cancel(timer);
|
||||
timer = null;
|
||||
const snapshot = readSnapshot();
|
||||
if (!snapshot?.expectedVisible) {
|
||||
stalledAtMs = null;
|
||||
warned = false;
|
||||
return;
|
||||
}
|
||||
if (snapshot.visible && snapshot.opacity > 0 && snapshot.contentReady) {
|
||||
if (warned) {
|
||||
deps.warn('Windows overlay visibility recovered', {
|
||||
...snapshot,
|
||||
elapsedMs: deps.now() - stalledAtMs!,
|
||||
});
|
||||
}
|
||||
stalledAtMs = null;
|
||||
warned = false;
|
||||
return;
|
||||
}
|
||||
stalledAtMs ??= deps.now();
|
||||
const elapsedMs = deps.now() - stalledAtMs;
|
||||
if (elapsedMs >= 5_000 && (!warned || deps.now() - lastWarningAtMs >= 30_000)) {
|
||||
const reason = !snapshot.tracking
|
||||
? 'mpv-window-not-tracked'
|
||||
: !snapshot.contentReady
|
||||
? 'renderer-not-ready'
|
||||
: !snapshot.visible
|
||||
? 'window-hidden'
|
||||
: 'window-transparent';
|
||||
deps.warn('Windows overlay is not visible after startup or tracking loss', {
|
||||
...snapshot,
|
||||
reason,
|
||||
elapsedMs,
|
||||
});
|
||||
warned = true;
|
||||
lastWarningAtMs = deps.now();
|
||||
}
|
||||
timer = deps.schedule(
|
||||
check,
|
||||
warned ? 30_000 - (deps.now() - lastWarningAtMs) : 5_000 - elapsedMs,
|
||||
);
|
||||
timer.unref?.();
|
||||
};
|
||||
|
||||
return {
|
||||
update(reader: () => OverlayVisibilitySnapshot | null) {
|
||||
readSnapshot = reader;
|
||||
// Frequent polls must not postpone a warning or bypass its rate limit.
|
||||
check();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const diagnosticsByWindow = new WeakMap<
|
||||
BrowserWindow,
|
||||
ReturnType<typeof createOverlayVisibilityDiagnostics>
|
||||
>();
|
||||
|
||||
type OverlayVisibilityWatchArgs = {
|
||||
mainWindow: BrowserWindow;
|
||||
windowTracker: BaseWindowTracker | null;
|
||||
visibleOverlayVisible: boolean;
|
||||
modalActive?: boolean;
|
||||
suspendVisibleOverlay?: boolean;
|
||||
};
|
||||
|
||||
export function readOverlayVisibilitySnapshot(
|
||||
args: OverlayVisibilityWatchArgs,
|
||||
): OverlayVisibilitySnapshot | null {
|
||||
const window = args.mainWindow;
|
||||
if (window.isDestroyed()) return null;
|
||||
const tracker = args.windowTracker as
|
||||
| (BaseWindowTracker & {
|
||||
getDiagnosticState?: () => { targetPid?: number | null } & Record<string, unknown>;
|
||||
})
|
||||
| null;
|
||||
const trackerState = tracker?.getDiagnosticState?.();
|
||||
const flags = window as unknown as Record<string, unknown>;
|
||||
const minimized = tracker?.isTargetWindowMinimized?.() ?? false;
|
||||
const tracking = tracker?.isTracking() ?? false;
|
||||
// The overlay may be requested with no mpv to attach to (auto_start_overlay, tray,
|
||||
// --show-visible-overlay), so it only counts as stalled once an mpv has answered on the
|
||||
// socket. That still covers an mpv whose window never matches, which never starts tracking.
|
||||
const mpvIdentified = tracking || (trackerState?.targetPid ?? null) !== null;
|
||||
return {
|
||||
...trackerState,
|
||||
expectedVisible:
|
||||
args.visibleOverlayVisible &&
|
||||
mpvIdentified &&
|
||||
!args.modalActive &&
|
||||
!args.suspendVisibleOverlay &&
|
||||
!minimized,
|
||||
visible: window.isVisible(),
|
||||
opacity: window.getOpacity?.() ?? 1,
|
||||
contentReady: flags[OVERLAY_WINDOW_CONTENT_READY_FLAG] === true,
|
||||
documentLoaded: flags[OVERLAY_WINDOW_DOCUMENT_LOADED_FLAG] === true,
|
||||
rendererLoading: window.webContents?.isLoading?.() ?? null,
|
||||
tracking,
|
||||
trackerInitialized: tracker !== null,
|
||||
geometry: tracker?.getGeometry() ?? null,
|
||||
focused: tracker?.isTargetWindowFocused?.() ?? false,
|
||||
minimized,
|
||||
};
|
||||
}
|
||||
|
||||
export function watchWindowsOverlayVisibility(args: OverlayVisibilityWatchArgs): void {
|
||||
const window = args.mainWindow;
|
||||
let diagnostics = diagnosticsByWindow.get(window);
|
||||
if (!diagnostics) {
|
||||
diagnostics = createOverlayVisibilityDiagnostics({
|
||||
warn: (message, details) => log.warn(message, details),
|
||||
now: Date.now,
|
||||
schedule: setTimeout,
|
||||
cancel: clearTimeout,
|
||||
});
|
||||
diagnosticsByWindow.set(window, diagnostics);
|
||||
}
|
||||
diagnostics.update(() => readOverlayVisibilitySnapshot(args));
|
||||
}
|
||||
@@ -589,6 +589,34 @@ runScenarios([
|
||||
},
|
||||
]);
|
||||
|
||||
test('only a live Windows overlay reads visibility diagnostics', () => {
|
||||
const diagnosticReads = (platform: Platform, overrides: Partial<VisibilityArgs> = {}): number => {
|
||||
let reads = 0;
|
||||
createHarness({ platform, tracker: tracked() }).run({
|
||||
windowTracker: {
|
||||
isTracking: () => true,
|
||||
getGeometry: () => GEOMETRY,
|
||||
getDiagnosticState: () => {
|
||||
reads += 1;
|
||||
return { targetPid: 4242 };
|
||||
},
|
||||
} as unknown as VisibilityArgs['windowTracker'],
|
||||
...overrides,
|
||||
});
|
||||
return reads;
|
||||
};
|
||||
|
||||
assert.equal(diagnosticReads('windows'), 1);
|
||||
assert.equal(diagnosticReads('linux'), 0);
|
||||
assert.equal(diagnosticReads('macos'), 0);
|
||||
assert.equal(
|
||||
diagnosticReads('windows', {
|
||||
mainWindow: { isDestroyed: () => true } as unknown as VisibilityArgs['mainWindow'],
|
||||
}),
|
||||
0,
|
||||
);
|
||||
});
|
||||
|
||||
test('Windows visible overlay restores opacity and rebinds after the deferred reveal delay', async () => {
|
||||
const h = createHarness({ platform: 'windows', tracker: tracked() });
|
||||
assertOutcome(h.run(), { opacity: 0, zOrderSyncs: 1 });
|
||||
|
||||
@@ -3,6 +3,7 @@ import { BaseWindowTracker } from '../../window-trackers';
|
||||
import { WindowGeometry } from '../../types';
|
||||
import { applyOverlayClickThrough } from './overlay-click-through';
|
||||
import { OVERLAY_WINDOW_CONTENT_READY_FLAG } from './overlay-window-flags';
|
||||
import { watchWindowsOverlayVisibility } from './overlay-visibility-diagnostics';
|
||||
|
||||
const WINDOWS_OVERLAY_REVEAL_DELAY_MS = 48;
|
||||
const pendingWindowsOverlayRevealTimeoutByWindow = new WeakMap<
|
||||
@@ -65,7 +66,7 @@ function isOverlayWindowContentReady(window: BrowserWindow): boolean {
|
||||
);
|
||||
}
|
||||
|
||||
export function updateVisibleOverlayVisibility(args: {
|
||||
function applyVisibleOverlayVisibility(args: {
|
||||
visibleOverlayVisible: boolean;
|
||||
modalActive?: boolean;
|
||||
forceMousePassthrough?: boolean;
|
||||
@@ -453,6 +454,15 @@ export function updateVisibleOverlayVisibility(args: {
|
||||
args.syncOverlayShortcuts();
|
||||
}
|
||||
|
||||
export function updateVisibleOverlayVisibility(
|
||||
args: Parameters<typeof applyVisibleOverlayVisibility>[0],
|
||||
): void {
|
||||
applyVisibleOverlayVisibility(args);
|
||||
if (args.isWindowsPlatform && args.mainWindow && !args.mainWindow.isDestroyed()) {
|
||||
watchWindowsOverlayVisibility({ ...args, mainWindow: args.mainWindow });
|
||||
}
|
||||
}
|
||||
|
||||
export function setVisibleOverlayVisible(options: {
|
||||
visible: boolean;
|
||||
setVisibleOverlayVisibleState: (visible: boolean) => void;
|
||||
|
||||
Reference in new issue
Block a user