-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinitializeTray.js
42 lines (33 loc) · 1.13 KB
/
initializeTray.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
const path = require('path')
const { app, nativeTheme, nativeImage, Menu, Tray } = require('electron')
let appTray = null
// Function to get path of icon file
const getTrayImage = function () {
// Calculate percentage
const timerStatus = global.systems.timer.getStatus()
const percentage = timerStatus.remainingTime / timerStatus.totalDuration * 100
const percentageMultOfFive = Math.round(percentage / 5) * 5
const folder = process.platform === 'darwin'
? 'template'
: nativeTheme.shouldUseDarkColors
? 'white'
: 'black'
const imagePath = path.join(__dirname, `../tray_assets/${folder}/${percentageMultOfFive}.png`)
return nativeImage.createFromPath(imagePath)
}
const contextMenu = Menu.buildFromTemplate([
{ label: app.getName(), enabled: false },
{ type: 'separator' },
{ label: 'Quit', click: app.exit }
])
appTray = new Tray(getTrayImage())
appTray.setToolTip(app.getName())
appTray.setContextMenu(contextMenu)
appTray.on('click', () => {
global.mainWindow.show()
global.mainWindow.focus()
})
// Update system tray icon on an interval
setInterval(() => {
appTray.setImage(getTrayImage())
}, 5000)