feat: 优化项目结构

This commit is contained in:
DEV_DSW
2025-12-09 15:45:57 +08:00
parent dc4606a9ed
commit 06ad5ac4b0
10 changed files with 5 additions and 5 deletions

72
src/preload.ts Normal file
View File

@@ -0,0 +1,72 @@
import { contextBridge, ipcRenderer } from 'electron'
import { IPCChannel, IPCAPI } from '@/shared/types/ipc.types'
const api: IPCAPI = {
versions: process.versions,
external: {
open: (url: string) => ipcRenderer.invoke('external-open', url)
},
window: {
minimize: () => ipcRenderer.send('window-min'),
maximize: () => ipcRenderer.send('window-max'),
close: () => ipcRenderer.send('window-close')
},
app: {
setFrameless: (route?: string) => ipcRenderer.invoke('app:set-frameless', route)
},
tabs: {
create: (url?: string) => ipcRenderer.invoke('tab:create', url),
list: () => ipcRenderer.invoke('tab:list'),
navigate: (tabId: string, url: string) => ipcRenderer.invoke('tab:navigate', { tabId, url }),
reload: (tabId: string) => ipcRenderer.invoke('tab:reload', tabId),
back: (tabId: string) => ipcRenderer.invoke('tab:back', tabId),
forward: (tabId: string) => ipcRenderer.invoke('tab:forward', tabId),
switch: (tabId: string) => ipcRenderer.invoke('tab:switch', tabId),
close: (tabId: string) => ipcRenderer.invoke('tab:close', tabId),
on: (event: 'tab-updated' | 'tab-created' | 'tab-closed' | 'tab-switched', handler: (payload: any) => void) => {
const listener = (_e: any, payload: any) => handler(payload)
ipcRenderer.on(event, listener)
return () => ipcRenderer.removeListener(event, listener)
}
},
// 通过 IPC 调用主进程
readFile: (filePath: string) => ipcRenderer.invoke('read-file', filePath),
// 同步调用
invoke: (channel: IPCChannel, ...args: any[]) => ipcRenderer.sendSync('ipc:invoke', channel, ...args),
// 异步调用
invokeAsync: (channel: IPCChannel, ...args: any[]) => {
try {
ipcRenderer.invoke('ipc:invokeAsync', channel, ...args)
} catch (error) {
throw error
}
},
// 监听主进程消息
on: (event: IPCChannel, callback: (...args: any[]) => void) => {
const subscription = (_event: any, ...args: any[]) => callback(...args)
ipcRenderer.on(event, subscription)
return () => ipcRenderer.removeListener(event, subscription)
},
// 发送消息到主进程
send: (channel: IPCChannel, ...args: any[]) => ipcRenderer.send(channel, ...args),
// 获取窗口ID
getCurrentWindowId: () => ipcRenderer.sendSync(IPCChannel.GET_WINDOW_ID),
// 发送日志
logToMain: (logLevel: string, message: string) => ipcRenderer.send('log-to-main', logLevel, message),
// 打开新窗口
openNewTab: (url: string) => ipcRenderer.invoke('open-new-tab', url)
}
contextBridge.exposeInMainWorld('ipcAPI', api)