-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
130 lines (118 loc) · 3.07 KB
/
index.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
var { app, ipcMain, globalShortcut, Menu } = require('electron')
var menubar = require('menubar')
var isDev = require('electron-is-dev')
var path = require('path')
var mb = menubar({
dir: path.join(__dirname, '/app'),
width: 440,
height: 570,
icon: path.join(__dirname, '/app/logo.png'),
preloadWindow: true,
windowPosition: 'topRight',
alwaysOnTop: true
})
mb.on('show', function () {
mb.window.webContents.send('show')
// mb.window.openDevTools()
})
mb.app.on('will-quit', function () {
globalShortcut.unregisterAll()
})
mb.app.on('activate', function () {
mb.showWindow()
})
// when receive the abort message, close the app
ipcMain.on('abort', function () {
mb.hideWindow()
})
// update shortcuts when preferences change
ipcMain.on('update-preference', function (evt, pref, initialization) {
registerShortcut(pref['open-window-shortcut'], initialization)
// Make packaged app (not dev app) start at login
if (!isDev) {
app.setLoginItemSettings({
openAtLogin: pref['open-at-login'],
openAsHidden: true
})
}
})
var template = [
{
label: 'Mojibar',
submenu: [
{
label: 'Undo',
accelerator: 'CommandOrControl+Z',
selector: 'undo:'
},
{
label: 'Redo',
accelerator: 'Shift+CommandOrControl+Z',
selector: 'redo:'
},
{
label: 'Cut',
accelerator: 'CommandOrControl+X',
selector: 'cut:'
},
{
label: 'Copy',
accelerator: 'CommandOrControl+C',
selector: 'copy:'
},
{
label: 'Paste',
accelerator: 'CommandOrControl+V',
selector: 'paste:'
},
{
label: 'Select All',
accelerator: 'CommandOrControl+A',
selector: 'selectAll:'
},
{
label: 'Reload',
accelerator: 'CmdOrCtrl+R',
click: function (item, focusedWindow) { if (focusedWindow) focusedWindow.reload() }
},
{
label: 'Preference',
accelerator: 'CommandOrControl+,',
click: function () { mb.window.webContents.send('open-preference') }
},
{
label: 'Quit App',
accelerator: 'CommandOrControl+Q',
selector: 'terminate:'
},
{
label: 'Toggle DevTools',
accelerator: 'Alt+CommandOrControl+I',
click: function () { mb.window.toggleDevTools() }
}
]
}
]
mb.on('ready', function ready() {
// Build default menu for text editing and devtools. (gone since electron 0.25.2)
var menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)
})
// Register a shortcut listener.
var registerShortcut = function (keybinding, initialization) {
globalShortcut.unregisterAll()
try {
var ret = globalShortcut.register(keybinding, function () {
if (mb.window.isVisible()) {
return mb.hideWindow()
}
mb.showWindow()
mb.window.focus()
})
} catch (err) {
mb.window.webContents.send('preference-updated', false, initialization)
}
if (ret) {
mb.window.webContents.send('preference-updated', true, initialization)
}
}