Skip to content

Commit

Permalink
[desktop] make window manager aware of quit-on-update, close #1617
Browse files Browse the repository at this point in the history
  • Loading branch information
ganthern authored and charlag committed Jan 23, 2020
1 parent e146ddf commit df2ec7b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
6 changes: 4 additions & 2 deletions flow/electron.js
Expand Up @@ -202,8 +202,9 @@ declare module 'electron' {
}

declare export class App {
on(AppEvent, (Event, ...Array<any>) => void): App,
once(AppEvent, (Event, ...Array<any>) => void): App,
on(AppEvent, (Event, ...Array<any>) => any): App,
once(AppEvent, (Event, ...Array<any>) => any): App,
emit(AppEvent) : App,
requestSingleInstanceLock(): void,
quit(): void,
exit(code: Number): void,
Expand Down Expand Up @@ -598,6 +599,7 @@ export type AppEvent
| 'gpu-process-crashed'
| 'accessibility-support-changed'
| 'session-created'
| 'enable-force-quit'


// https://github.com/electron/electron/blob/master/docs/api/browser-window.md#instance-events
Expand Down
11 changes: 5 additions & 6 deletions src/desktop/DesktopWindowManager.js
Expand Up @@ -19,11 +19,10 @@ export type WindowBounds = {|
|}

const windows: ApplicationWindow[] = []
app.forceQuit = false
app.once('before-quit', () => {
console.log("before-quit")
app.forceQuit = true
})
let forceQuit = false
let enableForceQuit = () => forceQuit = true
app.once('before-quit', enableForceQuit)
.once('enable-force-quit', enableForceQuit)

export class WindowManager {
_conf: DesktopConfigHandler
Expand Down Expand Up @@ -53,7 +52,7 @@ export class WindowManager {
windows.unshift(w)
w.on('close', ev => {
// we don't want to actually close windows where someone is logged in, just hide them
if (this._conf.getDesktopConfig('runAsTrayApp') && w.getUserInfo() != null && !app.forceQuit) {
if (this._conf.getDesktopConfig('runAsTrayApp') && w.getUserInfo() != null && !forceQuit) {
ev.preventDefault()
w.hide()
}
Expand Down
2 changes: 1 addition & 1 deletion src/desktop/ElectronUpdater.js
Expand Up @@ -149,7 +149,7 @@ export class ElectronUpdater {
})
.then((res) => {
if (res === NotificationResult.Click) {
app.forceQuit = true
app.emit('enable-force-quit')
autoUpdater.quitAndInstall(false, true)
}
})
Expand Down
14 changes: 12 additions & 2 deletions test/client/desktop/ElectronUpdaterTest.js
Expand Up @@ -26,7 +26,8 @@ o.spec("ElectronUpdater Test", function (done, timeout) {
const electron = {
app: {
getPath: (path: string) => `/mock-${path}/`,
getVersion: (): string => "3.45.0"
getVersion: (): string => "3.45.0",
emit: ()=>{}
}
}

Expand Down Expand Up @@ -133,6 +134,7 @@ o.spec("ElectronUpdater Test", function (done, timeout) {
//mock node modules
const forgeMock = n.mock('node-forge', nodeForge).set()
const autoUpdaterMock = n.mock('electron-updater', {autoUpdater}).set().autoUpdater
const electronMock = n.mock('electron', electron).set()

//mock our modules
n.mock('./DesktopTray', desktopTray).set()
Expand Down Expand Up @@ -177,6 +179,8 @@ o.spec("ElectronUpdater Test", function (done, timeout) {
icon: 'this is an icon'
})

o(electronMock.app.emit.callCount).equals(1)
o(electronMock.app.emit.args[0]).equals('enable-force-quit')
o(autoUpdaterMock.quitAndInstall.callCount).equals(1)
o(autoUpdaterMock.quitAndInstall.args[0]).equals(false)
o(autoUpdaterMock.quitAndInstall.args[1]).equals(true)
Expand Down Expand Up @@ -292,11 +296,13 @@ o.spec("ElectronUpdater Test", function (done, timeout) {
icon: 'this is an icon'
})

o(electronMock.app.emit.callCount).equals(1)
o(electronMock.app.emit.args[0]).equals('enable-force-quit')
o(autoUpdaterMock.quitAndInstall.callCount).equals(1)
o(autoUpdaterMock.quitAndInstall.args[0]).equals(false)
o(autoUpdaterMock.quitAndInstall.args[1]).equals(true)
done()
}, 190)
}, 250)
})

o("retry after autoUpdater reports an error", done => {
Expand Down Expand Up @@ -420,6 +426,7 @@ o.spec("ElectronUpdater Test", function (done, timeout) {
publicKeyFromPem: (pem: string) => n.spyify(pem === "no" ? rightKey : wrongKey)
}).set()
const autoUpdaterMock = n.mock('electron-updater', {autoUpdater}).set().autoUpdater
const electronMock = n.mock('electron', electron).set()

//mock our modules
n.mock('./DesktopTray', desktopTray).set()
Expand Down Expand Up @@ -465,6 +472,9 @@ o.spec("ElectronUpdater Test", function (done, timeout) {
icon: 'this is an icon'
})


o(electronMock.app.emit.callCount).equals(1)
o(electronMock.app.emit.args[0]).equals('enable-force-quit')
o(autoUpdaterMock.quitAndInstall.callCount).equals(1)
o(autoUpdaterMock.quitAndInstall.args[0]).equals(false)
o(autoUpdaterMock.quitAndInstall.args[1]).equals(true)
Expand Down

0 comments on commit df2ec7b

Please sign in to comment.