134 lines
3.3 KiB
JavaScript
134 lines
3.3 KiB
JavaScript
|
||
// 用于app控制应用程序寿命和BrowserWindow创建本机浏览器窗口的模块 引入electron
|
||
const { app, BrowserWindow, ipcMain, Menu } = require('electron')
|
||
const path = require('path')
|
||
var mainWindow = null
|
||
var ifclick = true
|
||
|
||
//定义一个数组 保存两个跳转页面的配置
|
||
var pageConfig = [
|
||
{
|
||
|
||
path: "./login.html",
|
||
config: {
|
||
title: "登录",
|
||
width: 300,
|
||
height: 400,
|
||
// resizable: false,
|
||
//网页功能设置
|
||
webPreferences: {
|
||
//是否启用node.js
|
||
nodeIntegration: true,
|
||
|
||
//是否在web工作器中启用node集成
|
||
nodeIntegrationInWorker: true,
|
||
|
||
//是否独立js环境中运行electron api和指定的preload脚本
|
||
contextIsolation: false
|
||
}
|
||
},
|
||
},
|
||
{
|
||
path: "./index.html",
|
||
config: {
|
||
title: "首页",
|
||
width: 800,
|
||
height: 600,
|
||
webPreferences: {
|
||
//是否启用node.js
|
||
nodeIntegration: true,
|
||
|
||
//是否在web工作器中启用node集成
|
||
nodeIntegrationInWorker: true,
|
||
|
||
//是否独立js环境中运行electron api和指定的preload脚本
|
||
contextIsolation: false
|
||
}
|
||
}
|
||
}
|
||
]
|
||
//数组 + 下标 取值,所以定义下标
|
||
var pageIndex = 0
|
||
const createWindow = () => {
|
||
//创建浏览器窗口
|
||
mainWindow = new BrowserWindow(pageConfig[pageIndex].config)
|
||
|
||
//loadFile加载本地html
|
||
mainWindow.loadFile(pageConfig[pageIndex].path)
|
||
|
||
//取消默认菜单
|
||
Menu.setApplicationMenu(null)
|
||
//开发者工具打开
|
||
mainWindow.webContents.openDevTools()
|
||
}
|
||
|
||
//主进程监听
|
||
ipcMain.on("openIndex", (event, arg) => {
|
||
console.log("主进程收到的消息:", arg);
|
||
pageIndex = arg;
|
||
//关闭登录窗口
|
||
mainWindow.close();
|
||
//创建新窗口tall
|
||
createWindow()
|
||
})
|
||
|
||
|
||
// 主进程收到数据并回应,渲染进程接收回应 渲染进程向主进程通信,主进程主动和渲染进程通信
|
||
//【ipcMain主进程监听】
|
||
ipcMain.on("startShake", (event, arg) => {
|
||
// console.log('ifclick',ifclick);
|
||
// if(!ifclick) return;
|
||
//获取当前窗口位置(x,y)
|
||
var position = mainWindow.getPosition();
|
||
//设置来回抖动位置
|
||
var newPosition = [
|
||
position[0] - 30,
|
||
position[0] + 30,
|
||
position[0]
|
||
]
|
||
// ifclick = false
|
||
//for循环执行抖动(定时器 时间 position[1])
|
||
for (let i = 0; i < newPosition.length; i++) {
|
||
console.log('newPosition[i]',newPosition[i]);
|
||
setTimeout(() => {
|
||
console.log('i',i);
|
||
mainWindow.setPosition(
|
||
newPosition[i],
|
||
position[1]
|
||
)
|
||
// if(i == newPosition.length - 1){
|
||
// ifclick = true
|
||
// }
|
||
}, 30 * i)
|
||
}
|
||
|
||
|
||
// newPosition.forEach((v, i) => {
|
||
// mainWindow.setPosition(
|
||
// v,
|
||
// position[1]
|
||
// )
|
||
// })
|
||
|
||
|
||
|
||
})
|
||
|
||
|
||
//当app准备好的时候,调用创建浏览器方法
|
||
app.whenReady().then(() => {
|
||
createWindow()
|
||
|
||
//有窗口则监听窗口,没有则创建窗口
|
||
app.on('activate', () => {
|
||
//在macOS上,在应用程序中重新创建窗口是很常见的
|
||
//单击dock图标,没有其他窗口打开。
|
||
if (BrowserWindow.getAllWindows().length === 0) createWindow()
|
||
})
|
||
})
|
||
|
||
//使用Cmd+Q显式。
|
||
//当窗口关闭的时候,调用quit()退出离开(macOS上除外)
|
||
app.on('window-all-closed', () => {
|
||
if (process.platform !== 'darwin') app.quit()
|
||
}) |