117 lines
2.6 KiB
JavaScript
117 lines
2.6 KiB
JavaScript
// electron 模块可以用来控制应用的生命周期和创建原生浏览窗口
|
|
const { app, BrowserWindow, Menu, ipcMain, dialog} = require('electron')
|
|
|
|
let windowConfig = [
|
|
{
|
|
windowObj: null,
|
|
url: './page/login.html',
|
|
config: {
|
|
title: '',
|
|
width: 251,
|
|
height: 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 index = 0
|
|
|
|
const createWindow = () => {
|
|
// 创建浏览窗口
|
|
windowConfig[index].windowObj = new BrowserWindow(windowConfig[index].config)
|
|
|
|
// 加载 index.html
|
|
windowConfig[index].windowObj.loadFile(windowConfig[index].url)
|
|
|
|
// 打开开发工具
|
|
windowConfig[index].windowObj.webContents.openDevTools()
|
|
}
|
|
|
|
ipcMain.on('openIndex', (event) => {
|
|
windowConfig[index].windowObj.close()
|
|
index = 1
|
|
createWindow()
|
|
})
|
|
|
|
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)
|
|
})
|
|
|
|
|
|
|
|
ipcMain.handle('openFileSelect', async (event) => {
|
|
const { canceled, filePaths } = await dialog.showOpenDialog({
|
|
filters: [
|
|
{ name: 'Images', extensions: ['jpg', 'png', 'gif', 'jpeg'] },
|
|
]
|
|
})
|
|
if (!canceled) {
|
|
return filePaths[0]
|
|
}
|
|
})
|
|
|
|
|
|
|
|
// 这段程序将会在 Electron 结束初始化
|
|
// 和创建浏览器窗口的时候调用
|
|
// 部分 API 在 ready 事件触发后才能使用。
|
|
app.whenReady().then(() => {
|
|
createWindow()
|
|
})
|
|
|
|
// 除了 macOS 外,当所有窗口都被关闭的时候退出程序。 因此, 通常
|
|
// 对应用程序和它们的菜单栏来说应该时刻保持激活状态,
|
|
// 直到用户使用 Cmd + Q 明确退出
|
|
app.on('window-all-closed', () => {
|
|
if (process.platform !== 'darwin') app.quit()
|
|
})
|
|
|
|
|
|
const menu = Menu.buildFromTemplate([])
|
|
Menu.setApplicationMenu(menu) |