Files
zn-ai/electron/preload/index.ts
duanshuwen b5a67ff650 feat: implement custom window controls and replace header bar with title bar
- Add window handlers for minimize, maximize, close, and check if maximized in ipcMain.
- Update preload script to use new window control IPC events.
- Refactor window service to remove old IPC event handlers and use new handlers.
- Remove old HeaderBar and DragRegion components, replacing them with a new TitleBar component.
- Update Layout component to use TitleBar instead of HeaderBar.
- Remove useWinManager hook as its functionality is now integrated into TitleBar.
- Update login page to remove HeaderBar and adjust layout accordingly.
- Update constants to remove old window IPC events.
- Update package dependencies to replace @iconify/vue with @lucide/vue.
2026-04-14 23:38:42 +08:00

75 lines
3.1 KiB
TypeScript

import { contextBridge, ipcRenderer } from 'electron'
import { IPC_EVENTS } from '@lib/constants';
const api: WindowApi = {
versions: process.versions,
external: {
open: (url: string) => ipcRenderer.invoke('external-open', url)
},
platform: process.platform,
windowMinimize: () => ipcRenderer.invoke('window:minimize'),
windowMaximize: () => ipcRenderer.invoke('window:maximize'),
windowClose: () => ipcRenderer.invoke('window:close'),
windowIsMaximized: () => ipcRenderer.invoke('window:isMaximized'),
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)
},
// 通过 IPC 调用主进程
readFile: (filePath: string) => ipcRenderer.invoke(IPC_EVENTS.READ_FILE, filePath),
// 异步调用(映射为 electron 的 invoke)
invoke: (channel: IPC_EVENTS, ...args: any[]) => ipcRenderer.invoke(channel, ...args),
// 异步调用(为了兼容老代码)
invokeAsync: (channel: IPC_EVENTS, ...args: any[]) => ipcRenderer.invoke(channel, ...args),
// 监听主进程消息
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),
},
// 执行脚本
executeScript: (params: any) => ipcRenderer.invoke(IPC_EVENTS.EXECUTE_SCRIPT, params),
// 打开渠道
openChannel: (channels: any) => ipcRenderer.invoke(IPC_EVENTS.OPEN_CHANNEL, channels),
// 脚本管理
scriptApi: {
list: () => ipcRenderer.invoke(IPC_EVENTS.SCRIPT_LIST),
get: (id: string) => ipcRenderer.invoke(IPC_EVENTS.SCRIPT_GET, id),
save: (input: any) => ipcRenderer.invoke(IPC_EVENTS.SCRIPT_SAVE, input),
delete: (id: string) => ipcRenderer.invoke(IPC_EVENTS.SCRIPT_DELETE, id),
toggle: (id: string, enabled: boolean) => ipcRenderer.invoke(IPC_EVENTS.SCRIPT_TOGGLE, id, enabled),
run: (id: string) => ipcRenderer.invoke(IPC_EVENTS.SCRIPT_RUN, id),
startRecording: (url?: string) => ipcRenderer.invoke(IPC_EVENTS.SCRIPT_RECORD_START, url),
stopRecording: () => ipcRenderer.invoke(IPC_EVENTS.SCRIPT_RECORD_STOP),
codegen: (id: string, url?: string) => ipcRenderer.invoke(IPC_EVENTS.SCRIPT_CODEGEN, id, url),
},
}
contextBridge.exposeInMainWorld('api', api)