76 lines
3.3 KiB
TypeScript
76 lines
3.3 KiB
TypeScript
import { contextBridge, ipcRenderer } from 'electron'
|
|
import { IPC_EVENTS } from '@common/constants';
|
|
|
|
const api: WindowApi = {
|
|
versions: process.versions,
|
|
|
|
external: {
|
|
open: (url: string) => ipcRenderer.invoke('external-open', url)
|
|
},
|
|
|
|
closeWindow: () => ipcRenderer.send(IPC_EVENTS.WINDOW_CLOSE),
|
|
minimizeWindow: () => ipcRenderer.send(IPC_EVENTS.WINDOW_MINIMIZE),
|
|
maximizeWindow: () => ipcRenderer.send(IPC_EVENTS.WINDOW_MAXIMIZE),
|
|
onWindowMaximized: (callback: (isMaximized: boolean) => void) => ipcRenderer.on(IPC_EVENTS.WINDOW_MAXIMIZE + 'back', (_, isMaximized) => callback(isMaximized)),
|
|
isWindowMaximized: () => ipcRenderer.invoke(IPC_EVENTS.IS_WINDOW_MAXIMIZED),
|
|
viewIsReady: () => ipcRenderer.send(IPC_EVENTS.RENDERER_IS_READY),
|
|
|
|
app: {
|
|
setFrameless: (route?: string) => ipcRenderer.invoke(IPC_EVENTS.APP_SET_FRAMELESS, route),
|
|
loadPage: (page: string) => ipcRenderer.invoke(IPC_EVENTS.APP_LOAD_PAGE, page)
|
|
},
|
|
|
|
tabs: {
|
|
create: (url?: string) => ipcRenderer.invoke(IPC_EVENTS.TAB_CREATE, url),
|
|
list: () => ipcRenderer.invoke(IPC_EVENTS.TAB_LIST),
|
|
navigate: (tabId: string, url: string) => ipcRenderer.invoke(IPC_EVENTS.TAB_NAVIGATE, { tabId, url }),
|
|
reload: (tabId: string) => ipcRenderer.invoke(IPC_EVENTS.TAB_RELOAD, tabId),
|
|
back: (tabId: string) => ipcRenderer.invoke(IPC_EVENTS.TAB_BACK, tabId),
|
|
forward: (tabId: string) => ipcRenderer.invoke(IPC_EVENTS.TAB_FORWARD, tabId),
|
|
switch: (tabId: string) => ipcRenderer.invoke(IPC_EVENTS.TAB_SWITCH, tabId),
|
|
close: (tabId: string) => ipcRenderer.invoke(IPC_EVENTS.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(IPC_EVENTS.READ_FILE, filePath),
|
|
|
|
// 同步调用
|
|
invoke: (channel: IPC_EVENTS, ...args: any[]) => ipcRenderer.sendSync(IPC_EVENTS.INVOKE, channel, ...args),
|
|
|
|
// 异步调用
|
|
invokeAsync: (channel: IPC_EVENTS, ...args: any[]) => {
|
|
try {
|
|
ipcRenderer.invoke(IPC_EVENTS.INVOKE_ASYNC, channel, ...args)
|
|
} catch (error) {
|
|
throw error
|
|
}
|
|
},
|
|
|
|
// 监听主进程消息
|
|
on: (event: IPC_EVENTS, callback: (...args: any[]) => void) => {
|
|
const subscription = (_event: any, ...args: any[]) => callback(...args)
|
|
ipcRenderer.on(event, subscription)
|
|
return () => ipcRenderer.removeListener(event, subscription)
|
|
},
|
|
|
|
// 发送消息到主进程
|
|
send: (channel: IPC_EVENTS, ...args: any[]) => ipcRenderer.send(channel, ...args),
|
|
|
|
// 获取窗口ID
|
|
getCurrentWindowId: () => ipcRenderer.sendSync(IPC_EVENTS.GET_WINDOW_ID),
|
|
|
|
// 发送日志
|
|
logger: {
|
|
debug: (message: string, ...meta: any[]) => ipcRenderer.send(IPC_EVENTS.LOG_DEBUG, message, ...meta),
|
|
info: (message: string, ...meta: any[]) => ipcRenderer.send(IPC_EVENTS.LOG_INFO, message, ...meta),
|
|
warn: (message: string, ...meta: any[]) => ipcRenderer.send(IPC_EVENTS.LOG_WARN, message, ...meta),
|
|
error: (message: string, ...meta: any[]) => ipcRenderer.send(IPC_EVENTS.LOG_ERROR, message, ...meta),
|
|
}
|
|
}
|
|
|
|
contextBridge.exposeInMainWorld('api', api) |