feat(overlay): forward mpv mouse button and wheel bindings (#279)

This commit is contained in:
sudacode authored and GitHub committed 2026-10-01 00:44:51 -07:00
1 parent 9fadbc8cdd
commit 9e79fa93c2
18 files changed
+500 -76

No files matched your search

+16
View File
@@ -6,6 +6,7 @@ import {
createMpvKeybindingRows,
keyboardEventToConfigKey,
mouseEventToConfigKey,
wheelEventToConfigKey,
} from './key-input';
test('keyboardEventToConfigKey formats Electron accelerators from learned input', () => {
@@ -93,6 +94,21 @@ test('mouseEventToConfigKey formats mpv mouse buttons from learned input', () =>
);
});
test('wheelEventToConfigKey formats mpv wheel keys only for mpv keybindings', () => {
const wheel = {
deltaX: 0,
deltaY: -120,
deltaMode: 0,
ctrlKey: true,
altKey: false,
shiftKey: false,
metaKey: false,
};
assert.equal(wheelEventToConfigKey(wheel, 'dom-code'), 'Ctrl+WHEEL_UP');
assert.equal(wheelEventToConfigKey({ ...wheel, deltaY: 0 }, 'dom-code'), null);
assert.equal(wheelEventToConfigKey(wheel, 'accelerator'), null);
});
test('MPV keybinding rows save default key moves as a disable plus replacement', () => {
const defaults: Keybinding[] = [{ key: 'Space', command: ['cycle', 'pause'] }];
const rows = createMpvKeybindingRows(defaults, []);
+19 -13
View File
@@ -1,4 +1,5 @@
import type { Keybinding } from '../types/runtime';
import { MPV_MOUSE_BUTTON_BY_BUTTON, wheelEventToMpvWheel } from '../shared/mpv-input-bindings';
export type KeyInputMode = 'accelerator' | 'dom-code' | 'code' | 'mpv-key';
@@ -19,6 +20,9 @@ export interface MouseInputLike {
metaKey: boolean;
}
export type WheelInputLike = Omit<MouseInputLike, 'button'> &
Parameters<typeof wheelEventToMpvWheel>[0];
export interface MpvKeybindingRow {
defaultKey: string;
key: string;
@@ -87,14 +91,6 @@ const MPV_KEY_BY_CODE: Record<string, string> = {
Tab: 'TAB',
};
const MPV_MOUSE_BUTTON_BY_BUTTON: Record<number, string> = {
0: 'MBTN_LEFT',
1: 'MBTN_MID',
2: 'MBTN_RIGHT',
3: 'MBTN_BACK',
4: 'MBTN_FORWARD',
};
function commandEquals(a: Keybinding['command'], b: Keybinding['command']): boolean {
return JSON.stringify(a) === JSON.stringify(b);
}
@@ -169,12 +165,22 @@ export function keyboardEventToConfigKey(
}
export function mouseEventToConfigKey(input: MouseInputLike, mode: KeyInputMode): string | null {
if (mode !== 'dom-code') {
return null;
}
const key = MPV_MOUSE_BUTTON_BY_BUTTON[input.button];
if (!key) {
return key ? mouseInputToConfigKey(input, key, mode) : null;
}
export function wheelEventToConfigKey(input: WheelInputLike, mode: KeyInputMode): string | null {
const key = wheelEventToMpvWheel(input)?.key;
return key ? mouseInputToConfigKey(input, key, mode) : null;
}
// Mouse input is only bindable through mpv keybindings, which use DOM-code keys.
function mouseInputToConfigKey(
input: Omit<MouseInputLike, 'button'>,
key: string,
mode: KeyInputMode,
): string | null {
if (mode !== 'dom-code') {
return null;
}
@@ -6,6 +6,7 @@ import {
keyboardEventToConfigKey,
mouseEventToConfigKey,
parseMpvCommandText,
wheelEventToConfigKey,
type KeyInputMode,
type MpvKeybindingRow,
} from './key-input';
@@ -43,11 +44,13 @@ function startKeyLearning(
let onKeyDown: (event: KeyboardEvent) => void;
let onBlur: () => void;
let onMouseDown: (event: MouseEvent) => void;
let onWheel: (event: WheelEvent) => void;
const stop = (): void => {
window.removeEventListener('keydown', onKeyDown, true);
window.removeEventListener('blur', onBlur, true);
window.removeEventListener('mousedown', onMouseDown, true);
window.removeEventListener('wheel', onWheel, true);
button.classList.remove('learning');
if (button.textContent === 'Press Keys...') {
button.textContent = previousText;
@@ -85,9 +88,19 @@ function startKeyLearning(
}
};
onWheel = (event: WheelEvent): void => {
const next = wheelEventToConfigKey(event, mode);
if (!next) return;
event.preventDefault();
event.stopPropagation();
stop();
onValue(next);
};
window.addEventListener('keydown', onKeyDown, true);
window.addEventListener('blur', onBlur, true);
window.addEventListener('mousedown', onMouseDown, true);
window.addEventListener('wheel', onWheel, { capture: true, passive: false });
activeKeyLearningStop = stop;
}