mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-02-28 06:22:45 -08:00
refactor: extract overlay shortcut lifecycle runtime
This commit is contained in:
52
src/core/services/overlay-shortcut-lifecycle-service.ts
Normal file
52
src/core/services/overlay-shortcut-lifecycle-service.ts
Normal 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);
|
||||||
|
}
|
||||||
49
src/main.ts
49
src/main.ts
@@ -111,13 +111,17 @@ import {
|
|||||||
} from "./core/services/shortcut-fallback-service";
|
} from "./core/services/shortcut-fallback-service";
|
||||||
import {
|
import {
|
||||||
registerOverlayShortcutsService,
|
registerOverlayShortcutsService,
|
||||||
unregisterOverlayShortcutsService,
|
|
||||||
} from "./core/services/overlay-shortcut-service";
|
} from "./core/services/overlay-shortcut-service";
|
||||||
import { runOverlayShortcutLocalFallback } from "./core/services/overlay-shortcut-fallback-runner";
|
import { runOverlayShortcutLocalFallback } from "./core/services/overlay-shortcut-fallback-runner";
|
||||||
import { createOverlayShortcutRuntimeHandlers } from "./core/services/overlay-shortcut-runtime-service";
|
import { createOverlayShortcutRuntimeHandlers } from "./core/services/overlay-shortcut-runtime-service";
|
||||||
import { createNumericShortcutSessionService } from "./core/services/numeric-shortcut-session-service";
|
import { createNumericShortcutSessionService } from "./core/services/numeric-shortcut-session-service";
|
||||||
import { handleCliCommandService } from "./core/services/cli-command-service";
|
import { handleCliCommandService } from "./core/services/cli-command-service";
|
||||||
import { cycleSecondarySubModeService } from "./core/services/secondary-subtitle-service";
|
import { cycleSecondarySubModeService } from "./core/services/secondary-subtitle-service";
|
||||||
|
import {
|
||||||
|
refreshOverlayShortcutsRuntimeService,
|
||||||
|
syncOverlayShortcutsRuntimeService,
|
||||||
|
unregisterOverlayShortcutsRuntimeService,
|
||||||
|
} from "./core/services/overlay-shortcut-lifecycle-service";
|
||||||
import {
|
import {
|
||||||
copyCurrentSubtitleService,
|
copyCurrentSubtitleService,
|
||||||
handleMineSentenceDigitService,
|
handleMineSentenceDigitService,
|
||||||
@@ -986,28 +990,43 @@ function handleMineSentenceDigit(count: number): void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function registerOverlayShortcuts(): void {
|
function registerOverlayShortcuts(): void {
|
||||||
const shortcuts = getConfiguredShortcuts();
|
|
||||||
const handlers = getOverlayShortcutRuntimeHandlers();
|
|
||||||
shortcutsRegistered = registerOverlayShortcutsService(
|
shortcutsRegistered = registerOverlayShortcutsService(
|
||||||
shortcuts,
|
getConfiguredShortcuts(),
|
||||||
handlers.overlayHandlers,
|
getOverlayShortcutRuntimeHandlers().overlayHandlers,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getOverlayShortcutLifecycleDeps() {
|
||||||
|
return {
|
||||||
|
getConfiguredShortcuts: () => getConfiguredShortcuts(),
|
||||||
|
getOverlayHandlers: () => getOverlayShortcutRuntimeHandlers().overlayHandlers,
|
||||||
|
cancelPendingMultiCopy: () => cancelPendingMultiCopy(),
|
||||||
|
cancelPendingMineSentenceMultiple: () => cancelPendingMineSentenceMultiple(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
function unregisterOverlayShortcuts(): void {
|
function unregisterOverlayShortcuts(): void {
|
||||||
if (!shortcutsRegistered) return;
|
shortcutsRegistered = unregisterOverlayShortcutsRuntimeService(
|
||||||
|
shortcutsRegistered,
|
||||||
cancelPendingMultiCopy();
|
getOverlayShortcutLifecycleDeps(),
|
||||||
cancelPendingMineSentenceMultiple();
|
);
|
||||||
|
|
||||||
unregisterOverlayShortcutsService(getConfiguredShortcuts());
|
|
||||||
|
|
||||||
shortcutsRegistered = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function shouldOverlayShortcutsBeActive(): boolean { return overlayRuntimeInitialized; }
|
function shouldOverlayShortcutsBeActive(): boolean { return overlayRuntimeInitialized; }
|
||||||
function syncOverlayShortcuts(): void { if (shouldOverlayShortcutsBeActive()) { registerOverlayShortcuts(); } else { unregisterOverlayShortcuts(); } }
|
function syncOverlayShortcuts(): void {
|
||||||
function refreshOverlayShortcuts(): void { unregisterOverlayShortcuts(); syncOverlayShortcuts(); }
|
shortcutsRegistered = syncOverlayShortcutsRuntimeService(
|
||||||
|
shouldOverlayShortcutsBeActive(),
|
||||||
|
shortcutsRegistered,
|
||||||
|
getOverlayShortcutLifecycleDeps(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
function refreshOverlayShortcuts(): void {
|
||||||
|
shortcutsRegistered = refreshOverlayShortcutsRuntimeService(
|
||||||
|
shouldOverlayShortcutsBeActive(),
|
||||||
|
shortcutsRegistered,
|
||||||
|
getOverlayShortcutLifecycleDeps(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function updateVisibleOverlayVisibility(): void {
|
function updateVisibleOverlayVisibility(): void {
|
||||||
updateVisibleOverlayVisibilityService({
|
updateVisibleOverlayVisibilityService({
|
||||||
|
|||||||
Reference in New Issue
Block a user