- Reorganize project structure with new electron and shared directories - Add comprehensive i18n support with Chinese, English, and Japanese locales - Update build configurations and TypeScript paths for new structure - Add various UI components including chat interface and task management - Include Windows release binaries and localization files - Update dependencies and fix import paths throughout the codebase
96 lines
2.5 KiB
TypeScript
96 lines
2.5 KiB
TypeScript
import { ipcMain, BaseWindow } from 'electron'
|
|
|
|
type Handler = (...args: any[]) => any
|
|
type AsyncHandler = (...args: any[]) => Promise<any>
|
|
|
|
export class IPCManager {
|
|
private static instance: IPCManager
|
|
private handlers: Map<string, Handler>
|
|
private asyncHandlers: Map<string, AsyncHandler>
|
|
private constructor () {
|
|
this.handlers = new Map()
|
|
this.asyncHandlers = new Map()
|
|
this.initialize()
|
|
}
|
|
|
|
public static getInstance(): IPCManager {
|
|
if (!IPCManager.instance) {
|
|
IPCManager.instance =new IPCManager()
|
|
}
|
|
|
|
return IPCManager.instance
|
|
}
|
|
|
|
private initialize(): void {
|
|
//注册同步处理器
|
|
ipcMain.on('ipc:invoke',(event, channel,...args) => {
|
|
try {
|
|
const handler = this.handlers.get(channel)
|
|
|
|
if (handler) {
|
|
event.returnValue = handler(...args)
|
|
} else {
|
|
event.returnValue = { success: false, error: `No handler for channel: ${channel}`}
|
|
}
|
|
} catch (error) {
|
|
event.returnValue = { success: false, error:(error as Error).message}
|
|
}
|
|
})
|
|
|
|
// 注册异步处理器
|
|
ipcMain.handle('ipc:invokeAsync', async (_event, channel, ...args) => {
|
|
try {
|
|
const handler = this.asyncHandlers.get(channel)
|
|
|
|
if (handler) {
|
|
return await handler(...args)
|
|
}
|
|
|
|
// throw new Error(`No async handler for channel: ${channel}`)
|
|
} catch (error) {
|
|
// throw error
|
|
}
|
|
})
|
|
|
|
ipcMain.handle('get-window-id',(event: any) => {
|
|
event.returnValue = event.sender.id
|
|
})
|
|
}
|
|
|
|
// 注册同步处理器
|
|
public register(channel:string, handler:Handler):void {
|
|
this.handlers.set(channel, handler)
|
|
}
|
|
|
|
// 注册异步处理器
|
|
public registerAsync(channel:string, handler:AsyncHandler):void {
|
|
this.asyncHandlers.set(channel, handler)
|
|
}
|
|
|
|
// 广播消息给所有窗口
|
|
public broadcast(channel:string, ...args:any[]):void {
|
|
BaseWindow.getAllWindows().forEach(window => {
|
|
if (!window.isDestroyed()) {
|
|
(window as any).webContents.send(channel, ...args)
|
|
}
|
|
})
|
|
}
|
|
|
|
// 发送消息给指定窗口
|
|
public sendToWindow(windowId: number, channel:string, ...args:any[]):void {
|
|
const window = BaseWindow.fromId(windowId)
|
|
|
|
if (window && !window.isDestroyed()) {
|
|
(window as any).webContents.send(channel, ...args)
|
|
}
|
|
}
|
|
|
|
// 清理所有处理器
|
|
public clear():void {
|
|
this.handlers.clear()
|
|
this.asyncHandlers.clear()
|
|
}
|
|
}
|
|
|
|
export const ipcManager = IPCManager.getInstance()
|