Skip to content

fix: support dual key bindings for Ctrl+Shift layout switching#141

Merged
wyu71 merged 1 commit intolinuxdeepin:masterfrom
wyu71:master
Apr 17, 2026
Merged

fix: support dual key bindings for Ctrl+Shift layout switching#141
wyu71 merged 1 commit intolinuxdeepin:masterfrom
wyu71:master

Conversation

@wyu71
Copy link
Copy Markdown
Contributor

@wyu71 wyu71 commented Apr 16, 2026

Return two key combinations for Ctrl+Shift shortcut instead of single value.

Ctrl+Shift快捷键返回两组按键组合,支持多组快捷键写入配置。

Log: 修复Ctrl+Shift切换快捷键支持双按键组合
PMS: BUG-357563
Influence: Ctrl+Shift布局切换快捷键现在支持两组按键绑定,切换行为更准确。

Summary by Sourcery

Bug Fixes:

  • Fix Ctrl+Shift layout switching so it correctly supports and persists two key binding combinations instead of a single value.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Apr 16, 2026

Reviewer's Guide

Implements dual key binding support for the Ctrl+Shift layout-switching shortcut by returning two key combinations from the enum mapping and writing them into separate config entries, while keeping existing behavior for other shortcuts and ensuring a fallback single-binding path.

Sequence diagram for dual key binding write-back on Ctrl+Shift selection

sequenceDiagram
    actor User
    participant ShortcutsComboBox
    participant ShortcutsModule
    participant Fcitx5ConfigProxy

    User ->> ShortcutsComboBox: select CtrlShift option
    ShortcutsComboBox ->> ShortcutsModule: onActivated(currentIndex)
    ShortcutsModule ->> ShortcutsModule: reversed = reverseEnumerateForwardKeys(currentIndex)
    alt reversed is two dimensional array (CtrlShift)
        ShortcutsModule ->> Fcitx5ConfigProxy: setValue(Hotkey/EnumerateForwardKeys/0, [Control, Shift_L], true)
        ShortcutsModule ->> Fcitx5ConfigProxy: setValue(Hotkey/EnumerateForwardKeys/1, [Control, Shift, Control_L], true)
    else reversed is single binding (e.g. AltShift)
        ShortcutsModule ->> Fcitx5ConfigProxy: setValue(Hotkey/EnumerateForwardKeys/0, reversed, true)
        ShortcutsModule ->> Fcitx5ConfigProxy: setValue(Hotkey/EnumerateForwardKeys/1, [""], true)
    end
Loading

File-Level Changes

Change Details Files
Return dual key combinations for the Ctrl+Shift shortcut instead of a single enum token.
  • Adjust the mapping logic so that when the enum value is "Ctrl+Shift", it returns an array of two key sequences rather than a single identifier string.
  • Preserve existing handling for "Alt+Shift" and "None" while special-casing only the Ctrl+Shift shortcut.
src/dcc-fcitx5configtool/qml/ShortcutsModule.qml
Write dual key combinations into two configuration indices when saving Ctrl+Shift shortcuts, with backward-compatible handling for single shortcuts.
  • Capture the result of reverseEnumerateForwardKeys and branch on whether it is a nested array (two key combos) or a simple value (single combo).
  • For dual combos, write each key sequence into "Hotkey/EnumerateForwardKeys/0" and "Hotkey/EnumerateForwardKeys/1" respectively.
  • For single combos, continue to write the main key sequence to index 0 and explicitly clear index 1 with an empty key list to avoid stale configuration.
src/dcc-fcitx5configtool/qml/ShortcutsModule.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
Copy Markdown

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

  • reverseEnumerateForwardKeys now returns heterogeneous types (string vs nested array) but only this onActivated handler is aware of that; consider normalizing its return type or centralizing the branching logic so other current/future callers don’t have to special-case it.
  • The Ctrl+Shift handling directly embeds the two key sequences as literals; it may be clearer and less error-prone to derive these from a shared mapping/utility (similar to the existing ALT_SHIFT handling) rather than hardcoding the arrays inline.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- reverseEnumerateForwardKeys now returns heterogeneous types (string vs nested array) but only this onActivated handler is aware of that; consider normalizing its return type or centralizing the branching logic so other current/future callers don’t have to special-case it.
- The Ctrl+Shift handling directly embeds the two key sequences as literals; it may be clearer and less error-prone to derive these from a shared mapping/utility (similar to the existing ALT_SHIFT handling) rather than hardcoding the arrays inline.

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.

Return two key combinations for Ctrl+Shift shortcut instead of single value.

Ctrl+Shift快捷键返回两组按键组合,支持多组快捷键写入配置。

Log: 修复Ctrl+Shift切换快捷键支持双按键组合
PMS: BUG-357563
Influence: Ctrl+Shift布局切换快捷键现在支持两组按键绑定,切换行为更准确。
@deepin-ci-robot
Copy link
Copy Markdown

deepin pr auto review

这份代码的修改主要是对快捷键配置逻辑的优化和重构,特别是针对枚举快捷键的处理方式。以下是对该代码的详细审查意见:

1. 语法与逻辑审查

优点:

  • 常量提取:将配置组名 "Hotkey" 和配置项名(如 "TriggerKeys")提取为 readonly property(如 cfgGroup, cfgTriggerKey),这极大地提高了代码的可维护性。如果未来配置路径变更,只需修改一处。
  • 逻辑修正:在 reverseEnumerateForwardKeys 函数中,重写了快捷键转换逻辑。原代码似乎试图通过字符串分割和大小写转换来生成按键序列,但新代码直接根据 FCitx5 的实际需求返回了明确的键位组合数组(例如 ["Control", "Shift_L"])。这修复了潜在的逻辑错误,特别是对于 Ctrl+Shift 这种需要两个绑定来处理不同按键顺序的情况,注释解释得非常清楚。
  • 数据结构一致性reverseEnumerateForwardKeys 现在统一返回二维数组 [binding0, binding1],这比之前返回字符串或一维数组更符合底层 setValue 接口(看起来接受数组)的预期,也使得处理"无第二绑定"的情况([""])更加规范。

潜在问题与建议:

  • reverseEnumerateForwardKeys 的边界处理
    • 代码中处理了 index < 0index >= enumKeys.length 的情况,返回 [[""], [""]]。这是一个安全的默认值(表示无快捷键)。
    • onActivated 中直接调用了 reverseEnumerateForwardKeys(currentIndex)。由于 currentIndex 是由 ComboBox 控制的,理论上它不会越界。但如果 enumKeys 数组在运行时被动态修改(虽然这里是 readonly),可能会有风险。目前的实现是安全的。
  • calculateTriggerKeys 的逻辑
    • 该函数尝试获取 TriggerKeys 的第二个配置项("1")。代码使用了 hasOwnProperty 检查,这是很好的实践。
    • 然而,在 onActivated 的逻辑中,对于 currentIndex === 0 (Shift) 的情况,显式写入了两个键位 Shift_LShift_R。而对于其他情况(如 Ctrl+Space),它调用 reverseTriggerKeys 并将第二个键位设为 [""]。这与 reverseEnumerateForwardKeys 的逻辑保持一致,逻辑上是通顺的。

2. 代码质量

优点:

  • 注释清晰:新增的注释(如 // Ctrl+Shift needs two key bindings...)非常清晰地解释了为什么需要这种特定的数据结构,这对于维护者理解复杂的快捷键映射逻辑非常有帮助。
  • 代码风格:使用了 let 声明局部变量,符合 QML/JS 的现代风格。缩进和格式规范。
  • 错误处理:在多个地方使用了 try-catch 块来包裹配置读取操作,防止后端配置读取失败导致 QML 界面崩溃。

改进建议:

  • 魔法字符串:虽然提取了配置路径,但具体的键值名称(如 "Control", "Shift_L", "CTRL_SHIFT" 等)仍然散落在代码中。如果这些键值在多处使用,建议进一步提取为常量,例如:
    readonly property string keyControl: "Control"
    readonly property string keyShiftL: "Shift_L"
    // ...
  • 函数命名reverseEnumerateForwardKeys 这个名字中的 "reverse" 可能会让新开发者困惑(是反转数组?还是反向查找?)。实际上它是将 UI 的索引转换为后端的配置值。或许改名为 getEnumKeysConfigFromIndexmapIndexToEnumConfig 会更直观。

3. 代码性能

  • 配置读取:代码中多次调用 dccData.fcitx5ConfigProxy.globalConfigOption。虽然这是必要的,但要注意如果这是一个同步的 IPC 调用,频繁调用可能会影响 UI 流畅性。从代码结构看,这主要发生在初始化(Component.onCompleted)和回调(onRequestConfigFinished)中,这是合理的。
  • 字符串操作:新代码移除了旧版本中复杂的 split 和循环字符串处理,改为直接返回数组字面量,这减少了运行时的计算开销,提高了性能。

4. 代码安全

  • 输入验证reverseEnumerateForwardKeysindex 进行了边界检查,防止越界访问数组,这是很好的安全实践。
  • 空值处理:使用了 || []|| "" 来处理可能为 nullundefined 的返回值,防止了 JavaScript 中的常见错误。
  • 配置写入:在 onActivated 中写入配置时,使用了 cfgEnum0cfgEnum1 这种预定义的路径,避免了字符串拼接错误,提高了安全性。

总结

这是一次高质量的代码重构。主要改进点在于:

  1. 修复了快捷键映射逻辑:特别是针对 Ctrl+Shift 的双绑定处理,这很可能是修复了一个实际存在的 Bug(即只有一种按键顺序能触发切换)。
  2. 提升了代码可维护性:通过提取常量,减少了硬编码字符串。
  3. 增强了健壮性:更好的边界检查和空值处理。

唯一的小建议是考虑将具体的按键名称字符串(如 "Shift_L")也提取为常量,并考虑重命名 reverse... 系列函数以更准确地反映其功能(索引转配置)。除此之外,代码逻辑严密,注释详尽,符合高质量 QML 代码的标准。

@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: lzwind, wyu71

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

@wyu71 wyu71 merged commit 22a99f5 into linuxdeepin:master Apr 17, 2026
13 of 14 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