Skip to content

Commit 00143bd

Browse files
authored
🦄 refactor: 合并任务栏歌词相关 IPC 通道 (#860)
合并之前散乱的任务栏歌词相关 IPC 通道,以便之后更好地进行扩展
1 parent b1668f3 commit 00143bd

27 files changed

Lines changed: 694 additions & 427 deletions

electron.vite.config.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,20 @@ import AutoImport from "unplugin-auto-import/vite";
55
import { NaiveUiResolver } from "unplugin-vue-components/resolvers";
66
import Components from "unplugin-vue-components/vite";
77
import viteCompression from "vite-plugin-compression";
8-
import { MainEnv } from "./env";
8+
import type { MainEnv } from "./env";
99
// import VueDevTools from "vite-plugin-vue-devtools";
1010
import wasm from "vite-plugin-wasm";
1111

12+
const commonResolve = {
13+
alias: {
14+
"@": resolve(__dirname, "src/"),
15+
"@emi": resolve(__dirname, "native/external-media-integration"),
16+
"@shared": resolve(__dirname, "src/types/shared"),
17+
"@opencc": resolve(__dirname, "native/ferrous-opencc-wasm/pkg"),
18+
"@native": resolve(__dirname, "native"),
19+
},
20+
};
21+
1222
export default defineConfig(({ mode }) => {
1323
// 读取环境变量
1424
const getEnv = (name: keyof MainEnv): string => {
@@ -29,6 +39,7 @@ export default defineConfig(({ mode }) => {
2939
},
3040
},
3141
},
42+
resolve: commonResolve,
3243
},
3344
// 预加载
3445
preload: {
@@ -39,6 +50,7 @@ export default defineConfig(({ mode }) => {
3950
},
4051
},
4152
},
53+
resolve: commonResolve,
4254
},
4355
// 渲染进程
4456
renderer: {
@@ -66,15 +78,7 @@ export default defineConfig(({ mode }) => {
6678
viteCompression(),
6779
wasm(),
6880
],
69-
resolve: {
70-
alias: {
71-
"@": resolve(__dirname, "src/"),
72-
"@emi": resolve(__dirname, "native/external-media-integration"),
73-
"@shared": resolve(__dirname, "src/types/shared.ts"),
74-
"@opencc": resolve(__dirname, "native/ferrous-opencc-wasm/pkg"),
75-
"@native": resolve(__dirname, "native"),
76-
},
77-
},
81+
resolve: commonResolve,
7882
css: {
7983
preprocessorOptions: {
8084
scss: {

electron/main/ipc/ipc-mac-statusbar.ts

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1+
import type { LyricLine } from "@applemusic-like-lyrics/lyric";
2+
import { TASKBAR_IPC_CHANNELS, type SyncStatePayload, type SyncTickPayload } from "@shared";
13
import { ipcMain } from "electron";
24
import { useStore } from "../store";
35
import { getMainTray } from "../tray";
46
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";
117

12-
13-
let macLyricLines: MacLyricLine[] = [];
8+
let macLyricLines: LyricLine[] = [];
149
let macCurrentTime = 0;
1510
let macOffset = 0;
1611
let macIsPlaying = false;
@@ -51,7 +46,7 @@ const startInterpolation = (store: ReturnType<typeof useStore>) => {
5146
*/
5247
const 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

Comments
 (0)