const {app, Menu, BrowserWindow,ipcMain,dialog} = require('electron') const path = require('path') const {menuRebuild} = require('./src/comment/TopMenu.js') const Store = require('electron-store'); const yaml = require('js-yaml'); const store = new Store(); const createWindow = () => { // Create the browser window. const win = new BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: true, // 启用后nodeIntegration会无效 contextIsolation: false, preload: path.join(__dirname, 'preload.js') } }) // 加载 index.html // win.loadFile('./public/index.html') win.loadURL('http://localhost:3000') // 打开开发工具 win.webContents.openDevTools() // let okPush =false let devToolPush = false win.webContents.on('before-input-event', (event, input) => { console.log("input.type.toLowerCase()", input) if (input.control && input.key.toLowerCase() === 's' && input.type.toLowerCase() === "keydown" && input.isAutoRepeat === false) { // if (input.type.toLowerCase()==="keydown"&& !okPush){ win.webContents.send("pushHotkeys", "CTRL+S") console.log("pushHotkeys", "CTRL+S", input.type) // } event.preventDefault() } // console.log('Pressed ',input.key) if (input.key.toUpperCase() === 'F5' && input.type.toLowerCase() === "keydown" && input.isAutoRepeat === false) { win.reload() } if (input.key.toUpperCase() === 'F12' && input.type.toLowerCase() === "keydown" && input.isAutoRepeat === false) { if (win.webContents.isDevToolsOpened()) { win.webContents.closeDevTools() }else { win.webContents.openDevTools() } } }) Menu.setApplicationMenu(Menu.buildFromTemplate(menuRebuild(win))) } // 这段程序将会在 Electron 结束初始化和创建浏览器窗口的时候调用 // 部分 API 在 ready 事件触发后才能使用。 app.whenReady().then(() => { createWindow() // 在 macOS 系统内, 如果没有已开启的应用窗口 // 点击托盘图标时通常会重新创建一个新窗口 app.on('activate', () => { if (BrowserWindow.getAllWindows().length === 0) createWindow() }) ipcMain.handle("saveFileWithName",(listen,args)=>{ console.log("saveFileWithName") return dialog.showSaveDialog({"title":"保存文件",}) }) ipcMain.handle("electronStoreSave",(listen,key,value)=>{ console.log("electronStoreSave",key,value) if (store.get(key)===value){ return ; } console.log("electronStoreSaveDisk",key,value) return store.set(key,value) }) ipcMain.handle("electronStoreGet",(listen,args)=>{ console.log("electronStoreGet:",store.get(args)) return store.get(args) }) ipcMain.handle("electronStoreDelete",(listen,args)=>{ console.log("electronStoreDelete") return store.delete(args) }) console.log("配置文件地址",app.getPath('userData')+"/config.json") }) // 除了 macOS 外,当所有窗口都被关闭的时候退出程序。 因此, 通常 // 对应用程序和它们的菜单栏来说应该时刻保持激活状态, // 直到用户使用 Cmd + Q 明确退出 app.on('window-all-closed', () => { if (process.platform !== 'darwin') app.quit() })