From 10522fbd86318d037e860b2617801a3c767ac800 Mon Sep 17 00:00:00 2001 From: covidisahoax Date: Tue, 3 Dec 2024 07:14:39 -0800 Subject: [PATCH 1/5] fix 'Object has been destroyed' error on macOS when attempting to open URL with no windows open --- src/main/index.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/index.js b/src/main/index.js index eb3376c0776d2..d263e9779c19b 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -1442,6 +1442,7 @@ function runApp() { app.on('window-all-closed', () => { // Clean up resources (datastores' compaction + Electron cache and storage data clearing) cleanUpResources().finally(() => { + mainWindow = 'all-windows-closed' if (process.platform !== 'darwin') { app.quit() } @@ -1507,7 +1508,14 @@ function runApp() { app.on('open-url', (event, url) => { event.preventDefault() - if (mainWindow && mainWindow.webContents) { + if (mainWindow === 'all-windows-closed') { + app.once('browser-window-created', (_, mainWindow) => { + mainWindow.webContents.once('did-finish-load', () => { + setTimeout(function() { mainWindow.webContents.send(IpcChannels.OPEN_URL, baseUrl(url), { isLaunchLink: true }) }, 1000) + }) + }) + createWindow() + } else if (mainWindow && mainWindow.webContents) { mainWindow.webContents.send(IpcChannels.OPEN_URL, baseUrl(url)) } else { startupUrl = baseUrl(url) From 6014dc00e83f5827b172cbac19cfda07a2797c81 Mon Sep 17 00:00:00 2001 From: covidisahoax Date: Wed, 4 Dec 2024 11:05:23 -0800 Subject: [PATCH 2/5] add comment --- src/main/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/index.js b/src/main/index.js index d263e9779c19b..3e39a394a9235 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -1511,7 +1511,9 @@ function runApp() { if (mainWindow === 'all-windows-closed') { app.once('browser-window-created', (_, mainWindow) => { mainWindow.webContents.once('did-finish-load', () => { - setTimeout(function() { mainWindow.webContents.send(IpcChannels.OPEN_URL, baseUrl(url), { isLaunchLink: true }) }, 1000) + // A timeout here is necessary or the new window won't receive the URL + setTimeout(function() { mainWindow.webContents.send( + IpcChannels.OPEN_URL, baseUrl(url), { isLaunchLink: true }) }, 1000) }) }) createWindow() From 86323669393a706fb40e361c76e1b3c0f0cb0344 Mon Sep 17 00:00:00 2001 From: covidisahoax Date: Wed, 4 Dec 2024 13:42:09 -0800 Subject: [PATCH 3/5] handle URLs passed via command line with no open windows on macOS --- src/main/index.js | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/main/index.js b/src/main/index.js index 3e39a394a9235..d8c07e2ad592d 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -254,13 +254,18 @@ function runApp() { app.on('second-instance', (_, commandLine, __) => { // Someone tried to run a second instance, we should focus our window - if (mainWindow && typeof commandLine !== 'undefined') { - if (mainWindow.isMinimized()) mainWindow.restore() - mainWindow.focus() - + if (typeof commandLine !== 'undefined') { const url = getLinkUrl(commandLine) - if (url) { - mainWindow.webContents.send(IpcChannels.OPEN_URL, url) + if (mainWindow === 'all-windows-closed') { + if (url) macOSnewURLWindow(url) + createWindow() + } else if (mainWindow && mainWindow.webContents) { + if (mainWindow.isMinimized()) mainWindow.restore() + mainWindow.focus() + + if (url) { + mainWindow.webContents.send(IpcChannels.OPEN_URL, url) + } } } }) @@ -1509,13 +1514,7 @@ function runApp() { event.preventDefault() if (mainWindow === 'all-windows-closed') { - app.once('browser-window-created', (_, mainWindow) => { - mainWindow.webContents.once('did-finish-load', () => { - // A timeout here is necessary or the new window won't receive the URL - setTimeout(function() { mainWindow.webContents.send( - IpcChannels.OPEN_URL, baseUrl(url), { isLaunchLink: true }) }, 1000) - }) - }) + macOSnewURLWindow(baseUrl(url)) createWindow() } else if (mainWindow && mainWindow.webContents) { mainWindow.webContents.send(IpcChannels.OPEN_URL, baseUrl(url)) @@ -1560,6 +1559,15 @@ function runApp() { } } + function macOSnewURLWindow(url) { + app.once('browser-window-created', (_, mainWindow) => { + mainWindow.webContents.once('did-finish-load', () => { + // A timeout here is necessary or the new window won't receive the URL + setTimeout(function() { mainWindow.webContents.send(IpcChannels.OPEN_URL, url, { isLaunchLink: true }) }, 1000) + }) + }) + } + /* * Auto Updater * From 6910e51a8008f0d1b97e8504697306ee65dc95b9 Mon Sep 17 00:00:00 2001 From: covidisahoax Date: Wed, 4 Dec 2024 15:34:25 -0800 Subject: [PATCH 4/5] refactor recent changes to URL handling --- src/main/index.js | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/src/main/index.js b/src/main/index.js index d8c07e2ad592d..17426f20e8eb8 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -256,15 +256,15 @@ function runApp() { // Someone tried to run a second instance, we should focus our window if (typeof commandLine !== 'undefined') { const url = getLinkUrl(commandLine) - if (mainWindow === 'all-windows-closed') { - if (url) macOSnewURLWindow(url) - createWindow() - } else if (mainWindow && mainWindow.webContents) { - if (mainWindow.isMinimized()) mainWindow.restore() - mainWindow.focus() - - if (url) { + if (url) { + if (mainWindow && mainWindow.webContents) { + if (mainWindow.isMinimized()) mainWindow.restore() + mainWindow.focus() + mainWindow.webContents.send(IpcChannels.OPEN_URL, url) + } else { + startupUrl = url + createWindow() } } } @@ -834,10 +834,11 @@ function runApp() { }) } - ipcMain.once(IpcChannels.APP_READY, () => { + ipcMain.on(IpcChannels.APP_READY, () => { if (startupUrl) { mainWindow.webContents.send(IpcChannels.OPEN_URL, startupUrl, { isLaunchLink: true }) } + startupUrl = null }) function relaunch() { @@ -1447,7 +1448,7 @@ function runApp() { app.on('window-all-closed', () => { // Clean up resources (datastores' compaction + Electron cache and storage data clearing) cleanUpResources().finally(() => { - mainWindow = 'all-windows-closed' + mainWindow = null if (process.platform !== 'darwin') { app.quit() } @@ -1513,13 +1514,11 @@ function runApp() { app.on('open-url', (event, url) => { event.preventDefault() - if (mainWindow === 'all-windows-closed') { - macOSnewURLWindow(baseUrl(url)) - createWindow() - } else if (mainWindow && mainWindow.webContents) { + if (mainWindow && mainWindow.webContents) { mainWindow.webContents.send(IpcChannels.OPEN_URL, baseUrl(url)) } else { startupUrl = baseUrl(url) + if (app.isReady()) createWindow() } }) @@ -1559,15 +1558,6 @@ function runApp() { } } - function macOSnewURLWindow(url) { - app.once('browser-window-created', (_, mainWindow) => { - mainWindow.webContents.once('did-finish-load', () => { - // A timeout here is necessary or the new window won't receive the URL - setTimeout(function() { mainWindow.webContents.send(IpcChannels.OPEN_URL, url, { isLaunchLink: true }) }, 1000) - }) - }) - } - /* * Auto Updater * From aff781a4595a3ef43e23a9d3ec40185149af0e10 Mon Sep 17 00:00:00 2001 From: covidisahoax Date: Wed, 4 Dec 2024 16:55:28 -0800 Subject: [PATCH 5/5] fix regression (trying to launch a second instance wouldn't have focused main window) --- src/main/index.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/main/index.js b/src/main/index.js index 17426f20e8eb8..4bd741a4accb9 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -256,16 +256,14 @@ function runApp() { // Someone tried to run a second instance, we should focus our window if (typeof commandLine !== 'undefined') { const url = getLinkUrl(commandLine) - if (url) { - if (mainWindow && mainWindow.webContents) { - if (mainWindow.isMinimized()) mainWindow.restore() - mainWindow.focus() - - mainWindow.webContents.send(IpcChannels.OPEN_URL, url) - } else { - startupUrl = url - createWindow() - } + if (mainWindow && mainWindow.webContents) { + if (mainWindow.isMinimized()) mainWindow.restore() + mainWindow.focus() + + if (url) mainWindow.webContents.send(IpcChannels.OPEN_URL, url) + } else { + if (url) startupUrl = url + createWindow() } } })