refactor: extract overlay shortcut lifecycle runtime

This commit is contained in:
2026-02-09 23:09:59 -08:00
parent dbfd9105de
commit 9c4a9769a5
2 changed files with 86 additions and 15 deletions

View File

@@ -0,0 +1,52 @@
import { OverlayShortcutHandlers, registerOverlayShortcutsService, unregisterOverlayShortcutsService } from "./overlay-shortcut-service";
import { ConfiguredShortcuts } from "../utils/shortcut-config";
export interface OverlayShortcutLifecycleDeps {
getConfiguredShortcuts: () => ConfiguredShortcuts;
getOverlayHandlers: () => OverlayShortcutHandlers;
cancelPendingMultiCopy: () => void;
cancelPendingMineSentenceMultiple: () => void;
}
export function registerOverlayShortcutsRuntimeService(
deps: OverlayShortcutLifecycleDeps,
): boolean {
return registerOverlayShortcutsService(
deps.getConfiguredShortcuts(),
deps.getOverlayHandlers(),
);
}
export function unregisterOverlayShortcutsRuntimeService(
shortcutsRegistered: boolean,
deps: OverlayShortcutLifecycleDeps,
): boolean {
if (!shortcutsRegistered) return shortcutsRegistered;
deps.cancelPendingMultiCopy();
deps.cancelPendingMineSentenceMultiple();
unregisterOverlayShortcutsService(deps.getConfiguredShortcuts());
return false;
}
export function syncOverlayShortcutsRuntimeService(
shouldBeActive: boolean,
shortcutsRegistered: boolean,
deps: OverlayShortcutLifecycleDeps,
): boolean {
if (shouldBeActive) {
return registerOverlayShortcutsRuntimeService(deps);
}
return unregisterOverlayShortcutsRuntimeService(shortcutsRegistered, deps);
}
export function refreshOverlayShortcutsRuntimeService(
shouldBeActive: boolean,
shortcutsRegistered: boolean,
deps: OverlayShortcutLifecycleDeps,
): boolean {
const cleared = unregisterOverlayShortcutsRuntimeService(
shortcutsRegistered,
deps,
);
return syncOverlayShortcutsRuntimeService(shouldBeActive, cleared, deps);
}

View File

@@ -111,13 +111,17 @@ import {
} from "./core/services/shortcut-fallback-service";
import {
registerOverlayShortcutsService,
unregisterOverlayShortcutsService,
} from "./core/services/overlay-shortcut-service";
import { runOverlayShortcutLocalFallback } from "./core/services/overlay-shortcut-fallback-runner";
import { createOverlayShortcutRuntimeHandlers } from "./core/services/overlay-shortcut-runtime-service";
import { createNumericShortcutSessionService } from "./core/services/numeric-shortcut-session-service";
import { handleCliCommandService } from "./core/services/cli-command-service";
import { cycleSecondarySubModeService } from "./core/services/secondary-subtitle-service";
import {
refreshOverlayShortcutsRuntimeService,
syncOverlayShortcutsRuntimeService,
unregisterOverlayShortcutsRuntimeService,
} from "./core/services/overlay-shortcut-lifecycle-service";
import {
copyCurrentSubtitleService,
handleMineSentenceDigitService,
@@ -986,28 +990,43 @@ function handleMineSentenceDigit(count: number): void {
}
function registerOverlayShortcuts(): void {
const shortcuts = getConfiguredShortcuts();
const handlers = getOverlayShortcutRuntimeHandlers();
shortcutsRegistered = registerOverlayShortcutsService(
shortcuts,
handlers.overlayHandlers,
getConfiguredShortcuts(),
getOverlayShortcutRuntimeHandlers().overlayHandlers,
);
}
function getOverlayShortcutLifecycleDeps() {
return {
getConfiguredShortcuts: () => getConfiguredShortcuts(),
getOverlayHandlers: () => getOverlayShortcutRuntimeHandlers().overlayHandlers,
cancelPendingMultiCopy: () => cancelPendingMultiCopy(),
cancelPendingMineSentenceMultiple: () => cancelPendingMineSentenceMultiple(),
};
}
function unregisterOverlayShortcuts(): void {
if (!shortcutsRegistered) return;
cancelPendingMultiCopy();
cancelPendingMineSentenceMultiple();
unregisterOverlayShortcutsService(getConfiguredShortcuts());
shortcutsRegistered = false;
shortcutsRegistered = unregisterOverlayShortcutsRuntimeService(
shortcutsRegistered,
getOverlayShortcutLifecycleDeps(),
);
}
function shouldOverlayShortcutsBeActive(): boolean { return overlayRuntimeInitialized; }
function syncOverlayShortcuts(): void { if (shouldOverlayShortcutsBeActive()) { registerOverlayShortcuts(); } else { unregisterOverlayShortcuts(); } }
function refreshOverlayShortcuts(): void { unregisterOverlayShortcuts(); syncOverlayShortcuts(); }
function syncOverlayShortcuts(): void {
shortcutsRegistered = syncOverlayShortcutsRuntimeService(
shouldOverlayShortcutsBeActive(),
shortcutsRegistered,
getOverlayShortcutLifecycleDeps(),
);
}
function refreshOverlayShortcuts(): void {
shortcutsRegistered = refreshOverlayShortcutsRuntimeService(
shouldOverlayShortcutsBeActive(),
shortcutsRegistered,
getOverlayShortcutLifecycleDeps(),
);
}
function updateVisibleOverlayVisibility(): void {
updateVisibleOverlayVisibilityService({