assistant-note/main.js

63 lines
2.2 KiB
JavaScript

// npm start
const { app,Menu, BrowserWindow } = require('electron')
const path = require('path')
const {menuRebuild} = require('./src/comment/TopMenu.js')
// const {SAVE} = require("./src/utils/HotkeyConst");
// const {PUSH_HOTKEY} = require("./src/utils/MainContentsChannel");
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()
win.webContents.on('before-input-event', (event, input) => {
if (input.control && input.key.toLowerCase() === 's') {
console.log("win.webContents.send(\"pushHotkeys\",\"CTRL+S\")")
win.webContents.send("pushHotkeys","CTRL+S")
// event.preventDefault()
}
console.log('Pressed ',input.key)
if (input.key.toUpperCase()==='F5'){
win.reload()
}
if (input.key.toUpperCase()==='F12'){
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()
})
})
// 除了 macOS 外,当所有窗口都被关闭的时候退出程序。 因此, 通常
// 对应用程序和它们的菜单栏来说应该时刻保持激活状态,
// 直到用户使用 Cmd + Q 明确退出
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit()
})