Skip to content

fix: Adapt the first item in the menu#3007

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

fix: Adapt the first item in the menu#3007
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
pengfeixx:fix-311355

Conversation

@pengfeixx
Copy link
Contributor

@pengfeixx pengfeixx commented Feb 5, 2026

Adapt the first item in the menu

Log: Adapt the first item in the menu

Summary by Sourcery

Bug Fixes:

  • Correct menu focus handling by checking against the dynamic root first item instead of a hardcoded name.

Adapt the first item in the menu

Log: Adapt the first item in the menu
@sourcery-ai
Copy link

sourcery-ai bot commented Feb 5, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adjusts the logic that decides when to restore focus to the menu list by comparing the active object to the root’s first item instead of checking for a specific name.

Flow diagram for updated focus restoration logic

flowchart TD
    A["User activates a menu item"] --> B["rightView.replace with activeObj"]
    B --> C["Get rootFirstItem = first child of DccApp.root or null"]
    C --> D{activeObj != rootFirstItem?}
    D -- Yes --> E["list.forceActiveFocus()"]
    D -- No --> F["Do not change focus"]
Loading

File-Level Changes

Change Details Files
Update focus-restoration condition to be based on the actual first root item rather than a hard-coded name.
  • Introduce a local variable to resolve the first child of DccApp.root when present.
  • Change the conditional guarding list.forceActiveFocus() to compare activeObj against the resolved first root item instead of checking activeObj.name against the literal "system".
src/dde-control-center/plugin/SecondPage.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 left some high level feedback:

  • Consider guarding against DccApp.root being null/undefined before accessing DccApp.root.children to avoid potential runtime errors in edge cases where the root is not yet initialized.
  • Relying on DccApp.root.children[0] implicitly assumes the root's first child is always the menu item you want; if that relationship might change, consider a more explicit way to identify the root menu item (e.g., an id or role property).
  • Use const instead of var for rootFirstItem since the reference is not reassigned, which makes the intent clearer and avoids accidental reassignments.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider guarding against `DccApp.root` being null/undefined before accessing `DccApp.root.children` to avoid potential runtime errors in edge cases where the root is not yet initialized.
- Relying on `DccApp.root.children[0]` implicitly assumes the root's first child is always the menu item you want; if that relationship might change, consider a more explicit way to identify the root menu item (e.g., an id or role property).
- Use `const` instead of `var` for `rootFirstItem` since the reference is not reassigned, which makes the intent clearer and avoids accidental reassignments.

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.

@deepin-ci-robot
Copy link

deepin pr auto review

这段代码修改主要是在页面切换逻辑中,改变了判断是否强制焦点的条件。以下是对该代码变更的详细审查意见:

1. 语法逻辑审查

  • 变更分析
    • 原代码if (activeObj.name !== "system")
      • 逻辑:通过检查当前激活对象的 name 属性是否等于字符串 "system" 来决定是否执行 list.forceActiveFocus()
    • 新代码
      var rootFirstItem = DccApp.root.children.length > 0 ? DccApp.root.children[0] : null
      if (activeObj !== rootFirstItem) {
      • 逻辑:先获取 DccApp.root.children 的第一个元素,然后判断当前激活对象 activeObj 是否引用相同(即是否是同一个对象实例)。
  • 潜在问题
    • 逻辑不一致:原代码是基于名称/类型(name属性)的判断,而新代码是基于对象实例(引用)的判断。这两者在语义上有很大区别。
      • 如果原意是"当不在系统主页时获取焦点",那么新代码可能引入Bug。例如,如果 rootFirstItem 是主页对象,但 activeObj 是主页的另一个实例或副本,新代码会认为它们不相等并强制获取焦点,而原代码可能不会。
      • 如果 activeObjname 是 "system",但它并不是 rootFirstItem,两者的行为会完全相反。
    • 健壮性:新代码增加了对 DccApp.root.children.length 的检查,这是一个好的防御性编程实践,防止数组越界。

2. 代码质量审查

  • 变量作用域rootFirstItem 是在函数块内定义的局部变量,作用域控制得当。
  • 可读性
    • 新代码的逻辑意图(比较对象引用)比硬编码字符串 "system" 在某些上下文中可能更清晰(例如不依赖特定的命名约定),但前提是开发者必须清楚 rootFirstItem 代表什么。
    • 建议添加注释解释为什么 activeObj 不能等于 rootFirstItem,以方便后续维护。

3. 代码性能审查

  • 属性访问DccApp.root.children[0] 涉及属性链访问和数组索引操作。虽然开销极小,但在高频调用的函数中需留意。不过考虑到这是页面切换操作,通常不是高频事件,因此性能影响可以忽略。
  • 比较操作:引用比较(!==)通常比字符串比较(!==)更快,但这点性能差异在此场景下微乎其微。

4. 代码安全审查

  • 空值检查:新代码使用了三元运算符 DccApp.root.children.length > 0 ? ... : null,这有效地防止了访问空数组导致的 ReferenceErrorundefined 错误。
  • 类型安全:QML 是弱类型语言,activeObjrootFirstItem 的类型依赖于运行时数据。如果 DccApp.root.children[0] 返回的不是预期的对象类型,后续逻辑可能出错。

改进建议

  1. 逻辑确认:请务必确认将"字符串比较"改为"对象引用比较"是否符合业务需求。如果原意是区分"系统页面"和其他页面,建议保留 name 属性的判断,或者确保 rootFirstItem 一定是"系统页面"的实例。

  2. 增强可读性:建议将复杂的逻辑提取或注释:

    // 获取根节点的第一个子项(通常为主页/系统页)
    var rootFirstItem = DccApp.root.children.length > 0 ? DccApp.root.children[0] : null
    // 如果当前激活的页面不是根主页,则强制列表获取焦点
    if (activeObj !== rootFirstItem) {
        list.forceActiveFocus()
    }
  3. 防御性编程:如果 activeObj 可能为 null,建议增加判空保护:

    if (activeObj && activeObj !== rootFirstItem) {
        list.forceActiveFocus()
    }
  4. 代码风格:如果项目中有类似的获取根节点的逻辑,建议将其封装为一个工具函数,例如 getRootItem(),以避免重复编写 DccApp.root.children.length > 0 ? ... 这样的样板代码。

总结

这段代码修改在安全性(增加了数组长度检查)和性能(引用比较)上略有提升,但在逻辑上发生了重大变化(从属性值比较变为引用比较)。最大的风险在于新旧逻辑的语义是否等价。建议在合并前进行充分的测试,特别是针对页面切换和焦点管理的场景。

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: caixr23, 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

/forcemerge

@deepin-bot
Copy link

deepin-bot bot commented Feb 5, 2026

This pr force merged! (status: blocked)

@deepin-bot deepin-bot bot merged commit 11a49d7 into linuxdeepin:master Feb 5, 2026
16 of 18 checks passed
@pengfeixx pengfeixx deleted the fix-311355 branch February 5, 2026 07:52
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