refactor(main): move plugins, session actions and subtitle generation into assemblies (#327)

* refactor(main): build overlay geometry and pointer interaction in the overlay assembly

Inject Electron's screen (and the stats-window promotion) into the overlay geometry and
visible-overlay interaction runtimes so they load under bun test, then construct both in
createOverlayAssembly together with the overlay visibility composer and the mpv subtitle
suppression handlers. Replace the three regex tests that pinned the moved Linux input and
shape code with behaviour tests on the runtimes.

* refactor(main): move the autoplay-ready gate into the playback assembly

The autoplay-ready gate, its post-warm subtitle prime, the current-media tokenization gate and
managed local subtitle selection all read playback state, and playback signals through the
gate, so createPlaybackAssembly now builds them. The overlay reaches the gate through a getter.

Replace the last four regex guards with behaviour tests: the gate holds the release until the
visible overlay has measured the line and ignores untokenized lines during warmup (playback),
cached autoplay priming never paints raw first and the sidebar media path is tagged only after
prefetch loads (priming runtime), and dashboard mining overrides the Hachidori Anki server only
when the proxy is on (stats server runtime). main-wiring.test.ts is now empty and removed.

* refactor(main): move the plugin host, panel and windows into a plugins assembly

* refactor(main): move session action dispatch and renderer mpv commands into an assembly

createSessionActionsAssembly owns the session action dispatcher, the secondary-subtitle mode cycle, queueing the clipboard's video, and the mpv commands the overlay renderer sends. The runtime option cycle both paths used is now one function.

* refactor(main): move Whisper subtitle generation into an assembly

* refactor(main): resolve subtitle annotation options in a runtime module

The runtime-option-over-config resolution that the texthooker page and the MeCab startup decision share moves out of main.ts into createAnnotationOptions.

* test(main): cover a non-boolean runtime annotation option value

* docs: describe the plugins, session actions and subtitle generation assemblies

Update ownership for overlay geometry and pointer interaction, the autoplay-ready gate, and annotation options; point the overlay priming entry points at their new modules; the main-wiring regex debt is gone.

* build: export MpvClientEvents so declaration emit can name the plugins assembly's return type
This commit is contained in:
sudacode authored and GitHub committed 2026-11-04 21:54:49 -08:00
1 parent 14ef7e823c
commit f89f78b861
28 files changed
+2005 -1147

No files matched your search

+205
View File
@@ -0,0 +1,205 @@
import {
cycleSecondarySubMode as cycleSecondarySubModeCore,
playNextSubtitleRuntime,
replayCurrentSubtitleRuntime,
sendMpvCommandRuntime,
type createOverlayManager,
} from '../../core/services';
import { applyRuntimeOptionResultRuntime } from '../../core/services/runtime-options-ipc';
import { dispatchSessionAction as dispatchSessionActionCore } from '../../core/services/session-actions';
import type { Logger } from '../../logger';
import { IPC_CHANNELS } from '../../shared/ipc/contracts';
import type { ResolvedConfig, SecondarySubMode } from '../../types';
import type { RuntimeOptionApplyResult, RuntimeOptionId } from '../../types/runtime-options';
import type { SessionActionDispatchRequest } from '../../types/runtime';
import type { MpvCommandFromIpcRuntimeDeps } from '../ipc-mpv-command';
import { appendClipboardVideoToQueueRuntime } from '../runtime/clipboard-queue';
import { createAppendClipboardVideoToQueueHandler } from '../runtime/overlay-main-actions';
import type { createOverlayNotificationsRuntime } from '../runtime/overlay-notifications-runtime';
import { createCycleSecondarySubModeRuntimeHandler } from '../runtime/secondary-sub-mode-runtime-handler';
import { openSubtitleSelectionModal } from '../runtime/subtitle-selection';
import type { AppState } from '../state';
import type { MiningAssembly } from './mining';
import type { OverlayModalsAssembly } from './overlay-modals';
import type { PluginsAssembly } from './plugins';
import type { StatsAssembly } from './stats';
import type { YoutubeAssembly } from './youtube';
/** What session actions call into; every sibling here is built before the assembly. */
export interface SessionActionsAssemblyDeps {
appState: AppState;
getConfig: () => ResolvedConfig;
logger: Pick<Logger, 'warn'>;
overlayManager: Pick<ReturnType<typeof createOverlayManager>, 'broadcastToOverlayWindows'>;
getOverlayNotifications: () => Pick<
ReturnType<typeof createOverlayNotificationsRuntime>,
| 'showConfiguredPlaybackFeedback'
| 'showConfiguredStatusNotification'
| 'toggleNotificationHistoryPanel'
>;
stats: Pick<StatsAssembly, 'toggleStatsOverlay' | 'markActiveVideoWatched'>;
mining: Pick<
MiningAssembly,
| 'copyCurrentSubtitle'
| 'copySubtitleCount'
| 'updateLastCardFromClipboard'
| 'triggerFieldGrouping'
| 'mineSentenceCard'
| 'mineSentenceCount'
| 'markLastCardAsAudioCard'
>;
overlayModals: Pick<
OverlayModalsAssembly,
| 'openRuntimeOptionsPalette'
| 'openJimakuOverlay'
| 'openTsukihimeOverlay'
| 'openSessionHelpOverlay'
| 'openOverlayHostedModalWithOsd'
| 'openSubtitleGenerationOverlay'
| 'openCharacterDictionaryManagerOverlay'
| 'openControllerSelectOverlay'
| 'openControllerDebugOverlay'
| 'openPlaylistBrowser'
>;
youtube: Pick<YoutubeAssembly, 'openTrackPickerFromPlayback'>;
plugins: { host: Pick<PluginsAssembly['host'], 'runAction'> };
toggleVisibleOverlay: () => void;
triggerSubsyncFromConfig: () => Promise<void>;
readClipboardText: () => string;
/** Raw mpv OSD, bypassing the configured notification type. */
showMpvOsd: (text: string) => void;
/** Renderer-issued mpv commands; playback sanitizes subtitle seeks. */
sendRendererMpvCommand: (command: (string | number)[]) => void;
}
export type SessionActionsAssembly = ReturnType<typeof createSessionActionsAssembly>;
/**
* Owns what a shortcut, controller button, CLI command or plugin can trigger in the running
* session: the session action dispatcher, the secondary-subtitle mode cycle, queueing the
* clipboard's video, and the mpv commands the overlay renderer sends.
*/
export function createSessionActionsAssembly(deps: SessionActionsAssemblyDeps) {
const { appState, overlayManager, overlayModals, mining } = deps;
function setSecondarySubMode(mode: SecondarySubMode): void {
appState.secondarySubMode = mode;
}
const cycleSecondarySubMode = createCycleSecondarySubModeRuntimeHandler({
cycleSecondarySubModeMainDeps: {
getSecondarySubMode: () => appState.secondarySubMode,
setSecondarySubMode: (mode: SecondarySubMode) => {
setSecondarySubMode(mode);
},
getLastSecondarySubToggleAtMs: () => appState.lastSecondarySubToggleAtMs,
setLastSecondarySubToggleAtMs: (timestampMs: number) => {
appState.lastSecondarySubToggleAtMs = timestampMs;
},
broadcastSecondarySubMode: (mode) => {
overlayManager.broadcastToOverlayWindows('secondary-subtitle:mode', mode);
},
showMpvOsd: (text: string) =>
deps.getOverlayNotifications().showConfiguredPlaybackFeedback(text),
},
cycleSecondarySubMode: (cycleDeps) => cycleSecondarySubModeCore(cycleDeps),
});
const appendClipboardVideoToQueue = createAppendClipboardVideoToQueueHandler({
appendClipboardVideoToQueueRuntime,
getMpvClient: () => appState.mpvClient,
readClipboardText: () => deps.readClipboardText(),
showMpvOsd: (text) => deps.getOverlayNotifications().showConfiguredStatusNotification(text),
sendMpvCommand: (command) => {
sendMpvCommandRuntime(appState.mpvClient, command);
},
});
/** Steps a runtime option and reports the new value as playback feedback. */
function cycleRuntimeOption(id: RuntimeOptionId, direction: 1 | -1): RuntimeOptionApplyResult {
if (!appState.runtimeOptionsManager) {
return { ok: false, error: 'Runtime options manager unavailable' };
}
return applyRuntimeOptionResultRuntime(
appState.runtimeOptionsManager.cycleOption(id, direction),
(text) => deps.getOverlayNotifications().showConfiguredPlaybackFeedback(text),
);
}
async function dispatchSessionAction(request: SessionActionDispatchRequest): Promise<void> {
await dispatchSessionActionCore(request, {
toggleStatsOverlay: async () => await deps.stats.toggleStatsOverlay(),
toggleVisibleOverlay: () => deps.toggleVisibleOverlay(),
copyCurrentSubtitle: () => mining.copyCurrentSubtitle(),
copySubtitleCount: (count) => mining.copySubtitleCount(count),
updateLastCardFromClipboard: () => mining.updateLastCardFromClipboard(),
triggerFieldGrouping: () => mining.triggerFieldGrouping(),
triggerSubsyncFromConfig: () => deps.triggerSubsyncFromConfig(),
mineSentenceCard: () => mining.mineSentenceCard(),
mineSentenceCount: (count) => mining.mineSentenceCount(count),
toggleSecondarySub: () => cycleSecondarySubMode(),
toggleSubtitleSidebar: () =>
overlayManager.broadcastToOverlayWindows(IPC_CHANNELS.event.subtitleSidebarToggle),
toggleNotificationHistory: () =>
deps.getOverlayNotifications().toggleNotificationHistoryPanel(),
appendClipboardVideoToQueue: () => {
appendClipboardVideoToQueue();
},
markLastCardAsAudioCard: () => mining.markLastCardAsAudioCard(),
markActiveVideoWatched: () => deps.stats.markActiveVideoWatched(),
openRuntimeOptionsPalette: () => overlayModals.openRuntimeOptionsPalette(),
openJimaku: () => overlayModals.openJimakuOverlay(),
openTsukihime: () => overlayModals.openTsukihimeOverlay(),
openSessionHelp: () => overlayModals.openSessionHelpOverlay(),
openSubtitleSelection: () => {
if (!deps.getConfig().subtitleSelection.enabled) return;
overlayModals.openOverlayHostedModalWithOsd(
openSubtitleSelectionModal,
'Subtitle selection overlay unavailable.',
'Failed to open subtitle selection overlay.',
);
},
openSubtitleGeneration: () => overlayModals.openSubtitleGenerationOverlay(),
openCharacterDictionaryManager: () => overlayModals.openCharacterDictionaryManagerOverlay(),
openControllerSelect: () => overlayModals.openControllerSelectOverlay(),
openControllerDebug: () => overlayModals.openControllerDebugOverlay(),
openYoutubeTrackPicker: () => deps.youtube.openTrackPickerFromPlayback(),
openPlaylistBrowser: () => overlayModals.openPlaylistBrowser(),
replayCurrentSubtitle: () => replayCurrentSubtitleRuntime(appState.mpvClient),
playNextSubtitle: () => playNextSubtitleRuntime(appState.mpvClient),
cycleRuntimeOption,
playNextPlaylistItem: () =>
sendMpvCommandRuntime(appState.mpvClient, ['playlist-next', 'force']),
runPluginAction: async (pluginActionId) => {
const ran = await deps.plugins.host.runAction(pluginActionId);
if (!ran) deps.logger.warn(`Plugin action is not registered: ${pluginActionId}`);
return ran;
},
showMpvOsd: (text) => deps.getOverlayNotifications().showConfiguredPlaybackFeedback(text),
});
}
/** The mpv commands the overlay renderer sends over IPC (`mpv:command`). */
const mpvCommandDeps: MpvCommandFromIpcRuntimeDeps = {
triggerSubsyncFromConfig: () => deps.triggerSubsyncFromConfig(),
openRuntimeOptionsPalette: () => overlayModals.openRuntimeOptionsPalette(),
openJimaku: () => overlayModals.openJimakuOverlay(),
openTsukihime: () => overlayModals.openTsukihimeOverlay(),
openYoutubeTrackPicker: () => deps.youtube.openTrackPickerFromPlayback(),
openPlaylistBrowser: () => overlayModals.openPlaylistBrowser(),
cycleRuntimeOption,
showMpvOsd: (text: string) =>
deps.getOverlayNotifications().showConfiguredStatusNotification(text),
showRawMpvOsd: (text: string) => deps.showMpvOsd(text),
showPlaybackFeedback: (text: string) =>
deps.getOverlayNotifications().showConfiguredPlaybackFeedback(text),
replayCurrentSubtitle: () => replayCurrentSubtitleRuntime(appState.mpvClient),
playNextSubtitle: () => playNextSubtitleRuntime(appState.mpvClient),
sendMpvCommand: (rawCommand: (string | number)[]) => deps.sendRendererMpvCommand(rawCommand),
getMpvClient: () => appState.mpvClient,
isMpvConnected: () => Boolean(appState.mpvClient && appState.mpvClient.connected),
hasRuntimeOptionsManager: () => appState.runtimeOptionsManager !== null,
};
return { dispatchSessionAction, cycleSecondarySubMode, setSecondarySubMode, mpvCommandDeps };
}