Skip to content

fix: Fix menu highlight remaining after main menu is closed#567

Merged
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
pengfeixx:fix-306683
Feb 6, 2026
Merged

fix: Fix menu highlight remaining after main menu is closed#567
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
pengfeixx:fix-306683

Conversation

@pengfeixx
Copy link
Contributor

@pengfeixx pengfeixx commented Feb 6, 2026

Fix menu highlight remaining after main menu is closed

Log: Fix menu highlight remaining after main menu is closed
pms: BUG-306683

Summary by Sourcery

Bug Fixes:

  • Fix menu items remaining highlighted after the main menu or their submenu is closed.

@sourcery-ai
Copy link

sourcery-ai bot commented Feb 6, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Updates the Menu delegate to keep items highlighted when hovered or when their submenu is open, preventing the highlight from disappearing when the main menu closes.

Sequence diagram for menu highlight behavior with submenu visibility

sequenceDiagram
    actor User
    participant MainMenu
    participant MenuItem
    participant SubMenu

    User->>MainMenu: movePointerTo(menuItem)
    MainMenu->>MenuItem: set hovered = true
    MenuItem->>MenuItem: highlighted = hovered || (subMenu && subMenu.visible)

    User->>MenuItem: click
    MenuItem->>SubMenu: open()
    SubMenu-->>MenuItem: visible = true
    MenuItem->>MenuItem: highlighted = hovered || (subMenu && subMenu.visible)

    User->>MainMenu: close()
    MainMenu-->>MenuItem: hovered = false
    SubMenu-->>MenuItem: visible = true
    MenuItem->>MenuItem: highlighted = hovered || (subMenu && subMenu.visible)

    User->>SubMenu: movePointerAndInteract()
Loading

File-Level Changes

Change Details Files
Adjust menu item highlight behavior to depend on hover state and submenu visibility.
  • Replace the simple MenuItem delegate with a customized delegate body.
  • Bind the highlighted property to be true when the item is hovered.
  • Extend the highlighted condition to remain true while an associated submenu exists and is visible.
qt6/src/qml/Menu.qml

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

18202781743
18202781743 previously approved these changes Feb 6, 2026
Fix menu highlight remaining after main menu is closed

Log: Fix menu highlight remaining after main menu is closed
pms: BUG-306683
@deepin-ci-robot
Copy link
Contributor

deepin pr auto review

这段代码主要修改了 MenuItem.qml 中的 highlighted 属性的逻辑。以下是对该修改的详细审查意见:

1. 语法逻辑审查

现状
代码修改为:

highlighted: hovered || (subMenu && subMenu.visible)

分析

  • 逻辑正确性:逻辑上没有语法错误。它表示当鼠标悬停(hovered)为真,或者当存在子菜单(subMenu)且子菜单可见(subMenu.visible)时,菜单项将处于高亮状态。
  • 场景覆盖:这通常用于处理级联菜单的场景。当子菜单打开时,保持父菜单项的高亮状态,符合常见的 UI/UX 交互规范。

2. 代码质量审查

意见

  • 可读性:代码简洁明了,能够直观表达意图。
  • 依赖性:该逻辑依赖于 subMenu 属性是否存在。在 Qt Quick 的 T.MenuItem (Templates.MenuItem) 中,subMenu 属性是存在的,但建议确认当前使用的 Qt 版本(Qt 6)中该属性是否已稳定且行为符合预期。
  • 潜在风险:如果 subMenunull(例如该菜单项没有子菜单),subMenu.visible 的访问在 QML 中通常是安全的(会返回 undefined 或 false,不会导致崩溃),但显式检查 subMenu 的存在性是一个好习惯,代码中已经做了 subMenu && ... 的判断,处理得当。

3. 代码性能审查

意见

  • 属性绑定开销highlighted 属性现在绑定到了 hoveredsubMenu.visible。这意味着每当鼠标移动改变 hovered 状态,或者子菜单的可见性发生变化时,都会触发 highlighted 的重新计算。
  • 影响评估
    • hovered 变化非常频繁,但 hovered 本身就是高频变化的属性,UI 必须响应它,因此这部分开销是必要的。
    • subMenu.visible 变化频率较低(仅在打开/关闭菜单时),性能影响可忽略不计。
  • 结论:性能开销在可接受范围内,无需优化。

4. 代码安全审查

意见

  • 空指针安全:如前所述,表达式 (subMenu && subMenu.visible) 利用了短路求值特性。如果 subMenunullundefined,表达式会立即停止求值并返回 false,避免了访问 null.visible 导致的错误。这是安全的写法。
  • 状态一致性:需要确保 subMenu.visible 的状态由底层的 C++ 或 QML 逻辑正确管理。如果子菜单关闭但 visible 属性未及时更新为 false,可能会导致父菜单项错误地保持高亮。

5. 改进建议

虽然代码本身没有大问题,但为了增强健壮性,可以考虑以下微调:

建议 1:显式处理 null 检查(可选,增强可读性)
目前的写法已经足够安全,但如果团队代码规范要求非常严格,可以写得更加明确,尽管 QML 的动态特性通常不需要这样做。

// 现有写法已经很好,这是仅为演示的备选方案
highlighted: hovered || (subMenu !== null && subMenu.visible)

建议 2:添加注释说明意图
由于这是针对级联菜单交互的特定逻辑,添加注释可以帮助其他开发者理解为什么要在 hovered 之外增加对 subMenu 的判断。

// 保持高亮状态:当鼠标悬停时,或者子菜单处于打开状态时
highlighted: hovered || (subMenu && subMenu.visible)

总结

这段代码修改是合理且安全的。它解决了级联菜单中父菜单项在子菜单打开时失去高亮状态的 UX 问题。语法正确,性能影响微小,且具备基本的空值安全保护。建议采纳并添加简短注释以说明业务逻辑。

@deepin-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: 18202781743, pengfeixx

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@pengfeixx
Copy link
Contributor Author

/merge

@deepin-bot
Copy link
Contributor

deepin-bot bot commented Feb 6, 2026

This pr cannot be merged! (status: unstable)

@pengfeixx
Copy link
Contributor Author

/forcemerge

@deepin-bot
Copy link
Contributor

deepin-bot bot commented Feb 6, 2026

This pr force merged! (status: unstable)

@deepin-bot deepin-bot bot merged commit fef6c93 into linuxdeepin:master Feb 6, 2026
19 of 20 checks passed
@pengfeixx pengfeixx deleted the fix-306683 branch February 6, 2026 06:53
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