1.首先判断是什么系统:
const isMac = process.platform === "darwin"
//判断是否是mac系统
2.然后执行操作:mac系统的退出是app.close() ,win系统的退出是app.quit()
isMac?app.close(): app.quit()
3.监听操作:(在主进程的createWindow内监听)
const createWindow = () => {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
icon: './src/static/image/maoniu-logo.png',
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
// 集成 node
nodeIntegration: true,
// 关闭上下文隔离,兼容 require 引入
contextIsolation: false,
}
});
// and load the index.html of the app.
mainWindow.loadFile(path.join(__dirname, 'index.html'));
// 启用remote模块
remote.enable(mainWindow.webContents)
require('./main/ipcMain.js')
require('./main/menu.js')
mainWindow.on('close', (event) => {
const choice = dialog.showMessageBoxSync({
type: "question",
message: "确定退出吗?",
buttons: ['确定', '取消', '最小化到任务栏'],
detail: '再玩一会吧',
defaultId: 2,
cancelId: 1,
icon: path.join(__dirname, './static/image/maoniu-logo.png')
})
// 定义取消关闭的值
const isCancel = (choice === 1)
// 最小化到任务栏
if(choice===2){
mainWindow.hide()
event.preventDefault()//取消事件的默认动作
}
// 取消关闭
if (isCancel) {
event.preventDefault()//取消事件的默认动作
}
if (choice===0) {
app.exit();
event.preventDefault()//取消事件的默认动作
}
});
// Open the DevTools.
// mainWindow.webContents.openDevTools();
};