2024-09-26 11:57:23 +08:00
|
|
|
import { BrowserWindow, nativeImage, nativeTheme, ThumbarButton } from "electron";
|
|
|
|
|
import { join } from "path";
|
2025-10-25 23:43:54 +08:00
|
|
|
import { isWin } from "../utils/config";
|
|
|
|
|
import { thumbarLog } from "../logger";
|
2024-09-26 11:57:23 +08:00
|
|
|
|
|
|
|
|
enum ThumbarKeys {
|
|
|
|
|
Play = "play",
|
|
|
|
|
Pause = "pause",
|
|
|
|
|
Prev = "prev",
|
|
|
|
|
Next = "next",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ThumbarMap = Map<ThumbarKeys, ThumbarButton>;
|
|
|
|
|
|
|
|
|
|
export interface Thumbar {
|
|
|
|
|
clearThumbar(): void;
|
2024-10-16 16:19:33 +08:00
|
|
|
updateThumbar(playing: boolean, clean?: boolean): void;
|
2024-09-26 11:57:23 +08:00
|
|
|
}
|
|
|
|
|
|
2025-10-25 23:43:54 +08:00
|
|
|
// 缩略图单例
|
|
|
|
|
let thumbar: Thumbar | null = null;
|
|
|
|
|
|
2024-09-26 11:57:23 +08:00
|
|
|
// 工具栏图标
|
|
|
|
|
const thumbarIcon = (filename: string) => {
|
|
|
|
|
// 是否为暗色
|
|
|
|
|
const isDark = nativeTheme.shouldUseDarkColors;
|
|
|
|
|
// 返回图标
|
|
|
|
|
return nativeImage.createFromPath(
|
|
|
|
|
join(__dirname, `../../public/icons/thumbar/${filename}-${isDark ? "dark" : "light"}.png`),
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 缩略图工具栏
|
|
|
|
|
const createThumbarButtons = (win: BrowserWindow): ThumbarMap => {
|
|
|
|
|
return new Map<ThumbarKeys, ThumbarButton>()
|
|
|
|
|
.set(ThumbarKeys.Prev, {
|
|
|
|
|
tooltip: "上一曲",
|
|
|
|
|
icon: thumbarIcon("prev"),
|
2024-10-16 16:19:33 +08:00
|
|
|
click: () => win.webContents.send("playPrev"),
|
2024-09-26 11:57:23 +08:00
|
|
|
})
|
|
|
|
|
.set(ThumbarKeys.Next, {
|
|
|
|
|
tooltip: "下一曲",
|
|
|
|
|
icon: thumbarIcon("next"),
|
2024-10-16 16:19:33 +08:00
|
|
|
click: () => win.webContents.send("playNext"),
|
2024-09-26 11:57:23 +08:00
|
|
|
})
|
|
|
|
|
.set(ThumbarKeys.Play, {
|
|
|
|
|
tooltip: "播放",
|
|
|
|
|
icon: thumbarIcon("play"),
|
|
|
|
|
click: () => win.webContents.send("play"),
|
|
|
|
|
})
|
|
|
|
|
.set(ThumbarKeys.Pause, {
|
|
|
|
|
tooltip: "暂停",
|
|
|
|
|
icon: thumbarIcon("pause"),
|
2024-10-16 16:19:33 +08:00
|
|
|
click: () => win.webContents.send("pause"),
|
2024-09-26 11:57:23 +08:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 创建缩略图工具栏
|
|
|
|
|
class createThumbar implements Thumbar {
|
|
|
|
|
// 窗口
|
|
|
|
|
private _win: BrowserWindow;
|
|
|
|
|
// 工具栏
|
|
|
|
|
private _thumbar: ThumbarMap;
|
|
|
|
|
// 工具栏按钮
|
|
|
|
|
private _prev: ThumbarButton;
|
|
|
|
|
private _next: ThumbarButton;
|
|
|
|
|
private _play: ThumbarButton;
|
|
|
|
|
private _pause: ThumbarButton;
|
|
|
|
|
constructor(win: BrowserWindow) {
|
|
|
|
|
// 初始化数据
|
|
|
|
|
this._win = win;
|
|
|
|
|
this._thumbar = createThumbarButtons(win);
|
|
|
|
|
// 工具栏按钮
|
|
|
|
|
this._play = this._thumbar.get(ThumbarKeys.Play)!;
|
|
|
|
|
this._pause = this._thumbar.get(ThumbarKeys.Pause)!;
|
|
|
|
|
this._prev = this._thumbar.get(ThumbarKeys.Prev)!;
|
|
|
|
|
this._next = this._thumbar.get(ThumbarKeys.Next)!;
|
|
|
|
|
// 初始化工具栏
|
|
|
|
|
this.updateThumbar();
|
|
|
|
|
}
|
|
|
|
|
// 更新工具栏
|
2024-10-16 16:19:33 +08:00
|
|
|
updateThumbar(playing: boolean = false, clean: boolean = false) {
|
2024-09-26 11:57:23 +08:00
|
|
|
if (clean) return this.clearThumbar();
|
|
|
|
|
this._win.setThumbarButtons([this._prev, playing ? this._pause : this._play, this._next]);
|
|
|
|
|
}
|
|
|
|
|
// 清除工具栏
|
|
|
|
|
clearThumbar() {
|
|
|
|
|
this._win.setThumbarButtons([]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-25 23:43:54 +08:00
|
|
|
/**
|
|
|
|
|
* 初始化缩略图工具栏
|
|
|
|
|
* @param win 窗口
|
|
|
|
|
* @returns 缩略图工具栏
|
|
|
|
|
*/
|
2024-09-26 11:57:23 +08:00
|
|
|
export const initThumbar = (win: BrowserWindow) => {
|
|
|
|
|
try {
|
|
|
|
|
// 若非 Win
|
|
|
|
|
if (!isWin) return null;
|
2025-10-25 23:43:54 +08:00
|
|
|
thumbarLog.info("🚀 ThumbarButtons Startup");
|
|
|
|
|
thumbar = new createThumbar(win);
|
|
|
|
|
return thumbar;
|
2024-09-26 11:57:23 +08:00
|
|
|
} catch (error) {
|
2025-10-25 23:43:54 +08:00
|
|
|
thumbarLog.error("❌ ThumbarButtons Error", error);
|
2024-09-26 11:57:23 +08:00
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-10-25 23:43:54 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取缩略图工具栏
|
|
|
|
|
* @returns 缩略图工具栏
|
|
|
|
|
*/
|
|
|
|
|
export const getThumbar = () => thumbar;
|