-
Notifications
You must be signed in to change notification settings - Fork 904
Expand file tree
/
Copy pathsetup-global-shortcut.js
More file actions
58 lines (50 loc) · 1.73 KB
/
setup-global-shortcut.js
File metadata and controls
58 lines (50 loc) · 1.73 KB
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
const { globalShortcut, ipcMain } = require('electron')
const i18n = require('i18next')
const createToggler = require('./create-toggler')
const store = require('../common/store')
const { IS_MAC } = require('../common/consts')
const { showDialog } = require('../dialogs')
const ipcMainEvents = require('../common/ipc-main-events')
// This function registers a global shortcut/accelerator with a certain action
// and (de)activates it according to its 'settingsOption' value on settings.
module.exports = function ({ settingsOption, accelerator, action, confirmationDialog }) {
const activate = ({ newValue, oldValue = null, feedback = null }) => {
if (newValue === oldValue) return
if (newValue === true) {
if (feedback && confirmationDialog) {
if (showDialog({
...confirmationDialog,
buttons: [
i18n.t('enable'),
i18n.t('cancel')
]
}) !== 0) {
// User canceled
return
}
}
globalShortcut.register(accelerator, action)
} else {
globalShortcut.unregister(accelerator)
}
return true
}
activate({ newValue: store.get(settingsOption, false) })
createToggler(settingsOption, activate)
if (!IS_MAC) {
return
}
// On macOS, when registering accelerators in the menubar, we need to
// unregister them globally before the menubar is open. Otherwise they
// won't work unless the user closes the menubar.
ipcMain.on(ipcMainEvents.MENUBAR_OPEN, () => {
if (store.get(settingsOption, false)) {
globalShortcut.unregister(accelerator)
}
})
ipcMain.on(ipcMainEvents.MENUBAR_CLOSE, () => {
if (store.get(settingsOption, false)) {
globalShortcut.register(accelerator, action)
}
})
}