Skip to content

Commit 5ab998b

Browse files
committed
✨ feat: 优化状态栏状态展示
1 parent d797ac0 commit 5ab998b

3 files changed

Lines changed: 103 additions & 24 deletions

File tree

electron/main/ipc/ipc-window.ts

Lines changed: 73 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -118,33 +118,84 @@ const initWindowsIpc = (): void => {
118118
mainWin.webContents.send(eventName, ...args);
119119
});
120120

121-
// 显示进度
122-
ipcMain.on("set-bar", (_event, val: number | "none" | "indeterminate" | "error" | "paused") => {
121+
// 进度条状态
122+
let currentProgress = -1;
123+
let currentMode: "normal" | "paused" | "error" | "indeterminate" = "normal";
124+
125+
// 更新进度条
126+
const updateProgressBar = () => {
123127
const mainWin = mainWindow.getWin();
124128
if (!mainWin) return;
125-
switch (val) {
126-
case "none":
127-
mainWin?.setProgressBar(-1);
128-
break;
129-
case "indeterminate":
130-
mainWin?.setProgressBar(2, { mode: "indeterminate" });
131-
break;
132-
case "error":
133-
mainWin?.setProgressBar(1, { mode: "error" });
134-
break;
135-
case "paused":
136-
mainWin?.setProgressBar(1, { mode: "paused" });
137-
break;
138-
default:
139-
if (typeof val === "number") {
140-
mainWin?.setProgressBar(val / 100);
141-
} else {
142-
mainWin?.setProgressBar(-1);
143-
}
144-
break;
129+
130+
if (currentProgress < 0) {
131+
mainWin.setProgressBar(-1);
132+
} else {
133+
mainWin.setProgressBar(currentProgress, { mode: currentMode as any });
145134
}
135+
};
136+
137+
// 设置进度
138+
ipcMain.on("set-bar-progress", (_event, progress: number | "none") => {
139+
if (progress === "none") {
140+
currentProgress = -1;
141+
} else {
142+
currentProgress = progress / 100;
143+
}
144+
updateProgressBar();
145+
});
146+
147+
// 设置模式
148+
ipcMain.on("set-bar-mode", (_event, mode: "normal" | "paused" | "error" | "indeterminate") => {
149+
currentMode = mode;
150+
updateProgressBar();
146151
});
147152

153+
// 显示进度 (兼容旧版,建议使用 set-bar-progress 和 set-bar-mode)
154+
ipcMain.on(
155+
"set-bar",
156+
(
157+
_event,
158+
val:
159+
| number
160+
| "none"
161+
| "indeterminate"
162+
| "error"
163+
| "paused"
164+
| { progress: number; mode: "normal" | "paused" | "error" | "indeterminate" },
165+
) => {
166+
if (typeof val === "object" && val !== null) {
167+
currentProgress = val.progress / 100;
168+
currentMode = val.mode === "normal" ? "normal" : val.mode;
169+
updateProgressBar();
170+
return;
171+
}
172+
173+
switch (val) {
174+
case "none":
175+
currentProgress = -1;
176+
break;
177+
case "indeterminate":
178+
currentProgress = 2; // Electron treat > 1 as indeterminate usually, but let's stick to mode
179+
currentMode = "indeterminate";
180+
break;
181+
case "error":
182+
currentMode = "error";
183+
break;
184+
case "paused":
185+
currentMode = "paused";
186+
break;
187+
default:
188+
if (typeof val === "number") {
189+
currentProgress = val / 100;
190+
} else {
191+
currentProgress = -1;
192+
}
193+
break;
194+
}
195+
updateProgressBar();
196+
},
197+
);
198+
148199
// 开启控制台
149200
ipcMain.on("open-dev-tools", () => {
150201
const mainWin = mainWindow.getWin();

src/core/player/PlayerController.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,15 @@ class PlayerController {
174174
await audioManager.play(url, { fadeIn: !!fadeTime, fadeDuration: fadeTime, autoPlay });
175175
// 恢复进度
176176
if (seek > 0) audioManager.seek(seek / 1000);
177+
// 如果不自动播放,设置任务栏暂停状态
178+
if (!autoPlay) {
179+
playerIpc.sendTaskbarMode("paused");
180+
if (seek > 0) {
181+
const duration = this.getDuration();
182+
const progress = calculateProgress(seek, duration);
183+
playerIpc.sendTaskbarProgress(progress);
184+
}
185+
}
177186
} catch (error) {
178187
console.error("❌ 音频播放失败:", error);
179188
throw error;
@@ -315,6 +324,8 @@ class PlayerController {
315324
lastfmScrobbler.resume();
316325
// IPC 通知
317326
playerIpc.sendPlayStatus(true);
327+
playerIpc.sendTaskbarMode("normal");
328+
playerIpc.sendTaskbarProgress(statusStore.progress);
318329
// ipcService.sendSongChange(playTitle, name || "", artist || "", album || "");
319330
console.log(`▶️ [${musicStore.playSong?.id}] 歌曲播放:`, name);
320331
});
@@ -328,6 +339,8 @@ class PlayerController {
328339
}
329340
if (!isElectron) window.document.title = "SPlayer";
330341
playerIpc.sendPlayStatus(false);
342+
playerIpc.sendTaskbarMode("paused");
343+
playerIpc.sendTaskbarProgress(statusStore.progress);
331344
lastfmScrobbler.pause();
332345
console.log(`⏸️ [${musicStore.playSong?.id}] 歌曲暂停`);
333346
});
@@ -416,6 +429,11 @@ class PlayerController {
416429
window.$message.error("本地文件无法播放");
417430
statusStore.playLoading = false;
418431
this.retryInfo.count = 0;
432+
// 如果列表只有一首,直接停止
433+
if (dataStore.playList.length <= 1) {
434+
this.pause(true);
435+
return;
436+
}
419437
await this.nextOrPrev("next");
420438
return;
421439
}

src/core/player/PlayerIpc.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,28 @@ export const sendSongChange = (title: string, name: string, artist: string, albu
3434
};
3535

3636
/**
37-
* 发送进度
37+
* 发送状态栏进度
3838
* @param progress 进度
3939
*/
4040
export const sendTaskbarProgress: (progress: number | "none") => void = throttle(
4141
(progress: number | "none") => {
4242
if (isElectron) {
43-
window.electron.ipcRenderer.send("set-bar", progress);
43+
window.electron.ipcRenderer.send("set-bar-progress", progress);
4444
}
4545
},
4646
1000,
4747
);
4848

49+
/**
50+
* 发送状态栏模式
51+
* @param mode 模式
52+
*/
53+
export const sendTaskbarMode = (mode: "normal" | "paused" | "error" | "indeterminate") => {
54+
if (isElectron) {
55+
window.electron.ipcRenderer.send("set-bar-mode", mode);
56+
}
57+
};
58+
4959
/**
5060
* 发送 Socket 实时进度
5161
*/

0 commit comments

Comments
 (0)