Skip to content

fix(shortcuts): display correct group name for each shortcut type#273

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:release/eaglefrom
GongHeng2017:202606091441-eagle-fix
May 9, 2026
Merged

fix(shortcuts): display correct group name for each shortcut type#273
deepin-bot[bot] merged 1 commit into
linuxdeepin:release/eaglefrom
GongHeng2017:202606091441-eagle-fix

Conversation

@GongHeng2017
Copy link
Copy Markdown
Contributor

Replace hardcoded "Settings" group name with type-specific names (Settings, File, Display, Tools, Edit) based on ShortCutType enum.

修复快捷键分组名称硬编码为"Settings"的bug,根据ShortCutType枚举
正确显示对应的分组名称。

Log: 修复快捷键分组名称显示错误
Bug: https://pms.uniontech.com/bug-view-354843.html
Influence: 快捷键列表中各分组将显示正确的名称,不再统一显示为"Settings"。

Replace hardcoded "Settings" group name with type-specific names
(Settings, File, Display, Tools, Edit) based on ShortCutType enum.

修复快捷键分组名称硬编码为"Settings"的bug,根据ShortCutType枚举
正确显示对应的分组名称。

Log: 修复快捷键分组名称显示错误
Bug: https://pms.uniontech.com/bug-view-354843.html
Influence: 快捷键列表中各分组将显示正确的名称,不再统一显示为"Settings"。
@GongHeng2017 GongHeng2017 force-pushed the 202606091441-eagle-fix branch from bc38e8d to 9497ff4 Compare May 9, 2026 06:51
@deepin-ci-robot
Copy link
Copy Markdown

deepin pr auto review

这段代码主要修改了版权声明年份,并重构了快捷键显示逻辑,将原本硬编码的 "Settings" 改为了根据 ShortCutType 动态获取组名。以下是对这段代码的审查意见:

1. 语法逻辑

  • 版权年份:将版权年份从 "2019 ~ 2020" 和 "2023" 统一修改为 "2019 - 2026"。从语法和逻辑上讲,这没有问题,但建议确认 "2026" 是否是预期的结束年份(通常如果项目持续维护,可能会使用当前年份或留空)。
  • Switch-Case 逻辑:新增的 switch-case 逻辑用于根据 ShortCutType 获取对应的组名,逻辑清晰,但缺少 default 分支。如果 ShortCutType 枚举在未来扩展,未处理的类型会导致 strType 未初始化,可能引发未定义行为。

2. 代码质量

  • 初始化问题strTypeswitch-case 中被赋值,但未初始化。如果 type 不匹配任何 casestrType 将是未初始化的 QString,可能导致运行时问题。建议添加 default 分支或初始化 strType
  • 可维护性switch-caseShortCutType 映射到字符串,这种方式在枚举类型较少时是可接受的,但如果未来扩展,可能需要维护两处代码(枚举定义和字符串映射)。可以考虑使用 Q_ENUMQMetaEnum 或静态映射表来简化维护。

3. 代码性能

  • 性能影响:新增的 switch-case 逻辑对性能影响极小,因为 ShortCutType 是枚举类型,switch 会编译为跳转表或简单的条件判断,效率很高。
  • JSON 操作QJsonObjectQJsonArray 的操作是必要的,没有明显的性能问题。但如果 m_shortcutMap 中数据量很大,可以考虑预分配 QJsonArray 的容量(如 items.reserve(m_shortcutMap[type].size()))以减少动态扩容的开销。

4. 代码安全

  • 未处理的枚举值:如前所述,switch-case 缺少 default 分支,可能导致未初始化的 strType 被插入 JSON 对象。建议添加:
    default: strType = tr("Unknown"); break;
    或在 switch 前初始化:
    QString strType = tr("Unknown");
  • 硬编码字符串:虽然使用了 tr() 以支持国际化,但组名(如 "Settings"、"File")仍然是硬编码的。如果这些字符串在其他地方也需要使用,建议提取为常量或配置文件,避免重复定义。

5. 改进建议

以下是改进后的代码示例:

QString strType;
switch (type) {
case ShortCutType::Settings: strType = tr("Settings"); break;
case ShortCutType::File: strType = tr("File"); break;
case ShortCutType::Display: strType = tr("Display"); break;
case ShortCutType::Tools: strType = tr("Tools"); break;
case ShortCutType::Edit: strType = tr("Edit"); break;
default: strType = tr("Unknown"); break; // 添加默认分支
}

group.insert("groupName", strType);

QJsonArray items;
items.reserve(m_shortcutMap[type].size()); // 预分配容量
for (const auto &d : m_shortcutMap[type]) {
    QJsonObject jsonItem;
    jsonItem.insert("name", d.second);
    jsonItem.insert("value", d.first);
    items.append(jsonItem);
}
group.insert("groupItems", items);

6. 其他建议

  • 版权年份:建议确认 "2026" 是否是预期的年份,或改为 "2019 - present" 以避免频繁更新。
  • 枚举与字符串映射:如果未来需要频繁映射枚举到字符串,可以考虑使用 QHashQMap 静态映射表:
    static const QHash<ShortCutType, QString> shortcutTypeNames = {
        {ShortCutType::Settings, tr("Settings")},
        {ShortCutType::File, tr("File")},
        {ShortCutType::Display, tr("Display")},
        {ShortCutType::Tools, tr("Tools")},
        {ShortCutType::Edit, tr("Edit")}
    };
    QString strType = shortcutTypeNames.value(type, tr("Unknown"));

总结

这段代码的逻辑是正确的,但可以通过添加 default 分支和预分配 QJsonArray 容量来提高健壮性和性能。此外,版权年份和枚举映射的维护方式也值得进一步优化。

@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: GongHeng2017, max-lvs

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

@GongHeng2017
Copy link
Copy Markdown
Contributor Author

/merge

@deepin-bot
Copy link
Copy Markdown
Contributor

deepin-bot Bot commented May 9, 2026

This pr cannot be merged! (status: unstable)

@GongHeng2017
Copy link
Copy Markdown
Contributor Author

/forcemerge

@deepin-bot
Copy link
Copy Markdown
Contributor

deepin-bot Bot commented May 9, 2026

This pr force merged! (status: unstable)

@deepin-bot deepin-bot Bot merged commit 9f169f7 into linuxdeepin:release/eagle May 9, 2026
7 of 9 checks passed
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