feat(keybindings): add mouse button support for mpv keybindings (#103)

This commit is contained in:
2026-05-31 22:22:38 -07:00
committed by GitHub
parent e6a004ab8b
commit 487143802a
14 changed files with 281 additions and 5 deletions
+41 -2
View File
@@ -322,18 +322,31 @@ function installKeyboardTestGlobals() {
}
}
function dispatchDocumentMouseDown(event: { button: number; target?: unknown }): void {
function dispatchMousedown(event: {
button: number;
ctrlKey?: boolean;
metaKey?: boolean;
altKey?: boolean;
shiftKey?: boolean;
target?: unknown;
}): void {
const listeners = documentListeners.get('mousedown') ?? [];
const mouseEvent = {
button: event.button,
target: event.target ?? null,
ctrlKey: event.ctrlKey ?? false,
metaKey: event.metaKey ?? false,
altKey: event.altKey ?? false,
shiftKey: event.shiftKey ?? false,
preventDefault: () => {},
target: event.target ?? null,
};
for (const listener of listeners) {
listener(mouseEvent);
}
}
const dispatchDocumentMouseDown = dispatchMousedown;
function dispatchFocusInOnPopup(): void {
const listeners = documentListeners.get('focusin') ?? [];
const focusEvent = {
@@ -389,6 +402,7 @@ function installKeyboardTestGlobals() {
windowFocusCalls: () => windowFocusCalls,
dispatchKeydown,
dispatchDocumentMouseDown,
dispatchMousedown,
dispatchFocusInOnPopup,
dispatchWindowEvent,
setPopupVisible: (value: boolean) => {
@@ -1036,6 +1050,31 @@ test('paused configured subtitle-jump keybinding re-applies pause after backward
}
});
test('configured mouse button keybinding dispatches through overlay mouse handling', async () => {
const { handlers, testGlobals } = createKeyboardHandlerHarness();
try {
await handlers.setupMpvInputForwarding();
handlers.updateSessionBindings([
{
sourcePath: 'keybindings[0].key',
originalKey: 'MBTN_BACK',
key: { code: 'MBTN_BACK', modifiers: [] },
actionType: 'mpv-command',
command: ['sub-seek', -1],
},
] as never);
testGlobals.setPlaybackPausedResponse(false);
testGlobals.dispatchMousedown({ button: 3 });
await wait(0);
assert.deepEqual(testGlobals.mpvCommands.slice(-1), [['sub-seek', -1]]);
} finally {
testGlobals.restore();
}
});
test('configured subtitle-jump keybinding preserves pause when pause state is unknown', async () => {
const { handlers, testGlobals } = createKeyboardHandlerHarness();
+28
View File
@@ -37,6 +37,13 @@ export function createKeyboardHandlers(
const CHORD_TIMEOUT_MS = 1000;
const MPV_INPUT_FORWARDING_CONFIG_LOAD_TIMEOUT_MS = 50;
const KEYBOARD_SELECTED_WORD_CLASS = 'keyboard-selected';
const MOUSE_BUTTON_CODE_BY_BUTTON: Record<number, string> = {
0: 'MBTN_LEFT',
1: 'MBTN_MID',
2: 'MBTN_RIGHT',
3: 'MBTN_BACK',
4: 'MBTN_FORWARD',
};
let pendingSelectionAnchorAfterSubtitleSeek: 'start' | 'end' | null = null;
let pendingLookupRefreshAfterSubtitleSeek = false;
let resetSelectionToStartOnNextSubtitleSync = false;
@@ -81,6 +88,19 @@ export function createKeyboardHandlers(
return parts.join('+');
}
function mouseEventToString(e: MouseEvent): string | null {
const code = MOUSE_BUTTON_CODE_BY_BUTTON[e.button];
if (!code) return null;
const parts: string[] = [];
if (e.ctrlKey) parts.push('Ctrl');
if (e.altKey) parts.push('Alt');
if (e.shiftKey) parts.push('Shift');
if (e.metaKey) parts.push('Meta');
parts.push(code);
return parts.join('+');
}
function updateConfiguredShortcuts(
shortcuts: Required<ShortcutsConfig>,
statsToggleKey?: string,
@@ -1216,6 +1236,14 @@ export function createKeyboardHandlers(
});
document.addEventListener('mousedown', (e: MouseEvent) => {
const mouseString = mouseEventToString(e);
const binding = mouseString ? ctx.state.sessionBindingMap.get(mouseString) : undefined;
if (binding) {
e.preventDefault();
dispatchSessionBinding(binding);
return;
}
if (e.button === 2 && !isInteractiveTarget(e.target)) {
e.preventDefault();
void window.electronAPI
@@ -57,6 +57,11 @@ const KEY_NAME_MAP: Record<string, string> = {
Super: 'Meta',
Meta: 'Meta',
Backspace: 'Backspace',
MBTN_LEFT: 'Mouse Left',
MBTN_MID: 'Mouse Middle',
MBTN_RIGHT: 'Mouse Right',
MBTN_BACK: 'Mouse Back',
MBTN_FORWARD: 'Mouse Forward',
};
function normalizeKeyToken(token: string): string {