|
| 1 | +import { ipcMain } from "electron"; |
| 2 | +import { useStore } from "../store"; |
| 3 | +import { getMainTray } from "../tray"; |
| 4 | +import 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"; |
| 11 | + |
| 12 | + |
| 13 | +let macLyricLines: MacLyricLine[] = []; |
| 14 | +let macCurrentTime = 0; |
| 15 | +let macOffset = 0; |
| 16 | +let macIsPlaying = false; |
| 17 | +let macLastLyricIndex = -1; // 上一次显示的歌词行索引 |
| 18 | +let interpolationTimer: NodeJS.Timeout | null = null; // 插值计时器 |
| 19 | +let macLastUpdateTime: number = 0; // 上次更新 macCurrentTime 的时间戳 |
| 20 | + |
| 21 | +const LYRIC_UPDATE_INTERVAL = 50; // ms, 歌词更新频率 |
| 22 | +const PROGRESS_SYNC_THRESHOLD_MS = 100; // ms, 进度同步阈值,如果误差超过此值才同步 |
| 23 | + |
| 24 | +/** |
| 25 | + * 停止插值计时器 |
| 26 | + */ |
| 27 | +const stopInterpolation = () => { |
| 28 | + if (interpolationTimer) { |
| 29 | + clearInterval(interpolationTimer); |
| 30 | + interpolationTimer = null; |
| 31 | + } |
| 32 | +}; |
| 33 | + |
| 34 | +/** |
| 35 | + * 启动插值计时器 |
| 36 | + */ |
| 37 | +const startInterpolation = (store: ReturnType<typeof useStore>) => { |
| 38 | + stopInterpolation(); // 先停止任何已存在的计时器 |
| 39 | + macLastUpdateTime = Date.now(); // 在启动新的插值计时器时,重置 macLastUpdateTime |
| 40 | + interpolationTimer = setInterval(() => { |
| 41 | + const now = Date.now(); |
| 42 | + const elapsedTime = now - macLastUpdateTime; |
| 43 | + macCurrentTime += elapsedTime; |
| 44 | + macLastUpdateTime = now; |
| 45 | + updateMacStatusBarLyric(store); |
| 46 | + }, LYRIC_UPDATE_INTERVAL); |
| 47 | +}; |
| 48 | + |
| 49 | +/** |
| 50 | + * 根据当前时间查找对应的歌词行索引 |
| 51 | + */ |
| 52 | +const findCurrentLyricIndex = ( |
| 53 | + currentTime: number, |
| 54 | + lyrics: MacLyricLine[], |
| 55 | + offset: number = 0, |
| 56 | +): number => { |
| 57 | + // 提前 300ms 显示下一行歌词,以看起来更舒服 |
| 58 | + const targetTime = currentTime - offset + 300; |
| 59 | + let index = -1; |
| 60 | + |
| 61 | + for (let i = lyrics.length - 1; i >= 0; i--) { |
| 62 | + if (lyrics[i].startTime <= targetTime) { |
| 63 | + index = i; |
| 64 | + break; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return index; |
| 69 | +}; |
| 70 | + |
| 71 | +/** |
| 72 | + * 更新 macOS 状态栏歌词(只在新行时才更新) |
| 73 | + */ |
| 74 | +const updateMacStatusBarLyric = (store: ReturnType<typeof useStore>) => { |
| 75 | + const tray = getMainTray(); |
| 76 | + if (!tray) return; |
| 77 | + |
| 78 | + const showWhenPaused = store.get("taskbar.showWhenPaused") ?? true; |
| 79 | + if (!macIsPlaying && !showWhenPaused) { |
| 80 | + // 如果不显示,则清空标题 |
| 81 | + tray.setMacStatusBarLyricTitle(""); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + // 如果歌词为空,则清空标题并返回 |
| 86 | + if (macLyricLines.length === 0) { |
| 87 | + tray.setMacStatusBarLyricTitle(""); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + const currentLyricIndex = findCurrentLyricIndex(macCurrentTime, macLyricLines, macOffset); |
| 92 | + |
| 93 | + // 如果行索引没有变化,不更新 |
| 94 | + if (currentLyricIndex === macLastLyricIndex) return; |
| 95 | + macLastLyricIndex = currentLyricIndex; |
| 96 | + |
| 97 | + const currentLyric = |
| 98 | + currentLyricIndex !== -1 |
| 99 | + ? macLyricLines[currentLyricIndex].words |
| 100 | + .map((w) => w.word ?? "") |
| 101 | + .join("") |
| 102 | + .trim() |
| 103 | + : ""; |
| 104 | + |
| 105 | + tray.setMacStatusBarLyricTitle(currentLyric); |
| 106 | +}; |
| 107 | + |
| 108 | +export const initMacStatusBarIpc = () => { |
| 109 | + const store = useStore(); |
| 110 | + |
| 111 | + // 初始化时读取新的 macOS 专属设置 |
| 112 | + const isMacosLyricEnabled = store.get("macos.statusBarLyric.enabled") ?? false; |
| 113 | + const tray = getMainTray(); |
| 114 | + tray?.setMacStatusBarLyricShow(isMacosLyricEnabled); // 根据新设置初始化显示状态 |
| 115 | + |
| 116 | + // 新增 macOS 专属设置切换监听 |
| 117 | + ipcMain.on("macos-lyric:toggle", (_event, show: boolean) => { |
| 118 | + store.set("macos.statusBarLyric.enabled", show); // 更新 store |
| 119 | + const tray = getMainTray(); |
| 120 | + |
| 121 | + // 触发 "mac-toggle-statusbar-lyric" 事件,让 ipc-tray 响应 |
| 122 | + ipcMain.emit("mac-toggle-statusbar-lyric", null, show); |
| 123 | + |
| 124 | + const mainWin = mainWindow.getWin(); // 获取主窗口实例 |
| 125 | + if (mainWin && !mainWin.isDestroyed()) { |
| 126 | + // 发送更新给渲染进程,同步 Pinia store |
| 127 | + mainWin.webContents.send("setting:update-macos-lyric-enabled", show); |
| 128 | + if (show) { |
| 129 | + |
| 130 | + mainWin.webContents.send("mac-statusbar:request-data"); // 请求新数据 |
| 131 | + } else { |
| 132 | + tray?.setMacStatusBarLyricTitle(""); // 关闭时清空歌词 |
| 133 | + stopInterpolation(); // 关闭时停止计时器 |
| 134 | + } |
| 135 | + } else if (!show) { |
| 136 | + // 如果主窗口不可用且正在关闭,也清空歌词 |
| 137 | + tray?.setMacStatusBarLyricTitle(""); |
| 138 | + stopInterpolation(); // 关闭时停止计时器 |
| 139 | + } |
| 140 | + }); |
| 141 | + |
| 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()); |
| 150 | + } |
| 151 | + }); |
| 152 | + |
| 153 | + // macOS 状态栏歌词专用进度更新 |
| 154 | + ipcMain.on("mac-statusbar:update-progress", (_event, progress: UpdateProgressPayload) => { |
| 155 | + |
| 156 | + // 进度到达,这是启动更新和插值的“门禁” |
| 157 | + if (progress.currentTime !== undefined) { |
| 158 | + const diff = Math.abs(progress.currentTime - macCurrentTime); |
| 159 | + |
| 160 | + // 如果误差在阈值之内,并且当前正在播放,则不进行时间同步,让内部状态保持稳定 |
| 161 | + // 否则,进行校准 |
| 162 | + if (!(diff <= PROGRESS_SYNC_THRESHOLD_MS && macIsPlaying)) { |
| 163 | + macCurrentTime = progress.currentTime; |
| 164 | + macLastUpdateTime = Date.now(); // 校准时更新时间戳 |
| 165 | + } |
| 166 | + } |
| 167 | + if (progress.offset !== undefined) { |
| 168 | + macOffset = progress.offset; |
| 169 | + } |
| 170 | + // 收到精确进度或误差较大同步后,立即更新一次歌词显示 |
| 171 | + updateMacStatusBarLyric(store); |
| 172 | + // 如果此时是播放状态,确保插值器运行 |
| 173 | + if (macIsPlaying) { |
| 174 | + startInterpolation(store); |
| 175 | + } |
| 176 | + }); |
| 177 | + |
| 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", () => { |
| 193 | + // macOS 请求歌词数据,转发请求并等待响应 |
| 194 | + const mainWin = mainWindow.getWin(); |
| 195 | + if (mainWin && !mainWin.isDestroyed()) { |
| 196 | + mainWin.webContents.send("mac-statusbar:request-data"); |
| 197 | + } |
| 198 | + }); |
| 199 | +}; |
0 commit comments