const { app, BrowserWindow, Menu, shell, dialog } = require('electron'); const { autoUpdater } = require('electron-updater'); const path = require('path'); // Keep a global reference of the window object let mainWindow; function createWindow() { // Create the browser window mainWindow = new BrowserWindow({ width: 1200, height: 800, minWidth: 800, minHeight: 600, icon: path.join(__dirname, 'img/logo.png'), webPreferences: { nodeIntegration: false, contextIsolation: true, enableRemoteModule: false, webSecurity: true }, show: false // Don't show until ready }); // Load the home page mainWindow.loadFile(path.join(__dirname, 'home.html')); // Show window when ready to prevent visual flash mainWindow.once('ready-to-show', () => { mainWindow.show(); }); // Handle external links mainWindow.webContents.setWindowOpenHandler(({ url }) => { shell.openExternal(url); return { action: 'deny' }; }); // Emitted when the window is closed mainWindow.on('closed', () => { mainWindow = null; }); // Create application menu createMenu(); } function createMenu() { const template = [ { label: 'File', submenu: [ { label: 'New Note', accelerator: 'CmdOrCtrl+N', click: () => { mainWindow.loadFile(path.join(__dirname, 'index.html')); } }, { label: 'Home', accelerator: 'CmdOrCtrl+H', click: () => { mainWindow.loadFile(path.join(__dirname, 'home.html')); } }, { type: 'separator' }, { label: 'Check for Updates', click: () => { autoUpdater.checkForUpdatesAndNotify(); } }, { type: 'separator' }, { label: 'Quit', accelerator: process.platform === 'darwin' ? 'Cmd+Q' : 'Ctrl+Q', click: () => { app.quit(); } } ] }, { label: 'Edit', submenu: [ { role: 'undo' }, { role: 'redo' }, { type: 'separator' }, { role: 'cut' }, { role: 'copy' }, { role: 'paste' }, { role: 'selectall' } ] }, { label: 'View', submenu: [ { role: 'reload' }, { role: 'forceReload' }, { role: 'toggleDevTools' }, { type: 'separator' }, { role: 'resetZoom' }, { role: 'zoomIn' }, { role: 'zoomOut' }, { type: 'separator' }, { role: 'togglefullscreen' } ] }, { label: 'Window', submenu: [ { role: 'minimize' }, { role: 'close' } ] } ]; // macOS specific menu adjustments if (process.platform === 'darwin') { template.unshift({ label: app.getName(), submenu: [ { role: 'about' }, { type: 'separator' }, { role: 'services' }, { type: 'separator' }, { role: 'hide' }, { role: 'hideothers' }, { role: 'unhide' }, { type: 'separator' }, { role: 'quit' } ] }); // Window menu template[4].submenu = [ { role: 'close' }, { role: 'minimize' }, { role: 'zoom' }, { type: 'separator' }, { role: 'front' } ]; } const menu = Menu.buildFromTemplate(template); Menu.setApplicationMenu(menu); } // Auto-updater configuration function setupAutoUpdater() { // Configure auto-updater autoUpdater.checkForUpdatesAndNotify(); // Auto-updater events autoUpdater.on('checking-for-update', () => { console.log('Checking for update...'); }); autoUpdater.on('update-available', (info) => { console.log('Update available:', info); dialog.showMessageBox(mainWindow, { type: 'info', title: 'Update Available', message: 'A new version is available. It will be downloaded in the background.', detail: `Version ${info.version} is now available. The update will be downloaded and installed automatically.`, buttons: ['OK'] }); }); autoUpdater.on('update-not-available', (info) => { console.log('Update not available:', info); }); autoUpdater.on('error', (err) => { console.error('Error in auto-updater:', err); }); autoUpdater.on('download-progress', (progressObj) => { let log_message = "Download speed: " + progressObj.bytesPerSecond; log_message = log_message + ' - Downloaded ' + progressObj.percent + '%'; log_message = log_message + ' (' + progressObj.transferred + "/" + progressObj.total + ')'; console.log(log_message); }); autoUpdater.on('update-downloaded', (info) => { console.log('Update downloaded:', info); dialog.showMessageBox(mainWindow, { type: 'info', title: 'Update Ready', message: 'Update downloaded and ready to install', detail: 'The application will restart to apply the update.', buttons: ['Restart Now', 'Later'] }).then((result) => { if (result.response === 0) { autoUpdater.quitAndInstall(); } }); }); } // This method will be called when Electron has finished initialization app.whenReady().then(() => { createWindow(); // Set up auto-updater after window is created setTimeout(() => { setupAutoUpdater(); }, 3000); // Wait 3 seconds after app start app.on('activate', () => { // On macOS, re-create a window when the dock icon is clicked if (BrowserWindow.getAllWindows().length === 0) { createWindow(); } }); }); // Quit when all windows are closed app.on('window-all-closed', () => { // On macOS, keep the app running even when all windows are closed if (process.platform !== 'darwin') { app.quit(); } }); // Security: Prevent new window creation app.on('web-contents-created', (event, contents) => { contents.on('new-window', (event, url) => { event.preventDefault(); shell.openExternal(url); }); });