From 7cfbce75056f9aeda07af10b64ee3de4e9884653 Mon Sep 17 00:00:00 2001 From: Misaka Date: Sat, 28 Feb 2026 22:19:21 +0800 Subject: [PATCH] feat: setup IPC communication framework - Add IPC types definition with all communication channels - Implement preload script with API surface for renderer process - Add IPC handler registration stub in main entry point Co-Authored-By: Claude Sonnet 4.5 --- src/main/index.ts | 6 ++++++ src/main/models/ipc.types.ts | 10 ++++++++++ src/preload/index.ts | 32 +++++++++++++++++++++++++++----- 3 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 src/main/models/ipc.types.ts diff --git a/src/main/index.ts b/src/main/index.ts index 06e06b4..92404fb 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -52,6 +52,12 @@ app.whenReady().then(() => { // IPC test ipcMain.on('ping', () => console.log('pong')) + // IPC Handler Registration + // TODO: Register IPC handlers for: + // - automation:clean, automation:extract, automation:stop, automation:progress + // - database:query-production-orders, database:query-materials-to-delete + // - auth:login, auth:logout + createWindow() app.on('activate', function () { diff --git a/src/main/models/ipc.types.ts b/src/main/models/ipc.types.ts new file mode 100644 index 0000000..70dc5be --- /dev/null +++ b/src/main/models/ipc.types.ts @@ -0,0 +1,10 @@ +export interface IPCChannels { + 'automation:clean': any; + 'automation:extract': any; + 'automation:stop': any; + 'automation:progress': any; + 'database:query-production-orders': any; + 'database:query-materials-to-delete': any; + 'auth:login': any; + 'auth:logout': any; +} diff --git a/src/preload/index.ts b/src/preload/index.ts index 2d18524..a9e0cf9 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -1,8 +1,28 @@ -import { contextBridge } from 'electron' +import { contextBridge, ipcRenderer } from 'electron' import { electronAPI } from '@electron-toolkit/preload' -// Custom APIs for renderer -const api = {} +const API = { + automation: { + clean: (params: any) => ipcRenderer.invoke('automation:clean', params), + extract: (params: any) => ipcRenderer.invoke('automation:extract', params), + stop: () => ipcRenderer.invoke('automation:stop'), + onProgress: (callback: (progress: any) => void) => { + const listener = (_: any, progress: any) => callback(progress); + ipcRenderer.on('automation:progress', listener); + return () => ipcRenderer.removeListener('automation:progress', listener); + }, + }, + database: { + queryProductionOrders: (productionIds: string[]) => + ipcRenderer.invoke('database:query-production-orders', productionIds), + queryMaterialsToDelete: (managerNames: string[] | null) => + ipcRenderer.invoke('database:query-materials-to-delete', managerNames), + }, + auth: { + login: (credentials: any) => ipcRenderer.invoke('auth:login', credentials), + logout: () => ipcRenderer.invoke('auth:logout'), + }, +} // Use `contextBridge` APIs to expose Electron APIs to // renderer only if context isolation is enabled, otherwise @@ -10,7 +30,7 @@ const api = {} if (process.contextIsolated) { try { contextBridge.exposeInMainWorld('electron', electronAPI) - contextBridge.exposeInMainWorld('api', api) + contextBridge.exposeInMainWorld('api', API) } catch (error) { console.error(error) } @@ -18,5 +38,7 @@ if (process.contextIsolated) { // @ts-ignore (define in dts) window.electron = electronAPI // @ts-ignore (define in dts) - window.api = api + window.api = API } + +export type API = typeof API