Skip to content

feat(shortcut): add dynamic category metadata#93

Open
yixinshark wants to merge 1 commit into
linuxdeepin:masterfrom
yixinshark:fix/shortcut-dynamic-categories
Open

feat(shortcut): add dynamic category metadata#93
yixinshark wants to merge 1 commit into
linuxdeepin:masterfrom
yixinshark:fix/shortcut-dynamic-categories

Conversation

@yixinshark

@yixinshark yixinshark commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Add string-based shortcut categories and ListCategories D-Bus metadata for Wayland.
  • Translate category display names through each shortcut provider's i18n domain.
  • Update shortcut DConfig category values and generated translation sources.

Test plan

  • Verified by user.
  • Ran git diff --check.
  • Ran shortcut i18n update target and generated dde-app zh_CN QM with 29 finished translations.

Log: add dynamic category metadata
Pms: BUG-367657,BUG-365993

Summary by Sourcery

Introduce string-based shortcut categories and expose localized category metadata over D-Bus for shortcuts and gestures.

New Features:

  • Add ListCategories D-Bus API returning category metadata including key, display name, order, and custom flag.
  • Expose localized category names and custom-category markers in ShortcutInfo and GestureInfo for consumers.

Enhancements:

  • Migrate shortcut and gesture category fields from numeric enums to logical string keys, including reserved framework categories.
  • Adjust config loading and immutability rules to rely solely on the modifiable flag instead of system-category checks.
  • Update i18n extraction tooling to collect translations for both shortcut display names and category keys.
  • Extend keybinding and dde-app translation catalogs with category strings such as System, Window, Workspace, and Custom, plus additional shortcut-related texts.

@sourcery-ai

sourcery-ai Bot commented Jun 29, 2026

Copy link
Copy Markdown

Reviewer's Guide

This PR refactors shortcut and gesture category handling to use string-based category keys, exposes translated category metadata over D-Bus, and updates translation extraction and TS files so both display names and categories are localized consistently across apps and the keybinding framework.

Sequence diagram for ListCategories D-Bus metadata flow

sequenceDiagram
    actor ControlCenter
    participant KeybindingManager
    participant TranslationManager

    ControlCenter->>KeybindingManager: ListCategories()
    KeybindingManager->>KeybindingManager: reservedCategoryOrder()
    loop each distinct config.category in m_keyConfigsMap
        KeybindingManager->>TranslationManager: translate(config.appId, config.category)
        TranslationManager-->>KeybindingManager: localizedCategory
        KeybindingManager->>KeybindingManager: build CategoryInfo
    end
    KeybindingManager->>KeybindingManager: std_sort_by_order(result)
    KeybindingManager-->>ControlCenter: QList<CategoryInfo]
Loading

File-Level Changes

Change Details Files
Expose localized category metadata via new D-Bus types and APIs and extend ShortcutInfo/GestureInfo payloads accordingly.
  • Introduce CategoryInfo struct with key, displayName, order, isCustom and register it as a Qt and QDBus metatype
  • Add ListCategories() D-Bus method that builds a unique, ordered list of categories from modifiable key configs using per-app translation domains
  • Change ListShortcutsByCategory to take a QString category key instead of an int and propagate this through ShortcutInfo
  • Extend ShortcutInfo and GestureInfo with localLanguageCategory and isCustom flags and update their QDBusArgument operators and construction logic to fill these fields
src/plugin-qt/shortcut/src/core/keybindingmanager.h
src/plugin-qt/shortcut/src/core/keybindingmanager.cpp
src/plugin-qt/shortcut/src/core/gesturemanager.h
src/plugin-qt/shortcut/src/core/gesturemanager.cpp
Switch internal shortcut/gesture configuration to string-based category keys and simplify mutability rules.
  • Replace the Category enum with a CategoryKey namespace exposing framework-reserved string keys for System, Window, Workspace, and Custom
  • Change BaseConfig, KeyConfig, and GestureConfig category fields from int to QString and adjust DConfig parsing to read category as string
  • Relax configCanNotChanged logic so non-modifiable configs alone are treated as immutable, independent of category value
src/plugin-qt/shortcut/src/core/shortcutconfig.h
src/plugin-qt/shortcut/src/config/configloader.cpp
Ensure category strings participate in i18n and add TS entries for framework category labels.
  • Extend extract_shortcuts_i18n.py to collect and emit both displayName and category values from modifiable shortcut/gesture JSON configs into the dummy C++ translation source
  • Add TS messages for System, Window, Workspace, and Custom (where applicable) to keybinding and dde-app shortcut translation files, including zh_CN with completed translations and many other locales with unfinished stubs
  • Set language attribute on org.deepin.dde.shortcut.dde-app_zh_CN.ts and remove obsolete Task Switcher message from multiple locale TS files
src/plugin-qt/shortcut/tools/extract_shortcuts_i18n.py
src/plugin-qt/shortcut/translations/org.deepin.dde.keybinding_en.ts
src/plugin-qt/shortcut/translations/org.deepin.dde.keybinding_zh_CN.ts
src/plugin-qt/shortcut/dde-app-shortcuts/translations/org.deepin.dde.shortcut.dde-app_zh_CN.ts
src/plugin-qt/shortcut/dde-app-shortcuts/translations/org.deepin.dde.shortcut.dde-app.ts
src/plugin-qt/shortcut/dde-app-shortcuts/translations/org.deepin.dde.shortcut.dde-app_*.ts

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

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

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.

@yixinshark yixinshark force-pushed the fix/shortcut-dynamic-categories branch from 0415f93 to f0d6d00 Compare June 29, 2026 07:45
@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: yixinshark

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

Expose shortcut category keys as strings, add a ListCategories D-Bus API, and return localized category display names from the shortcut service. Update DConfig shortcut categories and i18n extraction so system, window, workspace, custom, and app-provided categories are translated through the same per-app translation path.

将快捷键分类从数字枚举改为字符串键,新增 ListCategories D-Bus 接口,并由快捷键服务返回本地化后的分类显示名。同步更新 DConfig 快捷键分类和翻译提取逻辑,使系统、窗口、工作区、自定义以及应用提供的分类都走同一套按应用翻译的路径。

Log: add dynamic category metadata
Pms: BUG-367657,BUG-365993
Change-Id: I3c0440ff9a91c48b1946b228ac942973ba40c41c
@yixinshark yixinshark force-pushed the fix/shortcut-dynamic-categories branch from f0d6d00 to 37ceced Compare June 29, 2026 07:54
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:60分

■ 【总体评价】

代码实现了快捷键类别从整型到字符串的架构级重构,但构建脚本存在代码注入风险
因构建脚本未转义外部输入导致存在中危代码注入漏洞,触发安全上限扣分

■ 【详细分析】

  • 1.语法逻辑(基本正确)✓

代码将 Category 从枚举改为字符串命名空间,configloader.cpp 中解析逻辑从 toInt() 同步改为 toString(),ListShortcutsByCategory 参数类型匹配,核心重构逻辑自洽
潜在问题:configloader.cpp 中 configCanNotChanged 的判断去除了对 System 类别的强依赖,变为仅判断 modifiable,需确认这是预期行为而非遗漏
建议:在提交信息或代码注释中明确说明 configCanNotChanged 逻辑简化的原因

  • 2.代码质量(良好)✓

代码结构清晰,新增的 ListCategories 方法使用哈希表去重和预定义排序,翻译文件及提取脚本同步更新,整体符合规范
潜在问题:numlock 的 JSON 配置文件中遗漏了 name[zh_CN] 和 description 字段的更新,仍保留旧格式"快捷键类别,1:系统"
建议:补全 numlock 对应 JSON 中的描述字段更新,保持所有配置文件的一致性

  • 3.代码性能(高效)✓

ListCategories 方法通过 QHash 进行 O(1) 去重,随后使用 std::sort 进行排序,整体时间复杂度为 O(N log N),对于系统快捷键数量规模完全无性能瓶颈
建议:无需优化

  • 4.代码安全(存在 1 个安全漏洞)✕

漏洞对比统计:新增漏洞 1 个,减少漏洞 0 个,持平 0 个
构建脚本在处理外部第三方应用提供的配置文件时,缺乏对字符串的转义处理,存在代码注入风险

  • 安全漏洞1():【中危】[代码注入] 在 [extract_shortcuts_i18n.py] 中,[第三方应用可在快捷键配置的 category 或 displayName 字段中注入双引号等特殊字符,脚本将其直接拼接到 f-string 写入 C++ 源码,导致构建时语法破坏或潜在的代码注入执行] ——非常重要

  • 建议:在将 name 写入文件前,使用 replace() 方法转义反斜杠和双引号,如 name.replace('\', '\\').replace('"', '\"')

■ 【改进建议代码示例】

--- a/src/plugin-qt/shortcut/tools/extract_shortcuts_i18n.py
+++ b/src/plugin-qt/shortcut/tools/extract_shortcuts_i18n.py
@@ -48,7 +48,9 @@ def extract_translations(config_dir, output_file, app_id):
         f.write('// The array is marked as unused to suppress compiler warnings\n')
         f.write('[[maybe_unused]] static const char* shortcut_names[] = {\n')
         for name in sorted(translatable):
-            f.write(f'    QT_TRANSLATE_NOOP("{app_id}", "{name}"),\n')
+            safe_name = name.replace('\\', '\\\\').replace('"', '\\"')
+            safe_app_id = app_id.replace('\\', '\\\\').replace('"', '\\"')
+            f.write(f'    QT_TRANSLATE_NOOP("{safe_app_id}", "{safe_name}"),\n')
         f.write('};\n')

@deepin-ci-robot

Copy link
Copy Markdown

@yixinshark: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
github-pr-review-ci 37ceced link true /test github-pr-review-ci

Full PR test history. Your PR dashboard.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

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.

2 participants