更新(ui): 添加IPC通信和快捷键注册功能

 更新(ui): 创建窗口函数现在返回BrowserWindow对象
This commit is contained in:
yuruo
2024-06-07 08:59:07 +08:00
parent f4cffb65f8
commit 6601adb155
5 changed files with 29 additions and 6 deletions

View File

@@ -1,9 +1,13 @@
import { app } from 'electron'
import { createWindow } from './window'
import * as ipc from './ipc'
import { registerShortCut } from './shortcut'
app.whenReady().then(() => {
createWindow()
const window = createWindow()
ipc.registerIpc(window)
registerShortCut(window)
})

View File

@@ -0,0 +1,19 @@
import { BrowserWindow } from "electron"
const { app, globalShortcut } = require('electron')
export const registerShortCut = (win: BrowserWindow) => {
app.whenReady().then(() => {
// Register a 'CommandOrControl+X' shortcut listener.
const ret = globalShortcut.register('CommandOrControl+X', () => {
win.show()
})
if (!ret) {
console.log('registration failed')
}
})
app.on('will-quit', () => {
// Unregister all shortcuts.
globalShortcut.unregisterAll()
})}

View File

@@ -2,10 +2,9 @@
import { BrowserWindow, shell } from 'electron'
import { is } from '@electron-toolkit/utils'
import icon from '../../../resources/icon.png?asset'
import * as ipc from '../ipc'
import { join } from 'path'
export function createWindow(): void { // Create the browser window.
export function createWindow(): BrowserWindow { // Create the browser window.
const mainWindow = new BrowserWindow({
width: 600,
height: 350,
@@ -20,7 +19,6 @@ export function createWindow(): void { // Create the browser window.
sandbox: false
}
})
ipc.registerIpc(mainWindow)
mainWindow.webContents.openDevTools()
mainWindow.on('ready-to-show', () => {
@@ -39,5 +37,7 @@ export function createWindow(): void { // Create the browser window.
} else {
mainWindow.loadFile(join(__dirname, '../renderer/index.html'))
}
return mainWindow
}

View File

@@ -1,11 +1,11 @@
import { app, BrowserWindow, ipcMain } from 'electron'
import { electronApp, optimizer } from '@electron-toolkit/utils'
import { createWindow } from './code/window'
import "./code"
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
// Set app user model id for windows
electronApp.setAppUserModelId('com.electron')