Files
AlgerMusicPlayer/src/preload/index.ts

125 lines
4.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { electronAPI } from '@electron-toolkit/preload';
import type { IpcRendererEvent } from 'electron';
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'),
quitApp: () => ipcRenderer.send('quit-app'),
dragStart: (data) => ipcRenderer.send('drag-start', data),
miniTray: () => ipcRenderer.send('mini-tray'),
miniWindow: () => ipcRenderer.send('mini-window'),
restore: () => ipcRenderer.send('restore-window'),
restart: () => ipcRenderer.send('restart'),
resizeWindow: (width, height) => ipcRenderer.send('resize-window', width, height),
resizeMiniWindow: (showPlaylist) => ipcRenderer.send('resize-mini-window', showPlaylist),
openLyric: () => ipcRenderer.send('open-lyric'),
sendLyric: (data) => ipcRenderer.send('send-lyric', data),
sendSong: (data) => ipcRenderer.send('update-current-song', data),
unblockMusic: (id, data, enabledSources) =>
ipcRenderer.invoke('unblock-music', id, data, enabledSources),
importCustomApiPlugin: () => ipcRenderer.invoke('import-custom-api-plugin'),
importLxMusicScript: () => ipcRenderer.invoke('import-lx-music-script'),
// 歌词窗口关闭事件
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));
},
// 语言相关
onLanguageChanged: (callback: (locale: string) => void) => {
ipcRenderer.on('language-changed', (_event, locale) => {
callback(locale);
});
},
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',
'scan-local-music',
'scan-local-music-with-stats',
'parse-local-music-metadata'
];
if (validChannels.includes(channel)) {
return ipcRenderer.invoke(channel, ...args);
}
return Promise.reject(new Error(`未授权的 IPC 通道: ${channel}`));
},
// 搜索建议
getSearchSuggestions: (keyword: string) => ipcRenderer.invoke('get-search-suggestions', keyword),
// 落雪音乐 HTTP 请求(绕过 CORS
lxMusicHttpRequest: (request: { url: string; options: any; requestId: string }) =>
ipcRenderer.invoke('lx-music-http-request', request),
lxMusicHttpCancel: (requestId: string) => ipcRenderer.invoke('lx-music-http-cancel', requestId),
// 本地音乐扫描相关
scanLocalMusic: (folderPath: string) => ipcRenderer.invoke('scan-local-music', folderPath),
scanLocalMusicWithStats: (folderPath: string) =>
ipcRenderer.invoke('scan-local-music-with-stats', folderPath),
parseLocalMusicMetadata: (filePaths: string[]) =>
ipcRenderer.invoke('parse-local-music-metadata', filePaths)
};
// 创建带类型的ipcRenderer对象暴露给渲染进程
const ipc = {
// 发送消息到主进程(无返回值)
send: (channel: string, ...args: any[]) => {
ipcRenderer.send(channel, ...args);
},
// 调用主进程方法(有返回值)
invoke: (channel: string, ...args: any[]) => {
return ipcRenderer.invoke(channel, ...args);
},
// 监听主进程消息
on: (channel: string, listener: (...args: any[]) => void) => {
const wrappedListener = (_event: IpcRendererEvent, ...args: any[]) => listener(...args);
ipcRenderer.on(channel, wrappedListener);
return () => {
ipcRenderer.removeListener(channel, wrappedListener);
};
},
// 移除所有监听器
removeAllListeners: (channel: string) => {
ipcRenderer.removeAllListeners(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);
contextBridge.exposeInMainWorld('ipcRenderer', ipc);
} catch (error) {
console.error(error);
}
} else {
// @ts-ignore (define in dts)
window.electron = electronAPI;
// @ts-ignore (define in dts)
window.api = api;
// @ts-ignore (define in dts)
window.ipcRenderer = ipc;
}