import { electronAPI } from '@electron-toolkit/preload'; import { contextBridge, ipcRenderer } from 'electron'; // Custom APIs for renderer const api = { minimize: () => ipcRenderer.send('minimize-window'), maximize: () => ipcRenderer.send('maximize-window'), close: () => ipcRenderer.send('close-window'), dragStart: (data) => ipcRenderer.send('drag-start', data), miniTray: () => ipcRenderer.send('mini-tray'), restart: () => ipcRenderer.send('restart'), openLyric: () => ipcRenderer.send('open-lyric'), sendLyric: (data) => ipcRenderer.send('send-lyric', data), unblockMusic: (id) => ipcRenderer.invoke('unblock-music', id), // 歌词窗口关闭事件 onLyricWindowClosed: (callback: () => void) => { ipcRenderer.on('lyric-window-closed', () => callback()); }, // 更新相关 startDownload: (url: string) => ipcRenderer.send('start-download', url), onDownloadProgress: (callback: (progress: number, status: string) => void) => { ipcRenderer.on('download-progress', (_event, progress, status) => callback(progress, status)); }, onDownloadComplete: (callback: (success: boolean, filePath: string) => void) => { ipcRenderer.on('download-complete', (_event, success, filePath) => callback(success, filePath)); }, removeDownloadListeners: () => { ipcRenderer.removeAllListeners('download-progress'); ipcRenderer.removeAllListeners('download-complete'); }, // 歌词缓存相关 invoke: (channel: string, ...args: any[]) => { const validChannels = [ 'get-lyrics', 'clear-lyrics-cache', 'get-system-fonts', 'get-cached-lyric', 'cache-lyric', 'clear-lyric-cache' ]; if (validChannels.includes(channel)) { return ipcRenderer.invoke(channel, ...args); } return Promise.reject(new Error(`未授权的 IPC 通道: ${channel}`)); } }; // 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(error); } } else { // @ts-ignore (define in dts) window.electron = electronAPI; // @ts-ignore (define in dts) window.api = api; }