From 4466713d1a32209143bd7f69473bd416344b513f Mon Sep 17 00:00:00 2001 From: alger Date: Thu, 21 Dec 2023 16:43:51 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9E=20fix(app):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E6=89=98=E7=9B=98=E9=80=8F=E6=98=8E=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 55 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/app.js b/app.js index f64f2f8..a8de65a 100644 --- a/app.js +++ b/app.js @@ -1,4 +1,4 @@ -const { app, BrowserWindow, ipcMain, Tray, Menu, globalShortcut } = require('electron') +const { app, BrowserWindow, ipcMain, Tray, Menu, globalShortcut, nativeImage } = require('electron') const path = require('path') let mainWin = null @@ -20,31 +20,36 @@ function createWindow() { } else { win.loadURL(`file://${__dirname}/dist/index.html`) } - const tray = new Tray(path.join(__dirname, 'public/icon.png')) - tray.setTitle('Alger Music') - tray.setToolTip('Alger Music') - tray.setContextMenu( - Menu.buildFromTemplate([ - { - label: '显示', - click: () => { - win.show() - }, - }, - { - label: '退出', - click: () => { - win.destroy() - }, - }, - ]) - ) - tray.click = () => { - win.show() - } + const image = nativeImage.createFromPath(path.join(__dirname, 'public/icon.png')) + const tray = new Tray(image) - // 快捷键显示 隐藏 - + // 创建一个上下文菜单 + const contextMenu = Menu.buildFromTemplate([ + { + label: '显示', + click: () => { + win.show() + }, + }, + { + label: '退出', + click: () => { + win.destroy() + }, + }, + ]) + + // 设置系统托盘图标的上下文菜单 + tray.setContextMenu(contextMenu) + + // 当系统托盘图标被点击时,切换窗口的显示/隐藏 + tray.on('click', () => { + if (win.isVisible()) { + win.hide() + } else { + win.show() + } + }) } app.whenReady().then(createWindow)