eme_small_desk/electron/main.js

142 lines
4.2 KiB
JavaScript

const { app, BrowserWindow, Menu, shell } = require('electron')
const path = require('path')
const isDev = process.env.NODE_ENV === 'development'
// 保持对窗口对象的全局引用
let mainWindow
function createWindow() {
// 创建浏览器窗口
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
minWidth: 800,
minHeight: 600,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
enableRemoteModule: false,
webSecurity: true,
},
icon: path.join(__dirname, '../public/favicon.ico'),
show: false, // 先不显示,等加载完成后再显示
titleBarStyle: 'default',
})
// 加载应用
if (isDev) {
// 开发环境:加载本地开发服务器
mainWindow.loadURL('http://localhost:8080')
// 打开开发者工具
mainWindow.webContents.openDevTools()
} else {
// 生产环境:加载打包后的文件
mainWindow.loadFile(path.join(__dirname, '../dist/index.html'))
}
// 窗口准备好后显示
mainWindow.once('ready-to-show', () => {
mainWindow.show()
})
// 当窗口被关闭时触发
mainWindow.on('closed', () => {
mainWindow = null
})
// 处理外部链接
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
shell.openExternal(url)
return { action: 'deny' }
})
// 设置菜单
createMenu()
}
// 创建应用菜单
function createMenu() {
const template = [
{
label: '文件',
submenu: [
{
label: '退出',
accelerator: process.platform === 'darwin' ? 'Cmd+Q' : 'Ctrl+Q',
click: () => {
app.quit()
},
},
],
},
{
label: '编辑',
submenu: [
{ role: 'undo', label: '撤销' },
{ role: 'redo', label: '重做' },
{ type: 'separator' },
{ role: 'cut', label: '剪切' },
{ role: 'copy', label: '复制' },
{ role: 'paste', label: '粘贴' },
],
},
{
label: '视图',
submenu: [
{ role: 'reload', label: '重新加载' },
{ role: 'forceReload', label: '强制重新加载' },
{ role: 'toggleDevTools', label: '开发者工具' },
{ type: 'separator' },
{ role: 'resetZoom', label: '实际大小' },
{ role: 'zoomIn', label: '放大' },
{ role: 'zoomOut', label: '缩小' },
{ type: 'separator' },
{ role: 'togglefullscreen', label: '全屏' },
],
},
{
label: '窗口',
submenu: [
{ role: 'minimize', label: '最小化' },
{ role: 'close', label: '关闭' },
],
},
{
label: '帮助',
submenu: [
{
label: '关于',
click: () => {
// 可以在这里显示关于对话框
console.log('关于流程自动化机器人')
},
},
],
},
]
const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)
}
// 当 Electron 完成初始化并准备创建浏览器窗口时调用此方法
app.whenReady().then(createWindow)
// 当所有窗口都被关闭时退出应用
app.on('window-all-closed', () => {
// 在 macOS 上,应用和菜单栏通常会保持活跃状态,直到用户使用 Cmd + Q 明确退出
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
// 在 macOS 上,当单击 dock 图标并且没有其他窗口打开时,通常在应用程序中重新创建窗口
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
// 在这个文件中,你可以包含应用程序的其余特定主进程代码
// 也可以将它们放在单独的文件中并在此处导入