Skip to content

feat(keyboard): wire Wayland custom shortcut CRUD#3332

Merged
yixinshark merged 1 commit into
linuxdeepin:stack/shortcut-dynamic-categories-basefrom
yixinshark:feat/keyboard-wayland-custom-crud
Jul 8, 2026
Merged

feat(keyboard): wire Wayland custom shortcut CRUD#3332
yixinshark merged 1 commit into
linuxdeepin:stack/shortcut-dynamic-categories-basefrom
yixinshark:feat/keyboard-wayland-custom-crud

Conversation

@yixinshark

Copy link
Copy Markdown
Contributor

Summary

  • Wire Wayland Add/Modify/Delete custom shortcut calls through the new shortcut service APIs.
  • Fetch custom shortcut commands asynchronously for runtime custom rows.
  • Keep dynamic category and custom row updates synchronized in the shortcut model.

Test plan

  • cmake --build build --target keyboard mouse

Dependency

Stacked on #3320. Base branch stack/shortcut-dynamic-categories-base points to the #3320 commit so this PR contains only the Wayland custom shortcut CRUD commit.

@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.

Sorry @yixinshark, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@yixinshark yixinshark force-pushed the feat/keyboard-wayland-custom-crud branch 3 times, most recently from fc6436e to 38ddd00 Compare July 3, 2026 09:49
Wire the control center's add/modify/delete custom shortcut flows to the
new dde-services Keybinding1 custom CRUD methods on Wayland, replacing
the previous early-return stubs. Each Wayland call is observed with a
QDBusPendingCallWatcher so failures trigger refreshShortcut()/onAdded/
onShortcutChanged instead of mutating the local model optimistically.

Fetch a custom shortcut's command via the new GetShortcutCommand D-Bus
method: requestShortcutCommand fires one async call per custom shortcut
after ListAllShortcuts/SearchShortcuts/GetShortcut/ShortcutChanged, and
onGetShortcutCommandFinished overlays the Exec field onto the already-
built JSON object and re-emits shortcutQueried. The whole ShortcutInfo
JSON is stashed on the watcher so the handler needs no field-by-field
reconstruction.

ShortcutModel::onKeyBindingChanged now handles insert-on-change for new
ids arriving via ShortcutChanged: section-bearing configs are grouped
into m_groupInfos, sectionless Custom configs get the legacy "Custom"
group, and a dead unreachable tail (invalidateSystemShortcutNamesCache
+ addCustomInfo after two returning branches) is removed.

Extract KeyboardWorker::callModifyCustomShortcut() so the Wayland
modify path (modifyCustomShortcut and setNewCustomShortcut) shares one
watcher/id/type/connect setup. setNewCustomShortcut keeps the X11
fallback calling ModifyCustomShortcut directly, since on X11 it is also
invoked from onCustomConflictCleanFinished after conflict cleanup.

将控制中心自定义快捷键的增删改流程在 Wayland 下接入新的 dde-services
Keybinding1 自定义增删改方法,替换原先的提前 return 桩。每次 Wayland 调用
均用 QDBusPendingCallWatcher 观察结果,失败时触发 refreshShortcut()/
onAdded/onShortcutChanged,而不再乐观地修改本地模型。

通过新增的 GetShortcutCommand D-Bus 方法获取自定义快捷键命令:
requestShortcutCommand 在 ListAllShortcuts/SearchShortcuts/GetShortcut/
ShortcutChanged 之后对每个自定义快捷键发起一次异步调用,
onGetShortcutCommandFinished 将 Exec 字段叠加到已构建好的 JSON 对象上并
重新发射 shortcutQueried。整个 ShortcutInfo JSON 暂存在 watcher 上,处理
函数无需逐字段重建。

ShortcutModel::onKeyBindingChanged 现支持通过 ShortcutChanged 到达的新 id
的"变更即插入":带 Section 的配置归入 m_groupInfos,无 Section 的 Custom
配置归入传统"Custom"分组,并删除了两个 return 分支之后不可达的死代码尾段。

抽取 KeyboardWorker::callModifyCustomShortcut(),让 Wayland 修改路径
(modifyCustomShortcut 与 setNewCustomShortcut)共用一份 watcher/id/type/
connect 设置。setNewCustomShortcut 保留 X11 直接调用 ModifyCustomShortcut
的兜底,因为 X11 下它也会在冲突清理完成后由 onCustomConflictCleanFinished
调用。

Log: feat(keyboard): wire custom shortcut CRUD on Wayland
Change-Id: I793aa520968c92d03bb603721f00b1c7ac252fa5
@yixinshark yixinshark force-pushed the feat/keyboard-wayland-custom-crud branch from 38ddd00 to fcc40a4 Compare July 3, 2026 10:05
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:95分

■ 【总体评价】

代码完整实现了Wayland下自定义快捷键的异步增删改链路,逻辑严密且无明显缺陷。
评分理由:异步D-Bus回调处理正确,Model层动态插入逻辑完善,无安全漏洞,符合优秀标准仅因微小的代码风格细节未满分。

■ 【详细分析】

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

代码在Controller层移除了直接忽略的stub,改为调用Worker的异步方法;Worker层通过QDBusPendingCallWatcher正确处理了Add/Modify/Delete的异步回调,失败时调用refreshShortcut回滚,成功时更新Model;Model层的onKeyBindingChanged补充了未知ID的insert分支,动态创建ShortcutInfo并正确插入m_infos和m_groupInfos,处理了Section为空的自定义快捷键归组逻辑。
潜在问题:无
建议:无

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

提取了fillShortcutInfoFromJson函数消除了JSON解析的重复代码;提取了callModifyCustomShortcut统一处理Wayland下的修改逻辑,降低了代码重复度;QML层通过pendingCommandId状态变量和脏检查机制有效避免了异步流程中的无意义回滚和竞态弹窗。
潜在问题:QML中pendingCommandId的清理分散在onCheckedChanged、onRequestDeleteKeys、onClosing、restoreShortcutView等多处,状态维护略显分散。
建议:可考虑将pendingCommandId及其清理逻辑封装到一个更内聚的QML状态机或小型的JS对象中,降低遗漏清理的风险。

  • 3.代码性能(无性能问题)✓

使用QDBusPendingCallWatcher实现真正的异步非阻塞调用,避免了主线程阻塞;Model层在更新搜索列表时采用线性遍历,但桌面环境快捷键数量通常在百级别,O(N)复杂度完全在可接受范围内;新增的动态分组操作也仅涉及轻量级的QList指针操作。
建议:无

  • 4.代码安全(存在0个安全漏洞)✓

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
代码仅作为D-Bus代理层转发用户输入至后端dde-services服务,未直接执行系统命令或进行文件操作;JSON解析使用Qt原生的QJsonDocument,对畸形数据有良好的容错性(返回空对象并被isEmpty检查拦截);日志输出仅包含D-Bus错误信息,无敏感数据泄露风险。
建议:无

■ 【改进建议代码示例】

// 无严重安全或逻辑缺陷,仅提供一处微小的防御性编程与性能改进示例
// 在 shortcutmodel.cpp 的 onKeyBindingChanged 中,优化字符串转换并增加防御性检查
void ShortcutModel::onKeyBindingChanged(const QString &value)
{
    // 建议改用 toUtf8() 避免 toStdString().c_str() 带来的隐式内存分配与转换开销
    const QJsonObject obj = QJsonDocument::fromJson(value.toUtf8()).object();
    const QString update_id = obj["Id"].toString();
    const int update_type = obj["Type"].toInt();
    // ... existing code ...
    
    // 新增 insert 分支时的防御性编程
    ShortcutInfo *info = new ShortcutInfo();
    if (!info) { 
        qWarning() << "Failed to allocate ShortcutInfo";
        return;
    }
    fillShortcutInfoFromJson(info, obj);
    
    if (!info->sectionKey.isEmpty()) {
        m_infos.append(info);
        // ...
    }
}

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

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

@yixinshark yixinshark merged commit 2e5e19c into linuxdeepin:stack/shortcut-dynamic-categories-base Jul 8, 2026
20 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