mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-02-27 18:22:41 -08:00
refactor: extract global shortcuts runtime wiring
This commit is contained in:
62
src/main.ts
62
src/main.ts
@@ -287,16 +287,7 @@ import {
|
||||
createBuildUpdateVisibleOverlayBoundsMainDepsHandler,
|
||||
} from './main/runtime/overlay-window-layout-main-deps';
|
||||
import { buildTrayMenuTemplateRuntime, resolveTrayIconPathRuntime } from './main/runtime/tray-runtime';
|
||||
import {
|
||||
createGetConfiguredShortcutsHandler,
|
||||
createRefreshGlobalAndOverlayShortcutsHandler,
|
||||
createRegisterGlobalShortcutsHandler,
|
||||
} from './main/runtime/global-shortcuts';
|
||||
import {
|
||||
createBuildGetConfiguredShortcutsMainDepsHandler,
|
||||
createBuildRefreshGlobalAndOverlayShortcutsMainDepsHandler,
|
||||
createBuildRegisterGlobalShortcutsMainDepsHandler,
|
||||
} from './main/runtime/global-shortcuts-main-deps';
|
||||
import { createGlobalShortcutsRuntimeHandlers } from './main/runtime/global-shortcuts-runtime-handlers';
|
||||
import { createAppendToMpvLogHandler, createShowMpvOsdHandler } from './main/runtime/mpv-osd-log';
|
||||
import {
|
||||
createBuildAppendToMpvLogMainDepsHandler,
|
||||
@@ -2505,56 +2496,31 @@ function openYomitanSettings(): void {
|
||||
openYomitanSettingsHandler();
|
||||
}
|
||||
|
||||
const buildGetConfiguredShortcutsMainDepsHandler = createBuildGetConfiguredShortcutsMainDepsHandler(
|
||||
{
|
||||
const {
|
||||
getConfiguredShortcuts,
|
||||
registerGlobalShortcuts,
|
||||
refreshGlobalAndOverlayShortcuts,
|
||||
} = createGlobalShortcutsRuntimeHandlers({
|
||||
getConfiguredShortcutsMainDeps: {
|
||||
getResolvedConfig: () => getResolvedConfig(),
|
||||
defaultConfig: DEFAULT_CONFIG,
|
||||
resolveConfiguredShortcuts,
|
||||
},
|
||||
);
|
||||
const getConfiguredShortcutsMainDeps = buildGetConfiguredShortcutsMainDepsHandler();
|
||||
const getConfiguredShortcutsHandler = createGetConfiguredShortcutsHandler(
|
||||
getConfiguredShortcutsMainDeps,
|
||||
);
|
||||
|
||||
const buildRegisterGlobalShortcutsMainDepsHandler =
|
||||
createBuildRegisterGlobalShortcutsMainDepsHandler({
|
||||
getConfiguredShortcuts: () => getConfiguredShortcuts(),
|
||||
buildRegisterGlobalShortcutsMainDeps: (getConfiguredShortcutsHandler) => ({
|
||||
getConfiguredShortcuts: () => getConfiguredShortcutsHandler(),
|
||||
registerGlobalShortcutsCore,
|
||||
toggleVisibleOverlay: () => toggleVisibleOverlay(),
|
||||
toggleInvisibleOverlay: () => toggleInvisibleOverlay(),
|
||||
openYomitanSettings: () => openYomitanSettings(),
|
||||
isDev,
|
||||
getMainWindow: () => overlayManager.getMainWindow(),
|
||||
});
|
||||
const registerGlobalShortcutsMainDeps = buildRegisterGlobalShortcutsMainDepsHandler();
|
||||
const registerGlobalShortcutsHandler = createRegisterGlobalShortcutsHandler(
|
||||
registerGlobalShortcutsMainDeps,
|
||||
);
|
||||
|
||||
const buildRefreshGlobalAndOverlayShortcutsMainDepsHandler =
|
||||
createBuildRefreshGlobalAndOverlayShortcutsMainDepsHandler({
|
||||
}),
|
||||
buildRefreshGlobalAndOverlayShortcutsMainDeps: (registerGlobalShortcutsHandler) => ({
|
||||
unregisterAllGlobalShortcuts: () => globalShortcut.unregisterAll(),
|
||||
registerGlobalShortcuts: () => registerGlobalShortcuts(),
|
||||
registerGlobalShortcuts: () => registerGlobalShortcutsHandler(),
|
||||
syncOverlayShortcuts: () => syncOverlayShortcuts(),
|
||||
});
|
||||
const refreshGlobalAndOverlayShortcutsMainDeps =
|
||||
buildRefreshGlobalAndOverlayShortcutsMainDepsHandler();
|
||||
const refreshGlobalAndOverlayShortcutsHandler = createRefreshGlobalAndOverlayShortcutsHandler(
|
||||
refreshGlobalAndOverlayShortcutsMainDeps,
|
||||
);
|
||||
|
||||
function registerGlobalShortcuts(): void {
|
||||
registerGlobalShortcutsHandler();
|
||||
}
|
||||
|
||||
function refreshGlobalAndOverlayShortcuts(): void {
|
||||
refreshGlobalAndOverlayShortcutsHandler();
|
||||
}
|
||||
|
||||
function getConfiguredShortcuts() {
|
||||
return getConfiguredShortcutsHandler();
|
||||
}
|
||||
}),
|
||||
});
|
||||
|
||||
const buildCycleSecondarySubModeMainDepsHandler = createBuildCycleSecondarySubModeMainDepsHandler(
|
||||
{
|
||||
|
||||
57
src/main/runtime/global-shortcuts-runtime-handlers.test.ts
Normal file
57
src/main/runtime/global-shortcuts-runtime-handlers.test.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import type { ConfiguredShortcuts } from '../../core/utils/shortcut-config';
|
||||
import { createGlobalShortcutsRuntimeHandlers } from './global-shortcuts-runtime-handlers';
|
||||
|
||||
function createShortcuts(): ConfiguredShortcuts {
|
||||
return {
|
||||
toggleVisibleOverlayGlobal: 'CommandOrControl+Shift+O',
|
||||
toggleInvisibleOverlayGlobal: 'CommandOrControl+Shift+I',
|
||||
copySubtitle: 's',
|
||||
copySubtitleMultiple: 'CommandOrControl+s',
|
||||
updateLastCardFromClipboard: 'c',
|
||||
triggerFieldGrouping: null,
|
||||
triggerSubsync: null,
|
||||
mineSentence: 'q',
|
||||
mineSentenceMultiple: 'w',
|
||||
multiCopyTimeoutMs: 5000,
|
||||
toggleSecondarySub: null,
|
||||
markAudioCard: null,
|
||||
openRuntimeOptions: null,
|
||||
openJimaku: null,
|
||||
};
|
||||
}
|
||||
|
||||
test('global shortcuts runtime handlers compose get/register/refresh flow', () => {
|
||||
const calls: string[] = [];
|
||||
const shortcuts = createShortcuts();
|
||||
const runtime = createGlobalShortcutsRuntimeHandlers({
|
||||
getConfiguredShortcutsMainDeps: {
|
||||
getResolvedConfig: () => ({}) as never,
|
||||
defaultConfig: {} as never,
|
||||
resolveConfiguredShortcuts: () => shortcuts,
|
||||
},
|
||||
buildRegisterGlobalShortcutsMainDeps: (getConfiguredShortcuts) => ({
|
||||
getConfiguredShortcuts,
|
||||
registerGlobalShortcutsCore: (options) => {
|
||||
calls.push('register');
|
||||
assert.equal(options.shortcuts, shortcuts);
|
||||
},
|
||||
toggleVisibleOverlay: () => calls.push('toggle-visible'),
|
||||
toggleInvisibleOverlay: () => calls.push('toggle-invisible'),
|
||||
openYomitanSettings: () => calls.push('open-yomitan'),
|
||||
isDev: false,
|
||||
getMainWindow: () => null,
|
||||
}),
|
||||
buildRefreshGlobalAndOverlayShortcutsMainDeps: (registerGlobalShortcuts) => ({
|
||||
unregisterAllGlobalShortcuts: () => calls.push('unregister'),
|
||||
registerGlobalShortcuts: () => registerGlobalShortcuts(),
|
||||
syncOverlayShortcuts: () => calls.push('sync-overlay'),
|
||||
}),
|
||||
});
|
||||
|
||||
assert.equal(runtime.getConfiguredShortcuts(), shortcuts);
|
||||
runtime.registerGlobalShortcuts();
|
||||
runtime.refreshGlobalAndOverlayShortcuts();
|
||||
assert.deepEqual(calls, ['register', 'unregister', 'register', 'sync-overlay']);
|
||||
});
|
||||
60
src/main/runtime/global-shortcuts-runtime-handlers.ts
Normal file
60
src/main/runtime/global-shortcuts-runtime-handlers.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import type { ConfiguredShortcuts } from '../../core/utils/shortcut-config';
|
||||
import {
|
||||
createGetConfiguredShortcutsHandler,
|
||||
createRefreshGlobalAndOverlayShortcutsHandler,
|
||||
createRegisterGlobalShortcutsHandler,
|
||||
} from './global-shortcuts';
|
||||
import {
|
||||
createBuildGetConfiguredShortcutsMainDepsHandler,
|
||||
createBuildRefreshGlobalAndOverlayShortcutsMainDepsHandler,
|
||||
createBuildRegisterGlobalShortcutsMainDepsHandler,
|
||||
} from './global-shortcuts-main-deps';
|
||||
|
||||
type GetConfiguredShortcutsMainDeps = Parameters<
|
||||
typeof createBuildGetConfiguredShortcutsMainDepsHandler
|
||||
>[0];
|
||||
type RegisterGlobalShortcutsMainDeps = Parameters<
|
||||
typeof createBuildRegisterGlobalShortcutsMainDepsHandler
|
||||
>[0];
|
||||
type RefreshGlobalAndOverlayShortcutsMainDeps = Parameters<
|
||||
typeof createBuildRefreshGlobalAndOverlayShortcutsMainDepsHandler
|
||||
>[0];
|
||||
|
||||
export function createGlobalShortcutsRuntimeHandlers(deps: {
|
||||
getConfiguredShortcutsMainDeps: GetConfiguredShortcutsMainDeps;
|
||||
buildRegisterGlobalShortcutsMainDeps: (
|
||||
getConfiguredShortcuts: () => ConfiguredShortcuts,
|
||||
) => RegisterGlobalShortcutsMainDeps;
|
||||
buildRefreshGlobalAndOverlayShortcutsMainDeps: (
|
||||
registerGlobalShortcuts: () => void,
|
||||
) => RefreshGlobalAndOverlayShortcutsMainDeps;
|
||||
}) {
|
||||
const getConfiguredShortcutsMainDeps = createBuildGetConfiguredShortcutsMainDepsHandler(
|
||||
deps.getConfiguredShortcutsMainDeps,
|
||||
)();
|
||||
const getConfiguredShortcutsHandler =
|
||||
createGetConfiguredShortcutsHandler(getConfiguredShortcutsMainDeps);
|
||||
const getConfiguredShortcuts = () => getConfiguredShortcutsHandler();
|
||||
|
||||
const registerGlobalShortcutsMainDeps = createBuildRegisterGlobalShortcutsMainDepsHandler(
|
||||
deps.buildRegisterGlobalShortcutsMainDeps(getConfiguredShortcuts),
|
||||
)();
|
||||
const registerGlobalShortcutsHandler =
|
||||
createRegisterGlobalShortcutsHandler(registerGlobalShortcutsMainDeps);
|
||||
const registerGlobalShortcuts = () => registerGlobalShortcutsHandler();
|
||||
|
||||
const refreshGlobalAndOverlayShortcutsMainDeps =
|
||||
createBuildRefreshGlobalAndOverlayShortcutsMainDepsHandler(
|
||||
deps.buildRefreshGlobalAndOverlayShortcutsMainDeps(registerGlobalShortcuts),
|
||||
)();
|
||||
const refreshGlobalAndOverlayShortcutsHandler = createRefreshGlobalAndOverlayShortcutsHandler(
|
||||
refreshGlobalAndOverlayShortcutsMainDeps,
|
||||
);
|
||||
const refreshGlobalAndOverlayShortcuts = () => refreshGlobalAndOverlayShortcutsHandler();
|
||||
|
||||
return {
|
||||
getConfiguredShortcuts,
|
||||
registerGlobalShortcuts,
|
||||
refreshGlobalAndOverlayShortcuts,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user