forked from ipfs/ipfs-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtray.js
360 lines (321 loc) · 11.5 KB
/
tray.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
const { Menu, Tray, shell, app, ipcMain } = require('electron')
const i18n = require('i18next')
const path = require('path')
const addToIpfs = require('./add-to-ipfs')
const logger = require('./common/logger')
const store = require('./common/store')
const moveRepositoryLocation = require('./move-repository-location')
const runGarbageCollector = require('./run-gc')
const ipcMainEvents = require('./utils/ipcMainEvents')
const { setCustomBinary, clearCustomBinary, hasCustomBinary } = require('./custom-ipfs-binary')
const { STATUS } = require('./daemon')
const { IS_MAC, IS_WIN, VERSION, GO_IPFS_VERSION } = require('./common/consts')
const CONFIG_KEYS = require('./common/config-keys')
const { SHORTCUT: SCREENSHOT_SHORTCUT, takeScreenshot } = require('./take-screenshot')
const { isSupported: supportsLaunchAtLogin } = require('./auto-launch')
function buildCheckbox (key, label) {
return {
id: key,
label: i18n.t(label),
click: () => { ipcMainEvents.TOGGLE(key) },
type: 'checkbox',
checked: false
}
}
// Notes on this: we are only supporting accelerators on macOS for now because
// they natively work as soon as the menu opens. They don't work like that on Windows
// or other OSes and must be registered globally. They still collide with global
// accelerator. Please see ../utils/setup-global-shortcut.js for more info.
function buildMenu (ctx) {
return Menu.buildFromTemplate([
...[
['ipfsIsStarting', 'yellow'],
['ipfsIsRunning', 'green'],
['ipfsIsStopping', 'yellow'],
['ipfsIsNotRunning', 'gray'],
['ipfsHasErrored', 'red'],
['runningWithGC', 'yellow'],
['runningWhileCheckingForUpdate', 'yellow']
].map(([status, color]) => ({
id: status,
label: i18n.t(status),
visible: false,
enabled: false,
icon: path.resolve(path.join(__dirname, `../assets/icons/status/${color}.png`))
})),
{
id: 'restartIpfs',
label: i18n.t('restart'),
click: () => { ctx.restartIpfs() },
visible: false,
accelerator: IS_MAC ? 'Command+R' : null
},
{
id: 'startIpfs',
label: i18n.t('start'),
click: () => { ctx.startIpfs() },
visible: false
},
{
id: 'stopIpfs',
label: i18n.t('stop'),
click: () => { ctx.stopIpfs() },
visible: false
},
{ type: 'separator' },
{
id: 'webuiStatus',
label: i18n.t('status'),
click: () => { ctx.launchWebUI('/') }
},
{
id: 'webuiFiles',
label: i18n.t('files'),
click: () => { ctx.launchWebUI('/files') }
},
{
id: 'webuiPeers',
label: i18n.t('peers'),
click: () => { ctx.launchWebUI('/peers') }
},
{ type: 'separator' },
{
id: 'takeScreenshot',
label: i18n.t('takeScreenshot'),
click: () => { takeScreenshot(ctx) },
accelerator: IS_MAC ? SCREENSHOT_SHORTCUT : null,
enabled: false
},
{ type: 'separator' },
{
label: IS_MAC ? i18n.t('settings.preferences') : i18n.t('settings.settings'),
submenu: [
{
id: 'webuiNodeSettings',
label: i18n.t('settings.openNodeSettings'),
click: () => { ctx.launchWebUI('/settings') }
},
{ type: 'separator' },
{
label: i18n.t('settings.appPreferences'),
enabled: false
},
buildCheckbox(CONFIG_KEYS.AUTO_LAUNCH, 'settings.launchOnStartup'),
buildCheckbox(CONFIG_KEYS.OPEN_WEBUI_LAUNCH, 'settings.openWebUIAtLaunch'),
buildCheckbox(CONFIG_KEYS.AUTO_GARBAGE_COLLECTOR, 'settings.automaticGC'),
buildCheckbox(CONFIG_KEYS.SCREENSHOT_SHORTCUT, 'settings.takeScreenshotShortcut'),
{ type: 'separator' },
{
label: i18n.t('settings.experiments'),
enabled: false
},
buildCheckbox(CONFIG_KEYS.EXPERIMENT_PUBSUB, 'settings.pubsub'),
buildCheckbox(CONFIG_KEYS.EXPERIMENT_PUBSUB_NAMESYS, 'settings.namesysPubsub')
]
},
{
label: i18n.t('advanced'),
submenu: [
{
label: i18n.t('openLogsDir'),
click: () => { shell.openPath(app.getPath('userData')) }
},
{
label: i18n.t('openRepoDir'),
click: () => { shell.openPath(store.get('ipfsConfig.path')) }
},
{
label: i18n.t('openConfigFile'),
click: () => { shell.openPath(store.path) }
},
{ type: 'separator' },
{
id: 'runGarbageCollector',
label: i18n.t('runGarbageCollector'),
click: () => { runGarbageCollector(ctx) },
enabled: false
},
{ type: 'separator' },
{
id: 'moveRepositoryLocation',
label: i18n.t('moveRepositoryLocation'),
click: () => { moveRepositoryLocation(ctx) }
},
{
id: 'setCustomBinary',
label: i18n.t('setCustomIpfsBinary'),
click: () => { setCustomBinary(ctx) },
visible: false
},
{
id: 'clearCustomBinary',
label: i18n.t('clearCustomIpfsBinary'),
click: () => { clearCustomBinary(ctx) },
visible: false
}
]
},
{
label: i18n.t('about'),
submenu: [
{
label: i18n.t('versions'),
enabled: false
},
{
label: `ipfs-desktop ${VERSION}`,
click: () => { shell.openExternal(`https://github.com/ipfs-shipyard/ipfs-desktop/releases/v${VERSION}`) }
},
{
label: hasCustomBinary()
? i18n.t('customIpfsBinary')
: `go-ipfs ${GO_IPFS_VERSION}`,
click: () => { shell.openExternal(`https://github.com/ipfs/go-ipfs/releases/v${GO_IPFS_VERSION}`) }
},
{ type: 'separator' },
{
id: 'checkForUpdates',
label: i18n.t('checkForUpdates'),
click: () => { ctx.manualCheckForUpdates() }
},
{
id: 'checkingForUpdates',
label: i18n.t('checkingForUpdates'),
enabled: false
},
{ type: 'separator' },
{
label: i18n.t('viewOnGitHub'),
click: () => { shell.openExternal('https://github.com/ipfs-shipyard/ipfs-desktop/blob/master/README.md') }
},
{
label: i18n.t('helpUsTranslate'),
click: () => { shell.openExternal('https://www.transifex.com/ipfs/public/') }
}
]
},
{
label: i18n.t('quit'),
click: () => { app.quit() },
accelerator: IS_MAC ? 'Command+Q' : null
}
])
}
const on = 'on'
const off = 'off'
function icon (color) {
const dir = path.resolve(path.join(__dirname, '../assets/icons/tray'))
if (!IS_MAC) {
return path.join(dir, `${color}-big.png`)
}
return path.join(dir, `${color}-22Template.png`)
}
// Ok this one is pretty ridiculous:
// Tray must be global or it will break due to GC:
// https://www.electronjs.org/docs/faq#my-apps-tray-disappeared-after-a-few-minutes
let tray = null
module.exports = function (ctx) {
logger.info('[tray] starting')
tray = new Tray(icon(off))
let menu = null
const state = {
status: null,
gcRunning: false,
isUpdating: false
}
// macOS tray drop files
tray.on('drop-files', async (_, files) => {
await addToIpfs(ctx, files)
ctx.launchWebUI('/files', { focus: false })
})
const popupMenu = (event) => {
// https://github.com/ipfs-shipyard/ipfs-desktop/issues/1762 ¯\_(ツ)_/¯
if (event && typeof event.preventDefault === 'function') event.preventDefault()
tray.popUpContextMenu()
}
if (!IS_MAC) {
// Show the context menu on left click on other
// platforms than macOS.
tray.on('click', popupMenu)
}
tray.on('right-click', popupMenu)
tray.on('double-click', () => ctx.launchWebUI('/'))
const setupMenu = () => {
menu = buildMenu(ctx)
tray.setContextMenu(menu)
tray.setToolTip('IPFS Desktop')
menu.on('menu-will-show', () => { ipcMain.emit(ipcMainEvents.MENUBAR_OPEN) })
menu.on('menu-will-close', () => { ipcMain.emit(ipcMainEvents.MENUBAR_CLOSE) })
updateMenu()
}
const updateMenu = () => {
const { status, gcRunning, isUpdating } = state
const errored = status === STATUS.STARTING_FAILED || status === STATUS.STOPPING_FAILED
menu.getMenuItemById('ipfsIsStarting').visible = status === STATUS.STARTING_STARTED && !gcRunning && !isUpdating
menu.getMenuItemById('ipfsIsRunning').visible = status === STATUS.STARTING_FINISHED && !gcRunning && !isUpdating
menu.getMenuItemById('ipfsIsStopping').visible = status === STATUS.STOPPING_STARTED && !gcRunning && !isUpdating
menu.getMenuItemById('ipfsIsNotRunning').visible = status === STATUS.STOPPING_FINISHED && !gcRunning && !isUpdating
menu.getMenuItemById('ipfsHasErrored').visible = errored && !gcRunning && !isUpdating
menu.getMenuItemById('runningWithGC').visible = gcRunning
menu.getMenuItemById('runningWhileCheckingForUpdate').visible = isUpdating
menu.getMenuItemById('startIpfs').visible = status === STATUS.STOPPING_FINISHED
menu.getMenuItemById('stopIpfs').visible = status === STATUS.STARTING_FINISHED
menu.getMenuItemById('restartIpfs').visible = (status === STATUS.STARTING_FINISHED || errored)
menu.getMenuItemById('webuiStatus').enabled = status === STATUS.STARTING_FINISHED
menu.getMenuItemById('webuiFiles').enabled = status === STATUS.STARTING_FINISHED
menu.getMenuItemById('webuiPeers').enabled = status === STATUS.STARTING_FINISHED
menu.getMenuItemById('webuiNodeSettings').enabled = status === STATUS.STARTING_FINISHED
menu.getMenuItemById('startIpfs').enabled = !gcRunning
menu.getMenuItemById('stopIpfs').enabled = !gcRunning
menu.getMenuItemById('restartIpfs').enabled = !gcRunning
menu.getMenuItemById(CONFIG_KEYS.AUTO_LAUNCH).enabled = supportsLaunchAtLogin()
menu.getMenuItemById('takeScreenshot').enabled = status === STATUS.STARTING_FINISHED
menu.getMenuItemById('moveRepositoryLocation').enabled = !gcRunning && status !== STATUS.STOPPING_STARTED
menu.getMenuItemById('runGarbageCollector').enabled = menu.getMenuItemById('ipfsIsRunning').visible && !gcRunning
menu.getMenuItemById('setCustomBinary').visible = !hasCustomBinary()
menu.getMenuItemById('clearCustomBinary').visible = hasCustomBinary()
menu.getMenuItemById('checkForUpdates').enabled = !isUpdating
menu.getMenuItemById('checkForUpdates').visible = !isUpdating
menu.getMenuItemById('checkingForUpdates').visible = isUpdating
if (status === STATUS.STARTING_FINISHED) {
tray.setImage(icon(on))
} else {
tray.setImage(icon(off))
}
// Update configuration checkboxes.
for (const key of Object.values(CONFIG_KEYS)) {
const enabled = store.get(key, false)
menu.getMenuItemById(key).checked = enabled
}
if (!IS_MAC && !IS_WIN) {
// On Linux, in order for changes made to individual MenuItems to take effect,
// you have to call setContextMenu again - https://electronjs.org/docs/api/tray
tray.setContextMenu(menu)
}
}
ipcMain.on('ipfsd', status => {
state.status = status
updateMenu()
})
ipcMain.on('gcRunning', () => {
state.gcRunning = true
updateMenu()
})
ipcMain.on('gcEnded', () => {
state.gcRunning = false
updateMenu()
})
ipcMain.on('updating', () => {
state.isUpdating = true
updateMenu()
})
ipcMain.on('updatingEnded', () => {
state.isUpdating = false
updateMenu()
})
ipcMain.on('configUpdated', () => { updateMenu() })
ipcMain.on('languageUpdated', () => { setupMenu() })
setupMenu()
ctx.tray = tray
logger.info('[tray] started')
}