const {app, Menu, BrowserWindow,ipcMain,dialog,shell } = require('electron') const path = require('path') const {menuRebuild} = require('./elsrc/TopMenu.js') const UploadUtils = require("./elsrc/sync/tencent/UploadUtils") const Store = require('electron-store'); const store = new Store(); const uploadUtil = new UploadUtils(store); const isDev = require('electron-is-dev') const createWindow = () => { // Create the browser window. const win = new BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: true, // 启用后nodeIntegration会无效 contextIsolation: false, // 添加预加载脚本 // __dirname 字符串指向当前正在执行脚本的路径 // path.join API 将多个路径联结在一起,创建一个跨平台的路径字符串。 preload: path.join(__dirname, 'preload.js') } }) console.log("isDev", isDev) if (isDev){ // 加载 index.html win.loadURL('http://localhost:3008') // 打开开发工具 win.webContents.openDevTools() }else { // 加载 index.html win.loadFile('./index.html') } // let okPush =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.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,uploadUtil))) win.once('ready-to-show', () => { win.show() }) return win } // 这段程序将会在 Electron 结束初始化和创建浏览器窗口的时候调用 // 部分 API 在 ready 事件触发后才能使用。 app.whenReady().then(() => { let browserWindow = createWindow(); // 在 macOS 系统内, 如果没有已开启的应用窗口 // 点击托盘图标时通常会重新创建一个新窗口 app.on('activate', () => { if (BrowserWindow.getAllWindows().length === 0) createWindow() // 同步数据 }) uploadUtil.syncFile() 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:",args) return store.get(args) }) ipcMain.handle("electronStoreDelete",(listen,args)=>{ console.log("electronStoreDelete") return store.delete(args) }) ipcMain.handle("showOpenDialog",(listen,args)=>{ console.log("showOpenDialog:"+args) // return dialog.showOpenDialog({defaultPath:args.filePath, // properties: ['openFile']} ) return shell.openPath(args) }) console.log("配置文件地址",app.getPath('userData')+"/config.json") console.log("操作系统:"+process.platform) }) // 除了 macOS 外,当所有窗口都被关闭的时候退出程序。 因此, 通常 // 对应用程序和它们的菜单栏来说应该时刻保持激活状态, // 直到用户使用 Cmd + Q 明确退出 app.on('window-all-closed', () => { if (process.platform !== 'darwin') app.quit() })