增加“关闭时最小化到托盘”的设置 (#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
+43
View File
@@ -0,0 +1,43 @@
const { app, Menu } = require('electron')
const { isMac } = require('../../common/utils')
if (isMac) {
const template = [
{
label: app.getName(),
submenu: [
{ label: '关于洛雪音乐', role: 'about' },
{ type: 'separator' },
{ label: '隐藏', role: 'hide' },
{ label: '显示其他', role: 'hideothers' },
{ label: '显示全部', role: 'unhide' },
{ type: 'separator' },
{ label: '退出', accelerator: 'Command+Q', click: () => app.quit() },
],
},
{
label: '窗口',
role: 'window',
submenu: [
{ label: '最小化', role: 'minimize' },
{ label: '关闭', role: 'close' },
],
},
{
label: '编辑',
submenu: [
{ label: '撤销', accelerator: 'CmdOrCtrl+Z', role: 'undo' },
{ label: '恢复', accelerator: 'Shift+CmdOrCtrl+Z', role: 'redo' },
{ type: 'separator' },
{ label: '剪切', accelerator: 'CmdOrCtrl+X', role: 'cut' },
{ label: '复制', accelerator: 'CmdOrCtrl+C', role: 'copy' },
{ label: '粘贴', accelerator: 'CmdOrCtrl+V', role: 'paste' },
{ label: '选择全部', accelerator: 'CmdOrCtrl+A', role: 'selectAll' },
],
},
]
Menu.setApplicationMenu(Menu.buildFromTemplate(template))
} else {
Menu.setApplicationMenu(null)
}
+2
View File
@@ -0,0 +1,2 @@
require('./appMenu')
require('./tray')
+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
}