mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-02-28 06:22:45 -08:00
refactor: extract overlay runtime and anki/jimaku orchestration
This commit is contained in:
169
src/core/services/anki-jimaku-runtime-service.ts
Normal file
169
src/core/services/anki-jimaku-runtime-service.ts
Normal file
@@ -0,0 +1,169 @@
|
||||
import { AnkiIntegration } from "../../anki-integration";
|
||||
import {
|
||||
AnkiConnectConfig,
|
||||
JimakuApiResponse,
|
||||
JimakuEntry,
|
||||
JimakuFileEntry,
|
||||
JimakuLanguagePreference,
|
||||
JimakuMediaInfo,
|
||||
KikuFieldGroupingChoice,
|
||||
KikuFieldGroupingRequestData,
|
||||
} from "../../types";
|
||||
import { sortJimakuFiles } from "../../jimaku/utils";
|
||||
import { registerAnkiJimakuIpcHandlers } from "./anki-jimaku-ipc-service";
|
||||
|
||||
interface MpvClientLike {
|
||||
connected: boolean;
|
||||
send: (payload: { command: string[] }) => void;
|
||||
}
|
||||
|
||||
interface RuntimeOptionsManagerLike {
|
||||
getEffectiveAnkiConnectConfig: (config?: AnkiConnectConfig) => AnkiConnectConfig;
|
||||
}
|
||||
|
||||
interface SubtitleTimingTrackerLike {
|
||||
cleanup: () => void;
|
||||
}
|
||||
|
||||
export function registerAnkiJimakuIpcRuntimeService(options: {
|
||||
patchAnkiConnectEnabled: (enabled: boolean) => void;
|
||||
getResolvedConfig: () => { ankiConnect?: AnkiConnectConfig };
|
||||
getRuntimeOptionsManager: () => RuntimeOptionsManagerLike | null;
|
||||
getSubtitleTimingTracker: () => SubtitleTimingTrackerLike | null;
|
||||
getMpvClient: () => MpvClientLike | null;
|
||||
getAnkiIntegration: () => AnkiIntegration | null;
|
||||
setAnkiIntegration: (integration: AnkiIntegration | null) => void;
|
||||
showDesktopNotification: (title: string, options: { body?: string; icon?: string }) => void;
|
||||
createFieldGroupingCallback: () => (
|
||||
data: KikuFieldGroupingRequestData,
|
||||
) => Promise<KikuFieldGroupingChoice>;
|
||||
broadcastRuntimeOptionsChanged: () => void;
|
||||
getFieldGroupingResolver: () => ((choice: KikuFieldGroupingChoice) => void) | null;
|
||||
setFieldGroupingResolver: (resolver: ((choice: KikuFieldGroupingChoice) => void) | null) => void;
|
||||
parseMediaInfo: (mediaPath: string | null) => JimakuMediaInfo;
|
||||
getCurrentMediaPath: () => string | null;
|
||||
jimakuFetchJson: <T>(
|
||||
endpoint: string,
|
||||
query?: Record<string, string | number | boolean | null | undefined>,
|
||||
) => Promise<JimakuApiResponse<T>>;
|
||||
getJimakuMaxEntryResults: () => number;
|
||||
getJimakuLanguagePreference: () => JimakuLanguagePreference;
|
||||
resolveJimakuApiKey: () => Promise<string | null>;
|
||||
isRemoteMediaPath: (mediaPath: string) => boolean;
|
||||
downloadToFile: (
|
||||
url: string,
|
||||
destPath: string,
|
||||
headers: Record<string, string>,
|
||||
) => Promise<{ ok: true; path: string } | { ok: false; error: { error: string; code?: number; retryAfter?: number } }>;
|
||||
}): void {
|
||||
registerAnkiJimakuIpcHandlers({
|
||||
setAnkiConnectEnabled: (enabled) => {
|
||||
options.patchAnkiConnectEnabled(enabled);
|
||||
const config = options.getResolvedConfig();
|
||||
const subtitleTimingTracker = options.getSubtitleTimingTracker();
|
||||
const mpvClient = options.getMpvClient();
|
||||
const ankiIntegration = options.getAnkiIntegration();
|
||||
|
||||
if (enabled && !ankiIntegration && subtitleTimingTracker && mpvClient) {
|
||||
const runtimeOptionsManager = options.getRuntimeOptionsManager();
|
||||
const effectiveAnkiConfig = runtimeOptionsManager
|
||||
? runtimeOptionsManager.getEffectiveAnkiConnectConfig(config.ankiConnect)
|
||||
: config.ankiConnect;
|
||||
const integration = new AnkiIntegration(
|
||||
effectiveAnkiConfig as never,
|
||||
subtitleTimingTracker as never,
|
||||
mpvClient as never,
|
||||
(text: string) => {
|
||||
if (mpvClient) {
|
||||
mpvClient.send({
|
||||
command: ["show-text", text, "3000"],
|
||||
});
|
||||
}
|
||||
},
|
||||
options.showDesktopNotification,
|
||||
options.createFieldGroupingCallback(),
|
||||
);
|
||||
integration.start();
|
||||
options.setAnkiIntegration(integration);
|
||||
console.log("AnkiConnect integration enabled");
|
||||
} else if (!enabled && ankiIntegration) {
|
||||
ankiIntegration.destroy();
|
||||
options.setAnkiIntegration(null);
|
||||
console.log("AnkiConnect integration disabled");
|
||||
}
|
||||
|
||||
options.broadcastRuntimeOptionsChanged();
|
||||
},
|
||||
clearAnkiHistory: () => {
|
||||
const subtitleTimingTracker = options.getSubtitleTimingTracker();
|
||||
if (subtitleTimingTracker) {
|
||||
subtitleTimingTracker.cleanup();
|
||||
console.log("AnkiConnect subtitle timing history cleared");
|
||||
}
|
||||
},
|
||||
respondFieldGrouping: (choice) => {
|
||||
const resolver = options.getFieldGroupingResolver();
|
||||
if (resolver) {
|
||||
resolver(choice);
|
||||
options.setFieldGroupingResolver(null);
|
||||
}
|
||||
},
|
||||
buildKikuMergePreview: async (request) => {
|
||||
const integration = options.getAnkiIntegration();
|
||||
if (!integration) {
|
||||
return { ok: false, error: "AnkiConnect integration not enabled" };
|
||||
}
|
||||
return integration.buildFieldGroupingPreview(
|
||||
request.keepNoteId,
|
||||
request.deleteNoteId,
|
||||
request.deleteDuplicate,
|
||||
);
|
||||
},
|
||||
getJimakuMediaInfo: () => options.parseMediaInfo(options.getCurrentMediaPath()),
|
||||
searchJimakuEntries: async (query) => {
|
||||
console.log(`[jimaku] search-entries query: "${query.query}"`);
|
||||
const response = await options.jimakuFetchJson<JimakuEntry[]>(
|
||||
"/api/entries/search",
|
||||
{
|
||||
anime: true,
|
||||
query: query.query,
|
||||
},
|
||||
);
|
||||
if (!response.ok) return response;
|
||||
const maxResults = options.getJimakuMaxEntryResults();
|
||||
console.log(
|
||||
`[jimaku] search-entries returned ${response.data.length} results (capped to ${maxResults})`,
|
||||
);
|
||||
return { ok: true, data: response.data.slice(0, maxResults) };
|
||||
},
|
||||
listJimakuFiles: async (query) => {
|
||||
console.log(
|
||||
`[jimaku] list-files entryId=${query.entryId} episode=${query.episode ?? "all"}`,
|
||||
);
|
||||
const response = await options.jimakuFetchJson<JimakuFileEntry[]>(
|
||||
`/api/entries/${query.entryId}/files`,
|
||||
{
|
||||
episode: query.episode ?? undefined,
|
||||
},
|
||||
);
|
||||
if (!response.ok) return response;
|
||||
const sorted = sortJimakuFiles(
|
||||
response.data,
|
||||
options.getJimakuLanguagePreference(),
|
||||
);
|
||||
console.log(`[jimaku] list-files returned ${sorted.length} files`);
|
||||
return { ok: true, data: sorted };
|
||||
},
|
||||
resolveJimakuApiKey: () => options.resolveJimakuApiKey(),
|
||||
getCurrentMediaPath: () => options.getCurrentMediaPath(),
|
||||
isRemoteMediaPath: (mediaPath) => options.isRemoteMediaPath(mediaPath),
|
||||
downloadToFile: (url, destPath, headers) =>
|
||||
options.downloadToFile(url, destPath, headers),
|
||||
onDownloadedSubtitle: (pathToSubtitle) => {
|
||||
const mpvClient = options.getMpvClient();
|
||||
if (mpvClient && mpvClient.connected) {
|
||||
mpvClient.send({ command: ["sub-add", pathToSubtitle, "select"] });
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
59
src/core/services/field-grouping-service.ts
Normal file
59
src/core/services/field-grouping-service.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import {
|
||||
KikuFieldGroupingChoice,
|
||||
KikuFieldGroupingRequestData,
|
||||
} from "../../types";
|
||||
|
||||
export function createFieldGroupingCallbackService(options: {
|
||||
getVisibleOverlayVisible: () => boolean;
|
||||
getInvisibleOverlayVisible: () => boolean;
|
||||
setVisibleOverlayVisible: (visible: boolean) => void;
|
||||
setInvisibleOverlayVisible: (visible: boolean) => void;
|
||||
getResolver: () => ((choice: KikuFieldGroupingChoice) => void) | null;
|
||||
setResolver: (resolver: ((choice: KikuFieldGroupingChoice) => void) | null) => void;
|
||||
sendRequestToVisibleOverlay: (data: KikuFieldGroupingRequestData) => boolean;
|
||||
}): (data: KikuFieldGroupingRequestData) => Promise<KikuFieldGroupingChoice> {
|
||||
return async (
|
||||
data: KikuFieldGroupingRequestData,
|
||||
): Promise<KikuFieldGroupingChoice> => {
|
||||
return new Promise((resolve) => {
|
||||
const previousVisibleOverlay = options.getVisibleOverlayVisible();
|
||||
const previousInvisibleOverlay = options.getInvisibleOverlayVisible();
|
||||
let settled = false;
|
||||
|
||||
const finish = (choice: KikuFieldGroupingChoice): void => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
options.setResolver(null);
|
||||
resolve(choice);
|
||||
|
||||
if (!previousVisibleOverlay && options.getVisibleOverlayVisible()) {
|
||||
options.setVisibleOverlayVisible(false);
|
||||
}
|
||||
if (options.getInvisibleOverlayVisible() !== previousInvisibleOverlay) {
|
||||
options.setInvisibleOverlayVisible(previousInvisibleOverlay);
|
||||
}
|
||||
};
|
||||
|
||||
options.setResolver(finish);
|
||||
if (!options.sendRequestToVisibleOverlay(data)) {
|
||||
finish({
|
||||
keepNoteId: 0,
|
||||
deleteNoteId: 0,
|
||||
deleteDuplicate: true,
|
||||
cancelled: true,
|
||||
});
|
||||
return;
|
||||
}
|
||||
setTimeout(() => {
|
||||
if (!settled) {
|
||||
finish({
|
||||
keepNoteId: 0,
|
||||
deleteNoteId: 0,
|
||||
deleteDuplicate: true,
|
||||
cancelled: true,
|
||||
});
|
||||
}
|
||||
}, 90000);
|
||||
});
|
||||
};
|
||||
}
|
||||
103
src/core/services/overlay-runtime-init-service.ts
Normal file
103
src/core/services/overlay-runtime-init-service.ts
Normal file
@@ -0,0 +1,103 @@
|
||||
import { BrowserWindow } from "electron";
|
||||
import { AnkiIntegration } from "../../anki-integration";
|
||||
import { BaseWindowTracker, createWindowTracker } from "../../window-trackers";
|
||||
import {
|
||||
AnkiConnectConfig,
|
||||
KikuFieldGroupingChoice,
|
||||
KikuFieldGroupingRequestData,
|
||||
WindowGeometry,
|
||||
} from "../../types";
|
||||
|
||||
export function initializeOverlayRuntimeService(options: {
|
||||
backendOverride: string | null;
|
||||
getInitialInvisibleOverlayVisibility: () => boolean;
|
||||
createMainWindow: () => void;
|
||||
createInvisibleWindow: () => void;
|
||||
registerGlobalShortcuts: () => void;
|
||||
updateOverlayBounds: (geometry: WindowGeometry) => void;
|
||||
isVisibleOverlayVisible: () => boolean;
|
||||
isInvisibleOverlayVisible: () => boolean;
|
||||
updateVisibleOverlayVisibility: () => void;
|
||||
updateInvisibleOverlayVisibility: () => void;
|
||||
getOverlayWindows: () => BrowserWindow[];
|
||||
syncOverlayShortcuts: () => void;
|
||||
setWindowTracker: (tracker: BaseWindowTracker | null) => void;
|
||||
getResolvedConfig: () => { ankiConnect?: AnkiConnectConfig };
|
||||
getSubtitleTimingTracker: () => unknown | null;
|
||||
getMpvClient: () => { send?: (payload: { command: string[] }) => void } | null;
|
||||
getRuntimeOptionsManager: () => {
|
||||
getEffectiveAnkiConnectConfig: (config?: AnkiConnectConfig) => AnkiConnectConfig;
|
||||
} | null;
|
||||
setAnkiIntegration: (integration: unknown | null) => void;
|
||||
showDesktopNotification: (title: string, options: { body?: string; icon?: string }) => void;
|
||||
createFieldGroupingCallback: () => (
|
||||
data: KikuFieldGroupingRequestData,
|
||||
) => Promise<KikuFieldGroupingChoice>;
|
||||
}): {
|
||||
invisibleOverlayVisible: boolean;
|
||||
} {
|
||||
options.createMainWindow();
|
||||
options.createInvisibleWindow();
|
||||
const invisibleOverlayVisible = options.getInitialInvisibleOverlayVisibility();
|
||||
options.registerGlobalShortcuts();
|
||||
|
||||
const windowTracker = createWindowTracker(options.backendOverride);
|
||||
options.setWindowTracker(windowTracker);
|
||||
if (windowTracker) {
|
||||
windowTracker.onGeometryChange = (geometry: WindowGeometry) => {
|
||||
options.updateOverlayBounds(geometry);
|
||||
};
|
||||
windowTracker.onWindowFound = (geometry: WindowGeometry) => {
|
||||
options.updateOverlayBounds(geometry);
|
||||
if (options.isVisibleOverlayVisible()) {
|
||||
options.updateVisibleOverlayVisibility();
|
||||
}
|
||||
if (options.isInvisibleOverlayVisible()) {
|
||||
options.updateInvisibleOverlayVisibility();
|
||||
}
|
||||
};
|
||||
windowTracker.onWindowLost = () => {
|
||||
for (const window of options.getOverlayWindows()) {
|
||||
window.hide();
|
||||
}
|
||||
options.syncOverlayShortcuts();
|
||||
};
|
||||
windowTracker.start();
|
||||
}
|
||||
|
||||
const config = options.getResolvedConfig();
|
||||
const subtitleTimingTracker = options.getSubtitleTimingTracker();
|
||||
const mpvClient = options.getMpvClient();
|
||||
const runtimeOptionsManager = options.getRuntimeOptionsManager();
|
||||
|
||||
if (
|
||||
config.ankiConnect &&
|
||||
subtitleTimingTracker &&
|
||||
mpvClient &&
|
||||
runtimeOptionsManager
|
||||
) {
|
||||
const effectiveAnkiConfig =
|
||||
runtimeOptionsManager.getEffectiveAnkiConnectConfig(config.ankiConnect);
|
||||
const integration = new AnkiIntegration(
|
||||
effectiveAnkiConfig,
|
||||
subtitleTimingTracker as never,
|
||||
mpvClient as never,
|
||||
(text: string) => {
|
||||
if (mpvClient && typeof mpvClient.send === "function") {
|
||||
mpvClient.send({
|
||||
command: ["show-text", text, "3000"],
|
||||
});
|
||||
}
|
||||
},
|
||||
options.showDesktopNotification,
|
||||
options.createFieldGroupingCallback(),
|
||||
);
|
||||
integration.start();
|
||||
options.setAnkiIntegration(integration);
|
||||
}
|
||||
|
||||
options.updateVisibleOverlayVisibility();
|
||||
options.updateInvisibleOverlayVisibility();
|
||||
|
||||
return { invisibleOverlayVisible };
|
||||
}
|
||||
105
src/core/services/overlay-shortcut-runtime-service.ts
Normal file
105
src/core/services/overlay-shortcut-runtime-service.ts
Normal file
@@ -0,0 +1,105 @@
|
||||
import {
|
||||
OverlayShortcutFallbackHandlers,
|
||||
} from "./overlay-shortcut-fallback-runner";
|
||||
import { OverlayShortcutHandlers } from "./overlay-shortcut-service";
|
||||
|
||||
export interface OverlayShortcutRuntimeDeps {
|
||||
showMpvOsd: (text: string) => void;
|
||||
openRuntimeOptions: () => void;
|
||||
openJimaku: () => void;
|
||||
markAudioCard: () => Promise<void>;
|
||||
copySubtitleMultiple: (timeoutMs: number) => void;
|
||||
copySubtitle: () => void;
|
||||
toggleSecondarySub: () => void;
|
||||
updateLastCardFromClipboard: () => Promise<void>;
|
||||
triggerFieldGrouping: () => Promise<void>;
|
||||
triggerSubsync: () => Promise<void>;
|
||||
mineSentence: () => Promise<void>;
|
||||
mineSentenceMultiple: (timeoutMs: number) => void;
|
||||
}
|
||||
|
||||
function wrapAsync(
|
||||
task: () => Promise<void>,
|
||||
deps: OverlayShortcutRuntimeDeps,
|
||||
logLabel: string,
|
||||
osdLabel: string,
|
||||
): () => void {
|
||||
return () => {
|
||||
task().catch((err) => {
|
||||
console.error(`${logLabel} failed:`, err);
|
||||
deps.showMpvOsd(`${osdLabel}: ${(err as Error).message}`);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
export function createOverlayShortcutRuntimeHandlers(
|
||||
deps: OverlayShortcutRuntimeDeps,
|
||||
): {
|
||||
overlayHandlers: OverlayShortcutHandlers;
|
||||
fallbackHandlers: OverlayShortcutFallbackHandlers;
|
||||
} {
|
||||
const overlayHandlers: OverlayShortcutHandlers = {
|
||||
copySubtitle: () => {
|
||||
deps.copySubtitle();
|
||||
},
|
||||
copySubtitleMultiple: (timeoutMs) => {
|
||||
deps.copySubtitleMultiple(timeoutMs);
|
||||
},
|
||||
updateLastCardFromClipboard: wrapAsync(
|
||||
() => deps.updateLastCardFromClipboard(),
|
||||
deps,
|
||||
"updateLastCardFromClipboard",
|
||||
"Update failed",
|
||||
),
|
||||
triggerFieldGrouping: wrapAsync(
|
||||
() => deps.triggerFieldGrouping(),
|
||||
deps,
|
||||
"triggerFieldGrouping",
|
||||
"Field grouping failed",
|
||||
),
|
||||
triggerSubsync: wrapAsync(
|
||||
() => deps.triggerSubsync(),
|
||||
deps,
|
||||
"triggerSubsyncFromConfig",
|
||||
"Subsync failed",
|
||||
),
|
||||
mineSentence: wrapAsync(
|
||||
() => deps.mineSentence(),
|
||||
deps,
|
||||
"mineSentenceCard",
|
||||
"Mine sentence failed",
|
||||
),
|
||||
mineSentenceMultiple: (timeoutMs) => {
|
||||
deps.mineSentenceMultiple(timeoutMs);
|
||||
},
|
||||
toggleSecondarySub: () => deps.toggleSecondarySub(),
|
||||
markAudioCard: wrapAsync(
|
||||
() => deps.markAudioCard(),
|
||||
deps,
|
||||
"markLastCardAsAudioCard",
|
||||
"Audio card failed",
|
||||
),
|
||||
openRuntimeOptions: () => {
|
||||
deps.openRuntimeOptions();
|
||||
},
|
||||
openJimaku: () => {
|
||||
deps.openJimaku();
|
||||
},
|
||||
};
|
||||
|
||||
const fallbackHandlers: OverlayShortcutFallbackHandlers = {
|
||||
openRuntimeOptions: overlayHandlers.openRuntimeOptions,
|
||||
openJimaku: overlayHandlers.openJimaku,
|
||||
markAudioCard: overlayHandlers.markAudioCard,
|
||||
copySubtitleMultiple: overlayHandlers.copySubtitleMultiple,
|
||||
copySubtitle: overlayHandlers.copySubtitle,
|
||||
toggleSecondarySub: overlayHandlers.toggleSecondarySub,
|
||||
updateLastCardFromClipboard: overlayHandlers.updateLastCardFromClipboard,
|
||||
triggerFieldGrouping: overlayHandlers.triggerFieldGrouping,
|
||||
triggerSubsync: overlayHandlers.triggerSubsync,
|
||||
mineSentence: overlayHandlers.mineSentence,
|
||||
mineSentenceMultiple: overlayHandlers.mineSentenceMultiple,
|
||||
};
|
||||
|
||||
return { overlayHandlers, fallbackHandlers };
|
||||
}
|
||||
46
src/core/services/overlay-visibility-runtime-service.ts
Normal file
46
src/core/services/overlay-visibility-runtime-service.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
export function syncInvisibleOverlayMousePassthroughService(options: {
|
||||
hasInvisibleWindow: () => boolean;
|
||||
setIgnoreMouseEvents: (ignore: boolean, extra?: { forward: boolean }) => void;
|
||||
visibleOverlayVisible: boolean;
|
||||
invisibleOverlayVisible: boolean;
|
||||
}): void {
|
||||
if (!options.hasInvisibleWindow()) return;
|
||||
if (options.visibleOverlayVisible) {
|
||||
options.setIgnoreMouseEvents(true, { forward: true });
|
||||
} else if (options.invisibleOverlayVisible) {
|
||||
options.setIgnoreMouseEvents(false);
|
||||
}
|
||||
}
|
||||
|
||||
export function setVisibleOverlayVisibleService(options: {
|
||||
visible: boolean;
|
||||
setVisibleOverlayVisibleState: (visible: boolean) => void;
|
||||
updateVisibleOverlayVisibility: () => void;
|
||||
updateInvisibleOverlayVisibility: () => void;
|
||||
syncInvisibleOverlayMousePassthrough: () => void;
|
||||
shouldBindVisibleOverlayToMpvSubVisibility: () => boolean;
|
||||
isMpvConnected: () => boolean;
|
||||
setMpvSubVisibility: (visible: boolean) => void;
|
||||
}): void {
|
||||
options.setVisibleOverlayVisibleState(options.visible);
|
||||
options.updateVisibleOverlayVisibility();
|
||||
options.updateInvisibleOverlayVisibility();
|
||||
options.syncInvisibleOverlayMousePassthrough();
|
||||
if (
|
||||
options.shouldBindVisibleOverlayToMpvSubVisibility() &&
|
||||
options.isMpvConnected()
|
||||
) {
|
||||
options.setMpvSubVisibility(!options.visible);
|
||||
}
|
||||
}
|
||||
|
||||
export function setInvisibleOverlayVisibleService(options: {
|
||||
visible: boolean;
|
||||
setInvisibleOverlayVisibleState: (visible: boolean) => void;
|
||||
updateInvisibleOverlayVisibility: () => void;
|
||||
syncInvisibleOverlayMousePassthrough: () => void;
|
||||
}): void {
|
||||
options.setInvisibleOverlayVisibleState(options.visible);
|
||||
options.updateInvisibleOverlayVisibility();
|
||||
options.syncInvisibleOverlayMousePassthrough();
|
||||
}
|
||||
146
src/core/services/overlay-window-service.ts
Normal file
146
src/core/services/overlay-window-service.ts
Normal file
@@ -0,0 +1,146 @@
|
||||
import { BrowserWindow } from "electron";
|
||||
import * as path from "path";
|
||||
import { WindowGeometry } from "../../types";
|
||||
|
||||
export type OverlayWindowKind = "visible" | "invisible";
|
||||
|
||||
export function updateOverlayBoundsService(
|
||||
geometry: WindowGeometry,
|
||||
getOverlayWindows: () => BrowserWindow[],
|
||||
): void {
|
||||
if (!geometry) return;
|
||||
for (const window of getOverlayWindows()) {
|
||||
window.setBounds({
|
||||
x: geometry.x,
|
||||
y: geometry.y,
|
||||
width: geometry.width,
|
||||
height: geometry.height,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export function ensureOverlayWindowLevelService(window: BrowserWindow): void {
|
||||
if (process.platform === "darwin") {
|
||||
window.setAlwaysOnTop(true, "screen-saver", 1);
|
||||
window.setVisibleOnAllWorkspaces(true, { visibleOnFullScreen: true });
|
||||
window.setFullScreenable(false);
|
||||
return;
|
||||
}
|
||||
window.setAlwaysOnTop(true);
|
||||
}
|
||||
|
||||
export function enforceOverlayLayerOrderService(options: {
|
||||
visibleOverlayVisible: boolean;
|
||||
invisibleOverlayVisible: boolean;
|
||||
mainWindow: BrowserWindow | null;
|
||||
invisibleWindow: BrowserWindow | null;
|
||||
ensureOverlayWindowLevel: (window: BrowserWindow) => void;
|
||||
}): void {
|
||||
if (!options.visibleOverlayVisible || !options.invisibleOverlayVisible) return;
|
||||
if (!options.mainWindow || options.mainWindow.isDestroyed()) return;
|
||||
if (!options.invisibleWindow || options.invisibleWindow.isDestroyed()) return;
|
||||
|
||||
options.ensureOverlayWindowLevel(options.mainWindow);
|
||||
options.mainWindow.moveTop();
|
||||
}
|
||||
|
||||
export function createOverlayWindowService(
|
||||
kind: OverlayWindowKind,
|
||||
options: {
|
||||
isDev: boolean;
|
||||
overlayDebugVisualizationEnabled: boolean;
|
||||
ensureOverlayWindowLevel: (window: BrowserWindow) => void;
|
||||
onRuntimeOptionsChanged: () => void;
|
||||
setOverlayDebugVisualizationEnabled: (enabled: boolean) => void;
|
||||
isOverlayVisible: (kind: OverlayWindowKind) => boolean;
|
||||
tryHandleOverlayShortcutLocalFallback: (input: Electron.Input) => boolean;
|
||||
onWindowClosed: (kind: OverlayWindowKind) => void;
|
||||
},
|
||||
): BrowserWindow {
|
||||
const window = new BrowserWindow({
|
||||
show: false,
|
||||
width: 800,
|
||||
height: 600,
|
||||
x: 0,
|
||||
y: 0,
|
||||
transparent: true,
|
||||
frame: false,
|
||||
alwaysOnTop: true,
|
||||
skipTaskbar: true,
|
||||
resizable: false,
|
||||
hasShadow: false,
|
||||
focusable: true,
|
||||
webPreferences: {
|
||||
preload: path.join(__dirname, "..", "..", "preload.js"),
|
||||
contextIsolation: true,
|
||||
nodeIntegration: false,
|
||||
webSecurity: true,
|
||||
additionalArguments: [`--overlay-layer=${kind}`],
|
||||
},
|
||||
});
|
||||
|
||||
options.ensureOverlayWindowLevel(window);
|
||||
|
||||
const htmlPath = path.join(__dirname, "..", "..", "renderer", "index.html");
|
||||
|
||||
window
|
||||
.loadFile(htmlPath, {
|
||||
query: { layer: kind === "visible" ? "visible" : "invisible" },
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error("Failed to load HTML file:", err);
|
||||
});
|
||||
|
||||
window.webContents.on(
|
||||
"did-fail-load",
|
||||
(_event, errorCode, errorDescription, validatedURL) => {
|
||||
console.error(
|
||||
"Page failed to load:",
|
||||
errorCode,
|
||||
errorDescription,
|
||||
validatedURL,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
window.webContents.on("did-finish-load", () => {
|
||||
options.onRuntimeOptionsChanged();
|
||||
window.webContents.send(
|
||||
"overlay-debug-visualization:set",
|
||||
options.overlayDebugVisualizationEnabled,
|
||||
);
|
||||
});
|
||||
|
||||
if (kind === "visible") {
|
||||
window.webContents.on("devtools-opened", () => {
|
||||
options.setOverlayDebugVisualizationEnabled(true);
|
||||
});
|
||||
window.webContents.on("devtools-closed", () => {
|
||||
options.setOverlayDebugVisualizationEnabled(false);
|
||||
});
|
||||
}
|
||||
|
||||
window.webContents.on("before-input-event", (event, input) => {
|
||||
if (!options.isOverlayVisible(kind)) return;
|
||||
if (!options.tryHandleOverlayShortcutLocalFallback(input)) return;
|
||||
event.preventDefault();
|
||||
});
|
||||
|
||||
window.hide();
|
||||
|
||||
window.on("closed", () => {
|
||||
options.onWindowClosed(kind);
|
||||
});
|
||||
|
||||
window.on("blur", () => {
|
||||
if (!window.isDestroyed()) {
|
||||
options.ensureOverlayWindowLevel(window);
|
||||
}
|
||||
});
|
||||
|
||||
if (options.isDev && kind === "visible") {
|
||||
window.webContents.openDevTools({ mode: "detach" });
|
||||
}
|
||||
|
||||
return window;
|
||||
}
|
||||
Reference in New Issue
Block a user