const { app, BrowserWindow, Menu, Tray } = require('electron')
const path = require('path')
let tray = null
function createWindow() {
const win = new BrowserWindow({
width: 1200,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true, // 使用remote模块
preload: path.join(__dirname, 'preload.js')
}
})
win.loadFile('index.html')
win.webContents.openDevTools();
// 引入的自定义菜单
tray = new Tray('./static/Clover.png')
const contextMenu = Menu.buildFromTemplate([
{
label: '最大化', click: function () {
win.maximize()
}
},
{
label: '最小化', click: function () {
win.minimize()
}
},
{
label: '退出', click: function () {
// win.destroy()
app.quit()
}
}
])
tray.setToolTip('晓果electorn')
tray.setContextMenu(contextMenu)
// 点击托盘图标,显示主窗口
tray.on("click", () => {
win.show();
})
//双击打开应用
tray.on('double-click', () => {
win.show()
})
//实现点击关闭按钮 让应用保存在托盘里面,双击托盘打开应用
win.on('close', (e) => {
console.log('close--------')
if (win.isMinimized()) {
win = null;
} else {
e.preventDefault();
win.minimize();
}
});
}
app.whenReady().then(() => {
createWindow()
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})