Files
2024-09-27 02:06:13 +08:00

129 lines
3.2 KiB
JavaScript

// main.js
// electron 模块可以用来控制应用的生命周期和创建原生浏览窗口
const { app, BrowserWindow, Menu, ipcMain, dialog } = require('electron')
const { url } = require('inspector')
const path = require('path')
let windowConfig = [
{
windowObj: null,
url: './page/logein.html',
config: {
width: 800, //251,
height: 800, //320,
resizable: false,
webPreferences: {
nodeIntegration: true,
nodeIntegrationInWorker: true,
contextIsolation: false
}
}
},
{
windowObj: null,
url: './page/index.html',
config: {
title: '',
minHeight: 590,
minWidth: 710,
width: 900,
height: 650,
titleBarStyle: 'hidden',
titleBarOverlay: true,
webPreferences: {
nodeIntegration: true,
nodeIntegrationInWorker: true,
contextIsolation: false
}
}
}
]
let sakePosition = []
let index = 0
const createWindow = () => {
// 创建浏览窗口
//windowConfig[index].windowObj
windowConfig[index].windowObj = new BrowserWindow(windowConfig[index].config)
// 加载 index.html
windowConfig[index].windowObj.loadFile(windowConfig[index].url)
console.log(windowConfig[index].url)
// 打开开发工具
windowConfig[index].windowObj.webContents.openDevTools()
}
// 这段程序将会在 Electron 结束初始化
// 和创建浏览器窗口的时候调用
// 部分 API 在 ready 事件触发后才能使用。
app.whenReady().then(() => {
createWindow()
app.on('activate', () => {
// 在 macOS 系统内, 如果没有已开启的应用窗口
// 点击托盘图标时通常会重新创建一个新窗口
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
// 除了 macOS 外,当所有窗口都被关闭的时候退出程序。 因此, 通常
// 对应用程序和它们的菜单栏来说应该时刻保持激活状态,
// 直到用户使用 Cmd + Q 明确退出
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit()
})
// 在当前文件中你可以引入所有的主进程代码
// 也可以拆分成几个文件,然后用 require 导入。
//去除菜单
const menu = Menu.buildFromTemplate([])
Menu.setApplicationMenu(menu)
//登录点击触发 监听请求
ipcMain.on('set-title', (event, title) => {
windowConfig[index].windowObj.close()
index = 1
createWindow()
})
//图片
ipcMain.handle('openFileSelect', async (event) => {
const { canceled, filePaths } = await dialog.showOpenDialog({
filters: [
{ name: 'Images', extensions: ['jpg', 'png', 'gif', 'jpeg'] },
]
})
if (!canceled) {
return filePaths[0]
}
})
//窗口抖动
ipcMain.on("windowsShake", () => {
let position = windowConfig[index].windowObj.getPosition();
let shakePosition = [
position[0] - 30,
position[0],
position[0] + 30,
position[0] - 30,
position[0],
position[0] + 30,
position[0] - 30,
position[0],
position[0] + 30,
position[0],
];
let i = 0;
let shakeInterval = setInterval(() => {
if (i < shakePosition.length) {
windowConfig[index].windowObj.setPosition(shakePosition[i], position[1])
i += 1
} else {
clearInterval(shakeInterval)
}
}, 10)
})