import { BrowserWindow, shell } from 'electron'; import path from 'node:path'; import { getRedisStatus, setupRedisPubSub, testRedisConnection } from './redis'; /** * Window configuration - centralized values */ const WINDOW_CONFIG = { minHeight: 800, minWidth: 1080, maxHeight: 1080, maxWidth: 1920, height: 1024, width: 1280, titleBarStyle: 'hidden' as const, webPreferences: { nodeIntegration: false, contextIsolation: true, preload: path.join(__dirname, 'preload.cjs'), }, } as const; /** * Create and configure the main application window */ export function createMainWindow(): BrowserWindow { const mainWindow = new BrowserWindow(WINDOW_CONFIG); // Hide menu bar mainWindow.setMenuBarVisibility(false); // Set up window event listeners setupWindowEventListeners(mainWindow); // Set up external link handling setupExternalLinkHandling(mainWindow); // Load the appropriate content loadWindowContent(mainWindow); // Set up Redis pub/sub when window is ready mainWindow.webContents.once('dom-ready', async () => { console.info('[WINDOW] DOM ready, setting up Redis pub/sub...'); // Check Redis status const status = getRedisStatus(); console.info('[WINDOW] Redis status:', status); // Test connection if needed if (status !== 'connected') { console.info('[WINDOW] Testing Redis connection...'); const connected = await testRedisConnection(); console.info('[WINDOW] Redis connection test result:', connected); } setupRedisPubSub(mainWindow); console.info('[WINDOW] Redis pub/sub setup completed'); }); return mainWindow; } /** * Set up window event listeners for maximize/unmaximize */ function setupWindowEventListeners(mainWindow: BrowserWindow): void { mainWindow.on('maximize', () => { mainWindow.webContents.send('window-maximize-changed', true); }); mainWindow.on('unmaximize', () => { mainWindow.webContents.send('window-maximize-changed', false); }); } /** * Set up external link handling to open in default browser */ function setupExternalLinkHandling(mainWindow: BrowserWindow): void { mainWindow.webContents.on('will-navigate', (event, reqUrl) => { const requestedHost = new URL(reqUrl).host; const currentHost = new URL(mainWindow.webContents.getURL()).host; if (requestedHost && requestedHost !== currentHost) { event.preventDefault(); shell.openExternal(reqUrl); } }); } /** * Load window content based on environment */ function loadWindowContent(mainWindow: BrowserWindow): void { const isDev = process.env.NODE_ENV === 'development'; if (isDev) { mainWindow.setIcon(path.resolve(__dirname, '../../public/favicon.ico')); // Try different ports to find the Nuxt dev server const possiblePorts = [3000, 3001, 3002]; tryLoadDevServer(mainWindow, possiblePorts); mainWindow.webContents.openDevTools(); } else { mainWindow.loadFile(path.join(__dirname, '../renderer/index.html')); } } /** * Try to load the dev server from different ports */ function tryLoadDevServer(mainWindow: BrowserWindow, ports: number[], index = 0): void { if (index >= ports.length) { console.error('Could not find Nuxt dev server on any port'); return; } const port = ports[index]; const url = `http://localhost:${port}`; mainWindow.loadURL(url).catch(() => { // If this port fails, try the next one setTimeout(() => tryLoadDevServer(mainWindow, ports, index + 1), 100); }); }