From c04463306e7466ff4977e39ab54a79c98f29e90f Mon Sep 17 00:00:00 2001 From: Sergey Zhuravlev Date: Fri, 8 Nov 2024 17:59:59 +0300 Subject: [PATCH] fix: now electron app handle macos app reopening correctly (perhaps) (#2606) --- src/main/factories/protocol.ts | 4 +-- src/main/factories/setup.ts | 30 ------------------- src/main/index.ts | 55 +++++++++++++++++++++++----------- 3 files changed, 38 insertions(+), 51 deletions(-) delete mode 100644 src/main/factories/setup.ts diff --git a/src/main/factories/protocol.ts b/src/main/factories/protocol.ts index 63385c725b..305aba965c 100644 --- a/src/main/factories/protocol.ts +++ b/src/main/factories/protocol.ts @@ -12,9 +12,7 @@ export function registerDeepLinkProtocol() { } } -export function processUrl(url: string, mainWindow?: BrowserWindow) { - if (!mainWindow) return; - +export function processUrl(url: string, mainWindow: BrowserWindow) { const parsed = new URL(url); if (parsed.protocol !== `${APP_CONFIG.ELECTRON_PROTOCOL}:`) return; diff --git a/src/main/factories/setup.ts b/src/main/factories/setup.ts deleted file mode 100644 index f56fa37a48..0000000000 --- a/src/main/factories/setup.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { BrowserWindow, app, session } from 'electron'; -import installExtension, { REACT_DEVELOPER_TOOLS } from 'electron-devtools-installer'; - -import { ENVIRONMENT } from '../shared/constants/environment'; -import { PLATFORM } from '../shared/constants/platform'; - -export async function setupApplication(window: BrowserWindow): Promise { - app.on('activate', async () => { - if (!BrowserWindow.getAllWindows().length) return window; - - for (const w of BrowserWindow.getAllWindows().reverse()) { - w.restore(); - } - }); - - app.on('web-contents-created', (_, contents) => - contents.on('will-navigate', (event) => !ENVIRONMENT.IS_DEV && event.preventDefault()), - ); - - app.on('window-all-closed', () => !PLATFORM.IS_MAC && app.quit()); - - if (ENVIRONMENT.IS_DEV) { - await installExtension(REACT_DEVELOPER_TOOLS); - - // Reloading extensions for correct initialization in dev tools - session.defaultSession.getAllExtensions().map((e) => { - session.defaultSession.loadExtension(e.path); - }); - } -} diff --git a/src/main/index.ts b/src/main/index.ts index 6c4f142e73..e4836130e3 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -1,13 +1,11 @@ import 'source-map-support/register'; -import { type BrowserWindow, app } from 'electron'; - -import { APP_CONFIG } from '../../app.config'; +import { type BrowserWindow, app, session } from 'electron'; +import installExtension, { REACT_DEVELOPER_TOOLS } from 'electron-devtools-installer'; import { runAppSingleInstance } from './factories/instance'; import { setupLogger } from './factories/logs'; import { processUrl, registerDeepLinkProtocol } from './factories/protocol'; -import { setupApplication } from './factories/setup'; import { setupAutoUpdater } from './factories/updater'; import { createWindow } from './factories/window'; import { ENVIRONMENT } from './shared/constants/environment'; @@ -18,12 +16,12 @@ runAppSingleInstance(async () => { setupAutoUpdater(); registerDeepLinkProtocol(); + app.commandLine.appendSwitch('force-color-profile', 'srgb'); + app.commandLine.appendSwitch('autoplay-policy', 'no-user-gesture-required'); if (ENVIRONMENT.IS_DEV || ENVIRONMENT.IS_STAGE) { app.commandLine.appendSwitch('ignore-certificate-errors'); } - app.commandLine.appendSwitch('force-color-profile', 'srgb'); - app.commandLine.appendSwitch('autoplay-policy', 'no-user-gesture-required'); process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = 'true'; delete process.env.ELECTRON_ENABLE_SECURITY_WARNINGS; @@ -31,34 +29,55 @@ runAppSingleInstance(async () => { app.disableHardwareAcceleration(); } - // eslint-disable-next-line prefer-const - let mainWindow: BrowserWindow | undefined; + await app.whenReady(); + + let mainWindow: BrowserWindow | null = createWindow(); if (PLATFORM.IS_MAC) { // Protocol handler for macos app.on('open-url', (event, url) => { event.preventDefault(); - processUrl(url, mainWindow); + if (mainWindow) { + processUrl(url, mainWindow); + } }); } if (PLATFORM.IS_WINDOWS || PLATFORM.IS_LINUX) { // Protocol handler for win32/Linux - app.on('second-instance', (_, commandLine) => { + app.on('second-instance', () => { if (mainWindow) { if (mainWindow.isMinimized()) mainWindow.restore(); mainWindow.focus(); } - - const url = commandLine[commandLine.length - 1]; - if (!url.startsWith(APP_CONFIG.ELECTRON_PROTOCOL + '://')) return; - - processUrl(url, mainWindow); }); } - await app.whenReady(); + app.on('activate', async () => { + if (mainWindow === null) { + mainWindow = createWindow(); + } + }); + + app.on('web-contents-created', (_, contents) => + contents.on('will-navigate', (event) => !ENVIRONMENT.IS_DEV && event.preventDefault()), + ); + + app.on('window-all-closed', () => { + if (!PLATFORM.IS_MAC) { + app.quit(); + } - mainWindow = createWindow(); - await setupApplication(mainWindow); + mainWindow?.destroy(); + mainWindow = null; + }); + + if (ENVIRONMENT.IS_DEV) { + await installExtension(REACT_DEVELOPER_TOOLS); + + // Reloading extensions for correct initialization in dev tools + session.defaultSession.getAllExtensions().map((e) => { + session.defaultSession.loadExtension(e.path); + }); + } });