Browse Source

Merge pull request #864 from meriadec/prevent-multiple-instances

Quit app if not main instance
master
Gaëtan Renaudeau 7 years ago
committed by GitHub
parent
commit
8d06bcdf2b
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 44
      src/main/app.js

44
src/main/app.js

@ -17,10 +17,18 @@ import { terminateAllTheThings } from './terminator'
// necessary to prevent win from being garbage collected
let mainWindow = null
export const getMainWindow = () => mainWindow
const isSecondInstance = app.makeSingleInstance(() => {
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore()
mainWindow.focus()
}
})
// TODO put back OSX close behavior
// let forceClose = false
if (isSecondInstance) {
app.quit()
}
export const getMainWindow = () => mainWindow
const { UPGRADE_EXTENSIONS, ELECTRON_WEBPACK_WDS_PORT, DEV_TOOLS, DEV_TOOLS_MODE } = process.env
@ -35,17 +43,6 @@ const getWindowPosition = (height, width, display = screen.getPrimaryDisplay())
}
}
// TODO put back OSX close behavior
// const handleCloseWindow = w => e => {
// if (!forceClose) {
// e.preventDefault()
// w.webContents.send('lock')
// if (w !== null) {
// w.hide()
// }
// }
// }
const getDefaultUrl = () =>
__DEV__ ? `http://localhost:${ELECTRON_WEBPACK_WDS_PORT || ''}` : `file://${__dirname}/index.html`
@ -116,8 +113,6 @@ function createMainWindow() {
window.loadURL(url)
// TODO put back OSX close behavior
// window.on('close', handleCloseWindow(window))
window.on('close', terminateAllTheThings)
window.on('ready-to-show', () => {
@ -137,25 +132,12 @@ function createMainWindow() {
return window
}
// TODO put back OSX close behavior
// app.on('before-quit', () => {
// forceClose = true
// })
app.on('window-all-closed', () => {
// On macOS it is common for applications to stay open
// until the user explicitly quits
if (process.platform !== 'darwin') {
app.quit()
}
app.quit()
})
app.on('activate', () => {
// On macOS it is common to re-create a window
// even after all windows have been closed
if (mainWindow === null) {
mainWindow = createMainWindow()
} else {
if (mainWindow) {
mainWindow.show()
}
})

Loading…
Cancel
Save