assistant-note/main.js

71 lines
2.6 KiB
JavaScript
Raw Normal View History

2024-01-31 21:36:30 -05:00
const {app, Menu, BrowserWindow} = require('electron')
2023-11-01 22:15:06 -04:00
const path = require('path')
const {menuRebuild} = require('./src/comment/TopMenu.js')
2023-01-30 03:15:29 -05:00
const createWindow = () => {
2023-11-01 22:31:23 -04:00
// Create the browser window.
2023-01-30 03:15:29 -05:00
const win = new BrowserWindow({
width: 800,
2023-11-01 22:15:06 -04:00
height: 600,
webPreferences: {
2024-01-31 21:36:30 -05:00
nodeIntegration: true,
2024-01-24 01:50:27 -05:00
// 启用后nodeIntegration会无效
2024-01-31 21:36:30 -05:00
contextIsolation: false,
2023-11-01 22:15:06 -04:00
preload: path.join(__dirname, 'preload.js')
2023-11-01 22:31:23 -04:00
}
2023-01-30 03:15:29 -05:00
})
2023-11-01 22:31:23 -04:00
// 加载 index.html
2024-01-12 00:13:58 -05:00
// win.loadFile('./public/index.html')
win.loadURL('http://localhost:3000')
2023-11-01 22:31:23 -04:00
// 打开开发工具
2024-01-02 03:15:30 -05:00
win.webContents.openDevTools()
2024-01-31 21:36:30 -05:00
// let okPush =false
let devToolPush = false
2024-01-26 02:41:22 -05:00
win.webContents.on('before-input-event', (event, input) => {
2024-01-31 21:36:30 -05:00
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()
2024-01-27 05:56:17 -05:00
}
2024-01-31 21:36:30 -05:00
// console.log('Pressed ',input.key)
if (input.key.toUpperCase() === 'F5') {
2024-01-26 02:41:22 -05:00
win.reload()
}
2024-01-31 21:36:30 -05:00
if (input.key.toUpperCase() === 'F12') {
if (input.type.toLowerCase() === "keydown") {
devToolPush = true;
} else {
devToolPush = false;
}
if (win.webContents.isDevToolsOpened() && !devToolPush) {
2024-01-27 05:56:17 -05:00
win.webContents.closeDevTools()
2024-01-31 21:36:30 -05:00
}
if (!win.webContents.isDevToolsOpened() && !devToolPush) {
2024-01-27 05:56:17 -05:00
win.webContents.openDevTools()
}
}
2024-01-26 02:41:22 -05:00
})
Menu.setApplicationMenu(Menu.buildFromTemplate(menuRebuild(win)))
2023-01-30 03:15:29 -05:00
}
// 这段程序将会在 Electron 结束初始化和创建浏览器窗口的时候调用
2023-11-01 22:31:23 -04:00
// 部分 API 在 ready 事件触发后才能使用。
2023-01-30 03:15:29 -05:00
app.whenReady().then(() => {
createWindow()
2023-11-01 22:31:23 -04:00
// 在 macOS 系统内, 如果没有已开启的应用窗口
// 点击托盘图标时通常会重新创建一个新窗口
2023-11-01 22:15:06 -04:00
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
2024-01-31 21:36:30 -05:00
})
2023-11-01 22:14:40 -04:00
})
2023-11-01 22:31:23 -04:00
// 除了 macOS 外,当所有窗口都被关闭的时候退出程序。 因此, 通常
// 对应用程序和它们的菜单栏来说应该时刻保持激活状态,
// 直到用户使用 Cmd + Q 明确退出
2023-11-01 22:14:40 -04:00
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit()
2024-01-02 03:15:30 -05:00
})