From 9bd639eaf8ddb1433f746b428cdba537a606627c Mon Sep 17 00:00:00 2001 From: rhysd Date: Sat, 31 Oct 2015 17:22:38 +0900 Subject: [PATCH] Add makeSingleInstance() API to 'app' module Electron 0.34.1 introduced a new API `makeSingleInstance()`. https://github.com/atom/electron/blob/master/docs/api/app.md#appmakesingleinstancecallback --- github-electron/github-electron-main-tests.ts | 15 +++++++++++++++ github-electron/github-electron.d.ts | 6 ++++++ 2 files changed, 21 insertions(+) diff --git a/github-electron/github-electron-main-tests.ts b/github-electron/github-electron-main-tests.ts index 4e359e257..9ee0b1390 100644 --- a/github-electron/github-electron-main-tests.ts +++ b/github-electron/github-electron-main-tests.ts @@ -35,6 +35,21 @@ app.on('window-all-closed', () => { app.quit(); }); +// Check single instance app +var shouldQuit = app.makeSingleInstance(function(commandLine, workingDirectory) { + // Someone tried to run a second instance, we should focus our window + if (mainWindow) { + if (mainWindow.isMinimized()) mainWindow.restore(); + mainWindow.focus(); + } + return true; +}); + +if (shouldQuit) { + app.quit(); + process.exit(0); +} + // This method will be called when Electron has done everything // initialization and ready for creating browser windows. app.on('ready', () => { diff --git a/github-electron/github-electron.d.ts b/github-electron/github-electron.d.ts index 92fff65f7..9afcbdd63 100644 --- a/github-electron/github-electron.d.ts +++ b/github-electron/github-electron.d.ts @@ -986,6 +986,12 @@ declare module GitHubElectron { setUserTasks(tasks: Task[]): void; dock: BrowserWindow; commandLine: CommandLine; + /** + * This method makes your application a Single Instance Application instead of allowing + * multiple instances of your app to run, this will ensure that only a single instance + * of your app is running, and other instances signal this instance and exit. + */ + makeSingleInstance(callback: (args: string[], workingDirectory: string) => boolean): boolean; } interface CommandLine {