chore: add initial project configuration and structure
This commit is contained in:
Vendored
+13
@@ -0,0 +1,13 @@
|
||||
import type { ElectronAPI } from '@electron-toolkit/preload'
|
||||
import type { IpcServices } from '../main/ipc'
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
electron: ElectronAPI
|
||||
api: IpcServices & {
|
||||
on: (channel: string, callback: (...args: unknown[]) => void) => void
|
||||
removeListener: (channel: string, callback: (...args: unknown[]) => void) => void
|
||||
send: (channel: string, ...args: unknown[]) => void
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import { electronAPI } from '@electron-toolkit/preload'
|
||||
import { contextBridge, ipcRenderer } from 'electron'
|
||||
import { createIpcProxy } from 'electron-ipc-decorator/client'
|
||||
import type { IpcServices } from '../main/ipc'
|
||||
|
||||
// Create type-safe IPC proxy using electron-ipc-decorator
|
||||
const ipcServices = createIpcProxy<IpcServices>(ipcRenderer)
|
||||
|
||||
// Custom APIs for renderer
|
||||
const api = {
|
||||
// IPC Services (type-safe, using decorators)
|
||||
...ipcServices,
|
||||
|
||||
// Event listening API
|
||||
on: (channel: string, callback: (...args: unknown[]) => void) => {
|
||||
const subscription = (_event: Electron.IpcRendererEvent, ...args: unknown[]) =>
|
||||
callback(...args)
|
||||
ipcRenderer.on(channel, subscription)
|
||||
return subscription
|
||||
},
|
||||
removeListener: (channel: string, callback: (...args: unknown[]) => void) => {
|
||||
ipcRenderer.removeListener(channel, callback)
|
||||
},
|
||||
// Send message to main process
|
||||
send: (channel: string, ...args: unknown[]) => {
|
||||
ipcRenderer.send(channel, ...args)
|
||||
}
|
||||
}
|
||||
|
||||
// Use `contextBridge` APIs to expose Electron APIs to
|
||||
// renderer only if context isolation is enabled, otherwise
|
||||
// just add to the DOM global.
|
||||
if (process.contextIsolated) {
|
||||
try {
|
||||
contextBridge.exposeInMainWorld('electron', electronAPI)
|
||||
contextBridge.exposeInMainWorld('api', api)
|
||||
} catch (error) {
|
||||
console.error('Failed to expose APIs in context bridge:', error)
|
||||
}
|
||||
} else {
|
||||
// @ts-expect-error (define in dts)
|
||||
window.electron = electronAPI
|
||||
// @ts-expect-error (define in dts)
|
||||
window.api = api
|
||||
}
|
||||
Reference in New Issue
Block a user