Files
SubMiner/src/main/assemblies/overlay-modals.ts
T
sudacode 2924f04316 refactor(main): move domains into per-domain assemblies (#309)
* refactor(main): move the YouTube domain into an assembly

createYoutubeAssembly owns the YouTube flow, playback, media cache,
whisper subtitles, primary-subtitle notifications, and the in-app
browser, plus the browser quit-on-close state. main.ts builds it once
where the first YouTube runtime used to be and calls through the result.
Siblings built later are passed as getters or callbacks; tests pin that
construction calls none of them.

* docs(architecture): describe per-domain assemblies

* test(e2e): blank the line before mining the sub-delay cue

The previous test leaves the same cue on screen, so the overlay check
passed before the seek landed and --mine-sentence could read mpv's empty
mid-seek sub-text and skip the card.

* refactor(main): move the Jellyfin domain into an assembly

createJellyfinAssembly owns the Jellyfin composer call, its playback and
remote-progress state, the subtitle cache IO, mpv defaults, and the tray
discovery deps. main.ts builds it where the composer call used to be and
reads state through getActivePlayback/getQuitOnDisconnectArmed.

* refactor(main): move the AniList domain into an assembly

createAnilistAssembly owns the AniList setup and tracking composer calls,
the setup window, the state runtime, and the token, media-guess, in-flight
and attempted-update state they share. main.ts builds it where the setup
composer call used to be and keeps the rate limiter, which the stats
cover-art fetcher also uses.

* refactor(main): move config hot-reload, settings, and sync UI into assemblies

createSettingsAssembly owns config hot-reload and the settings window,
which share the hot-reload-applied handler; main.ts still supplies that
handler's cross-domain deps and the Yomitan deck-name helper.
createSyncAssembly owns the sync window, sync IPC runtime, and auto-sync
scheduler. Both are built where their blocks were; main.ts keeps the
registerHandlers()/start() calls at the same points.

* refactor(main): move dictionary runtimes into assemblies

createDictionariesAssembly owns the JLPT and frequency lookups and their
dictionary-root builders; main.ts still resolves the root paths at the
same point. createCharacterDictionaryAssembly owns the character
dictionary runtime, Yomitan auto-sync, and the portrait and name-candidate
lookups. Both are built where their blocks were; later siblings (Yomitan
profile policy, notifications, OSD, subtitle refresh) come in as getters
or callbacks.

* refactor(main): move overlay modal openers into an assembly

createOverlayModalsAssembly owns the shared open-with-notification helper
and the runtime options, Jimaku, TsukiHime, subtitle generation, session
help, changelog, character dictionary manager, controller, and playlist
browser openers. main.ts builds it where the openers used to live.

* refactor(logger): export the Logger type for assembly deps

The build emits declarations, and the Jellyfin assembly returns the logger
in its tray discovery deps, so tsc needs a nameable Logger type. Assemblies
now Pick from it directly.
2026-10-31 19:39:27 -07:00

211 lines
8.1 KiB
TypeScript

import type { Logger } from '../../logger';
import type { OverlayHostedModal } from '../../shared/ipc/contracts';
import type { ResolvedConfig } from '../../types';
import type { OverlayModalRuntime } from '../overlay-runtime';
import { openChangelogModal as openChangelogModalRuntime } from '../runtime/changelog-open';
import { openCharacterDictionaryManagerWithConfigGate } from '../runtime/character-dictionary-manager-gate';
import { openCharacterDictionaryManagerModal as openCharacterDictionaryManagerModalRuntime } from '../runtime/character-dictionary-open';
import { openControllerDebugModal as openControllerDebugModalRuntime } from '../runtime/controller-debug-open';
import { openControllerSelectModal as openControllerSelectModalRuntime } from '../runtime/controller-select-open';
import { openJimakuModal as openJimakuModalRuntime } from '../runtime/jimaku-open';
import type { createOverlayNotificationsRuntime } from '../runtime/overlay-notifications-runtime';
import { openPlaylistBrowser as openPlaylistBrowserRuntime } from '../runtime/playlist-browser-open';
import { openRuntimeOptionsModal as openRuntimeOptionsModalRuntime } from '../runtime/runtime-options-open';
import { openSessionHelpModal as openSessionHelpModalRuntime } from '../runtime/session-help-open';
import { openSubtitleGenerationModal } from '../runtime/subtitle-generation-open';
import { openTsukihimeModal as openTsukihimeModalRuntime } from '../runtime/tsukihime-open';
import type { AppState } from '../state';
type OverlayNotifications = Pick<
ReturnType<typeof createOverlayNotificationsRuntime>,
| 'getConfiguredStatusNotificationType'
| 'showConfiguredStatusNotification'
| 'showOverlayNotification'
>;
/** What the overlay-hosted modal openers need from the rest of the app. */
export interface OverlayModalsAssemblyDeps {
appState: Pick<AppState, 'mpvClient' | 'currentMediaPath'>;
getConfig: () => ResolvedConfig;
logger: Pick<Logger, 'warn' | 'error'>;
overlayModalRuntime: Pick<OverlayModalRuntime, 'waitForModalOpen'>;
getOverlayNotifications: () => OverlayNotifications;
ensureOverlayStartupPrereqs: () => void;
ensureOverlayWindowsReadyForVisibilityActions: () => void;
sendToActiveOverlayWindow: OverlayModalRuntime['sendToActiveOverlayWindow'];
showMpvOsd: (text: string) => void;
showDesktopNotification: (title: string, options: { body?: string }) => void;
}
export type OverlayModalsAssembly = ReturnType<typeof createOverlayModalsAssembly>;
/**
* Opens the overlay-hosted modals (runtime options, Jimaku, session help, playlist browser, ...)
* and reports through the configured status notification when one cannot open.
*/
export function createOverlayModalsAssembly(deps: OverlayModalsAssemblyDeps) {
const { appState, logger } = deps;
function createOverlayHostedModalOpenDeps(): {
ensureOverlayStartupPrereqs: () => void;
ensureOverlayWindowsReadyForVisibilityActions: () => void;
sendToActiveOverlayWindow: OverlayModalRuntime['sendToActiveOverlayWindow'];
waitForModalOpen: (modal: OverlayHostedModal, timeoutMs: number) => Promise<boolean>;
logWarn: (message: string) => void;
} {
return {
ensureOverlayStartupPrereqs: () => deps.ensureOverlayStartupPrereqs(),
ensureOverlayWindowsReadyForVisibilityActions: () =>
deps.ensureOverlayWindowsReadyForVisibilityActions(),
sendToActiveOverlayWindow: (channel, payload, runtimeOptions) =>
deps.sendToActiveOverlayWindow(channel, payload, runtimeOptions),
waitForModalOpen: (modal, timeoutMs) =>
deps.overlayModalRuntime.waitForModalOpen(modal, timeoutMs),
logWarn: (message) => logger.warn(message),
};
}
function openOverlayHostedModalWithOsd(
openModal: (deps: ReturnType<typeof createOverlayHostedModalOpenDeps>) => Promise<boolean>,
unavailableMessage: string,
failureLogMessage: string,
): void {
void openModal(createOverlayHostedModalOpenDeps())
.then((opened) => {
if (!opened) {
deps.getOverlayNotifications().showConfiguredStatusNotification(unavailableMessage, {
variant: 'warning',
});
}
})
.catch((error) => {
logger.error(failureLogMessage, error);
deps.getOverlayNotifications().showConfiguredStatusNotification(unavailableMessage, {
variant: 'error',
});
});
}
function openRuntimeOptionsPalette(): void {
openOverlayHostedModalWithOsd(
openRuntimeOptionsModalRuntime,
'Runtime options overlay unavailable.',
'Failed to open runtime options overlay.',
);
}
function openJimakuOverlay(): void {
openOverlayHostedModalWithOsd(
openJimakuModalRuntime,
'Jimaku overlay unavailable.',
'Failed to open Jimaku overlay.',
);
}
function openTsukihimeOverlay(): void {
openOverlayHostedModalWithOsd(
openTsukihimeModalRuntime,
'TsukiHime overlay unavailable.',
'Failed to open TsukiHime overlay.',
);
}
function openSubtitleGenerationOverlay(): void {
openOverlayHostedModalWithOsd(
openSubtitleGenerationModal,
'Subtitle generation overlay unavailable.',
'Failed to open subtitle generation overlay.',
);
}
// currentMediaPath follows every mpv path change and is cleared on disconnect; the
// client's currentVideoPath outlives the connection, so a reconnect could reuse it.
function isMediaPlaybackActive(): boolean {
return appState.mpvClient?.connected === true && Boolean(appState.currentMediaPath?.trim());
}
function openSessionHelpOverlay(): void {
openOverlayHostedModalWithOsd(
(modalDeps) => openSessionHelpModalRuntime({ ...modalDeps, isMediaPlaybackActive }),
'Session help overlay unavailable.',
'Failed to open session help overlay.',
);
}
function openChangelogOverlay(): void {
openOverlayHostedModalWithOsd(
openChangelogModalRuntime,
'Changelog overlay unavailable.',
'Failed to open changelog overlay.',
);
}
function openCharacterDictionaryManagerOverlay(): void {
openCharacterDictionaryManagerWithConfigGate({
isCharacterDictionaryEnabled: () => deps.getConfig().subtitleStyle.nameMatchEnabled,
getNotificationType: () =>
deps.getOverlayNotifications().getConfiguredStatusNotificationType(),
openManager: () => {
openOverlayHostedModalWithOsd(
openCharacterDictionaryManagerModalRuntime,
'Character dictionary manager unavailable.',
'Failed to open character dictionary manager.',
);
},
showOsd: (message) => deps.showMpvOsd(message),
showOverlayNotification: (payload) =>
deps.getOverlayNotifications().showOverlayNotification(payload),
showDesktopNotification: (title, options) => deps.showDesktopNotification(title, options),
logWarn: (message, error) => logger.warn(message, error),
});
}
function openControllerSelectOverlay(): void {
openOverlayHostedModalWithOsd(
openControllerSelectModalRuntime,
'Controller select overlay unavailable.',
'Failed to open controller select overlay.',
);
}
function openControllerDebugOverlay(): void {
openOverlayHostedModalWithOsd(
openControllerDebugModalRuntime,
'Controller debug overlay unavailable.',
'Failed to open controller debug overlay.',
);
}
function openPlaylistBrowser(): void {
if (!appState.mpvClient?.connected) {
deps
.getOverlayNotifications()
.showConfiguredStatusNotification('Playlist browser requires active playback.', {
title: 'Playlist browser',
variant: 'warning',
});
return;
}
openOverlayHostedModalWithOsd(
openPlaylistBrowserRuntime,
'Playlist browser overlay unavailable.',
'Failed to open playlist browser overlay.',
);
}
return {
createOverlayHostedModalOpenDeps,
openOverlayHostedModalWithOsd,
openRuntimeOptionsPalette,
openJimakuOverlay,
openTsukihimeOverlay,
openSubtitleGenerationOverlay,
openSessionHelpOverlay,
openChangelogOverlay,
openCharacterDictionaryManagerOverlay,
openControllerSelectOverlay,
openControllerDebugOverlay,
openPlaylistBrowser,
};
}