|
| 1 | +const { ipcMain, BrowserWindow } = require('electron'); |
| 2 | +const path = require('path'); |
| 3 | + |
| 4 | +const PHOENIX_WINDOW_PREFIX = 'phcode-'; |
| 5 | +const PHOENIX_EXTENSION_WINDOW_PREFIX = 'extn-'; |
| 6 | +const MAX_WINDOWS = 30; |
| 7 | + |
| 8 | +// Window registry: windowLabel -> BrowserWindow |
| 9 | +const windowRegistry = new Map(); |
| 10 | +// Reverse lookup: webContentsId -> windowLabel |
| 11 | +const webContentsToLabel = new Map(); |
| 12 | + |
| 13 | +function getNextLabel(prefix) { |
| 14 | + for (let i = 1; i <= MAX_WINDOWS; i++) { |
| 15 | + const label = `${prefix}${i}`; |
| 16 | + if (!windowRegistry.has(label)) { |
| 17 | + return label; |
| 18 | + } |
| 19 | + } |
| 20 | + throw new Error(`No free window label available for prefix: ${prefix}`); |
| 21 | +} |
| 22 | + |
| 23 | +function registerWindow(win, label) { |
| 24 | + windowRegistry.set(label, win); |
| 25 | + webContentsToLabel.set(win.webContents.id, label); |
| 26 | + |
| 27 | + win.on('closed', () => { |
| 28 | + windowRegistry.delete(label); |
| 29 | + webContentsToLabel.delete(win.webContents.id); |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | +function registerWindowIpcHandlers() { |
| 34 | + // Get all window labels (mirrors Tauri's _get_window_labels) |
| 35 | + ipcMain.handle('get-window-labels', () => { |
| 36 | + return Array.from(windowRegistry.keys()); |
| 37 | + }); |
| 38 | + |
| 39 | + // Get current window's label |
| 40 | + ipcMain.handle('get-current-window-label', (event) => { |
| 41 | + return webContentsToLabel.get(event.sender.id) || null; |
| 42 | + }); |
| 43 | + |
| 44 | + // Create new window (mirrors openURLInPhoenixWindow for Electron) |
| 45 | + ipcMain.handle('create-phoenix-window', async (event, url, options) => { |
| 46 | + const { windowTitle, fullscreen, resizable, height, minHeight, width, minWidth, isExtension } = options || {}; |
| 47 | + |
| 48 | + const prefix = isExtension ? PHOENIX_EXTENSION_WINDOW_PREFIX : PHOENIX_WINDOW_PREFIX; |
| 49 | + const label = getNextLabel(prefix); |
| 50 | + |
| 51 | + const win = new BrowserWindow({ |
| 52 | + width: width || 1366, |
| 53 | + height: height || 900, |
| 54 | + minWidth: minWidth || 800, |
| 55 | + minHeight: minHeight || 600, |
| 56 | + fullscreen: fullscreen || false, |
| 57 | + resizable: resizable !== false, |
| 58 | + title: windowTitle || label, |
| 59 | + webPreferences: { |
| 60 | + preload: path.join(__dirname, 'preload.js'), |
| 61 | + contextIsolation: true, |
| 62 | + nodeIntegration: false |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + registerWindow(win, label); |
| 67 | + await win.loadURL(url); |
| 68 | + |
| 69 | + return label; |
| 70 | + }); |
| 71 | + |
| 72 | + // Close current window |
| 73 | + ipcMain.handle('close-window', async (event) => { |
| 74 | + const win = BrowserWindow.fromWebContents(event.sender); |
| 75 | + if (win) { |
| 76 | + win.close(); |
| 77 | + } |
| 78 | + }); |
| 79 | + |
| 80 | + // Quit app (for last window scenario) |
| 81 | + ipcMain.handle('quit-app', (event, exitCode) => { |
| 82 | + const { app } = require('electron'); |
| 83 | + app.exit(exitCode || 0); |
| 84 | + }); |
| 85 | + |
| 86 | + // Focus current window |
| 87 | + ipcMain.handle('focus-window', (event) => { |
| 88 | + const win = BrowserWindow.fromWebContents(event.sender); |
| 89 | + if (win) { |
| 90 | + win.setAlwaysOnTop(true); |
| 91 | + win.focus(); |
| 92 | + win.setAlwaysOnTop(false); |
| 93 | + } |
| 94 | + }); |
| 95 | +} |
| 96 | + |
| 97 | +module.exports = { registerWindowIpcHandlers, registerWindow, windowRegistry }; |
0 commit comments