mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-04-10 04:19:25 -07:00
feat: wire session bindings through main, ipc, and cli runtime
This commit is contained in:
@@ -29,6 +29,13 @@ function makeArgs(overrides: Partial<CliArgs> = {}): CliArgs {
|
||||
triggerSubsync: false,
|
||||
markAudioCard: false,
|
||||
openRuntimeOptions: false,
|
||||
openJimaku: false,
|
||||
openYoutubePicker: false,
|
||||
openPlaylistBrowser: false,
|
||||
replayCurrentSubtitle: false,
|
||||
playNextSubtitle: false,
|
||||
shiftSubDelayPrevLine: false,
|
||||
shiftSubDelayNextLine: false,
|
||||
anilistStatus: false,
|
||||
anilistLogout: false,
|
||||
anilistSetup: false,
|
||||
|
||||
@@ -31,6 +31,13 @@ function makeArgs(overrides: Partial<CliArgs> = {}): CliArgs {
|
||||
markAudioCard: false,
|
||||
refreshKnownWords: false,
|
||||
openRuntimeOptions: false,
|
||||
openJimaku: false,
|
||||
openYoutubePicker: false,
|
||||
openPlaylistBrowser: false,
|
||||
replayCurrentSubtitle: false,
|
||||
playNextSubtitle: false,
|
||||
shiftSubDelayPrevLine: false,
|
||||
shiftSubDelayNextLine: false,
|
||||
anilistStatus: false,
|
||||
anilistLogout: false,
|
||||
anilistSetup: false,
|
||||
@@ -143,6 +150,9 @@ function createDeps(overrides: Partial<CliCommandServiceDeps> = {}) {
|
||||
openRuntimeOptionsPalette: () => {
|
||||
calls.push('openRuntimeOptionsPalette');
|
||||
},
|
||||
dispatchSessionAction: async () => {
|
||||
calls.push('dispatchSessionAction');
|
||||
},
|
||||
getAnilistStatus: () => ({
|
||||
tokenStatus: 'resolved',
|
||||
tokenSource: 'stored',
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { CliArgs, CliCommandSource, commandNeedsOverlayRuntime } from '../../cli/args';
|
||||
import type { SessionActionDispatchRequest } from '../../types/runtime';
|
||||
|
||||
export interface CliCommandServiceDeps {
|
||||
setLogLevel?: (level: NonNullable<CliArgs['logLevel']>) => void;
|
||||
@@ -32,6 +33,7 @@ export interface CliCommandServiceDeps {
|
||||
triggerSubsyncFromConfig: () => Promise<void>;
|
||||
markLastCardAsAudioCard: () => Promise<void>;
|
||||
openRuntimeOptionsPalette: () => void;
|
||||
dispatchSessionAction: (request: SessionActionDispatchRequest) => Promise<void>;
|
||||
getAnilistStatus: () => {
|
||||
tokenStatus: 'not_checked' | 'resolved' | 'error';
|
||||
tokenSource: 'none' | 'literal' | 'stored';
|
||||
@@ -168,6 +170,7 @@ export interface CliCommandDepsRuntimeOptions {
|
||||
};
|
||||
ui: UiCliRuntime;
|
||||
app: AppCliRuntime;
|
||||
dispatchSessionAction: (request: SessionActionDispatchRequest) => Promise<void>;
|
||||
getMultiCopyTimeoutMs: () => number;
|
||||
schedule: (fn: () => void, delayMs: number) => unknown;
|
||||
log: (message: string) => void;
|
||||
@@ -226,6 +229,7 @@ export function createCliCommandDepsRuntime(
|
||||
triggerSubsyncFromConfig: options.mining.triggerSubsyncFromConfig,
|
||||
markLastCardAsAudioCard: options.mining.markLastCardAsAudioCard,
|
||||
openRuntimeOptionsPalette: options.ui.openRuntimeOptionsPalette,
|
||||
dispatchSessionAction: options.dispatchSessionAction,
|
||||
getAnilistStatus: options.anilist.getStatus,
|
||||
clearAnilistToken: options.anilist.clearToken,
|
||||
openAnilistSetup: options.anilist.openSetup,
|
||||
@@ -268,6 +272,19 @@ export function handleCliCommand(
|
||||
source: CliCommandSource = 'initial',
|
||||
deps: CliCommandServiceDeps,
|
||||
): void {
|
||||
const dispatchCliSessionAction = (
|
||||
request: SessionActionDispatchRequest,
|
||||
logLabel: string,
|
||||
osdLabel: string,
|
||||
): void => {
|
||||
runAsyncWithOsd(
|
||||
() => deps.dispatchSessionAction?.(request) ?? Promise.resolve(),
|
||||
deps,
|
||||
logLabel,
|
||||
osdLabel,
|
||||
);
|
||||
};
|
||||
|
||||
if (args.logLevel) {
|
||||
deps.setLogLevel?.(args.logLevel);
|
||||
}
|
||||
@@ -381,6 +398,56 @@ export function handleCliCommand(
|
||||
);
|
||||
} else if (args.openRuntimeOptions) {
|
||||
deps.openRuntimeOptionsPalette();
|
||||
} else if (args.openJimaku) {
|
||||
dispatchCliSessionAction({ actionId: 'openJimaku' }, 'openJimaku', 'Open jimaku failed');
|
||||
} else if (args.openYoutubePicker) {
|
||||
dispatchCliSessionAction(
|
||||
{ actionId: 'openYoutubePicker' },
|
||||
'openYoutubePicker',
|
||||
'Open YouTube picker failed',
|
||||
);
|
||||
} else if (args.openPlaylistBrowser) {
|
||||
dispatchCliSessionAction(
|
||||
{ actionId: 'openPlaylistBrowser' },
|
||||
'openPlaylistBrowser',
|
||||
'Open playlist browser failed',
|
||||
);
|
||||
} else if (args.replayCurrentSubtitle) {
|
||||
dispatchCliSessionAction(
|
||||
{ actionId: 'replayCurrentSubtitle' },
|
||||
'replayCurrentSubtitle',
|
||||
'Replay subtitle failed',
|
||||
);
|
||||
} else if (args.playNextSubtitle) {
|
||||
dispatchCliSessionAction(
|
||||
{ actionId: 'playNextSubtitle' },
|
||||
'playNextSubtitle',
|
||||
'Play next subtitle failed',
|
||||
);
|
||||
} else if (args.shiftSubDelayPrevLine) {
|
||||
dispatchCliSessionAction(
|
||||
{ actionId: 'shiftSubDelayPrevLine' },
|
||||
'shiftSubDelayPrevLine',
|
||||
'Shift subtitle delay failed',
|
||||
);
|
||||
} else if (args.shiftSubDelayNextLine) {
|
||||
dispatchCliSessionAction(
|
||||
{ actionId: 'shiftSubDelayNextLine' },
|
||||
'shiftSubDelayNextLine',
|
||||
'Shift subtitle delay failed',
|
||||
);
|
||||
} else if (args.copySubtitleCount !== undefined) {
|
||||
dispatchCliSessionAction(
|
||||
{ actionId: 'copySubtitleMultiple', payload: { count: args.copySubtitleCount } },
|
||||
'copySubtitleMultiple',
|
||||
'Copy failed',
|
||||
);
|
||||
} else if (args.mineSentenceCount !== undefined) {
|
||||
dispatchCliSessionAction(
|
||||
{ actionId: 'mineSentenceMultiple', payload: { count: args.mineSentenceCount } },
|
||||
'mineSentenceMultiple',
|
||||
'Mine sentence failed',
|
||||
);
|
||||
} else if (args.anilistStatus) {
|
||||
const status = deps.getAnilistStatus();
|
||||
deps.log(`AniList token status: ${status.tokenStatus} (source=${status.tokenSource})`);
|
||||
|
||||
@@ -127,7 +127,9 @@ function createRegisterIpcDeps(overrides: Partial<IpcServiceDeps> = {}): IpcServ
|
||||
setMecabEnabled: () => {},
|
||||
handleMpvCommand: () => {},
|
||||
getKeybindings: () => [],
|
||||
getSessionBindings: () => [],
|
||||
getConfiguredShortcuts: () => ({}),
|
||||
dispatchSessionAction: async () => {},
|
||||
getStatsToggleKey: () => 'Backquote',
|
||||
getMarkWatchedKey: () => 'KeyW',
|
||||
getControllerConfig: () => createControllerConfigFixture(),
|
||||
@@ -226,7 +228,9 @@ test('createIpcDepsRuntime wires AniList handlers', async () => {
|
||||
getMecabTokenizer: () => null,
|
||||
handleMpvCommand: () => {},
|
||||
getKeybindings: () => [],
|
||||
getSessionBindings: () => [],
|
||||
getConfiguredShortcuts: () => ({}),
|
||||
dispatchSessionAction: async () => {},
|
||||
getStatsToggleKey: () => 'Backquote',
|
||||
getMarkWatchedKey: () => 'KeyW',
|
||||
getControllerConfig: () => createControllerConfigFixture(),
|
||||
@@ -382,7 +386,9 @@ test('registerIpcHandlers rejects malformed runtime-option payloads', async () =
|
||||
setMecabEnabled: () => {},
|
||||
handleMpvCommand: () => {},
|
||||
getKeybindings: () => [],
|
||||
getSessionBindings: () => [],
|
||||
getConfiguredShortcuts: () => ({}),
|
||||
dispatchSessionAction: async () => {},
|
||||
getStatsToggleKey: () => 'Backquote',
|
||||
getMarkWatchedKey: () => 'KeyW',
|
||||
getControllerConfig: () => createControllerConfigFixture(),
|
||||
@@ -707,7 +713,9 @@ test('registerIpcHandlers ignores malformed fire-and-forget payloads', () => {
|
||||
setMecabEnabled: () => {},
|
||||
handleMpvCommand: () => {},
|
||||
getKeybindings: () => [],
|
||||
getSessionBindings: () => [],
|
||||
getConfiguredShortcuts: () => ({}),
|
||||
dispatchSessionAction: async () => {},
|
||||
getStatsToggleKey: () => 'Backquote',
|
||||
getMarkWatchedKey: () => 'KeyW',
|
||||
getControllerConfig: () => createControllerConfigFixture(),
|
||||
@@ -786,7 +794,9 @@ test('registerIpcHandlers awaits saveControllerPreference through request-respon
|
||||
setMecabEnabled: () => {},
|
||||
handleMpvCommand: () => {},
|
||||
getKeybindings: () => [],
|
||||
getSessionBindings: () => [],
|
||||
getConfiguredShortcuts: () => ({}),
|
||||
dispatchSessionAction: async () => {},
|
||||
getStatsToggleKey: () => 'Backquote',
|
||||
getMarkWatchedKey: () => 'KeyW',
|
||||
getControllerConfig: () => createControllerConfigFixture(),
|
||||
@@ -872,7 +882,9 @@ test('registerIpcHandlers rejects malformed controller preference payloads', asy
|
||||
setMecabEnabled: () => {},
|
||||
handleMpvCommand: () => {},
|
||||
getKeybindings: () => [],
|
||||
getSessionBindings: () => [],
|
||||
getConfiguredShortcuts: () => ({}),
|
||||
dispatchSessionAction: async () => {},
|
||||
getStatsToggleKey: () => 'Backquote',
|
||||
getMarkWatchedKey: () => 'KeyW',
|
||||
getControllerConfig: () => createControllerConfigFixture(),
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import electron from 'electron';
|
||||
import type { IpcMainEvent } from 'electron';
|
||||
import type { BrowserWindow as ElectronBrowserWindow, IpcMainEvent } from 'electron';
|
||||
import type {
|
||||
CompiledSessionBinding,
|
||||
ControllerConfigUpdate,
|
||||
PlaylistBrowserMutationResult,
|
||||
PlaylistBrowserSnapshot,
|
||||
@@ -12,6 +13,7 @@ import type {
|
||||
SubtitlePosition,
|
||||
SubsyncManualRunRequest,
|
||||
SubsyncResult,
|
||||
SessionActionDispatchRequest,
|
||||
YoutubePickerResolveRequest,
|
||||
YoutubePickerResolveResult,
|
||||
} from '../../types';
|
||||
@@ -30,11 +32,14 @@ import {
|
||||
parseYoutubePickerResolveRequest,
|
||||
} from '../../shared/ipc/validators';
|
||||
|
||||
const { BrowserWindow, ipcMain } = electron;
|
||||
const { ipcMain } = electron;
|
||||
|
||||
export interface IpcServiceDeps {
|
||||
onOverlayModalClosed: (modal: OverlayHostedModal) => void;
|
||||
onOverlayModalOpened?: (modal: OverlayHostedModal) => void;
|
||||
onOverlayModalOpened?: (
|
||||
modal: OverlayHostedModal,
|
||||
senderWindow: ElectronBrowserWindow | null,
|
||||
) => void;
|
||||
openYomitanSettings: () => void;
|
||||
quitApp: () => void;
|
||||
toggleDevTools: () => void;
|
||||
@@ -56,7 +61,9 @@ export interface IpcServiceDeps {
|
||||
setMecabEnabled: (enabled: boolean) => void;
|
||||
handleMpvCommand: (command: Array<string | number>) => void;
|
||||
getKeybindings: () => unknown;
|
||||
getSessionBindings?: () => CompiledSessionBinding[];
|
||||
getConfiguredShortcuts: () => unknown;
|
||||
dispatchSessionAction?: (request: SessionActionDispatchRequest) => void | Promise<void>;
|
||||
getStatsToggleKey: () => string;
|
||||
getMarkWatchedKey: () => string;
|
||||
getControllerConfig: () => ResolvedControllerConfig;
|
||||
@@ -154,7 +161,10 @@ export interface IpcDepsRuntimeOptions {
|
||||
getMainWindow: () => WindowLike | null;
|
||||
getVisibleOverlayVisibility: () => boolean;
|
||||
onOverlayModalClosed: (modal: OverlayHostedModal) => void;
|
||||
onOverlayModalOpened?: (modal: OverlayHostedModal) => void;
|
||||
onOverlayModalOpened?: (
|
||||
modal: OverlayHostedModal,
|
||||
senderWindow: ElectronBrowserWindow | null,
|
||||
) => void;
|
||||
openYomitanSettings: () => void;
|
||||
quitApp: () => void;
|
||||
toggleVisibleOverlay: () => void;
|
||||
@@ -169,7 +179,9 @@ export interface IpcDepsRuntimeOptions {
|
||||
getMecabTokenizer: () => MecabTokenizerLike | null;
|
||||
handleMpvCommand: (command: Array<string | number>) => void;
|
||||
getKeybindings: () => unknown;
|
||||
getSessionBindings?: () => CompiledSessionBinding[];
|
||||
getConfiguredShortcuts: () => unknown;
|
||||
dispatchSessionAction?: (request: SessionActionDispatchRequest) => void | Promise<void>;
|
||||
getStatsToggleKey: () => string;
|
||||
getMarkWatchedKey: () => string;
|
||||
getControllerConfig: () => ResolvedControllerConfig;
|
||||
@@ -238,7 +250,9 @@ export function createIpcDepsRuntime(options: IpcDepsRuntimeOptions): IpcService
|
||||
},
|
||||
handleMpvCommand: options.handleMpvCommand,
|
||||
getKeybindings: options.getKeybindings,
|
||||
getSessionBindings: options.getSessionBindings ?? (() => []),
|
||||
getConfiguredShortcuts: options.getConfiguredShortcuts,
|
||||
dispatchSessionAction: options.dispatchSessionAction ?? (async () => {}),
|
||||
getStatsToggleKey: options.getStatsToggleKey,
|
||||
getMarkWatchedKey: options.getMarkWatchedKey,
|
||||
getControllerConfig: options.getControllerConfig,
|
||||
@@ -299,7 +313,8 @@ export function registerIpcHandlers(deps: IpcServiceDeps, ipc: IpcMainRegistrar
|
||||
(event: unknown, ignore: unknown, options: unknown = {}) => {
|
||||
if (typeof ignore !== 'boolean') return;
|
||||
const parsedOptions = parseOptionalForwardingOptions(options);
|
||||
const senderWindow = BrowserWindow.fromWebContents((event as IpcMainEvent).sender);
|
||||
const senderWindow =
|
||||
electron.BrowserWindow?.fromWebContents((event as IpcMainEvent).sender) ?? null;
|
||||
if (senderWindow && !senderWindow.isDestroyed()) {
|
||||
senderWindow.setIgnoreMouseEvents(ignore, parsedOptions);
|
||||
}
|
||||
@@ -311,11 +326,13 @@ export function registerIpcHandlers(deps: IpcServiceDeps, ipc: IpcMainRegistrar
|
||||
if (!parsedModal) return;
|
||||
deps.onOverlayModalClosed(parsedModal);
|
||||
});
|
||||
ipc.on(IPC_CHANNELS.command.overlayModalOpened, (_event: unknown, modal: unknown) => {
|
||||
ipc.on(IPC_CHANNELS.command.overlayModalOpened, (event: unknown, modal: unknown) => {
|
||||
const parsedModal = parseOverlayHostedModal(modal);
|
||||
if (!parsedModal) return;
|
||||
if (!deps.onOverlayModalOpened) return;
|
||||
deps.onOverlayModalOpened(parsedModal);
|
||||
const senderWindow =
|
||||
electron.BrowserWindow?.fromWebContents((event as IpcMainEvent).sender) ?? null;
|
||||
deps.onOverlayModalOpened(parsedModal, senderWindow);
|
||||
});
|
||||
|
||||
ipc.handle(
|
||||
@@ -431,10 +448,36 @@ export function registerIpcHandlers(deps: IpcServiceDeps, ipc: IpcMainRegistrar
|
||||
deps.handleMpvCommand(parsedCommand);
|
||||
});
|
||||
|
||||
ipc.handle(
|
||||
IPC_CHANNELS.command.dispatchSessionAction,
|
||||
async (_event: unknown, request: unknown) => {
|
||||
if (!request || typeof request !== 'object') {
|
||||
throw new Error('Invalid session action payload');
|
||||
}
|
||||
const actionId =
|
||||
typeof (request as Record<string, unknown>).actionId === 'string'
|
||||
? ((request as Record<string, unknown>).actionId as SessionActionDispatchRequest['actionId'])
|
||||
: null;
|
||||
if (!actionId) {
|
||||
throw new Error('Invalid session action id');
|
||||
}
|
||||
const payload =
|
||||
(request as Record<string, unknown>).payload &&
|
||||
typeof (request as Record<string, unknown>).payload === 'object'
|
||||
? ((request as Record<string, unknown>).payload as SessionActionDispatchRequest['payload'])
|
||||
: undefined;
|
||||
await deps.dispatchSessionAction?.({ actionId, payload });
|
||||
},
|
||||
);
|
||||
|
||||
ipc.handle(IPC_CHANNELS.request.getKeybindings, () => {
|
||||
return deps.getKeybindings();
|
||||
});
|
||||
|
||||
ipc.handle(IPC_CHANNELS.request.getSessionBindings, () => {
|
||||
return deps.getSessionBindings?.() ?? [];
|
||||
});
|
||||
|
||||
ipc.handle(IPC_CHANNELS.request.getConfigShortcuts, () => {
|
||||
return deps.getConfiguredShortcuts();
|
||||
});
|
||||
|
||||
@@ -1,10 +1,4 @@
|
||||
import electron from 'electron';
|
||||
import { ConfiguredShortcuts } from '../utils/shortcut-config';
|
||||
import { isGlobalShortcutRegisteredSafe } from './shortcut-fallback';
|
||||
import { createLogger } from '../../logger';
|
||||
|
||||
const { globalShortcut } = electron;
|
||||
const logger = createLogger('main:overlay-shortcut-service');
|
||||
|
||||
export interface OverlayShortcutHandlers {
|
||||
copySubtitle: () => void;
|
||||
@@ -42,140 +36,13 @@ export function shouldActivateOverlayShortcuts(args: {
|
||||
}
|
||||
|
||||
export function registerOverlayShortcuts(
|
||||
shortcuts: ConfiguredShortcuts,
|
||||
handlers: OverlayShortcutHandlers,
|
||||
_shortcuts: ConfiguredShortcuts,
|
||||
_handlers: OverlayShortcutHandlers,
|
||||
): boolean {
|
||||
let registeredAny = false;
|
||||
const registerOverlayShortcut = (
|
||||
accelerator: string,
|
||||
handler: () => void,
|
||||
label: string,
|
||||
): void => {
|
||||
if (isGlobalShortcutRegisteredSafe(accelerator)) {
|
||||
registeredAny = true;
|
||||
return;
|
||||
}
|
||||
const ok = globalShortcut.register(accelerator, handler);
|
||||
if (!ok) {
|
||||
logger.warn(`Failed to register overlay shortcut ${label}: ${accelerator}`);
|
||||
return;
|
||||
}
|
||||
registeredAny = true;
|
||||
};
|
||||
|
||||
if (shortcuts.copySubtitleMultiple) {
|
||||
registerOverlayShortcut(
|
||||
shortcuts.copySubtitleMultiple,
|
||||
() => handlers.copySubtitleMultiple(shortcuts.multiCopyTimeoutMs),
|
||||
'copySubtitleMultiple',
|
||||
);
|
||||
}
|
||||
|
||||
if (shortcuts.copySubtitle) {
|
||||
registerOverlayShortcut(shortcuts.copySubtitle, () => handlers.copySubtitle(), 'copySubtitle');
|
||||
}
|
||||
|
||||
if (shortcuts.triggerFieldGrouping) {
|
||||
registerOverlayShortcut(
|
||||
shortcuts.triggerFieldGrouping,
|
||||
() => handlers.triggerFieldGrouping(),
|
||||
'triggerFieldGrouping',
|
||||
);
|
||||
}
|
||||
|
||||
if (shortcuts.triggerSubsync) {
|
||||
registerOverlayShortcut(
|
||||
shortcuts.triggerSubsync,
|
||||
() => handlers.triggerSubsync(),
|
||||
'triggerSubsync',
|
||||
);
|
||||
}
|
||||
|
||||
if (shortcuts.mineSentence) {
|
||||
registerOverlayShortcut(shortcuts.mineSentence, () => handlers.mineSentence(), 'mineSentence');
|
||||
}
|
||||
|
||||
if (shortcuts.mineSentenceMultiple) {
|
||||
registerOverlayShortcut(
|
||||
shortcuts.mineSentenceMultiple,
|
||||
() => handlers.mineSentenceMultiple(shortcuts.multiCopyTimeoutMs),
|
||||
'mineSentenceMultiple',
|
||||
);
|
||||
}
|
||||
|
||||
if (shortcuts.toggleSecondarySub) {
|
||||
registerOverlayShortcut(
|
||||
shortcuts.toggleSecondarySub,
|
||||
() => handlers.toggleSecondarySub(),
|
||||
'toggleSecondarySub',
|
||||
);
|
||||
}
|
||||
|
||||
if (shortcuts.updateLastCardFromClipboard) {
|
||||
registerOverlayShortcut(
|
||||
shortcuts.updateLastCardFromClipboard,
|
||||
() => handlers.updateLastCardFromClipboard(),
|
||||
'updateLastCardFromClipboard',
|
||||
);
|
||||
}
|
||||
|
||||
if (shortcuts.markAudioCard) {
|
||||
registerOverlayShortcut(
|
||||
shortcuts.markAudioCard,
|
||||
() => handlers.markAudioCard(),
|
||||
'markAudioCard',
|
||||
);
|
||||
}
|
||||
|
||||
if (shortcuts.openRuntimeOptions) {
|
||||
registerOverlayShortcut(
|
||||
shortcuts.openRuntimeOptions,
|
||||
() => handlers.openRuntimeOptions(),
|
||||
'openRuntimeOptions',
|
||||
);
|
||||
}
|
||||
if (shortcuts.openJimaku) {
|
||||
registerOverlayShortcut(shortcuts.openJimaku, () => handlers.openJimaku(), 'openJimaku');
|
||||
}
|
||||
|
||||
return registeredAny;
|
||||
return false;
|
||||
}
|
||||
|
||||
export function unregisterOverlayShortcuts(shortcuts: ConfiguredShortcuts): void {
|
||||
if (shortcuts.copySubtitle) {
|
||||
globalShortcut.unregister(shortcuts.copySubtitle);
|
||||
}
|
||||
if (shortcuts.copySubtitleMultiple) {
|
||||
globalShortcut.unregister(shortcuts.copySubtitleMultiple);
|
||||
}
|
||||
if (shortcuts.updateLastCardFromClipboard) {
|
||||
globalShortcut.unregister(shortcuts.updateLastCardFromClipboard);
|
||||
}
|
||||
if (shortcuts.triggerFieldGrouping) {
|
||||
globalShortcut.unregister(shortcuts.triggerFieldGrouping);
|
||||
}
|
||||
if (shortcuts.triggerSubsync) {
|
||||
globalShortcut.unregister(shortcuts.triggerSubsync);
|
||||
}
|
||||
if (shortcuts.mineSentence) {
|
||||
globalShortcut.unregister(shortcuts.mineSentence);
|
||||
}
|
||||
if (shortcuts.mineSentenceMultiple) {
|
||||
globalShortcut.unregister(shortcuts.mineSentenceMultiple);
|
||||
}
|
||||
if (shortcuts.toggleSecondarySub) {
|
||||
globalShortcut.unregister(shortcuts.toggleSecondarySub);
|
||||
}
|
||||
if (shortcuts.markAudioCard) {
|
||||
globalShortcut.unregister(shortcuts.markAudioCard);
|
||||
}
|
||||
if (shortcuts.openRuntimeOptions) {
|
||||
globalShortcut.unregister(shortcuts.openRuntimeOptions);
|
||||
}
|
||||
if (shortcuts.openJimaku) {
|
||||
globalShortcut.unregister(shortcuts.openJimaku);
|
||||
}
|
||||
}
|
||||
export function unregisterOverlayShortcuts(_shortcuts: ConfiguredShortcuts): void {}
|
||||
|
||||
export function registerOverlayShortcutsRuntime(deps: OverlayShortcutLifecycleDeps): boolean {
|
||||
return registerOverlayShortcuts(deps.getConfiguredShortcuts(), deps.getOverlayHandlers());
|
||||
|
||||
109
src/core/services/session-actions.ts
Normal file
109
src/core/services/session-actions.ts
Normal file
@@ -0,0 +1,109 @@
|
||||
import type { RuntimeOptionApplyResult, RuntimeOptionId } from '../../types';
|
||||
import type { SessionActionId } from '../../types/session-bindings';
|
||||
import type { SessionActionDispatchRequest } from '../../types/runtime';
|
||||
|
||||
export interface SessionActionExecutorDeps {
|
||||
toggleStatsOverlay: () => void;
|
||||
toggleVisibleOverlay: () => void;
|
||||
copyCurrentSubtitle: () => void;
|
||||
copySubtitleCount: (count: number) => void;
|
||||
updateLastCardFromClipboard: () => Promise<void>;
|
||||
triggerFieldGrouping: () => Promise<void>;
|
||||
triggerSubsyncFromConfig: () => Promise<void>;
|
||||
mineSentenceCard: () => Promise<void>;
|
||||
mineSentenceCount: (count: number) => void;
|
||||
toggleSecondarySub: () => void;
|
||||
markLastCardAsAudioCard: () => Promise<void>;
|
||||
openRuntimeOptionsPalette: () => void;
|
||||
openJimaku: () => void;
|
||||
openYoutubeTrackPicker: () => void | Promise<void>;
|
||||
openPlaylistBrowser: () => boolean | void | Promise<boolean | void>;
|
||||
replayCurrentSubtitle: () => void;
|
||||
playNextSubtitle: () => void;
|
||||
shiftSubDelayToAdjacentSubtitle: (direction: 'next' | 'previous') => Promise<void>;
|
||||
cycleRuntimeOption: (id: RuntimeOptionId, direction: 1 | -1) => RuntimeOptionApplyResult;
|
||||
showMpvOsd: (text: string) => void;
|
||||
}
|
||||
|
||||
function resolveCount(count: number | undefined): number {
|
||||
const normalized = typeof count === 'number' && Number.isInteger(count) ? count : 1;
|
||||
return Math.min(9, Math.max(1, normalized));
|
||||
}
|
||||
|
||||
export async function dispatchSessionAction(
|
||||
request: SessionActionDispatchRequest,
|
||||
deps: SessionActionExecutorDeps,
|
||||
): Promise<void> {
|
||||
switch (request.actionId) {
|
||||
case 'toggleStatsOverlay':
|
||||
deps.toggleStatsOverlay();
|
||||
return;
|
||||
case 'toggleVisibleOverlay':
|
||||
deps.toggleVisibleOverlay();
|
||||
return;
|
||||
case 'copySubtitle':
|
||||
deps.copyCurrentSubtitle();
|
||||
return;
|
||||
case 'copySubtitleMultiple':
|
||||
deps.copySubtitleCount(resolveCount(request.payload?.count));
|
||||
return;
|
||||
case 'updateLastCardFromClipboard':
|
||||
await deps.updateLastCardFromClipboard();
|
||||
return;
|
||||
case 'triggerFieldGrouping':
|
||||
await deps.triggerFieldGrouping();
|
||||
return;
|
||||
case 'triggerSubsync':
|
||||
await deps.triggerSubsyncFromConfig();
|
||||
return;
|
||||
case 'mineSentence':
|
||||
await deps.mineSentenceCard();
|
||||
return;
|
||||
case 'mineSentenceMultiple':
|
||||
deps.mineSentenceCount(resolveCount(request.payload?.count));
|
||||
return;
|
||||
case 'toggleSecondarySub':
|
||||
deps.toggleSecondarySub();
|
||||
return;
|
||||
case 'markAudioCard':
|
||||
await deps.markLastCardAsAudioCard();
|
||||
return;
|
||||
case 'openRuntimeOptions':
|
||||
deps.openRuntimeOptionsPalette();
|
||||
return;
|
||||
case 'openJimaku':
|
||||
deps.openJimaku();
|
||||
return;
|
||||
case 'openYoutubePicker':
|
||||
await deps.openYoutubeTrackPicker();
|
||||
return;
|
||||
case 'openPlaylistBrowser':
|
||||
await deps.openPlaylistBrowser();
|
||||
return;
|
||||
case 'replayCurrentSubtitle':
|
||||
deps.replayCurrentSubtitle();
|
||||
return;
|
||||
case 'playNextSubtitle':
|
||||
deps.playNextSubtitle();
|
||||
return;
|
||||
case 'shiftSubDelayPrevLine':
|
||||
await deps.shiftSubDelayToAdjacentSubtitle('previous');
|
||||
return;
|
||||
case 'shiftSubDelayNextLine':
|
||||
await deps.shiftSubDelayToAdjacentSubtitle('next');
|
||||
return;
|
||||
case 'cycleRuntimeOption': {
|
||||
const runtimeOptionId = request.payload?.runtimeOptionId as RuntimeOptionId | undefined;
|
||||
if (!runtimeOptionId) {
|
||||
deps.showMpvOsd('Runtime option id is required.');
|
||||
return;
|
||||
}
|
||||
const direction = request.payload?.direction === -1 ? -1 : 1;
|
||||
const result = deps.cycleRuntimeOption(runtimeOptionId, direction);
|
||||
if (!result.ok && result.error) {
|
||||
deps.showMpvOsd(result.error);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
175
src/core/services/session-bindings.test.ts
Normal file
175
src/core/services/session-bindings.test.ts
Normal file
@@ -0,0 +1,175 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import type { Keybinding } from '../../types';
|
||||
import type { ConfiguredShortcuts } from '../utils/shortcut-config';
|
||||
import { SPECIAL_COMMANDS } from '../../config/definitions';
|
||||
import { compileSessionBindings } from './session-bindings';
|
||||
|
||||
function createShortcuts(overrides: Partial<ConfiguredShortcuts> = {}): ConfiguredShortcuts {
|
||||
return {
|
||||
toggleVisibleOverlayGlobal: null,
|
||||
copySubtitle: null,
|
||||
copySubtitleMultiple: null,
|
||||
updateLastCardFromClipboard: null,
|
||||
triggerFieldGrouping: null,
|
||||
triggerSubsync: null,
|
||||
mineSentence: null,
|
||||
mineSentenceMultiple: null,
|
||||
multiCopyTimeoutMs: 2500,
|
||||
toggleSecondarySub: null,
|
||||
markAudioCard: null,
|
||||
openRuntimeOptions: null,
|
||||
openJimaku: null,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function createKeybinding(key: string, command: Keybinding['command']): Keybinding {
|
||||
return { key, command };
|
||||
}
|
||||
|
||||
test('compileSessionBindings merges shortcuts and keybindings into one canonical list', () => {
|
||||
const result = compileSessionBindings({
|
||||
shortcuts: createShortcuts({
|
||||
toggleVisibleOverlayGlobal: 'Alt+Shift+O',
|
||||
openJimaku: 'Ctrl+Shift+J',
|
||||
}),
|
||||
keybindings: [
|
||||
createKeybinding('KeyF', ['cycle', 'fullscreen']),
|
||||
createKeybinding('Ctrl+Shift+Y', [SPECIAL_COMMANDS.YOUTUBE_PICKER_OPEN]),
|
||||
],
|
||||
platform: 'linux',
|
||||
});
|
||||
|
||||
assert.equal(result.warnings.length, 0);
|
||||
assert.deepEqual(
|
||||
result.bindings.map((binding) => ({
|
||||
actionType: binding.actionType,
|
||||
sourcePath: binding.sourcePath,
|
||||
code: binding.key.code,
|
||||
modifiers: binding.key.modifiers,
|
||||
target:
|
||||
binding.actionType === 'session-action'
|
||||
? binding.actionId
|
||||
: binding.command.join(' '),
|
||||
})),
|
||||
[
|
||||
{
|
||||
actionType: 'mpv-command',
|
||||
sourcePath: 'keybindings[0].key',
|
||||
code: 'KeyF',
|
||||
modifiers: [],
|
||||
target: 'cycle fullscreen',
|
||||
},
|
||||
{
|
||||
actionType: 'session-action',
|
||||
sourcePath: 'keybindings[1].key',
|
||||
code: 'KeyY',
|
||||
modifiers: ['ctrl', 'shift'],
|
||||
target: 'openYoutubePicker',
|
||||
},
|
||||
{
|
||||
actionType: 'session-action',
|
||||
sourcePath: 'shortcuts.openJimaku',
|
||||
code: 'KeyJ',
|
||||
modifiers: ['ctrl', 'shift'],
|
||||
target: 'openJimaku',
|
||||
},
|
||||
{
|
||||
actionType: 'session-action',
|
||||
sourcePath: 'shortcuts.toggleVisibleOverlayGlobal',
|
||||
code: 'KeyO',
|
||||
modifiers: ['alt', 'shift'],
|
||||
target: 'toggleVisibleOverlay',
|
||||
},
|
||||
],
|
||||
);
|
||||
});
|
||||
|
||||
test('compileSessionBindings resolves CommandOrControl per platform', () => {
|
||||
const input = {
|
||||
shortcuts: createShortcuts({
|
||||
toggleVisibleOverlayGlobal: 'CommandOrControl+Shift+O',
|
||||
}),
|
||||
keybindings: [],
|
||||
};
|
||||
|
||||
const windows = compileSessionBindings({ ...input, platform: 'win32' });
|
||||
const mac = compileSessionBindings({ ...input, platform: 'darwin' });
|
||||
|
||||
assert.deepEqual(windows.bindings[0]?.key.modifiers, ['ctrl', 'shift']);
|
||||
assert.deepEqual(mac.bindings[0]?.key.modifiers, ['shift', 'meta']);
|
||||
});
|
||||
|
||||
test('compileSessionBindings drops conflicting bindings that canonicalize to the same key', () => {
|
||||
const result = compileSessionBindings({
|
||||
shortcuts: createShortcuts({
|
||||
openJimaku: 'Ctrl+Shift+J',
|
||||
}),
|
||||
keybindings: [createKeybinding('Ctrl+Shift+J', [SPECIAL_COMMANDS.PLAYLIST_BROWSER_OPEN])],
|
||||
platform: 'linux',
|
||||
});
|
||||
|
||||
assert.deepEqual(result.bindings, []);
|
||||
assert.equal(result.warnings.length, 1);
|
||||
assert.equal(result.warnings[0]?.kind, 'conflict');
|
||||
assert.deepEqual(result.warnings[0]?.conflictingPaths, [
|
||||
'shortcuts.openJimaku',
|
||||
'keybindings[0].key',
|
||||
]);
|
||||
});
|
||||
|
||||
test('compileSessionBindings omits disabled bindings', () => {
|
||||
const result = compileSessionBindings({
|
||||
shortcuts: createShortcuts({
|
||||
openJimaku: null,
|
||||
toggleVisibleOverlayGlobal: 'Alt+Shift+O',
|
||||
}),
|
||||
keybindings: [createKeybinding('Ctrl+Shift+J', null)],
|
||||
platform: 'linux',
|
||||
});
|
||||
|
||||
assert.equal(result.warnings.length, 0);
|
||||
assert.deepEqual(result.bindings.map((binding) => binding.sourcePath), [
|
||||
'shortcuts.toggleVisibleOverlayGlobal',
|
||||
]);
|
||||
});
|
||||
|
||||
test('compileSessionBindings warns on unsupported shortcut and keybinding syntax', () => {
|
||||
const result = compileSessionBindings({
|
||||
shortcuts: createShortcuts({
|
||||
openJimaku: 'Hyper+J',
|
||||
}),
|
||||
keybindings: [createKeybinding('Ctrl+ß', ['cycle', 'fullscreen'])],
|
||||
platform: 'linux',
|
||||
});
|
||||
|
||||
assert.deepEqual(result.bindings, []);
|
||||
assert.deepEqual(
|
||||
result.warnings.map((warning) => `${warning.kind}:${warning.path}`),
|
||||
['unsupported:shortcuts.openJimaku', 'unsupported:keybindings[0].key'],
|
||||
);
|
||||
});
|
||||
|
||||
test('compileSessionBindings warns on deprecated toggleVisibleOverlayGlobal config', () => {
|
||||
const result = compileSessionBindings({
|
||||
shortcuts: createShortcuts(),
|
||||
keybindings: [],
|
||||
platform: 'linux',
|
||||
rawConfig: {
|
||||
shortcuts: {
|
||||
toggleVisibleOverlayGlobal: 'Alt+Shift+O',
|
||||
},
|
||||
} as never,
|
||||
});
|
||||
|
||||
assert.equal(result.bindings.length, 0);
|
||||
assert.deepEqual(result.warnings, [
|
||||
{
|
||||
kind: 'deprecated-config',
|
||||
path: 'shortcuts.toggleVisibleOverlayGlobal',
|
||||
value: 'Alt+Shift+O',
|
||||
message: 'Rename shortcuts.toggleVisibleOverlayGlobal to shortcuts.toggleVisibleOverlay.',
|
||||
},
|
||||
]);
|
||||
});
|
||||
426
src/core/services/session-bindings.ts
Normal file
426
src/core/services/session-bindings.ts
Normal file
@@ -0,0 +1,426 @@
|
||||
import type { Keybinding, ResolvedConfig } from '../../types';
|
||||
import type { ConfiguredShortcuts } from '../utils/shortcut-config';
|
||||
import type {
|
||||
CompiledMpvCommandBinding,
|
||||
CompiledSessionActionBinding,
|
||||
CompiledSessionBinding,
|
||||
PluginSessionBindingsArtifact,
|
||||
SessionActionId,
|
||||
SessionBindingWarning,
|
||||
SessionKeyModifier,
|
||||
SessionKeySpec,
|
||||
} from '../../types/session-bindings';
|
||||
import { SPECIAL_COMMANDS } from '../../config';
|
||||
|
||||
type PlatformKeyModel = 'darwin' | 'win32' | 'linux';
|
||||
|
||||
type CompileSessionBindingsInput = {
|
||||
keybindings: Keybinding[];
|
||||
shortcuts: ConfiguredShortcuts;
|
||||
statsToggleKey?: string | null;
|
||||
platform: PlatformKeyModel;
|
||||
rawConfig?: ResolvedConfig | null;
|
||||
};
|
||||
|
||||
type DraftBinding = {
|
||||
binding: CompiledSessionBinding;
|
||||
actionFingerprint: string;
|
||||
};
|
||||
|
||||
const MODIFIER_ORDER: SessionKeyModifier[] = ['ctrl', 'alt', 'shift', 'meta'];
|
||||
|
||||
const SESSION_SHORTCUT_ACTIONS: Array<{
|
||||
key: keyof Omit<ConfiguredShortcuts, 'multiCopyTimeoutMs'>;
|
||||
actionId: SessionActionId;
|
||||
}> = [
|
||||
{ key: 'toggleVisibleOverlayGlobal', actionId: 'toggleVisibleOverlay' },
|
||||
{ key: 'copySubtitle', actionId: 'copySubtitle' },
|
||||
{ key: 'copySubtitleMultiple', actionId: 'copySubtitleMultiple' },
|
||||
{ key: 'updateLastCardFromClipboard', actionId: 'updateLastCardFromClipboard' },
|
||||
{ key: 'triggerFieldGrouping', actionId: 'triggerFieldGrouping' },
|
||||
{ key: 'triggerSubsync', actionId: 'triggerSubsync' },
|
||||
{ key: 'mineSentence', actionId: 'mineSentence' },
|
||||
{ key: 'mineSentenceMultiple', actionId: 'mineSentenceMultiple' },
|
||||
{ key: 'toggleSecondarySub', actionId: 'toggleSecondarySub' },
|
||||
{ key: 'markAudioCard', actionId: 'markAudioCard' },
|
||||
{ key: 'openRuntimeOptions', actionId: 'openRuntimeOptions' },
|
||||
{ key: 'openJimaku', actionId: 'openJimaku' },
|
||||
];
|
||||
|
||||
function normalizeModifiers(modifiers: SessionKeyModifier[]): SessionKeyModifier[] {
|
||||
return [...new Set(modifiers)].sort(
|
||||
(left, right) => MODIFIER_ORDER.indexOf(left) - MODIFIER_ORDER.indexOf(right),
|
||||
);
|
||||
}
|
||||
|
||||
function normalizeCodeToken(token: string): string | null {
|
||||
const normalized = token.trim();
|
||||
if (!normalized) return null;
|
||||
if (/^[a-z]$/i.test(normalized)) {
|
||||
return `Key${normalized.toUpperCase()}`;
|
||||
}
|
||||
if (/^[0-9]$/.test(normalized)) {
|
||||
return `Digit${normalized}`;
|
||||
}
|
||||
|
||||
const exactMap: Record<string, string> = {
|
||||
space: 'Space',
|
||||
tab: 'Tab',
|
||||
enter: 'Enter',
|
||||
return: 'Enter',
|
||||
esc: 'Escape',
|
||||
escape: 'Escape',
|
||||
up: 'ArrowUp',
|
||||
down: 'ArrowDown',
|
||||
left: 'ArrowLeft',
|
||||
right: 'ArrowRight',
|
||||
backspace: 'Backspace',
|
||||
delete: 'Delete',
|
||||
slash: 'Slash',
|
||||
backslash: 'Backslash',
|
||||
minus: 'Minus',
|
||||
plus: 'Equal',
|
||||
equal: 'Equal',
|
||||
comma: 'Comma',
|
||||
period: 'Period',
|
||||
quote: 'Quote',
|
||||
semicolon: 'Semicolon',
|
||||
bracketleft: 'BracketLeft',
|
||||
bracketright: 'BracketRight',
|
||||
backquote: 'Backquote',
|
||||
};
|
||||
const lower = normalized.toLowerCase();
|
||||
if (exactMap[lower]) return exactMap[lower];
|
||||
if (
|
||||
/^key[a-z]$/i.test(normalized) ||
|
||||
/^digit[0-9]$/i.test(normalized) ||
|
||||
/^arrow(?:up|down|left|right)$/i.test(normalized) ||
|
||||
/^f\d{1,2}$/i.test(normalized)
|
||||
) {
|
||||
return normalized[0]!.toUpperCase() + normalized.slice(1);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function parseAccelerator(
|
||||
accelerator: string,
|
||||
platform: PlatformKeyModel,
|
||||
): { key: SessionKeySpec | null; message?: string } {
|
||||
const normalized = accelerator.replace(/\s+/g, '').replace(/cmdorctrl/gi, 'CommandOrControl');
|
||||
if (!normalized) {
|
||||
return { key: null, message: 'Empty accelerator is not supported.' };
|
||||
}
|
||||
|
||||
const parts = normalized.split('+').filter(Boolean);
|
||||
const keyToken = parts.pop();
|
||||
if (!keyToken) {
|
||||
return { key: null, message: 'Missing accelerator key token.' };
|
||||
}
|
||||
|
||||
const modifiers: SessionKeyModifier[] = [];
|
||||
for (const modifier of parts) {
|
||||
const lower = modifier.toLowerCase();
|
||||
if (lower === 'ctrl' || lower === 'control') {
|
||||
modifiers.push('ctrl');
|
||||
continue;
|
||||
}
|
||||
if (lower === 'alt' || lower === 'option') {
|
||||
modifiers.push('alt');
|
||||
continue;
|
||||
}
|
||||
if (lower === 'shift') {
|
||||
modifiers.push('shift');
|
||||
continue;
|
||||
}
|
||||
if (lower === 'meta' || lower === 'super' || lower === 'command' || lower === 'cmd') {
|
||||
modifiers.push('meta');
|
||||
continue;
|
||||
}
|
||||
if (lower === 'commandorcontrol') {
|
||||
modifiers.push(platform === 'darwin' ? 'meta' : 'ctrl');
|
||||
continue;
|
||||
}
|
||||
return {
|
||||
key: null,
|
||||
message: `Unsupported accelerator modifier: ${modifier}`,
|
||||
};
|
||||
}
|
||||
|
||||
const code = normalizeCodeToken(keyToken);
|
||||
if (!code) {
|
||||
return {
|
||||
key: null,
|
||||
message: `Unsupported accelerator key token: ${keyToken}`,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
key: {
|
||||
code,
|
||||
modifiers: normalizeModifiers(modifiers),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function parseDomKeyString(key: string): { key: SessionKeySpec | null; message?: string } {
|
||||
const parts = key
|
||||
.split('+')
|
||||
.map((part) => part.trim())
|
||||
.filter(Boolean);
|
||||
const keyToken = parts.pop();
|
||||
if (!keyToken) {
|
||||
return { key: null, message: 'Missing keybinding key token.' };
|
||||
}
|
||||
|
||||
const modifiers: SessionKeyModifier[] = [];
|
||||
for (const modifier of parts) {
|
||||
const lower = modifier.toLowerCase();
|
||||
if (lower === 'ctrl' || lower === 'control') {
|
||||
modifiers.push('ctrl');
|
||||
continue;
|
||||
}
|
||||
if (lower === 'alt' || lower === 'option') {
|
||||
modifiers.push('alt');
|
||||
continue;
|
||||
}
|
||||
if (lower === 'shift') {
|
||||
modifiers.push('shift');
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
lower === 'meta' ||
|
||||
lower === 'super' ||
|
||||
lower === 'command' ||
|
||||
lower === 'cmd' ||
|
||||
lower === 'commandorcontrol'
|
||||
) {
|
||||
modifiers.push(lower === 'commandorcontrol' ? 'ctrl' : 'meta');
|
||||
continue;
|
||||
}
|
||||
return {
|
||||
key: null,
|
||||
message: `Unsupported keybinding modifier: ${modifier}`,
|
||||
};
|
||||
}
|
||||
|
||||
const code = normalizeCodeToken(keyToken);
|
||||
if (!code) {
|
||||
return {
|
||||
key: null,
|
||||
message: `Unsupported keybinding token: ${keyToken}`,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
key: {
|
||||
code,
|
||||
modifiers: normalizeModifiers(modifiers),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function getSessionKeySpecSignature(key: SessionKeySpec): string {
|
||||
return [...key.modifiers, key.code].join('+');
|
||||
}
|
||||
|
||||
function resolveCommandBinding(
|
||||
binding: Keybinding,
|
||||
):
|
||||
| Omit<CompiledMpvCommandBinding, 'key' | 'sourcePath' | 'originalKey'>
|
||||
| Omit<CompiledSessionActionBinding, 'key' | 'sourcePath' | 'originalKey'>
|
||||
| null {
|
||||
const command = binding.command ?? [];
|
||||
const first = typeof command[0] === 'string' ? command[0] : '';
|
||||
if (first === SPECIAL_COMMANDS.SUBSYNC_TRIGGER) {
|
||||
return { actionType: 'session-action', actionId: 'triggerSubsync' };
|
||||
}
|
||||
if (first === SPECIAL_COMMANDS.RUNTIME_OPTIONS_OPEN) {
|
||||
return { actionType: 'session-action', actionId: 'openRuntimeOptions' };
|
||||
}
|
||||
if (first === SPECIAL_COMMANDS.JIMAKU_OPEN) {
|
||||
return { actionType: 'session-action', actionId: 'openJimaku' };
|
||||
}
|
||||
if (first === SPECIAL_COMMANDS.YOUTUBE_PICKER_OPEN) {
|
||||
return { actionType: 'session-action', actionId: 'openYoutubePicker' };
|
||||
}
|
||||
if (first === SPECIAL_COMMANDS.PLAYLIST_BROWSER_OPEN) {
|
||||
return { actionType: 'session-action', actionId: 'openPlaylistBrowser' };
|
||||
}
|
||||
if (first === SPECIAL_COMMANDS.REPLAY_SUBTITLE) {
|
||||
return { actionType: 'session-action', actionId: 'replayCurrentSubtitle' };
|
||||
}
|
||||
if (first === SPECIAL_COMMANDS.PLAY_NEXT_SUBTITLE) {
|
||||
return { actionType: 'session-action', actionId: 'playNextSubtitle' };
|
||||
}
|
||||
if (first === SPECIAL_COMMANDS.SHIFT_SUB_DELAY_TO_PREVIOUS_SUBTITLE_START) {
|
||||
return { actionType: 'session-action', actionId: 'shiftSubDelayPrevLine' };
|
||||
}
|
||||
if (first === SPECIAL_COMMANDS.SHIFT_SUB_DELAY_TO_NEXT_SUBTITLE_START) {
|
||||
return { actionType: 'session-action', actionId: 'shiftSubDelayNextLine' };
|
||||
}
|
||||
if (first.startsWith(SPECIAL_COMMANDS.RUNTIME_OPTION_CYCLE_PREFIX)) {
|
||||
const [, runtimeOptionId, rawDirection] = first.split(':');
|
||||
return {
|
||||
actionType: 'session-action',
|
||||
actionId: 'cycleRuntimeOption',
|
||||
payload: {
|
||||
runtimeOptionId,
|
||||
direction: rawDirection === 'prev' ? -1 : 1,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
actionType: 'mpv-command',
|
||||
command,
|
||||
};
|
||||
}
|
||||
|
||||
function getBindingFingerprint(binding: CompiledSessionBinding): string {
|
||||
if (binding.actionType === 'mpv-command') {
|
||||
return `mpv:${JSON.stringify(binding.command)}`;
|
||||
}
|
||||
return `session:${binding.actionId}:${JSON.stringify(binding.payload ?? null)}`;
|
||||
}
|
||||
|
||||
export function compileSessionBindings(
|
||||
input: CompileSessionBindingsInput,
|
||||
): {
|
||||
bindings: CompiledSessionBinding[];
|
||||
warnings: SessionBindingWarning[];
|
||||
} {
|
||||
const warnings: SessionBindingWarning[] = [];
|
||||
const candidates = new Map<string, DraftBinding[]>();
|
||||
const legacyToggleVisibleOverlayGlobal = (
|
||||
input.rawConfig?.shortcuts as Record<string, unknown> | undefined
|
||||
)?.toggleVisibleOverlayGlobal;
|
||||
const statsToggleKey = input.statsToggleKey ?? input.rawConfig?.stats.toggleKey ?? null;
|
||||
|
||||
if (legacyToggleVisibleOverlayGlobal !== undefined) {
|
||||
warnings.push({
|
||||
kind: 'deprecated-config',
|
||||
path: 'shortcuts.toggleVisibleOverlayGlobal',
|
||||
value: legacyToggleVisibleOverlayGlobal,
|
||||
message: 'Rename shortcuts.toggleVisibleOverlayGlobal to shortcuts.toggleVisibleOverlay.',
|
||||
});
|
||||
}
|
||||
|
||||
for (const shortcut of SESSION_SHORTCUT_ACTIONS) {
|
||||
const accelerator = input.shortcuts[shortcut.key];
|
||||
if (!accelerator) continue;
|
||||
const parsed = parseAccelerator(accelerator, input.platform);
|
||||
if (!parsed.key) {
|
||||
warnings.push({
|
||||
kind: 'unsupported',
|
||||
path: `shortcuts.${shortcut.key}`,
|
||||
value: accelerator,
|
||||
message: parsed.message ?? 'Unsupported accelerator syntax.',
|
||||
});
|
||||
continue;
|
||||
}
|
||||
const binding: CompiledSessionActionBinding = {
|
||||
sourcePath: `shortcuts.${shortcut.key}`,
|
||||
originalKey: accelerator,
|
||||
key: parsed.key,
|
||||
actionType: 'session-action',
|
||||
actionId: shortcut.actionId,
|
||||
};
|
||||
const signature = getSessionKeySpecSignature(parsed.key);
|
||||
const draft = candidates.get(signature) ?? [];
|
||||
draft.push({
|
||||
binding,
|
||||
actionFingerprint: getBindingFingerprint(binding),
|
||||
});
|
||||
candidates.set(signature, draft);
|
||||
}
|
||||
|
||||
if (statsToggleKey) {
|
||||
const parsed = parseDomKeyString(statsToggleKey);
|
||||
if (!parsed.key) {
|
||||
warnings.push({
|
||||
kind: 'unsupported',
|
||||
path: 'stats.toggleKey',
|
||||
value: statsToggleKey,
|
||||
message: parsed.message ?? 'Unsupported stats toggle key syntax.',
|
||||
});
|
||||
} else {
|
||||
const binding: CompiledSessionActionBinding = {
|
||||
sourcePath: 'stats.toggleKey',
|
||||
originalKey: statsToggleKey,
|
||||
key: parsed.key,
|
||||
actionType: 'session-action',
|
||||
actionId: 'toggleStatsOverlay',
|
||||
};
|
||||
const signature = getSessionKeySpecSignature(parsed.key);
|
||||
const draft = candidates.get(signature) ?? [];
|
||||
draft.push({
|
||||
binding,
|
||||
actionFingerprint: getBindingFingerprint(binding),
|
||||
});
|
||||
candidates.set(signature, draft);
|
||||
}
|
||||
}
|
||||
|
||||
input.keybindings.forEach((binding, index) => {
|
||||
if (!binding.command) return;
|
||||
const parsed = parseDomKeyString(binding.key);
|
||||
if (!parsed.key) {
|
||||
warnings.push({
|
||||
kind: 'unsupported',
|
||||
path: `keybindings[${index}].key`,
|
||||
value: binding.key,
|
||||
message: parsed.message ?? 'Unsupported keybinding syntax.',
|
||||
});
|
||||
return;
|
||||
}
|
||||
const resolved = resolveCommandBinding(binding);
|
||||
if (!resolved) return;
|
||||
const compiled: CompiledSessionBinding = {
|
||||
sourcePath: `keybindings[${index}].key`,
|
||||
originalKey: binding.key,
|
||||
key: parsed.key,
|
||||
...resolved,
|
||||
};
|
||||
const signature = getSessionKeySpecSignature(parsed.key);
|
||||
const draft = candidates.get(signature) ?? [];
|
||||
draft.push({
|
||||
binding: compiled,
|
||||
actionFingerprint: getBindingFingerprint(compiled),
|
||||
});
|
||||
candidates.set(signature, draft);
|
||||
});
|
||||
|
||||
const bindings: CompiledSessionBinding[] = [];
|
||||
for (const [signature, draftBindings] of candidates.entries()) {
|
||||
const uniqueFingerprints = new Set(draftBindings.map((entry) => entry.actionFingerprint));
|
||||
if (uniqueFingerprints.size > 1) {
|
||||
warnings.push({
|
||||
kind: 'conflict',
|
||||
path: draftBindings[0]!.binding.sourcePath,
|
||||
value: signature,
|
||||
conflictingPaths: draftBindings.map((entry) => entry.binding.sourcePath),
|
||||
message: `Conflicting session bindings compile to ${signature}; SubMiner will bind neither action.`,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
bindings.push(draftBindings[0]!.binding);
|
||||
}
|
||||
|
||||
bindings.sort((left, right) => left.sourcePath.localeCompare(right.sourcePath));
|
||||
return { bindings, warnings };
|
||||
}
|
||||
|
||||
export function buildPluginSessionBindingsArtifact(input: {
|
||||
bindings: CompiledSessionBinding[];
|
||||
warnings: SessionBindingWarning[];
|
||||
numericSelectionTimeoutMs: number;
|
||||
now?: Date;
|
||||
}): PluginSessionBindingsArtifact {
|
||||
return {
|
||||
version: 1,
|
||||
generatedAt: (input.now ?? new Date()).toISOString(),
|
||||
numericSelectionTimeoutMs: input.numericSelectionTimeoutMs,
|
||||
bindings: input.bindings,
|
||||
warnings: input.warnings,
|
||||
};
|
||||
}
|
||||
@@ -20,42 +20,6 @@ export interface RegisterGlobalShortcutsServiceOptions {
|
||||
}
|
||||
|
||||
export function registerGlobalShortcuts(options: RegisterGlobalShortcutsServiceOptions): void {
|
||||
const visibleShortcut = options.shortcuts.toggleVisibleOverlayGlobal;
|
||||
const normalizedVisible = visibleShortcut?.replace(/\s+/g, '').toLowerCase();
|
||||
const normalizedJimaku = options.shortcuts.openJimaku?.replace(/\s+/g, '').toLowerCase();
|
||||
const normalizedSettings = 'alt+shift+y';
|
||||
|
||||
if (visibleShortcut) {
|
||||
const toggleVisibleRegistered = globalShortcut.register(visibleShortcut, () => {
|
||||
options.onToggleVisibleOverlay();
|
||||
});
|
||||
if (!toggleVisibleRegistered) {
|
||||
logger.warn(
|
||||
`Failed to register global shortcut toggleVisibleOverlayGlobal: ${visibleShortcut}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (options.shortcuts.openJimaku && options.onOpenJimaku) {
|
||||
if (
|
||||
normalizedJimaku &&
|
||||
(normalizedJimaku === normalizedVisible || normalizedJimaku === normalizedSettings)
|
||||
) {
|
||||
logger.warn(
|
||||
'Skipped registering openJimaku because it collides with another global shortcut',
|
||||
);
|
||||
} else {
|
||||
const openJimakuRegistered = globalShortcut.register(options.shortcuts.openJimaku, () => {
|
||||
options.onOpenJimaku?.();
|
||||
});
|
||||
if (!openJimakuRegistered) {
|
||||
logger.warn(
|
||||
`Failed to register global shortcut openJimaku: ${options.shortcuts.openJimaku}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const settingsRegistered = globalShortcut.register('Alt+Shift+Y', () => {
|
||||
options.onOpenYomitanSettings();
|
||||
});
|
||||
|
||||
@@ -29,6 +29,13 @@ function makeArgs(overrides: Partial<CliArgs> = {}): CliArgs {
|
||||
triggerSubsync: false,
|
||||
markAudioCard: false,
|
||||
openRuntimeOptions: false,
|
||||
openJimaku: false,
|
||||
openYoutubePicker: false,
|
||||
openPlaylistBrowser: false,
|
||||
replayCurrentSubtitle: false,
|
||||
playNextSubtitle: false,
|
||||
shiftSubDelayPrevLine: false,
|
||||
shiftSubDelayNextLine: false,
|
||||
anilistStatus: false,
|
||||
anilistLogout: false,
|
||||
anilistSetup: false,
|
||||
|
||||
Reference in New Issue
Block a user