// main.js // 控制应用生命周期和创建原生浏览器窗口的模组 const { app, BrowserWindow, ipcMain, dialog, Menu } = require('electron') const path = require('path') var defaultOption = { width: 300, height: 500, resizable: false, } var BrowserWindowList = { LoginWindow: null, IndexWindow: null } var current = null function createWindow(url) { if (url.includes("login")) { current = "LoginWindow"; } else { current = "IndexWindow"; } BrowserWindowList[current] = new BrowserWindow({ ...defaultOption, webPreferences: { nodeIntegrationInWorker: true, nodeIntegration: true } }); // 加载 index.html BrowserWindowList[current].loadFile(path.join(__dirname, url)); if (current == "IndexWindow") { BrowserWindowList.LoginWindow.close(); BrowserWindowList.IndexWindow } // 打开开发工具 // BrowserWindowList[current].webContents.openDevTools() } var template = [ { label: '编辑', submenu: [ { label: '顶顶顶顶顶', accelerator: 'CmdOrCtrl+Z', role: 'undo' }, { label: 'Redo', accelerator: 'Shift+CmdOrCtrl+Z', role: 'redo' }, { type: 'separator' }, { label: 'Cut', accelerator: 'CmdOrCtrl+X', role: 'cut' }, { label: 'Copy', accelerator: 'CmdOrCtrl+C', role: 'copy' }, { label: 'Paste', accelerator: 'CmdOrCtrl+V', role: 'paste' }, { label: 'Select All', accelerator: 'CmdOrCtrl+A', role: 'selectall' }, ] }, { label: 'View', submenu: [ { label: 'Reload', accelerator: 'CmdOrCtrl+R', click: function (item, focusedWindow) { if (focusedWindow) focusedWindow.reload(); } }, { label: 'Toggle Full Screen', accelerator: (function () { if (process.platform == 'darwin') return 'Ctrl+Command+F'; else return 'F11'; })(), click: function (item, focusedWindow) { if (focusedWindow) focusedWindow.setFullScreen(!focusedWindow.isFullScreen()); } }, { label: 'Toggle Developer Tools', accelerator: (function () { if (process.platform == 'darwin') return 'Alt+Command+I'; else return 'Ctrl+Shift+I'; })(), click: function (item, focusedWindow) { if (focusedWindow) focusedWindow.toggleDevTools(); } }, ] }, { label: 'Window', role: 'window', submenu: [ { label: 'Minimize', accelerator: 'CmdOrCtrl+M', role: 'minimize' }, { label: 'Close', accelerator: 'CmdOrCtrl+W', role: 'close' }, ] }, { label: 'Help', role: 'help', submenu: [ { label: 'Learn More', click: function () { require('electron').shell.openExternal('http://electron.atom.io') } }, ] }, ]; var menu = Menu.buildFromTemplate(template); Menu.setApplicationMenu(menu); ipcMain.on("shake", function (event, arg) { var Position = BrowserWindowList[current].getPosition(); console.log("Position == ", Position); var newPosition = [ Position[0] + 30, Position[0] - 60, Position[0], Position[0] + 30, Position[0] - 60, Position[0], Position[0] + 30, Position[0] - 60, Position[0] ]; for (let i = 0; i < newPosition.length; i++) { setTimeout(function () { BrowserWindowList[current].setPosition( newPosition[i], Position[1], true ) }, 10 * i) } }) ipcMain.on("enterIndex", function (event, arg) { defaultOption.width = 800; defaultOption.height = 600; defaultOption.minWidth = 600; defaultOption.minHeight = 400; defaultOption.resizable = true; createWindow('./page/index.html') }) ipcMain.on("tc", function (event, arg) { console.log("arg: ", arg); dialog.showOpenDialog({ title: '哈哈哈哈', properties: ['openFile', 'multiSelections'] }) }) // 这段程序将会在 Electron 结束初始化 // 和创建浏览器窗口的时候调用 // 部分 API 在 ready 事件触发后才能使用。 app.whenReady().then(() => { createWindow('./page/login.html') app.on('activate', function () { // 通常在 macOS 上,当点击 dock 中的应用程序图标时,如果没有其他 // 打开的窗口,那么程序会重新创建一个窗口。 if (BrowserWindow.getAllWindows().length === 0) createWindow() }) }) // 除了 macOS 外,当所有窗口都被关闭的时候退出程序。 因此,通常对程序和它们在 // 任务栏上的图标来说,应当保持活跃状态,直到用户使用 Cmd + Q 退出。 app.on('window-all-closed', function () { if (process.platform !== 'darwin') app.quit() })