refactor: extract shortcut ui runtime deps

This commit is contained in:
2026-02-10 01:36:27 -08:00
parent a17c2296d5
commit cb93601e16
6 changed files with 246 additions and 28 deletions

View File

@@ -5,14 +5,18 @@ export interface GlobalShortcutConfig {
toggleInvisibleOverlayGlobal: string | null | undefined;
}
export function registerGlobalShortcutsService(options: {
export interface RegisterGlobalShortcutsServiceOptions {
shortcuts: GlobalShortcutConfig;
onToggleVisibleOverlay: () => void;
onToggleInvisibleOverlay: () => void;
onOpenYomitanSettings: () => void;
isDev: boolean;
getMainWindow: () => BrowserWindow | null;
}): void {
}
export function registerGlobalShortcutsService(
options: RegisterGlobalShortcutsServiceOptions,
): void {
const visibleShortcut = options.shortcuts.toggleVisibleOverlayGlobal;
const invisibleShortcut = options.shortcuts.toggleInvisibleOverlayGlobal;
const normalizedVisible = visibleShortcut?.replace(/\s+/g, "").toLowerCase();

View File

@@ -0,0 +1,97 @@
import test from "node:test";
import assert from "node:assert/strict";
import {
createGlobalShortcutRegistrationDepsRuntimeService,
createSecondarySubtitleCycleDepsRuntimeService,
createYomitanSettingsWindowDepsRuntimeService,
runOverlayShortcutLocalFallbackRuntimeService,
} from "./shortcut-ui-runtime-deps-service";
function makeOptions() {
return {
yomitanExt: null,
getYomitanSettingsWindow: () => null,
setYomitanSettingsWindow: () => {},
shortcuts: {
toggleVisibleOverlayGlobal: "Ctrl+Shift+O",
toggleInvisibleOverlayGlobal: "Ctrl+Alt+O",
},
onToggleVisibleOverlay: () => {},
onToggleInvisibleOverlay: () => {},
onOpenYomitanSettings: () => {},
isDev: false,
getMainWindow: () => null,
getSecondarySubMode: () => "hover" as const,
setSecondarySubMode: () => {},
getLastSecondarySubToggleAtMs: () => 0,
setLastSecondarySubToggleAtMs: () => {},
broadcastSecondarySubMode: () => {},
showMpvOsd: () => {},
getConfiguredShortcuts: () => ({
toggleVisibleOverlayGlobal: null,
toggleInvisibleOverlayGlobal: null,
copySubtitle: null,
copySubtitleMultiple: null,
updateLastCardFromClipboard: null,
triggerFieldGrouping: null,
triggerSubsync: null,
mineSentence: null,
mineSentenceMultiple: null,
multiCopyTimeoutMs: 5000,
toggleSecondarySub: null,
markAudioCard: null,
openRuntimeOptions: "Ctrl+R",
openJimaku: null,
}),
getOverlayShortcutFallbackHandlers: () => ({
openRuntimeOptions: () => {},
openJimaku: () => {},
markAudioCard: () => {},
copySubtitleMultiple: () => {},
copySubtitle: () => {},
toggleSecondarySub: () => {},
updateLastCardFromClipboard: () => {},
triggerFieldGrouping: () => {},
triggerSubsync: () => {},
mineSentence: () => {},
mineSentenceMultiple: () => {},
}),
shortcutMatcher: () => false,
};
}
test("shortcut ui deps builders return expected adapters", () => {
const options = makeOptions();
const yomitan = createYomitanSettingsWindowDepsRuntimeService(options);
const globalShortcuts = createGlobalShortcutRegistrationDepsRuntimeService(options);
const secondary = createSecondarySubtitleCycleDepsRuntimeService(options);
assert.equal(yomitan.yomitanExt, null);
assert.equal(typeof globalShortcuts.onOpenYomitanSettings, "function");
assert.equal(secondary.getSecondarySubMode(), "hover");
});
test("runOverlayShortcutLocalFallbackRuntimeService delegates and returns boolean", () => {
const options = {
...makeOptions(),
shortcutMatcher: () => true,
};
const handled = runOverlayShortcutLocalFallbackRuntimeService(
{
key: "r",
code: "KeyR",
alt: false,
control: true,
shift: false,
meta: false,
type: "keyDown",
} as Electron.Input,
options,
);
assert.equal(handled, true);
});

View File

@@ -0,0 +1,83 @@
import { Extension } from "electron";
import { SecondarySubMode } from "../../types";
import { ConfiguredShortcuts } from "../utils/shortcut-config";
import { CycleSecondarySubModeDeps } from "./secondary-subtitle-service";
import { OverlayShortcutFallbackHandlers, runOverlayShortcutLocalFallback } from "./overlay-shortcut-fallback-runner";
import { OpenYomitanSettingsWindowOptions } from "./yomitan-settings-service";
import { RegisterGlobalShortcutsServiceOptions } from "./shortcut-service";
export interface ShortcutUiRuntimeDepsOptions {
yomitanExt: Extension | null;
getYomitanSettingsWindow: OpenYomitanSettingsWindowOptions["getExistingWindow"];
setYomitanSettingsWindow: OpenYomitanSettingsWindowOptions["setWindow"];
shortcuts: RegisterGlobalShortcutsServiceOptions["shortcuts"];
onToggleVisibleOverlay: () => void;
onToggleInvisibleOverlay: () => void;
onOpenYomitanSettings: () => void;
isDev: boolean;
getMainWindow: RegisterGlobalShortcutsServiceOptions["getMainWindow"];
getSecondarySubMode: () => SecondarySubMode;
setSecondarySubMode: (mode: SecondarySubMode) => void;
getLastSecondarySubToggleAtMs: () => number;
setLastSecondarySubToggleAtMs: (timestampMs: number) => void;
broadcastSecondarySubMode: (mode: SecondarySubMode) => void;
showMpvOsd: (text: string) => void;
getConfiguredShortcuts: () => ConfiguredShortcuts;
getOverlayShortcutFallbackHandlers: () => OverlayShortcutFallbackHandlers;
shortcutMatcher: (
input: Electron.Input,
accelerator: string,
allowWhenRegistered?: boolean,
) => boolean;
}
export function createYomitanSettingsWindowDepsRuntimeService(
options: ShortcutUiRuntimeDepsOptions,
): OpenYomitanSettingsWindowOptions {
return {
yomitanExt: options.yomitanExt,
getExistingWindow: options.getYomitanSettingsWindow,
setWindow: options.setYomitanSettingsWindow,
};
}
export function createGlobalShortcutRegistrationDepsRuntimeService(
options: ShortcutUiRuntimeDepsOptions,
): RegisterGlobalShortcutsServiceOptions {
return {
shortcuts: options.shortcuts,
onToggleVisibleOverlay: options.onToggleVisibleOverlay,
onToggleInvisibleOverlay: options.onToggleInvisibleOverlay,
onOpenYomitanSettings: options.onOpenYomitanSettings,
isDev: options.isDev,
getMainWindow: options.getMainWindow,
};
}
export function createSecondarySubtitleCycleDepsRuntimeService(
options: ShortcutUiRuntimeDepsOptions,
): CycleSecondarySubModeDeps {
return {
getSecondarySubMode: options.getSecondarySubMode,
setSecondarySubMode: options.setSecondarySubMode,
getLastSecondarySubToggleAtMs: options.getLastSecondarySubToggleAtMs,
setLastSecondarySubToggleAtMs: options.setLastSecondarySubToggleAtMs,
broadcastSecondarySubMode: options.broadcastSecondarySubMode,
showMpvOsd: options.showMpvOsd,
};
}
export function runOverlayShortcutLocalFallbackRuntimeService(
input: Electron.Input,
options: ShortcutUiRuntimeDepsOptions,
): boolean {
return runOverlayShortcutLocalFallback(
input,
options.getConfiguredShortcuts(),
options.shortcutMatcher,
options.getOverlayShortcutFallbackHandlers(),
);
}

View File

@@ -1,10 +1,14 @@
import { BrowserWindow, Extension, session } from "electron";
export function openYomitanSettingsWindow(options: {
export interface OpenYomitanSettingsWindowOptions {
yomitanExt: Extension | null;
getExistingWindow: () => BrowserWindow | null;
setWindow: (window: BrowserWindow | null) => void;
}): void {
}
export function openYomitanSettingsWindow(
options: OpenYomitanSettingsWindowOptions,
): void {
console.log("openYomitanSettings called");
if (!options.yomitanExt) {