From 82f87613975958b61631532c8a6380f115f4cb0f Mon Sep 17 00:00:00 2001 From: Jack Schedel Date: Wed, 10 May 2023 11:54:22 -0400 Subject: [PATCH] Fixed 2 Electron Crashes (#288) Fixes #277 * fixed crash on attempting to open new instance, required refactoring BrowserWindow to global Uncaught Exception: ReferenceError: win is not defined at click (C:\Users\jacks\AppData\Local\Programs\better-chatgpt\resources\app.asar\electron\index.cjs:53:9) at MenuItem.click (node:electron/js2c/browser_init:2:30166) at a._executeCommand (node:electron/js2c/browser_init:2:35562) * fixed crash on attempting to open new instance, required refactoring BrowserWindow to global Uncaught Exception: ReferenceError: win is not defined at click (C:\Users\jacks\AppData\Local\Programs\better-chatgpt\resources\app.asar\electron\index.cjs:53:9) at MenuItem.click (node:electron/js2c/browser_init:2:30166) at a._executeCommand (node:electron/js2c/browser_init:2:35562) * fixed Electron process not closing on crash previously left the process still existing on crash (only viewable in task manager). error behavior is the same as it was before, just now must be explicitly defined. --- electron/index.cjs | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/electron/index.cjs b/electron/index.cjs index 9374fb8..c1e6949 100644 --- a/electron/index.cjs +++ b/electron/index.cjs @@ -1,8 +1,10 @@ const path = require('path'); -const { app, BrowserWindow, Tray, Menu } = require('electron'); +const {dialog, app, BrowserWindow, Tray, Menu } = require('electron'); const isDev = require('electron-is-dev'); const { autoUpdater } = require('electron-updater'); +let win = null; +const instanceLock = app.requestSingleInstanceLock(); if (require('electron-squirrel-startup')) app.quit(); @@ -17,7 +19,7 @@ function createWindow() { } autoUpdater.checkForUpdatesAndNotify(); - const win = new BrowserWindow({ + win = new BrowserWindow({ autoHideMenuBar: true, show: false, icon: iconPath, @@ -73,20 +75,35 @@ const createTray = (window) => { return tray; }; -app.whenReady().then(createWindow); - app.on('window-all-closed', () => { if (process.platform !== 'darwin') { app.quit(); } }); -app.on('activate', () => { - if (BrowserWindow.getAllWindows().length === 0) { - createWindow(); - } +process.on('uncaughtException', (error) => { + // Perform any necessary cleanup tasks here + dialog.showErrorBox('An error occurred', error.stack); + + // Exit the app + process.exit(1); }); +if (!instanceLock) { + app.quit() +} else { + app.on('second-instance', (event, commandLine, workingDirectory) => { + if (win) { + if (win.isMinimized()) win.restore() + win.focus() + } + }) + + app.whenReady().then(() => { + win = createWindow() + }) +} + const createServer = () => { // Dependencies const http = require('http');