增加“关闭时最小化到托盘”的设置 (#166)

添加托盘设置,调整主进程代码架构
This commit is contained in:
Lasy
2020-04-03 20:00:43 +08:00
committed by GitHub
parent 15a76486ef
commit 1e481edc67
36 changed files with 340 additions and 202 deletions
+42
View File
@@ -0,0 +1,42 @@
const { Tray, Menu } = require('electron')
const { isMac, isWin } = require('../../common/utils')
const { tray: TRAY_EVENT_NAME } = require('../events/_name')
const path = require('path')
global.lx_event.tray.on(TRAY_EVENT_NAME.create, () => {
createTray()
})
global.lx_event.tray.on(TRAY_EVENT_NAME.destroy, () => {
destroyTray()
})
let tray
function createTray() {
if (tray && !tray.isDestroyed() && global.appSetting.tray && global.appSetting.tray.isShow) return
const iconPath = path.join(global.__static, 'images/tray', isWin ? '256x256.ico' : isMac ? '512x512.icns' : '512x512.png')
// 托盘
tray = new Tray(iconPath)
const contextMenu = Menu.buildFromTemplate([
{
label: '退出',
role: 'quit',
},
])
tray.setToolTip('洛雪音乐助手')
tray.setContextMenu(contextMenu)
tray.on('click', () => {
const mainWindow = global.mainWindow
if (!mainWindow) return
mainWindow.isVisible()
? mainWindow.focus()
: mainWindow.show()
})
}
function destroyTray() {
if (!tray) return
tray.destroy()
tray = null
}