1+ import type { LyricLine } from "@applemusic-like-lyrics/lyric" ;
2+ import { TASKBAR_IPC_CHANNELS , type SyncStatePayload , type SyncTickPayload } from "@shared" ;
13import { ipcMain } from "electron" ;
24import { useStore } from "../store" ;
35import { getMainTray } from "../tray" ;
46import mainWindow from "../windows/main-window" ;
5- import { MacLyricLine } from "../../../src/types/lyric" ;
6- import {
7- UpdateLyricsPayload ,
8- UpdateProgressPayload ,
9- UpdateStatePayload ,
10- } from "../../../src/types/ipc" ;
117
12-
13- let macLyricLines : MacLyricLine [ ] = [ ] ;
8+ let macLyricLines : LyricLine [ ] = [ ] ;
149let macCurrentTime = 0 ;
1510let macOffset = 0 ;
1611let macIsPlaying = false ;
@@ -51,7 +46,7 @@ const startInterpolation = (store: ReturnType<typeof useStore>) => {
5146 */
5247const findCurrentLyricIndex = (
5348 currentTime : number ,
54- lyrics : MacLyricLine [ ] ,
49+ lyrics : LyricLine [ ] ,
5550 offset : number = 0 ,
5651) : number => {
5752 // 提前 300ms 显示下一行歌词,以看起来更舒服
@@ -126,8 +121,7 @@ export const initMacStatusBarIpc = () => {
126121 // 发送更新给渲染进程,同步 Pinia store
127122 mainWin . webContents . send ( "setting:update-macos-lyric-enabled" , show ) ;
128123 if ( show ) {
129-
130- mainWin . webContents . send ( "mac-statusbar:request-data" ) ; // 请求新数据
124+ mainWin . webContents . send ( TASKBAR_IPC_CHANNELS . REQUEST_DATA ) ; // 请求新数据
131125 } else {
132126 tray ?. setMacStatusBarLyricTitle ( "" ) ; // 关闭时清空歌词
133127 stopInterpolation ( ) ; // 关闭时停止计时器
@@ -139,33 +133,64 @@ export const initMacStatusBarIpc = () => {
139133 }
140134 } ) ;
141135
142- ipcMain . on ( "taskbar:update-lyrics" , ( _event , lyrics : UpdateLyricsPayload ) => {
143- // 新歌词到达,更新数据并重置索引
144- macLyricLines = lyrics . lines ?? [ ] ;
145- macLastLyricIndex = - 1 ;
146- // 确保新歌词到达后立即更新状态栏显示
147- const mainWin = mainWindow . getWin ( ) ;
148- if ( mainWin && ! mainWin . isDestroyed ( ) ) {
149- updateMacStatusBarLyric ( useStore ( ) ) ;
136+ ipcMain . on ( TASKBAR_IPC_CHANNELS . SYNC_STATE , ( _event , payload : SyncStatePayload ) => {
137+ switch ( payload . type ) {
138+ case "lyrics-loaded" : {
139+ macLyricLines = payload . data . lines ;
140+ macLastLyricIndex = - 1 ;
141+ // 确保新歌词到达后立即更新状态栏显示
142+ const mainWin = mainWindow . getWin ( ) ;
143+ if ( mainWin && ! mainWin . isDestroyed ( ) ) {
144+ updateMacStatusBarLyric ( useStore ( ) ) ;
145+ }
146+ break ;
147+ }
148+
149+ case "playback-state" :
150+ macIsPlaying = payload . data . isPlaying ;
151+ if ( ! macIsPlaying ) {
152+ stopInterpolation ( ) ;
153+ updateMacStatusBarLyric ( store ) ;
154+ }
155+ break ;
156+
157+ case "full-hydration" :
158+ if ( payload . data . lyrics ) {
159+ macLyricLines = payload . data . lyrics . lines ;
160+ macLastLyricIndex = - 1 ;
161+ }
162+ if ( payload . data . playback ) {
163+ macIsPlaying = payload . data . playback . isPlaying ;
164+ if ( payload . data . playback . tick ) {
165+ const [ currentTime , , offset ] = payload . data . playback . tick ;
166+ macCurrentTime = currentTime ;
167+ macOffset = offset ;
168+ }
169+ }
170+ updateMacStatusBarLyric ( store ) ;
171+ if ( macIsPlaying ) {
172+ startInterpolation ( store ) ;
173+ }
174+ break ;
150175 }
151176 } ) ;
152177
153- // macOS 状态栏歌词专用进度更新
154- ipcMain . on ( "mac-statusbar:update-progress" , ( _event , progress : UpdateProgressPayload ) => {
155-
178+ ipcMain . on ( TASKBAR_IPC_CHANNELS . SYNC_TICK , ( _ , payload : SyncTickPayload ) => {
179+ const [ currentTime , _duration , offset ] = payload ;
180+
156181 // 进度到达,这是启动更新和插值的“门禁”
157- if ( progress . currentTime !== undefined ) {
158- const diff = Math . abs ( progress . currentTime - macCurrentTime ) ;
182+ if ( currentTime !== undefined ) {
183+ const diff = Math . abs ( currentTime - macCurrentTime ) ;
159184
160185 // 如果误差在阈值之内,并且当前正在播放,则不进行时间同步,让内部状态保持稳定
161186 // 否则,进行校准
162187 if ( ! ( diff <= PROGRESS_SYNC_THRESHOLD_MS && macIsPlaying ) ) {
163- macCurrentTime = progress . currentTime ;
188+ macCurrentTime = currentTime ;
164189 macLastUpdateTime = Date . now ( ) ; // 校准时更新时间戳
165190 }
166191 }
167- if ( progress . offset !== undefined ) {
168- macOffset = progress . offset ;
192+ if ( offset !== undefined ) {
193+ macOffset = offset ;
169194 }
170195 // 收到精确进度或误差较大同步后,立即更新一次歌词显示
171196 updateMacStatusBarLyric ( store ) ;
@@ -175,25 +200,11 @@ export const initMacStatusBarIpc = () => {
175200 }
176201 } ) ;
177202
178- ipcMain . on ( "taskbar:update-state" , ( _event , state : UpdateStatePayload ) => {
179- // 根据播放状态更新 macOS 状态栏歌词显示逻辑
180- if ( state . isPlaying !== undefined ) {
181- macIsPlaying = state . isPlaying ;
182- // 当歌曲暂停时:停止歌词更新计时器,并进行一次最终更新以显示当前歌词
183- if ( ! macIsPlaying ) {
184- stopInterpolation ( ) ;
185- updateMacStatusBarLyric ( store ) ;
186- }
187- // 当歌曲开始播放时:不在这里直接启动歌词更新,而是等待 'mac-statusbar:update-progress' 事件
188- // 该事件作为“门禁”,负责启动歌词更新的插值计时器,以确保与播放进度的同步
189- }
190- } ) ;
191-
192- ipcMain . on ( "mac-statusbar:request-data" , ( ) => {
203+ ipcMain . on ( TASKBAR_IPC_CHANNELS . REQUEST_DATA , ( ) => {
193204 // macOS 请求歌词数据,转发请求并等待响应
194205 const mainWin = mainWindow . getWin ( ) ;
195206 if ( mainWin && ! mainWin . isDestroyed ( ) ) {
196- mainWin . webContents . send ( "mac-statusbar:request-data" ) ;
207+ mainWin . webContents . send ( TASKBAR_IPC_CHANNELS . REQUEST_DATA ) ;
197208 }
198209 } ) ;
199210} ;
0 commit comments