const { app, BrowserWindow, session, desktopCapturer } = require('electron'); const path = require('path'); const url = require('url'); function createWindow() { // Create a persistent session for chattz to keep the user logged in const sess = session.fromPartition('persist:chattz'); const win = new BrowserWindow({ width: 1200, height: 800, title: "Chattz Desktop", webPreferences: { nodeIntegration: false, contextIsolation: true, webSecurity: false, // Required for cross-origin fetch/ws from file:// with cookies session: sess } }); // Auto-approve media permissions (camera, microphone) sess.setPermissionCheckHandler((webContents, permission) => { if (permission === 'media') { return true; } return false; }); sess.setPermissionRequestHandler((webContents, permission, callback) => { if (permission === 'media') { callback(true); } else { callback(false); } }); // Handle screen share requests sess.setDisplayMediaRequestHandler((request, callback) => { desktopCapturer.getSources({ types: ['screen', 'window'] }).then((sources) => { // Provide the first screen source by default, or implement a picker window here if (sources && sources.length > 0) { // We prefer a screen over a window if available, simple heuristic const screenSource = sources.find(s => s.id.startsWith('screen')) || sources[0]; callback({ video: screenSource, audio: 'loopback' }); } else { callback(null); // Reject safely } }).catch(err => { console.error("Failed to get desktop sources for screen share", err); callback(null); }); }); const backendUrl = (process.env.CHATTZ_URL || 'https://discord.flegr.me').replace(/\/$/, ''); const indexPath = path.join(__dirname, 'desktop', 'index.html'); // Construct the local file URL with the backend parameter const localUrl = url.format({ pathname: indexPath, protocol: 'file:', slashes: true, query: { backend: backendUrl } }); console.log(`Loading desktop app from: ${localUrl}`); const loadDesktopApp = () => { win.loadURL(localUrl).catch((err) => { console.error(`Failed to load desktop file: ${err}`); }); }; // Use a navigation listener to detect when the remote login is complete win.webContents.on('will-navigate', (event, navigatedUrl) => { const normalizedNav = navigatedUrl.replace(/\/$/, ''); // If the user lands back on the remote root, they are logged in. // Redirect them back to our local bugfixed desktop UI. if (normalizedNav === backendUrl) { console.log("Detected remote landing (login success), returning to desktop UI..."); event.preventDefault(); loadDesktopApp(); } }); win.webContents.on('did-navigate', (event, navigatedUrl) => { const normalizedNav = navigatedUrl.replace(/\/$/, ''); if (normalizedNav === backendUrl) { loadDesktopApp(); } }); loadDesktopApp(); // Uncomment to debug // win.webContents.openDevTools(); } app.commandLine.appendSwitch('disable-webrtc-hw-encoding'); // Sometime helps resolve codec mismatch behavior app.commandLine.appendSwitch('log-level', '3'); // Silences standard Chromium warning logs (like SRTP transport unprotect logs which are benign timing issues) app.whenReady().then(() => { createWindow(); app.on('activate', () => { if (BrowserWindow.getAllWindows().length === 0) { createWindow(); } }); }); app.on('window-all-closed', () => { if (process.platform !== 'darwin') { app.quit(); } });