forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquit-helper.desktop.tsx
74 lines (63 loc) · 1.95 KB
/
quit-helper.desktop.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import * as SafeElectron from '../util/safe-electron.desktop'
import {quit} from '../desktop/app/ctl.desktop'
import {hideDockIcon} from '../desktop/app/dock-icon.desktop'
export type Context = 'uiWindow' | 'mainThread' | 'quitButton' | 'beforeQuit'
export type Action = 'closePopups' | 'quitMainWindow' | 'quitApp'
// Logic to figure out what to do given your context
function quitOnContext(context: Context): Array<Action> {
switch (context) {
case 'uiWindow':
return ['closePopups', 'quitMainWindow']
case 'mainThread':
case 'beforeQuit':
case 'quitButton':
return ['quitApp']
}
return []
}
function isMainThread() {
// the main thread's process.type is browser: https://github.com/electron/electron/blob/master/docs/api/process.md
return process.type === 'browser'
}
function _executeActions(actions: Array<Action>) {
actions.forEach(a => {
switch (a) {
case 'quitMainWindow':
hideDockIcon()
break
case 'closePopups':
SafeElectron.getApp().emit('close-windows')
break
case 'quitApp':
quit()
break
}
})
}
// Takes an array of actions, but makes an ipc call to have the main thread execute the actions
function _executeActionsFromRenderer(actions: Array<Action>) {
SafeElectron.getIpcRenderer().send('executeActions', actions)
}
export function executeActionsForContext(context: Context) {
const actions = quitOnContext(context)
if (isMainThread()) {
_executeActions(actions)
} else {
_executeActionsFromRenderer(actions)
}
}
export function setupExecuteActionsListener() {
SafeElectron.getIpcMain().on('executeActions', (_, actions) => {
console.log('executeActionsRecieved', actions)
_executeActions(actions)
})
}
const crossplatformQuit = (reason: Context) => {
executeActionsForContext(reason)
}
export {crossplatformQuit as quit}
export function hideWindow() {
SafeElectron.getRemote()
.getCurrentWindow()
.hide()
}