From 479db66eb07a03ae1e16f5a1e35aa3d6ecebc9e8 Mon Sep 17 00:00:00 2001 From: alger Date: Thu, 12 Mar 2026 18:31:16 +0800 Subject: [PATCH] =?UTF-8?q?fix(lyric):=20=E4=BF=AE=E5=A4=8D=E6=A1=8C?= =?UTF-8?q?=E9=9D=A2=E6=AD=8C=E8=AF=8D=E7=AA=97=E5=8F=A3=E9=A6=96=E6=AC=A1?= =?UTF-8?q?=E6=89=93=E5=BC=80=E6=97=A0=E6=AD=8C=E8=AF=8D=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 歌词窗口 Vue 加载完成后发送 lyric-ready 信号,主窗口收到后 发送完整歌词数据,替代不可靠的延迟猜测方案 --- src/main/lyric.ts | 7 +++++++ src/preload/index.d.ts | 1 + src/preload/index.ts | 4 ++++ src/renderer/hooks/MusicHook.ts | 14 +++++++++++--- src/renderer/types/electron.d.ts | 2 ++ src/renderer/views/lyric/index.vue | 3 +++ 6 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/main/lyric.ts b/src/main/lyric.ts index b7840aa..045378a 100644 --- a/src/main/lyric.ts +++ b/src/main/lyric.ts @@ -172,6 +172,13 @@ export const loadLyricWindow = (ipcMain: IpcMain, mainWin: BrowserWindow): void }); }); + // 歌词窗口 Vue 应用加载完成,通知主窗口发送完整歌词数据 + ipcMain.on('lyric-ready', () => { + if (mainWin && !mainWin.isDestroyed()) { + mainWin.webContents.send('lyric-window-ready'); + } + }); + ipcMain.on('send-lyric', (_, data) => { if (lyricWindow && !lyricWindow.isDestroyed()) { try { diff --git a/src/preload/index.d.ts b/src/preload/index.d.ts index fd77a4f..6358e45 100644 --- a/src/preload/index.d.ts +++ b/src/preload/index.d.ts @@ -19,6 +19,7 @@ interface API { sendSong: (data: any) => void; unblockMusic: (id: number, data: any, enabledSources?: string[]) => Promise; onLyricWindowClosed: (callback: () => void) => void; + onLyricWindowReady: (callback: () => void) => void; getAppUpdateState: () => Promise; checkAppUpdate: (manual?: boolean) => Promise; downloadAppUpdate: () => Promise; diff --git a/src/preload/index.ts b/src/preload/index.ts index ae6c6b1..1de3db8 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -28,6 +28,10 @@ const api = { onLyricWindowClosed: (callback: () => void) => { ipcRenderer.on('lyric-window-closed', () => callback()); }, + // 歌词窗口就绪事件(Vue 加载完成,可以接收数据) + onLyricWindowReady: (callback: () => void) => { + ipcRenderer.on('lyric-window-ready', () => callback()); + }, getAppUpdateState: () => ipcRenderer.invoke('app-update:get-state') as Promise, checkAppUpdate: (manual = false) => ipcRenderer.invoke('app-update:check', { manual }) as Promise, diff --git a/src/renderer/hooks/MusicHook.ts b/src/renderer/hooks/MusicHook.ts index 54fe308..07125ae 100644 --- a/src/renderer/hooks/MusicHook.ts +++ b/src/renderer/hooks/MusicHook.ts @@ -821,9 +821,11 @@ export const openLyric = () => { sendLyricToWin(); } - // 设置定时器,确保500ms后再次发送数据,以防窗口加载延迟 + // 延迟重发一次,以防窗口加载略慢 setTimeout(() => { - sendLyricToWin(); + if (isLyricWindowOpen.value) { + sendLyricToWin(); + } }, 500); // 启动歌词同步 @@ -936,11 +938,17 @@ export const initAudioListeners = async () => { // 初始化音频监听器 setupAudioListeners(); - // 监听歌词窗口关闭事件 + // 监听歌词窗口事件 if (isElectron) { window.api.onLyricWindowClosed(() => { isLyricWindowOpen.value = false; }); + // 歌词窗口 Vue 加载完成后,发送完整歌词数据 + window.api.onLyricWindowReady(() => { + if (isLyricWindowOpen.value) { + sendLyricToWin(); + } + }); } // 获取最新的音频实例 diff --git a/src/renderer/types/electron.d.ts b/src/renderer/types/electron.d.ts index cf6f3a8..74ddc7a 100644 --- a/src/renderer/types/electron.d.ts +++ b/src/renderer/types/electron.d.ts @@ -12,6 +12,8 @@ export interface IElectronAPI { unblockMusic: (_id: number) => Promise; importCustomApiPlugin: () => Promise<{ name: string; content: string } | null>; importLxMusicScript: () => Promise<{ name: string; content: string } | null>; + onLyricWindowClosed: (_callback: () => void) => void; + onLyricWindowReady: (_callback: () => void) => void; onLanguageChanged: (_callback: (_locale: string) => void) => void; store: { get: (_key: string) => Promise; diff --git a/src/renderer/views/lyric/index.vue b/src/renderer/views/lyric/index.vue index 2ef8127..0e84f32 100644 --- a/src/renderer/views/lyric/index.vue +++ b/src/renderer/views/lyric/index.vue @@ -642,6 +642,9 @@ onMounted(() => { console.error('Error parsing lyric data:', error); } }); + + // 通知主窗口歌词窗口已就绪,请求发送完整歌词数据 + windowData.electron.ipcRenderer.send('lyric-ready'); }); onUnmounted(() => {