mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-02-27 18:22:41 -08:00
refactor: extract shortcut ui runtime deps
This commit is contained in:
@@ -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();
|
||||
|
||||
97
src/core/services/shortcut-ui-runtime-deps-service.test.ts
Normal file
97
src/core/services/shortcut-ui-runtime-deps-service.test.ts
Normal 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);
|
||||
});
|
||||
83
src/core/services/shortcut-ui-runtime-deps-service.ts
Normal file
83
src/core/services/shortcut-ui-runtime-deps-service.ts
Normal 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(),
|
||||
);
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user