mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-11-22 16:46:10 -08:00
feat(plugins): add JavaScript plugin system with overlay panel (#303)
This commit is contained in:
1 parent
90fc7cf940
commit
a9a4c4f083
110 files changed
+5897
-344
No files matched your search
+143
-16
@@ -474,6 +474,17 @@ import { openSubsyncManualModal as openSubsyncManualModalRuntime } from './main/
|
||||
import { openSessionHelpModal as openSessionHelpModalRuntime } from './main/runtime/session-help-open';
|
||||
import { openChangelogModal as openChangelogModalRuntime } from './main/runtime/changelog-open';
|
||||
import { createChangelogRuntime } from './main/runtime/changelog/changelog-runtime';
|
||||
import {
|
||||
createPluginHost,
|
||||
PLUGIN_DEACTIVATE_TIMEOUT_MS,
|
||||
} from './core/services/plugins/plugin-host';
|
||||
import { buildPluginSubtitleContext } from './core/services/plugins/subtitle-context';
|
||||
import type { PluginSubtitleLine } from './core/services/plugins/types';
|
||||
import {
|
||||
createPluginPanelRuntime,
|
||||
openPluginPanelModal,
|
||||
} from './main/runtime/plugin-panel-runtime';
|
||||
import { createPluginWindowRuntime } from './main/runtime/plugin-window-runtime';
|
||||
import { openCharacterDictionaryManagerModal as openCharacterDictionaryManagerModalRuntime } from './main/runtime/character-dictionary-open';
|
||||
import { openControllerSelectModal as openControllerSelectModalRuntime } from './main/runtime/controller-select-open';
|
||||
import { openControllerDebugModal as openControllerDebugModalRuntime } from './main/runtime/controller-debug-open';
|
||||
@@ -1003,6 +1014,7 @@ function requestAppQuit(): void {
|
||||
logger.warn('Failed to stop stats server while quitting.', error);
|
||||
});
|
||||
if (!forceQuitTimer) {
|
||||
// Quit cleanup unloads plugins first, so allow their budget plus 2 s for the rest.
|
||||
forceQuitTimer = setTimeout(() => {
|
||||
logger.warn('App quit timed out; forcing process exit.');
|
||||
void createForceQuitHandler({
|
||||
@@ -1010,7 +1022,7 @@ function requestAppQuit(): void {
|
||||
logError: (error) => logger.error('Failed to finalize stats before forced exit.', error),
|
||||
exit: () => app.exit(0),
|
||||
})();
|
||||
}, 2000);
|
||||
}, PLUGIN_DEACTIVATE_TIMEOUT_MS + 2000);
|
||||
}
|
||||
app.quit();
|
||||
}
|
||||
@@ -2947,20 +2959,7 @@ const overlayVisibilityComposer = composeOverlayVisibilityRuntime({
|
||||
},
|
||||
});
|
||||
|
||||
function createOverlayHostedModalOpenDeps(): {
|
||||
ensureOverlayStartupPrereqs: () => void;
|
||||
ensureOverlayWindowsReadyForVisibilityActions: () => void;
|
||||
sendToActiveOverlayWindow: (
|
||||
channel: string,
|
||||
payload?: unknown,
|
||||
runtimeOptions?: {
|
||||
restoreOnModalClose?: OverlayHostedModal;
|
||||
preferModalWindow?: boolean;
|
||||
},
|
||||
) => boolean;
|
||||
waitForModalOpen: (modal: OverlayHostedModal, timeoutMs: number) => Promise<boolean>;
|
||||
logWarn: (message: string) => void;
|
||||
} {
|
||||
function createOverlayHostedModalOpenDeps(): Parameters<typeof openPluginPanelModal>[0] {
|
||||
return {
|
||||
ensureOverlayStartupPrereqs: () => ensureOverlayStartupPrereqs(),
|
||||
ensureOverlayWindowsReadyForVisibilityActions: () =>
|
||||
@@ -2972,6 +2971,108 @@ function createOverlayHostedModalOpenDeps(): {
|
||||
};
|
||||
}
|
||||
|
||||
const pluginPanelRuntime = createPluginPanelRuntime({
|
||||
openModal: (getSnapshot, signal) =>
|
||||
openPluginPanelModal(createOverlayHostedModalOpenDeps(), getSnapshot, signal),
|
||||
closeModal: () => overlayModalRuntime.handleOverlayModalClosed('plugin-panel'),
|
||||
// The panel may live in either window; broadcastToOverlayWindows only reaches the main one.
|
||||
broadcast: (channel, payload) => {
|
||||
for (const window of [overlayManager.getMainWindow(), overlayManager.getModalWindow()]) {
|
||||
if (window && !window.isDestroyed()) window.webContents.send(channel, payload);
|
||||
}
|
||||
},
|
||||
onOpenFailed: () =>
|
||||
overlayNotificationsRuntime.showConfiguredStatusNotification('Plugin panel unavailable.', {
|
||||
variant: 'warning',
|
||||
}),
|
||||
});
|
||||
|
||||
function getPluginCurrentSubtitle(): PluginSubtitleLine | null {
|
||||
const text = appState.currentSubText.trim();
|
||||
if (!text) return null;
|
||||
// Same view as mining: the parsed line with its cue timings on the subtitle file's timeline.
|
||||
const resolved = resolvePrimarySubtitle({
|
||||
liveText: appState.mpvClient?.currentSubText ?? '',
|
||||
currentTimeSec: toSubtitleClock(
|
||||
Number(appState.mpvClient?.currentTimePos),
|
||||
appState.mpvClient?.currentSubDelay ?? 0,
|
||||
),
|
||||
cues: appState.activeParsedSubtitleCues,
|
||||
});
|
||||
const resolvedText = resolved?.text.replace(/\n{2,}/g, '\n').trim();
|
||||
if (
|
||||
resolved &&
|
||||
resolvedText &&
|
||||
Number.isFinite(resolved.startTime) &&
|
||||
Number.isFinite(resolved.endTime) &&
|
||||
resolved.endTime > resolved.startTime
|
||||
) {
|
||||
return { text: resolvedText, startTime: resolved.startTime, endTime: resolved.endTime };
|
||||
}
|
||||
const client = appState.mpvClient;
|
||||
// The client reports a cleared sub-start as zero, so only trust timing for a live line.
|
||||
const hasTiming = client !== null && client.currentSubEnd > client.currentSubStart;
|
||||
return {
|
||||
text,
|
||||
startTime: hasTiming ? client.currentSubStart : null,
|
||||
endTime: hasTiming ? client.currentSubEnd : null,
|
||||
};
|
||||
}
|
||||
|
||||
const pluginWindowRuntime = createPluginWindowRuntime({
|
||||
createBrowserWindow: (options) => new BrowserWindow(options),
|
||||
promoteAboveOverlay: (window) => promoteSettingsWindowAboveOverlay(window),
|
||||
logError: (message) => logger.error(message),
|
||||
});
|
||||
|
||||
const pluginHost = createPluginHost({
|
||||
pluginsDir: path.join(CONFIG_DIR, 'plugins'),
|
||||
pluginDataDir: path.join(USER_DATA_PATH, 'plugin-data'),
|
||||
getPluginConfig: () => configService.getConfig().plugins,
|
||||
createLogger,
|
||||
isMpvConnected: () => appState.mpvClient?.connected === true,
|
||||
sendMpvCommand: (command) => sendMpvCommandRuntime(appState.mpvClient, command),
|
||||
requestMpvProperty: async (name) => {
|
||||
if (!appState.mpvClient) throw new Error('mpv is not connected.');
|
||||
return await appState.mpvClient.requestProperty(name);
|
||||
},
|
||||
requestMpvCommand: async (command) => {
|
||||
if (!appState.mpvClient) throw new Error('mpv is not connected.');
|
||||
return await appState.mpvClient.requestCommand(command);
|
||||
},
|
||||
ensureMpvRunning: () => ensureMpvConnectedForPlayback(),
|
||||
invokeAnkiConnect: (action, params, options) => {
|
||||
const ankiConfig = configService.getConfig().ankiConnect;
|
||||
const url = (
|
||||
appState.runtimeOptionsManager?.getEffectiveAnkiConnectConfig(ankiConfig) ?? ankiConfig
|
||||
).url;
|
||||
if (!url) return Promise.reject(new Error('ankiConnect.url is not configured.'));
|
||||
// No retries: a retried `sync` or `addNote` could run twice.
|
||||
return new AnkiConnectClient(url).invoke(action, params, {
|
||||
timeout: options.timeoutMs,
|
||||
maxRetries: 0,
|
||||
});
|
||||
},
|
||||
dispatchSessionAction: (request) => dispatchSessionAction(request),
|
||||
getCurrentSubtitle: () => getPluginCurrentSubtitle(),
|
||||
getSubtitleContext: (options) =>
|
||||
buildPluginSubtitleContext({
|
||||
cues: appState.activeParsedSubtitleCues,
|
||||
current: getPluginCurrentSubtitle(),
|
||||
recentLines: (appState.subtitleTimingTracker?.getRecentEntries(40) ?? []).map((entry) => ({
|
||||
text: entry.displayText,
|
||||
startTime: entry.startTime,
|
||||
endTime: entry.endTime,
|
||||
})),
|
||||
...options,
|
||||
}),
|
||||
notify: (message, variant) =>
|
||||
overlayNotificationsRuntime.showConfiguredStatusNotification(message, { variant }),
|
||||
openPanel: (content) => pluginPanelRuntime.open(content),
|
||||
openWindow: (request) => pluginWindowRuntime.open(request),
|
||||
unstable: { appState, configService, overlayManager },
|
||||
});
|
||||
|
||||
const mediaTimingFrameExtractor = createMediaTimingFrameExtractor();
|
||||
const mediaTimingReviewRuntime = createMediaTimingReviewRuntime({
|
||||
getMpvClient: () => appState.mpvClient,
|
||||
@@ -4201,6 +4302,7 @@ const {
|
||||
void appState.discordPresenceService?.stop();
|
||||
appState.discordPresenceService = null;
|
||||
},
|
||||
unloadPlugins: () => pluginHost.unloadAll(),
|
||||
},
|
||||
shouldRestoreWindowsOnActivateMainDeps: {
|
||||
isOverlayRuntimeInitialized: () => appState.overlayRuntimeInitialized,
|
||||
@@ -4468,6 +4570,9 @@ const { appReadyRuntimeRunner } = composeAppReadyRuntime({
|
||||
createMpvClient: () => {
|
||||
appState.mpvClient = createMpvClientRuntimeService();
|
||||
},
|
||||
startPlugins: () => {
|
||||
void pluginHost.loadAll();
|
||||
},
|
||||
getResolvedConfig: () => configService.getConfig(),
|
||||
getConfigWarnings: () => configService.getWarnings(),
|
||||
logConfigWarning: (warning) => appLogger.logConfigWarning(warning),
|
||||
@@ -5163,6 +5268,7 @@ function createMpvClientRuntimeService(): MpvIpcClient {
|
||||
client.on('media-path-change', aniSkipRuntime.handleMediaPathChange);
|
||||
client.on('time-pos-change', aniSkipRuntime.handleTimePosChange);
|
||||
client.on('client-message', aniSkipRuntime.handleClientMessage);
|
||||
pluginHost.attachMpvClient(client);
|
||||
return client;
|
||||
}
|
||||
|
||||
@@ -5289,6 +5395,14 @@ function initializeOverlayRuntime(): void {
|
||||
overlayModalRuntime.primeModalWindow();
|
||||
}
|
||||
appState.ankiIntegration?.setRecordCardsMinedCallback(recordTrackedCardsMined);
|
||||
appState.ankiIntegration?.setMiningProvenanceProvider(() => ({
|
||||
minedAt: Date.now(),
|
||||
media: pluginHost.getMediaSnapshot(),
|
||||
subtitle: getPluginCurrentSubtitle(),
|
||||
}));
|
||||
appState.ankiIntegration?.setCardsReadyCallback((noteIds, notes) =>
|
||||
pluginHost.emitAnkiEvent('card-mined', { noteIds, notes }),
|
||||
);
|
||||
appState.ankiIntegration?.setKnownWordCacheUpdatedCallback(refreshCurrentSubtitleAnnotations);
|
||||
appState.ankiIntegration?.setSubtitleMiningContextConsumer(consumePendingSubtitleMiningContext);
|
||||
appState.ankiIntegration?.setMediaTimingReviewCallback(mediaTimingReviewRuntime.requestReview);
|
||||
@@ -5706,6 +5820,11 @@ async function dispatchSessionAction(request: SessionActionDispatchRequest): Pro
|
||||
},
|
||||
playNextPlaylistItem: () =>
|
||||
sendMpvCommandRuntime(appState.mpvClient, ['playlist-next', 'force']),
|
||||
runPluginAction: async (pluginActionId) => {
|
||||
const ran = await pluginHost.runAction(pluginActionId);
|
||||
if (!ran) logger.warn(`Plugin action is not registered: ${pluginActionId}`);
|
||||
return ran;
|
||||
},
|
||||
showMpvOsd: (text) => showConfiguredPlaybackFeedback(text),
|
||||
});
|
||||
}
|
||||
@@ -5787,7 +5906,15 @@ const { registerIpcRuntimeHandlers } = composeIpcRuntimeHandlers({
|
||||
return false;
|
||||
}
|
||||
},
|
||||
onOverlayModalClosed: (modal, senderWindow) => {
|
||||
onOverlayModalClosed: (modal, senderWindow, instanceId) => {
|
||||
// A late close for a replaced plugin panel must not hide the panel now showing.
|
||||
if (
|
||||
modal === 'plugin-panel' &&
|
||||
instanceId !== undefined &&
|
||||
!pluginPanelRuntime.handleModalClosed(instanceId)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
if (modal === 'media-timing-review') {
|
||||
void mediaTimingReviewRuntime.dispose();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user