✨feat(mac/tray/lyric): 新增 macOS 状态栏歌词与托盘图标适配#843
✨feat(mac/tray/lyric): 新增 macOS 状态栏歌词与托盘图标适配#843Pissofdvpe wants to merge 2 commits intoimsyy:devfrom
Conversation
- 新增 macOS 状态栏歌词功能:在 macOS 平台,歌词将显示在系统状态栏区域,并根据播放进度实时更新,同时通过 IPC 消息进>行控制和同步。 - 实现 macOS 风格托盘图标适配:引入 `getTrayIcon()` 和 `getMenuIcon()` 方法,支持 macOS 模板图像,实现托盘和菜单图标 随系统亮/暗模式自动切换,提供更好的视觉集成。 - 维护跨平台兼容性:通过 `isMac` 判断进行逻辑隔离,确保 Windows 及其他平台原有的任务栏歌词窗口功能不受影响。 ~
Summary of ChangesHello @Pissofdvpe, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! 此拉取请求的核心目标是增强应用程序在 macOS 平台的用户体验,通过引入原生的状态栏歌词显示功能,并优化托盘图标和菜单图标的视觉风格,使其更好地融入 macOS 生态系统。同时,所有这些改进都经过精心设计,以确保不会影响现有 Windows 用户的任务栏歌词功能,从而保持了良好的跨平台兼容性。 Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
本次 PR 主要为 macOS 平台增加了状态栏歌词功能,并对托盘图标进行了适配,使其风格与系统更加统一。代码实现考虑了平台差异性,保证了 Windows 等其他平台原有功能不受影响,整体改动清晰。
我在代码中发现了一些可以优化的地方:
- 在
ipc-taskbar.ts中,存在几处代码重复和一处性能问题,可以通过重构来提升代码质量和运行效率。 - 在
ipc-tray.ts中,一处逻辑可以简化,避免冗余的函数调用。 - 在
tray/index.ts中,有一处未使用的变量和相关的死代码逻辑。
具体的修改建议请见各文件的评论。这些修改将有助于提升代码的可维护性和性能。
| if (currentLyricIndex === macLastLyricIndex) return; | ||
| macLastLyricIndex = currentLyricIndex; | ||
|
|
||
| const currentLyric = findCurrentLyric(macCurrentTime, macLyricLines, macOffset); |
There was a problem hiding this comment.
这里存在性能可以优化的点。findCurrentLyricIndex 已经找到了当前歌词的索引,但随后调用的 findCurrentLyric 又重复遍历了一遍歌词数组来获取歌词内容。这造成了不必要的性能开销。
建议直接使用 currentLyricIndex 来获取歌词,这样可以避免重复遍历,提高效率。修改后,findCurrentLyric 函数(40-56行)将不再被需要,可以一并移除。
| const currentLyric = findCurrentLyric(macCurrentTime, macLyricLines, macOffset); | |
| const currentLyric = currentLyricIndex > -1 ? macLyricLines[currentLyricIndex].words.map((w) => w.word ?? "").join("").trim() : ""; |
| // macOS 使用状态栏歌词,不使用任务栏歌词窗口 | ||
| if (isMac) { | ||
| const envEnabled = store.get("taskbar.enabled"); | ||
| const tray = getMainTray(); | ||
| tray?.setMacStatusBarLyricShow(envEnabled); | ||
| } else { | ||
| const envEnabled = store.get("taskbar.enabled"); | ||
|
|
||
| const tray = getMainTray(); | ||
| tray?.setTaskbarLyricShow(envEnabled); | ||
| const tray = getMainTray(); | ||
| tray?.setTaskbarLyricShow(envEnabled); | ||
|
|
||
| if (envEnabled) { | ||
| taskbarLyricWindow.create(); | ||
| if (envEnabled) { | ||
| taskbarLyricWindow.create(); | ||
| } | ||
| } |
There was a problem hiding this comment.
在 if (isMac) 和 else 两个分支中都重复获取了 envEnabled 和 tray 实例。可以将这些通用操作提取到 if 语句之前,以减少代码重复,使逻辑更清晰。
const envEnabled = store.get("taskbar.enabled");
const tray = getMainTray();
// macOS 使用状态栏歌词,不使用任务栏歌词窗口
if (isMac) {
tray?.setMacStatusBarLyricShow(envEnabled);
} else {
tray?.setTaskbarLyricShow(envEnabled);
if (envEnabled) {
taskbarLyricWindow.create();
}
}| ipcMain.on("mac-toggle-statusbar-lyric", (_, show: boolean) => { | ||
| if (!isMac) return; | ||
| macStatusBarLyricEnabled = show; | ||
| tray?.setMacStatusBarLyricShow(show); | ||
| // 如果关闭,恢复显示歌曲标题 | ||
| if (!show) { | ||
| tray?.setTitle(currentSongTitle); | ||
| } | ||
| }); |
There was a problem hiding this comment.
在 mac-toggle-statusbar-lyric 事件处理器中,当关闭状态栏歌词时 (show 为 false),会先调用 tray?.setMacStatusBarLyricShow(show),该函数内部会将托盘标题设置为 appName,然后又会调用 tray?.setTitle(currentSongTitle)。这会导致一次不必要的标题设置和潜在的界面闪烁。
建议将 currentSongTitle 作为参数传递给 setMacStatusBarLyricShow,由其内部统一处理标题恢复逻辑,从而简化代码并避免冗余调用。
ipcMain.on("mac-toggle-statusbar-lyric", (_, show: boolean) => {
if (!isMac) return;
macStatusBarLyricEnabled = show;
tray?.setMacStatusBarLyricShow(show, currentSongTitle);
});| if (this._checkInterval) { | ||
| clearInterval(this._checkInterval); | ||
| } |
|
这个 “状态栏歌词” 和现有的任务栏歌词实现看起来并没有什么关联,只是将托盘上应用图标旁边的名称换成了元数据和歌词。这个实现无法拥有任何动画效果或者自定义功能,也无法使用设置中的任务栏歌词选项。 建议改名并在设置里面添加一个 MacOS 专属的选项,比如 “在托盘上显示歌词” 之类的,或者至少让它和现有的任务栏歌词解耦 |
新增 macOS 状态栏歌词功能:
ipc-taskbar.ts新增 macOS 歌词处理逻辑,包括查找当前歌词、更新进度和播放状态,并通过 IPC 发送至主进程。ipc-tray.ts新增mac-toggle-statusbar-lyric和mac-update-statusbar-lyricIPC处理器,用于控制状态栏歌词的显示与内容更新。
实现 macOS 风格托盘图标与菜单图标适配:
tray/index.ts引入getTrayIcon()方法,用于为 macOS创建符合系统风格的模板图像托盘图标,使其能自适应亮/暗模式。
getMenuIcon()方法现在处理 macOS 菜单项图标的加载和尺寸调整,同样适应系统主题。CreateTray构造函数中的托盘图标加载逻辑,实现平台差异化。维护跨平台兼容性:
isMac判断进行逻辑隔离。先前Mac菜单栏(状态栏)效果:

新增特性效果:

