-
Notifications
You must be signed in to change notification settings - Fork 1
/
desktop.js
107 lines (91 loc) · 2.61 KB
/
desktop.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
'use strict'
const inherits = require('util').inherits
const App = require('./app')
const electron = require('electron')
const remote = electron.remote
const app = remote.app
const ipcRenderer = electron.ipcRenderer
const shell = electron.shell
const currentWindow = remote.getCurrentWindow()
const Menu = remote.Menu
const pkg = require('./package')
module.exports = Desktop
function Desktop() {
if (!(this instanceof Desktop))
return new Desktop()
App.call(this, document.body, currentWindow)
this.setupMenu()
currentWindow.on('focus', () => {
this.setBadge(false)
})
this.on('setBadge', (n) => {
this.setBadge(n)
})
this.on('openUrl', (url) => {
shell.openExternal(url)
})
}
inherits(Desktop, App)
Desktop.prototype._handlers = function _handlers() {
return {
'application:devtools': () => {
const win = this.window
if (win) win.toggleDevTools()
}
, 'application:next-panel': () => { this.panels.nextPanel() }
, 'application:prev-panel': () => { this.panels.previousPanel() }
, 'application:show-userbar': () => { this.panels.showUserbar() }
, 'application:hide-userbar': () => { this.panels.hideUserbar() }
, 'application:quit': () => { app.quit() }
, 'application:minimize': () => {
const win = this.window
if (win) win.minimize()
}
, 'application:maximize': () => {
const win = this.window
if (win) win.maximize()
}
, 'application:open-repo': () => { this.emit('openUrl', pkg.repository.url) }
}
}
Desktop.prototype.setupMenu = function setupMenu() {
const handlers = this._handlers()
const tmp = require(`./lib/menus/${process.platform}`)
for (let toplevel of tmp) {
const sub = toplevel.submenu
if (sub && sub.length) {
for (let item of sub) {
if (item.url) {
item.click = () => {
this.router.goto(item.url)
}
} else if (item.command) {
item.click = () => {
if (handlers[item.command]) {
handlers[item.command]()
} else {
ipcRenderer.send('command', item.command)
}
}
}
}
}
}
const menu = Menu.buildFromTemplate(tmp)
Menu.setApplicationMenu(menu)
}
Desktop.prototype.setBadge = function setBadge(n) {
if (!app.dock)
return
if (n === false) {
return app.dock.setBadge('')
} else if (n == null) {
// Don't add a badge if the current window is already focused
if (currentWindow.isFocused())
return app.dock.setBadge('')
this._notifications++
} else {
this._notifications = n
}
app.dock.setBadge(this._notifications.toString())
}