-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Refactor: migrate shortcuts to preferences #11050
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v2
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note
This issue/comment/review was translated by Claude.
In the preference storage, it is recommended to only save { enabled: boolean; key: string[] }, while all other non-modifiable properties should be defined in the template definition.
Original Content
preference的存储中,建议只保存 { enabled: bolean; key: string[] },其他不可修改的,全部在template def中定义即可
| }) | ||
|
|
||
| useShortcut('search_message', () => { | ||
| useShortcut('shortcut.app.search_message', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note
This issue/comment/review was translated by Claude.
You actually don't need to add the "shortcut." prefix anymore, you can simplify it a bit. BTW, TypeScript supports type inference with automatic prefix addition, so it won't affect TypeScript.
Original Content
这里其实不需要再加 shortcut. 前缀了,可以简化一些。btw,ts支持自动添加前缀后的类型推断,所以不会影响tsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note
This issue/comment/review was translated by Claude.
Some shortcuts may not be supported on all operating systems. For example, the text selection assistant is currently not supported on Linux. Therefore, it would be better to have system-specific settings.
Original Content
有些快捷键可能不是所有操作系统都支持,例如划词助手,现在linux是不支持的。所以如果有按系统的设定,会比较好。
Cherry Studio 快捷键系统重构设计文档 v2.0
目录
背景与目标
旧版快捷键系统存在以下问题:
configManager,与 v2 架构不兼容;switch-case硬编码,可维护性差;新版系统要实现:
preferenceService管理;switch-case依赖,改用 Map 注册;preferenceService自动推送变更。核心原则
关注点分离
复用基础设施
preferenceService(SQLite + 内存缓存 + IPC);架构分层
关键实现
1. 静态定义
packages/shared/shortcuts/definitions.ts中集中维护;scope(main / renderer / both)、category、persistOnBlur等元信息;enabledWhen支持动态启用(如 mini window 与 quick assistant 开关关联);preferenceSchemas.ts中声明默认值;definitions.ts中补充静态定义;2. 偏好系统
preferenceService读写;PreferenceShortcutType结构保持一致;ShortcutService/useShortcuts访问偏好时统一调用coerceShortcutPreference,确保 fallback 与类型安全;preferenceService.setMultiple实现。3. 主进程服务
ShortcutService负责:switch-case;persistOnBlur:例如show_main_window在窗口失焦时仍可触发;shortcut.app.show_settings会在需要时唤起窗口并调用window.navigate('/settings/provider'),避免重复 blur/focus。4. 渲染进程 Hook
useShortcut:从偏好获取绑定 → 转为react-hotkeys-hook字符串 → 注册快捷键;useShortcutDisplay:转换为 UI 显示字符串(⌘/Ctrl+等);useAllShortcuts:批量拉取配置 + diff 默认值,供设置面板使用;enableOnContentEditable等配置支撑设置页和富文本场景。5. 设置界面
ShortcutSettings直接消费useAllShortcuts;convertKeyToAccelerator/isValidShortcut/formatShortcutDisplay;preferenceService.setMultiple一次性写入默认配置;hasCustomBinding,区分用户自定义与继承默认值。数据流
启动阶段
preferenceService.initialize()载入缓存;shortcutService构造时注册处理器与订阅;shortcutService.registerForWindow,在focus时注册主进程快捷键。运行时变更
preferenceService.set/setMultiple;globalShortcut.unregisterAll()→ 按新配置重注册;usePreference/useMultiplePreferences自动收到更新,UI 即时刷新。默认快捷键
shortcut.app.show_main_windowCmd/Ctrl + Shift + Ashortcut.app.show_mini_windowCmd/Ctrl + Eshortcut.app.show_settingsCmd/Ctrl + ,shortcut.app.toggle_show_assistantsCmd/Ctrl + [shortcut.app.exit_fullscreenEscapeshortcut.app.zoom_in/out/resetCmd/Ctrl + = / - / 0shortcut.app.search_messageCmd/Ctrl + Shift + Fshortcut.chat.clearCmd/Ctrl + Lshortcut.chat.search_messageCmd/Ctrl + Fshortcut.chat.toggle_new_contextCmd/Ctrl + Kshortcut.chat.copy_last_messageCmd/Ctrl + Shift + Cshortcut.chat.edit_last_user_messageCmd/Ctrl + Shift + Eshortcut.topic.newCmd/Ctrl + Nshortcut.topic.renameCmd/Ctrl + Tshortcut.topic.toggle_show_topicsCmd/Ctrl + ]shortcut.selection.*迁移与兼容性
shortcut.topic.rename)在数据库不存在时继承新默认;configManager已彻底移除;IpcChannel.Shortcuts_Update与window.api.shortcuts.update相关逻辑已弃用;PreferenceMigrator中保留与旧 keys 的映射,确保升级顺畅。后续演进方向