From 05afbf87b2117f746fc823a0ae0b734b41360ac9 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 20 Oct 2018 19:37:49 +0200 Subject: [PATCH] Add limit of electron instances Former-commit-id: ac2ccd308637c48cdca4b838c8974b2ab2501578 [formerly ac2ccd308637c48cdca4b838c8974b2ab2501578 [formerly d6e6265ac08bff23a448e1b1ccdfb52bff800c59]] Former-commit-id: 28ed180395de36b1fd46f6f24f46ab6ecb90e4b3 Former-commit-id: 6f4094fd5e1cf8a361d1fd43d11aa830e1da339a --- src/main/index.ts | 82 ++++++++++++++++---------- src/renderer/app/index.tsx | 11 ++-- src/shared/services/app/index.ts | 6 +- src/shared/services/main/extensions.ts | 2 +- src/shared/utils/main/window.ts | 2 +- 5 files changed, 64 insertions(+), 39 deletions(-) diff --git a/src/main/index.ts b/src/main/index.ts index ee2a18186..72336059f 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -1,4 +1,4 @@ -import { app, ipcMain } from 'electron'; +import { app, ipcMain, BrowserWindow } from 'electron'; import { resolve } from 'path'; import { platform, homedir } from 'os'; import { mkdirSync, existsSync, writeFileSync } from 'fs'; @@ -19,7 +19,7 @@ app.setPath('userData', resolve(homedir(), '.wexond')); declare const global: Global; -let mainWindow: Electron.BrowserWindow; +let mainWindow: BrowserWindow; global.extensions = {}; global.backgroundPages = {}; @@ -31,44 +31,64 @@ global.locale = 'en-US'; global.userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'; -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 (mainWindow === null) { - mainWindow = createWindow(); +const shouldQuit = app.makeSingleInstance(argv => { + if (mainWindow) { + if (mainWindow.isMinimized()) { + mainWindow.restore(); + } + + mainWindow.focus(); + + if (argv[1] !== '.') { + mainWindow.webContents.send('open-url', argv[1]); + } } + + return true; }); -app.on('ready', () => { - for (const key in defaultPaths) { - const path = defaultPaths[key]; - const filePath = getPath(path); - if (existsSync(filePath)) continue; +if (shouldQuit) { + app.quit(); +} else { + 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 (mainWindow === null) { + mainWindow = createWindow(); + } + }); - if (path.indexOf('.') === -1) { - mkdirSync(filePath); - } else { - writeFileSync(filePath, filesContent[key], 'utf8'); + app.on('ready', () => { + for (const key in defaultPaths) { + const path = defaultPaths[key]; + const filePath = getPath(path); + if (existsSync(filePath)) continue; + + if (path.indexOf('.') === -1) { + mkdirSync(filePath); + } else { + writeFileSync(filePath, filesContent[key], 'utf8'); + } } - } - mainWindow = createWindow(); + mainWindow = createWindow(); - loadExtensions(mainWindow); + loadExtensions(mainWindow); - runAutoUpdaterService(mainWindow); - runExtensionsService(mainWindow); - runWebRequestService(mainWindow); + runAutoUpdaterService(mainWindow); + runExtensionsService(mainWindow); + runWebRequestService(mainWindow); - mainWindow.on('closed', () => { - mainWindow = null; + mainWindow.on('closed', () => { + mainWindow = null; + }); }); -}); -app.on('window-all-closed', () => { - if (platform() !== 'darwin') { - app.quit(); - } -}); + app.on('window-all-closed', () => { + if (platform() !== 'darwin') { + app.quit(); + } + }); -registerProtocols(); + registerProtocols(); +} diff --git a/src/renderer/app/index.tsx b/src/renderer/app/index.tsx index bbb712a57..2155d4ef7 100755 --- a/src/renderer/app/index.tsx +++ b/src/renderer/app/index.tsx @@ -19,6 +19,7 @@ const render = (AppComponent: any) => { document.getElementById('app'), ); }; + (async function setup() { runServices(); render(App); @@ -28,13 +29,13 @@ const render = (AppComponent: any) => { } if (store.tabsStore.groups.length === 0) { - const argv = remote.process.argv; - store.tabsStore.addGroup(); - if (argv.length > 1 && argv[1] !== '.') { - store.tabsStore.getSelectedTab().url = argv[1]; - store.pagesStore.getSelected().url = argv[1]; + const openedFilePath = remote.process.argv[1]; + + if (openedFilePath !== '.') { + store.tabsStore.getSelectedTab().url = openedFilePath; + store.pagesStore.getSelected().url = openedFilePath; } } diff --git a/src/shared/services/app/index.ts b/src/shared/services/app/index.ts index 60aaa238f..075eafdac 100644 --- a/src/shared/services/app/index.ts +++ b/src/shared/services/app/index.ts @@ -20,12 +20,16 @@ export const runServices = () => { ipcRenderer.on( 'get-tab-by-web-contents-id', - (e: any, webContentsId: number) => { + (e: Electron.IpcMessageEvent, webContentsId: number) => { const tab = store.tabsStore.getTabByWebContentsId(webContentsId); ipcRenderer.send('get-tab-by-web-contents-id', tab ? tab : {}); }, ); + ipcRenderer.on('open-url', (e: Electron.IpcMessageEvent, url: any) => { + store.tabsStore.addTab({ url, active: true }); + }); + runAutoUpdaterService(); runExtensionsService(); }; diff --git a/src/shared/services/main/extensions.ts b/src/shared/services/main/extensions.ts index 79260cc06..9b0b16711 100644 --- a/src/shared/services/main/extensions.ts +++ b/src/shared/services/main/extensions.ts @@ -171,7 +171,7 @@ export const runExtensionsService = (window: BrowserWindow) => { if (!alarm.periodInMinutes) { setTimeout(() => { - contents.send(`api-emit-event-alarms-onAlarm`, alarm); + contents.send('api-emit-event-alarms-onAlarm', alarm); }, alarm.scheduledTime - Date.now()); } } diff --git a/src/shared/utils/main/window.ts b/src/shared/utils/main/window.ts index 1b5c080a4..6db8551c4 100644 --- a/src/shared/utils/main/window.ts +++ b/src/shared/utils/main/window.ts @@ -1,4 +1,4 @@ -import { BrowserWindow } from 'electron'; +import { BrowserWindow, app } from 'electron'; import { readFileSync, writeFileSync, existsSync } from 'fs'; import { join } from 'path';