import { BrowserWindow, ipcMain, screen } from "electron";
import fs from "node:fs";
import path from "node:path";
import { app } from "electron";
let overlayWindow: BrowserWindow | null = null;
function writeOverlayHtml(): string {
const dir = path.join(app.getPath("temp"), "rpa-studio-overlay");
fs.mkdirSync(dir, { recursive: true });
const file = path.join(dir, "overlay.html");
const html = `
0/0
`;
fs.writeFileSync(file, html, "utf8");
return file;
}
export function showRunOverlay(mainWindow: BrowserWindow | null): void {
if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.minimize();
mainWindow.setProgressBar(0);
}
if (overlayWindow && !overlayWindow.isDestroyed()) {
overlayWindow.show();
return;
}
const w = 320;
const h = 56;
const workArea = screen.getPrimaryDisplay().workArea;
const x = workArea.x + workArea.width - w - 16;
const y = workArea.y + workArea.height - h - 16;
overlayWindow = new BrowserWindow({
width: w,
height: h,
x,
y,
frame: false,
resizable: false,
movable: false,
minimizable: false,
maximizable: false,
closable: false,
skipTaskbar: true,
alwaysOnTop: true,
fullscreenable: false,
hasShadow: true,
backgroundColor: "#1E2530",
webPreferences: {
contextIsolation: false,
nodeIntegration: true,
sandbox: false
}
});
overlayWindow.setAlwaysOnTop(true, "screen-saver");
overlayWindow.show();
const file = writeOverlayHtml();
overlayWindow.loadFile(file);
}
export function updateOverlayProgress(ratio: number): void {
if (overlayWindow && !overlayWindow.isDestroyed()) {
overlayWindow.setProgressBar(ratio >= 1 ? -1 : Math.max(0, Math.min(1, ratio)));
}
}
export function hideRunOverlay(mainWindow: BrowserWindow | null): void {
if (overlayWindow && !overlayWindow.isDestroyed()) {
overlayWindow.destroy();
overlayWindow = null;
}
if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.setProgressBar(-1);
mainWindow.restore();
}
}
export function registerOverlayIpc(
getMainWindow: () => BrowserWindow | null,
onStop: () => void
): void {
ipcMain.on("overlay:close", () => {
hideRunOverlay(getMainWindow());
});
ipcMain.on("overlay:stop", () => {
onStop();
});
}
export function overlayBroadcast(event: unknown): void {
if (overlayWindow && !overlayWindow.isDestroyed()) {
overlayWindow.webContents.send("run:event", event);
}
}