mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-11-23 04:46:08 -08:00
fix(overlay): keep forwarding mpv keys after a Yomitan popup closes (#332)
Decide whether the Yomitan popup is open from the popup host in the DOM instead of the cached yomitanPopupVisible flag, which can lag a close and kept input.conf keys from reaching mpv. Keep the imported mpv key list in place while a refresh is in flight, so a key pressed right after the overlay regains focus is not dropped. A failed reload still clears the list. At --log-level debug, log why the overlay kept an mpv-bound key (config-loading, modal, lookup-popup, interactive-target or no-imported-bindings) over a validated IPC channel. Refs #331
This commit is contained in:
1 parent
0c1865f4a6
commit
0ae70b4ab2
14 files changed
+227
-23
No files matched your search
@@ -150,6 +150,7 @@ function installKeyboardTestGlobals() {
|
||||
let markActiveVideoWatchedCalls = 0;
|
||||
let statsToggleOverlayCalls = 0;
|
||||
const openedModalNotifications: string[] = [];
|
||||
const unforwardedMpvKeys: Array<{ key: string; reason: string }> = [];
|
||||
let selectionClearCount = 0;
|
||||
let selectionAddCount = 0;
|
||||
let mutationRecordsTaken = 0;
|
||||
@@ -252,6 +253,9 @@ function installKeyboardTestGlobals() {
|
||||
sendMpvCommand: (command: Array<string | number>) => {
|
||||
mpvCommands.push(command);
|
||||
},
|
||||
reportUnforwardedMpvKey: (report: { key: string; reason: string }) => {
|
||||
unforwardedMpvKeys.push(report);
|
||||
},
|
||||
dispatchSessionAction: async (actionId: string, payload?: unknown) => {
|
||||
sessionActions.push({ actionId, payload });
|
||||
},
|
||||
@@ -518,6 +522,7 @@ function installKeyboardTestGlobals() {
|
||||
markActiveVideoWatchedCalls: () => markActiveVideoWatchedCalls,
|
||||
statsToggleOverlayCalls: () => statsToggleOverlayCalls,
|
||||
openedModalNotifications,
|
||||
unforwardedMpvKeys,
|
||||
getPlaybackPaused: async () => playbackPausedResponse,
|
||||
setPlaybackPausedResponse: (value: boolean | null) => {
|
||||
playbackPausedResponse = value;
|
||||
@@ -2570,6 +2575,27 @@ test('discovered mpv keys only run after SubMiner controls and stay out of sessi
|
||||
}
|
||||
});
|
||||
|
||||
test('imported mpv keys forward once the popup closes, even if the cached popup flag lags', async () => {
|
||||
const { handlers, testGlobals, ctx } = createKeyboardHandlerHarness();
|
||||
try {
|
||||
testGlobals.setGetMpvInputBindings(async () => ({ keys: ['r'], blockedKeys: [] }));
|
||||
await handlers.setupMpvInputForwarding();
|
||||
await wait(0);
|
||||
testGlobals.setPopupVisible(true);
|
||||
testGlobals.dispatchKeydown({ key: 'r', code: 'KeyR' });
|
||||
assert.deepEqual(testGlobals.mpvCommands, []);
|
||||
assert.deepEqual(testGlobals.unforwardedMpvKeys, [{ key: 'r', reason: 'lookup-popup' }]);
|
||||
|
||||
// #331: the popup closed, but a late popup event left the cached flag set.
|
||||
testGlobals.setPopupVisible(false);
|
||||
ctx.state.yomitanPopupVisible = true;
|
||||
testGlobals.dispatchKeydown({ key: 'r', code: 'KeyR' });
|
||||
assert.deepEqual(testGlobals.mpvCommands, [['keydown', 'r']]);
|
||||
} finally {
|
||||
testGlobals.restore();
|
||||
}
|
||||
});
|
||||
|
||||
test('stalled mpv discovery does not delay configured overlay controls', async () => {
|
||||
const { handlers, testGlobals } = createKeyboardHandlerHarness();
|
||||
try {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { CompiledSessionBinding, PrimarySubMode, ShortcutsConfig } from '../../types';
|
||||
import type { MpvKeyForwardBlock } from '../../types/session-bindings';
|
||||
import type { RendererContext } from '../context';
|
||||
import { createMpvInputForwarding } from './mpv-input-forwarding';
|
||||
import { MPV_MOUSE_BUTTON_BY_BUTTON, wheelEventToMpvWheel } from '../../shared/mpv-input-bindings';
|
||||
@@ -141,14 +142,24 @@ export function createKeyboardHandlers(
|
||||
// Imported mpv bindings only run when no SubMiner UI could be the input's target.
|
||||
// The subtitle sidebar is a side panel over playback, so only input inside it (a
|
||||
// `.modal` target) stays with SubMiner.
|
||||
// Why imported mpv input must stay in the overlay right now, or null when it may forward.
|
||||
// SubMiner's own keybindings never pass through here.
|
||||
function getMpvForwardBlock(target: EventTarget | null): MpvKeyForwardBlock | null {
|
||||
if (!keyboardConfigLoaded) return 'config-loading';
|
||||
if (ctx.state.playlistBrowserModalOpen || ctx.state.youtubePickerModalOpen) return 'modal';
|
||||
if (isLookupPopupOpen(ctx.state, document)) return 'lookup-popup';
|
||||
if (isInteractiveTarget(target)) return 'interactive-target';
|
||||
return null;
|
||||
}
|
||||
|
||||
function canForwardToMpv(target: EventTarget | null): boolean {
|
||||
return (
|
||||
keyboardConfigLoaded &&
|
||||
!ctx.state.playlistBrowserModalOpen &&
|
||||
!ctx.state.youtubePickerModalOpen &&
|
||||
!isLookupPopupOpen(ctx.state, document) &&
|
||||
!isInteractiveTarget(target)
|
||||
);
|
||||
return getMpvForwardBlock(target) === null;
|
||||
}
|
||||
|
||||
// Debug breadcrumb for "my mpv key does nothing": the main process logs it at debug level.
|
||||
function reportUnforwardedMpvKey(e: KeyboardEvent, reason: MpvKeyForwardBlock): void {
|
||||
const report = importedMpvBindings.unforwardedReport(e, reason);
|
||||
if (report) window.electronAPI.reportUnforwardedMpvKey(report);
|
||||
}
|
||||
|
||||
// Scrolling over subtitles still reaches mpv; only scrollable overlay UI keeps the wheel.
|
||||
@@ -1403,6 +1414,7 @@ export function createKeyboardHandlers(
|
||||
|
||||
if (isLookupPopupOpen(ctx.state, document)) {
|
||||
if (handleYomitanPopupKeybind(e)) {
|
||||
reportUnforwardedMpvKey(e, 'lookup-popup');
|
||||
e.preventDefault();
|
||||
return;
|
||||
}
|
||||
@@ -1469,7 +1481,13 @@ export function createKeyboardHandlers(
|
||||
e.preventDefault();
|
||||
return;
|
||||
}
|
||||
if (canForwardToMpv(e.target)) importedMpvBindings.keydown(e);
|
||||
const forwardBlock = getMpvForwardBlock(e.target);
|
||||
if (forwardBlock) {
|
||||
reportUnforwardedMpvKey(e, forwardBlock);
|
||||
} else if (!importedMpvBindings.keydown(e)) {
|
||||
// Reports only while nothing is imported; an unbound key is not worth a line.
|
||||
reportUnforwardedMpvKey(e, 'no-imported-bindings');
|
||||
}
|
||||
});
|
||||
|
||||
document.addEventListener('mousedown', (e: MouseEvent) => {
|
||||
|
||||
@@ -175,3 +175,50 @@ test('focus loss releases held keys and failed refresh clears stale bindings', a
|
||||
await forwarding.refresh();
|
||||
assert.equal(forwarding.keydown(keyEvent()), false);
|
||||
});
|
||||
|
||||
test('a reload keeps forwarding the current keys until it settles', async () => {
|
||||
let resolveReload: (snapshot: MpvInputBindingsSnapshot) => void = () => {};
|
||||
let loads = 0;
|
||||
const forwarding = createMpvInputForwarding({
|
||||
load: () => {
|
||||
loads += 1;
|
||||
if (loads === 1) return Promise.resolve({ keys: ['r'], blockedKeys: [] });
|
||||
return new Promise((resolve) => {
|
||||
resolveReload = resolve;
|
||||
});
|
||||
},
|
||||
send: () => {},
|
||||
});
|
||||
await forwarding.refresh();
|
||||
const reload = forwarding.refresh();
|
||||
assert.equal(forwarding.keydown(keyEvent()), true);
|
||||
forwarding.keyup(keyEvent());
|
||||
resolveReload({ keys: ['t'], blockedKeys: [] });
|
||||
await reload;
|
||||
assert.equal(forwarding.keydown(keyEvent()), false);
|
||||
assert.equal(forwarding.keydown(keyEvent({ key: 't', code: 'KeyT' })), true);
|
||||
});
|
||||
|
||||
test('unforwarded reports name keys mpv binds, or any key while nothing is imported', async () => {
|
||||
let snapshot: MpvInputBindingsSnapshot = { keys: [], blockedKeys: [] };
|
||||
const forwarding = createMpvInputForwarding({ load: async () => snapshot, send: () => {} });
|
||||
await forwarding.refresh();
|
||||
assert.deepEqual(forwarding.unforwardedReport(keyEvent(), 'lookup-popup'), {
|
||||
key: 'r',
|
||||
reason: 'no-imported-bindings',
|
||||
});
|
||||
assert.equal(
|
||||
forwarding.unforwardedReport(keyEvent({ key: 'Shift', code: 'ShiftLeft' }), 'lookup-popup'),
|
||||
null,
|
||||
);
|
||||
|
||||
snapshot = { keys: ['r', 't'], blockedKeys: [{ code: 'KeyT', modifiers: [] }] };
|
||||
await forwarding.refresh();
|
||||
assert.deepEqual(forwarding.unforwardedReport(keyEvent(), 'interactive-target'), {
|
||||
key: 'r',
|
||||
reason: 'interactive-target',
|
||||
});
|
||||
assert.equal(forwarding.unforwardedReport(keyEvent(), 'no-imported-bindings'), null);
|
||||
assert.equal(forwarding.unforwardedReport(keyEvent({ key: 't', code: 'KeyT' }), 'modal'), null);
|
||||
assert.equal(forwarding.unforwardedReport(keyEvent({ key: 'z', code: 'KeyZ' }), 'modal'), null);
|
||||
});
|
||||
@@ -4,7 +4,13 @@ import {
|
||||
normalizeMpvInputKey,
|
||||
wheelEventToMpvWheel,
|
||||
} from '../../shared/mpv-input-bindings';
|
||||
import type { MpvInputBindingsSnapshot } from '../../types/session-bindings';
|
||||
import type {
|
||||
MpvInputBindingsSnapshot,
|
||||
MpvKeyForwardBlock,
|
||||
UnforwardedMpvKeyReport,
|
||||
} from '../../types/session-bindings';
|
||||
|
||||
const MODIFIER_ONLY_KEYS = new Set(['Shift', 'Control', 'Alt', 'Meta', 'AltGraph']);
|
||||
|
||||
type ModifierState = Pick<KeyboardEvent, 'ctrlKey' | 'altKey' | 'shiftKey' | 'metaKey'>;
|
||||
type ForwardedKeyEvent = Parameters<typeof keyboardEventToMpvKey>[0] &
|
||||
@@ -31,25 +37,26 @@ export function createMpvInputForwarding(deps: {
|
||||
heldKeys.clear();
|
||||
}
|
||||
|
||||
// Every window focus refreshes, so the current list keeps forwarding until the reload
|
||||
// settles; a key pressed mid-reload would otherwise be dropped.
|
||||
function refresh(): Promise<void> {
|
||||
if (disposed) return Promise.resolve();
|
||||
generation += 1;
|
||||
keys.clear();
|
||||
blockedKeys = [];
|
||||
releaseAll();
|
||||
if (pending) return pending;
|
||||
pending = (async () => {
|
||||
let requestedGeneration: number;
|
||||
do {
|
||||
requestedGeneration = generation;
|
||||
let snapshot: MpvInputBindingsSnapshot | null = null;
|
||||
try {
|
||||
const snapshot = await deps.load();
|
||||
if (!disposed && requestedGeneration === generation) {
|
||||
keys = new Set(snapshot.keys);
|
||||
blockedKeys = snapshot.blockedKeys;
|
||||
}
|
||||
snapshot = await deps.load();
|
||||
} catch {
|
||||
// Discovery is optional. Keep the existing overlay controls available.
|
||||
// Discovery is optional: SubMiner's own bindings keep working without it.
|
||||
}
|
||||
if (!disposed && requestedGeneration === generation) {
|
||||
keys = new Set(snapshot?.keys ?? []);
|
||||
blockedKeys = snapshot?.blockedKeys ?? [];
|
||||
}
|
||||
} while (!disposed && requestedGeneration !== generation);
|
||||
})().finally(() => {
|
||||
@@ -98,6 +105,23 @@ export function createMpvInputForwarding(deps: {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* The debug report for a key the overlay kept from mpv, or null when mpv does not bind
|
||||
* it. With nothing imported yet, every nameable key is reported as such.
|
||||
*/
|
||||
function unforwardedReport(
|
||||
event: ForwardedKeyEvent,
|
||||
reason: MpvKeyForwardBlock,
|
||||
): UnforwardedMpvKeyReport | null {
|
||||
if (disposed || event.repeat || MODIFIER_ONLY_KEYS.has(event.key)) return null;
|
||||
const key = keyboardEventToMpvKey(event);
|
||||
if (!key) return null;
|
||||
if (keys.size === 0) return { key, reason: 'no-imported-bindings' };
|
||||
if (reason === 'no-imported-bindings') return null;
|
||||
if (isBlocked(event.code, event) || !keys.has(key)) return null;
|
||||
return { key, reason };
|
||||
}
|
||||
|
||||
function keyup(event: Pick<KeyboardEvent, 'code' | 'preventDefault'>): void {
|
||||
const key = heldKeys.get(event.code);
|
||||
if (!key) return;
|
||||
@@ -154,5 +178,15 @@ export function createMpvInputForwarding(deps: {
|
||||
releaseAll();
|
||||
}
|
||||
|
||||
return { refresh, keydown, keyup, wheel, mousedown, mouseup, releaseAll, dispose };
|
||||
return {
|
||||
refresh,
|
||||
keydown,
|
||||
keyup,
|
||||
wheel,
|
||||
mousedown,
|
||||
mouseup,
|
||||
releaseAll,
|
||||
dispose,
|
||||
unforwardedReport,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user