import { app, BrowserWindow, clipboard, ipcMain, shell } from 'electron'; import appUpdater from './Electron/updater'; import * as Sentry from '@sentry/electron'; import path from 'path'; // eslint-disable-next-line @typescript-eslint/no-explicit-any declare const MAIN_WINDOW_WEBPACK_ENTRY: any; // eslint-disable-next-line @typescript-eslint/no-explicit-any declare const MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY: any; // Handle creating/removing shortcuts on Windows when installing/uninstalling. if (require('electron-squirrel-startup')) { // eslint-disable-line global-require app.quit(); } const isDevelopment = process.env.NODE_ENV === 'development'; if (!isDevelopment) { Sentry.init({ dsn: 'https://bd2949c6091749049a6eb7c9ce2698c2@o49471.ingest.sentry.io/5740243' }); } const gotTheLock = app.requestSingleInstanceLock(); let mainWindow: BrowserWindow; const createWindow = (): void => { // Create the browser window. mainWindow = new BrowserWindow({ show: false, frame: isDevelopment, title: 'SolarCast', webPreferences: { nodeIntegration: false, contextIsolation: true, webviewTag: true, sandbox: true, preload: MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY, } }); // Load the index.html of the app. mainWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY); if (isDevelopment) { // Open the DevTools. mainWindow.maximize(); mainWindow.webContents.openDevTools(); } else { mainWindow.setFullScreen(true); mainWindow.setAlwaysOnTop(true); mainWindow.webContents.once('did-frame-finish-load', () => { if (process.platform === 'darwin' || process.platform === 'win32') { // Initate auto-updates on macOs and windows appUpdater(); } }); } // Events / messages from the renderer ipcMain.on('close-app', () => { app.quit(); }); ipcMain.on('copy', (_event, text: string) => { clipboard.writeText(text); }); ipcMain.on('open', (_event, url: string) => { shell.openExternal(url); }); ipcMain.on('get-version', (event) => { event.reply('set-version', app.getVersion()); }); // Only show if everything is loaded mainWindow.show(); }; if (gotTheLock) { app.on('second-instance', () => { // Someone tried to run a second instance, we should focus our window. if (mainWindow) { if (mainWindow.isMinimized()) { mainWindow.restore(); } mainWindow.focus(); } }); // This method will be called when Electron has finished // initialization and is ready to create browser windows. // Some APIs can only be used after this event occurs. app.on('ready', createWindow); // Quit when all windows are closed, except on macOS. There, it's common // for applications and their menu bar to stay active until the user quits // explicitly with Cmd + Q. app.on('window-all-closed', () => { if (process.platform !== 'darwin') { app.quit(); } }); app.on('activate', () => { // On OS X it's common to re-create a window in the app when the // dock icon is clicked and there are no other windows open. if (BrowserWindow.getAllWindows().length === 0) { createWindow(); } }); } else { app.quit(); } // In this file you can include the rest of your app's specific main process // code. You can also put them in separate files and import them here. if (!isDevelopment) { const appFolder = path.dirname(process.execPath); const updateExe = path.resolve(appFolder, '..', 'Update.exe'); const exeName = path.basename(process.execPath); // Start the application on Windows start. app.setLoginItemSettings({ openAtLogin: true, path: updateExe, args: [ '--processStart', `"${exeName}"`, '--process-start-args', '"--hidden"' ] }); }