Skip to content

🦄refactor(mac/tray/lyric): 重构 macOS 状态栏歌词功能,解决多项显示与交互问题,全面提升稳定性与用户体验#890

Merged
imsyy merged 3 commits into
SPlayer-Dev:devfrom
Pissofdvpe:dev
Feb 16, 2026
Merged

Conversation

@Pissofdvpe
Copy link
Copy Markdown
Contributor

本次提交对 macOS 状态栏歌词功能进行了深度优化与结构性重构,旨在全面提升其稳定性、响应速度、用户体验及代码可维护性,修复了系列显示与交互问题:

核心重构与优化:

  • 统一状态管理,消除功能切换的竞态条件

    • 通过 store.get("macos.statusBarLyric.enabled") 直接在关键逻辑点查询最新状态。
    • 将 macOS 状态栏歌词的启用状态集中至全局 Store 管理,取代了此前分散且易导致不一致的局部变量。
    • 此举显著增强了状态管理的一致性与可控性,从根本上解决了功能开启/关闭逻辑中因状态不同步导致的竞态条件和行为异常。
  • 明确模块职责边界,消除 UI 渲染冲突

    • 移除 electron/main/tray/index.ts 中冗余的 setMacStatusBarLyricShowsetMacStatusBarLyricTitle 方法,并通过 ipc-tray.ts 导出 getCurrentSongTitle
    • 使 ipc-mac-statusbar.ts 在歌词功能关闭时直接调用 tray?.setTitle(getCurrentSongTitle())
    • 精简了托盘(Tray)模块对状态栏标题的控制逻辑,赋予 macOS 歌词功能模块在激活时对状态栏标题的权威控制权。这彻底解决了歌词与歌曲信息相互覆盖、导致启动时显示歌曲名或拖动时闪烁的 UI 渲染冲突。
  • 增强歌词显示与更新机制,优化平滑度与精确性

    • updateMacStatusBarLyric 函数中添加 forceUpdate 参数和 isMacosLyricEnabled 检查,并将歌词更新的权威触发点统一到 TASKBAR_IPC_CHANNELS.SYNC_TICK 事件处理器。
    • 引入 updateMacStatusBarLyricforceUpdate 参数,并优化了歌词在启动和进度拖动时的更新策略。
    • 此机制确保歌词显示更为平滑、精确,解决了歌词在启动时延迟显示或拖动歌曲进度时跳动、闪烁的问题,显著提升了视觉流畅度。
  • 改进托盘菜单 UI 同步,提升交互一致性

    • 通过在 macos-lyric:toggle 事件处理器中调用 (tray as any)?.initTrayMenu() 来强制刷新托盘菜单,并修改 createTrayMenu 直接从 Store 读取状态。
    • 实现了托盘菜单项(“开启/关闭状态栏歌词”)与实际歌词功能启用状态的实时同步。解决了菜单标签未能及时反映真实状态的 UI bug,提升了用户交互的直观性与一致性。
  • 提升代码健壮性与可维护性

    • MainTray 接口中添加 initTrayMenu(): void 声明;将 CreateTray 类中的 initTrayMenu 方法改为 public
    • 移除 electron/main/index.ts 中不再使用的 ipcMainuseStore 导入。
    • 统一 electron/main/ipc/ipc-mac-statusbar.ts 中的歌词和 IPC payload 类型,使其与 @applemusic-like-lyrics/lyric@shared 模块保持一致。
    • 优化了类型接口与实现,移除了冗余代码及不再需要的导入,使整体代码结构更加清晰,降低了长期维护成本,为未来的功能扩展奠定了坚实基础。

本次重构全面提升了 macOS 状态栏歌词功能的内部机制,带来了更稳定、更流畅、更符合预期的用户体验。

- 统一状态管理,解决功能切换竞态条件
- 明确模块职责,消除歌词与歌曲信息覆盖问题
- 优化歌词显示更新机制,解决启动和拖动时的闪烁卡顿
- 提升代码健壮性,解决类型错误并精简冗余代码
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @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 状态栏歌词功能进行了全面的重构和深度优化。核心目标是提升其稳定性、响应速度和用户体验,同时解决了一系列显示与交互问题。通过统一状态管理、明确模块职责、优化歌词更新机制以及改进托盘菜单同步,显著提升了功能的可靠性和流畅性,并增强了代码的可维护性。

Highlights

  • 统一状态管理: 通过将 macOS 状态栏歌词的启用状态集中至全局 Store 管理,取代了此前分散的局部变量,从根本上解决了功能开启/关闭逻辑中因状态不同步导致的竞态条件和行为异常。
  • 明确模块职责边界: 移除了 electron/main/tray/index.ts 中冗余的歌词设置方法,并使 ipc-mac-statusbar.ts 在歌词功能关闭时直接调用 tray?.setTitle(getCurrentSongTitle()),解决了歌词与歌曲信息相互覆盖的 UI 渲染冲突。
  • 增强歌词显示与更新机制: 在 updateMacStatusBarLyric 函数中引入 forceUpdate 参数和 isMacosLyricEnabled 检查,并将歌词更新的权威触发点统一到 TASKBAR_IPC_CHANNELS.SYNC_TICK 事件处理器,确保歌词显示更平滑、精确,解决了启动时延迟显示或拖动进度时跳动的问题。
  • 改进托盘菜单 UI 同步: 通过在 macos-lyric:toggle 事件处理器中调用 (tray as any)?.initTrayMenu() 来强制刷新托盘菜单,并修改 createTrayMenu 直接从 Store 读取状态,实现了菜单项与实际歌词功能启用状态的实时同步。
  • 提升代码健壮性与可维护性: 优化了类型接口与实现,移除了冗余代码及不再需要的导入,使整体代码结构更加清晰,降低了长期维护成本,为未来的功能扩展奠定了坚实基础。
Changelog
  • electron/main/ipc/ipc-mac-statusbar.ts
    • 导入了 getCurrentSongTitle 函数,用于在歌词功能禁用时恢复托盘标题。
    • 修改了 updateMacStatusBarLyric 函数,增加了 forceUpdate 参数,并加入了 isMacosLyricEnabled 检查,确保在歌词功能禁用时显示歌曲标题。
    • 更新了 initMacStatusBarIpc 中的 macos-lyric:toggle 事件处理器,以强制刷新托盘菜单并根据歌词启用状态设置托盘标题。
    • 调整了 SYNC_STATESYNC_TICK 事件处理逻辑,将歌词更新的权威触发点统一到 SYNC_TICK,并移除了冗余的歌词更新调用。
  • electron/main/ipc/ipc-tray.ts
    • 移除了 macStatusBarLyricEnabled 局部状态变量,转而从全局 Store 获取状态。
    • 导出了 getCurrentSongTitle 函数,以便其他模块获取当前歌曲标题。
    • 修改了 play-song-change 事件处理逻辑,使其根据 Store 中 macOS 歌词的启用状态来决定是否更新托盘标题。
    • 移除了 mac-toggle-statusbar-lyric IPC 监听器,相关逻辑已转移到 ipc-mac-statusbar.ts
  • electron/main/tray/index.ts
    • 移除了 macStatusBarLyricShowmacStatusBarLyricTitle 状态变量,简化了托盘模块的内部状态管理。
    • 更新了 MainTray 接口,移除了 setMacStatusBarLyricShowsetMacStatusBarLyricTitle 方法,并添加了 initTrayMenu() 方法。
    • 修改了 createTrayMenu 函数,使其接受 store 参数,并直接从 Store 读取 macOS 歌词的启用状态来构建菜单项。
    • CreateTray 类中的 initTrayMenu 方法改为 public,并修改了构造函数和 initTrayMenu 方法,使其在创建和更新菜单时都传入 store 实例。
    • setTitle 方法中移除了 this._win.setTitle(title),使托盘标题的设置与主窗口标题解耦。
Activity
  • 没有检测到人工活动。
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

本次 PR 对 macOS 状态栏歌词功能进行了全面的重构,显著提升了代码的健壮性和可维护性。通过统一状态管理、明确模块职责和优化更新机制,解决了多项 UI 显示和交互问题。代码结构更加清晰,逻辑也更易于理解。

我发现了一些可以进一步改进的小地方,主要涉及代码的简洁性和类型安全,已在具体的代码行中提出了建议。

总体而言,这是一次高质量的重构,做得非常出色!

Comment thread electron/main/ipc/ipc-mac-statusbar.ts Outdated
Comment thread electron/main/ipc/ipc-mac-statusbar.ts Outdated
Comment thread electron/main/ipc/ipc-mac-statusbar.ts Outdated
Comment thread electron/main/ipc/ipc-mac-statusbar.ts Outdated
Comment thread electron/main/tray/index.ts Outdated
- 移除 electron/main/ipc/ipc-mac-statusbar.ts 中 updateMacStatusBarLyric 和 startInterpolation 函数的 store 参数,改为函数内部通过 useStore() 获取实例。
- 更新 ipc-mac-statusbar.ts 中所有对 updateMacStatusBarLyric 和 startInterpolation 的调用,移除 store 参数。
- 移除 electron/main/tray/index.ts 中 createTrayMenu 函数的 store 参数,改为函数内部通过 useStore() 获取实例。
- 移除 electron/main/tray/index.ts 中 CreateTray 类冗余的 _store 成员变量。
- 更新 CreateTray 类中所有对 createTrayMenu 的调用,移除 store 参数。
@imsyy imsyy requested a review from Copilot February 16, 2026 04:25
@imsyy imsyy merged commit a76f134 into SPlayer-Dev:dev Feb 16, 2026
4 checks passed
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the macOS status bar lyrics feature to improve stability, eliminate race conditions, and enhance maintainability. The key architectural change is migrating from scattered local state variables to centralized Store-based state management, clarifying module boundaries, and improving the lyric display update mechanism.

Changes:

  • Migrated macOS status bar lyrics state from local variables to centralized Store management (macos.statusBarLyric.enabled)
  • Removed redundant methods (setMacStatusBarLyricShow, setMacStatusBarLyricTitle) from tray module and exposed initTrayMenu() as public
  • Refactored lyric update mechanism with forceUpdate parameter and direct state checks in updateMacStatusBarLyric

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
electron/main/tray/index.ts Removed macOS-specific state variables and methods; exposed initTrayMenu as public; removed window title setting from setTitle method; updated menu to read state directly from Store
electron/main/ipc/ipc-tray.ts Removed local macOS lyric state tracking and old event handler; exported getCurrentSongTitle function; updated song change handler to check Store for macOS lyric state
electron/main/ipc/ipc-mac-statusbar.ts Refactored updateMacStatusBarLyric to check Store directly; added forceUpdate parameter; improved toggle event handler to call initTrayMenu and properly restore title; streamlined SYNC_STATE and SYNC_TICK event handling

Comment on lines +71 to +74
const updateMacStatusBarLyric = (
forceUpdate: boolean = false,
) => {
const store = useStore();
Copy link

Copilot AI Feb 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The updateMacStatusBarLyric function calls useStore() every time it's invoked, including during the 50ms interpolation timer interval. While electron-store handles internal singleton behavior, repeatedly instantiating the Store wrapper could have minor performance implications. Consider caching the store instance at the module level (e.g., const store = useStore() at the top of the file) to avoid repeated instantiation, similar to how macLyricLines and other state variables are maintained at the module level.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants