-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathtray.js
128 lines (119 loc) · 3.13 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
import { Menu, Tray, app, nativeImage } from 'electron'
import { isDarwin, isLinux, isWin } from './platform'
import { backgroundWindow } from './backgroundWin'
import { mainWindow } from './win'
const resourcesFolder = __dirname + '/resources/'
export default {
tray: null,
contextMenu: null,
osxContextMenu: null,
init() {
if (isDarwin) {
initOSXContextMenu.bind(this)()
} else {
initNormalContextMenu.bind(this)()
}
if (isDarwin) {
this.tray = new Tray(nativeImage.createEmpty())
this.tray.on('right-click', () => {
this.tray.popUpContextMenu(this.osxContextMenu)
})
this.tray.on('click', (event, bounds, position) => {
backgroundWindow.webContents.send('trayClick', { event, bounds, position })
})
} else if (isWin) {
this.tray = new Tray(`${resourcesFolder}win/icon.ico`)
} else if (isLinux) {
this.tray = new Tray(
process.env.PETAL_FORCE_OSX_ICON
? `${resourcesFolder}osx/${process.env.PETAL_FORCE_OSX_ICON}/logo.png`
: `${resourcesFolder}linux/icon.png`
)
}
if (!isDarwin) {
this.tray.setContextMenu(this.contextMenu)
}
},
setTrayImage(img) {
const bounds = this.tray.getBounds()
const Image = nativeImage.createFromDataURL(img).resize({
height: bounds ? bounds.height : 22,
})
this.tray.setImage(Image)
},
popUpContextMenu() {
this.tray.popUpContextMenu(this.osxContextMenu)
},
}
function initNormalContextMenu() {
this.contextMenu = Menu.buildFromTemplate([
{
label: '切换显示/隐藏',
enabled: true,
click: mainWindowVisiableSwitch,
},
{ type: 'separator' },
{
label: '切换暂停/开始',
click: () => mainWindow.webContents.send('pause'),
},
{
label: '红心',
click: () => mainWindow.webContents.send('love'),
},
{
label: '跳过',
click: () => mainWindow.webContents.send('skip'),
},
{
label: '垃圾桶',
click: () => mainWindow.webContents.send('trash'),
},
{ type: 'separator' },
{
label: '退出',
click: app.quit,
},
])
}
function initOSXContextMenu() {
this.osxContextMenu = Menu.buildFromTemplate([
{
label: '切换显示/隐藏',
enabled: true,
click: mainWindowVisiableSwitch,
},
{ type: 'separator' },
{
label: '贴附到状态栏',
type: 'checkbox',
checked: false,
enabled: true,
click: () => {
// windowTopSwitch
let appIconRect = this.tray.getBounds()
let mainWindowRect = mainWindow.getBounds()
let currentItem = this.osxContextMenu.items[2]
if (currentItem.checked === true) {
if (isDarwin) {
mainWindow.setPosition(appIconRect.x - mainWindowRect.width / 2 + appIconRect.width / 2, appIconRect.y)
}
} else {
mainWindow.center()
}
},
},
{ type: 'separator' },
{
label: '退出',
click: app.quit,
},
])
}
function mainWindowVisiableSwitch() {
if (mainWindow.isVisible()) {
mainWindow.hide()
} else {
mainWindow.show()
}
}