/
main.js
75 lines (64 loc) · 1.7 KB
/
main.js
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
75
let { app, Tray, Menu, BrowserWindow, globalShortcut } = require('electron');
let path = require('path');
let fs = require('fs');
let exec = require('child_process').exec;
let homedir = require('os').homedir();
let appIcon = null;
let win = null;
let tray = null;
let noHotkeysFound = [
{
label: 'Load shortcuts at ~/.hotkeysrc'
}
];
let iconPath = () => path.join(__dirname, 'images', `icon-Template.png`);
let contextMenu = (hotkeys) => {
return Menu.buildFromTemplate([
...(hotkeys || noHotkeysFound),
{
type: 'separator'
},
{
label: 'quit',
accelerator: 'Command+q',
selector: 'terminate:'
}
]);
};
let reloadApp = (params = {}) => {
console.log('reload app');
win = win || new BrowserWindow({ show: false });
tray = tray || new Tray(iconPath());
tray.setToolTip('hotkeysrc');
tray.setContextMenu(contextMenu(params.hotkeys));
tray.setImage(iconPath());
}
let setupHotKeys = (hotkeys) => {
let menuItems = hotkeys.map((hotkey) => {
// side effect, setup shortcut
globalShortcut.register(hotkey.shortcut, () => {
hotkey.apps.forEach((app) => {
console.log(`Running \`open ${app}\``);
exec(`open "${app}"`);
});
});
return {
label: hotkey.apps.map(a => a.split('/').slice(-1)).join(' ,'),
accelerator: hotkey.shortcut
}
});
hotkeys = menuItems;
reloadApp({hotkeys});
}
app.on('ready', () => {
app.dock.hide();
fs.readFile(path.join(homedir, '.hotkeysrc'), 'utf8', function (err, data) {
if (err) {
console.log('Please include your hotkeys at ~/.hotkeysrc');
}
setupHotKeys(JSON.parse(data));
});
});
app.on('will-quit', () => {
globalShortcut.unregisterAll()
});