75 lines
2.8 KiB
JavaScript
75 lines
2.8 KiB
JavaScript
const {app, Menu, BrowserWindow,ipcMain,dialog} = require('electron')
|
|
const path = require('path')
|
|
const {menuRebuild} = require('./src/comment/TopMenu.js')
|
|
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') {
|
|
win.reload()
|
|
}
|
|
if (input.key.toUpperCase() === 'F12') {
|
|
if (input.type.toLowerCase() === "keydown") {
|
|
devToolPush = true;
|
|
} else {
|
|
devToolPush = false;
|
|
}
|
|
if (win.webContents.isDevToolsOpened() && !devToolPush) {
|
|
win.webContents.closeDevTools()
|
|
}
|
|
if (!win.webContents.isDevToolsOpened() && !devToolPush) {
|
|
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":"保存文件",})
|
|
})
|
|
})
|
|
// 除了 macOS 外,当所有窗口都被关闭的时候退出程序。 因此, 通常
|
|
// 对应用程序和它们的菜单栏来说应该时刻保持激活状态,
|
|
// 直到用户使用 Cmd + Q 明确退出
|
|
app.on('window-all-closed', () => {
|
|
if (process.platform !== 'darwin') app.quit()
|
|
})
|
|
|