Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

✨ feat(desktop): support clearing hotkey bindings in ShortcutManager (#12727)

Innei5920500371ea
diff --git a/apps/desktop/src/main/core/ui/ShortcutManager.ts b/apps/desktop/src/main/core/ui/ShortcutManager.ts
index 736dc187497..ad7e6a4685f 100644
--- a/apps/desktop/src/main/core/ui/ShortcutManager.ts
+++ b/apps/desktop/src/main/core/ui/ShortcutManager.ts
@@ -84,13 +84,23 @@ export class ShortcutManager {
}
// 2. Basic format validation
- if (!accelerator || typeof accelerator !== 'string' || accelerator.trim() === '') {
+ if (typeof accelerator !== 'string') {
logger.error(`Invalid accelerator format: ${accelerator}`);
return { errorType: 'INVALID_FORMAT', success: false };
}
+ const trimmedAccelerator = accelerator.trim();
+
+ // Empty value means disable this shortcut binding
+ if (trimmedAccelerator === '') {
+ this.shortcutsConfig[id] = '';
+ this.saveShortcutsConfig();
+ this.registerConfiguredShortcuts();
+ return { success: true };
+ }
+
// Convert frontend format to Electron format
- const convertedAccelerator = this.convertAcceleratorFormat(accelerator.trim());
+ const convertedAccelerator = this.convertAcceleratorFormat(trimmedAccelerator);
const cleanAccelerator = convertedAccelerator.toLowerCase();
logger.debug(`Converted accelerator from ${accelerator} to ${convertedAccelerator}`);
@@ -221,7 +231,7 @@ export class ShortcutManager {
// If no configuration, use default configuration
if (!config || Object.keys(config).length === 0) {
logger.debug('No shortcuts config found, using defaults');
- this.shortcutsConfig = DEFAULT_SHORTCUTS_CONFIG;
+ this.shortcutsConfig = { ...DEFAULT_SHORTCUTS_CONFIG };
this.saveShortcutsConfig();
} else {
// Filter out invalid shortcuts that are not in DEFAULT_SHORTCUTS_CONFIG
@@ -257,7 +267,7 @@ export class ShortcutManager {
logger.debug('Loaded shortcuts config:', this.shortcutsConfig);
} catch (error) {
logger.error('Error loading shortcuts config:', error);
- this.shortcutsConfig = DEFAULT_SHORTCUTS_CONFIG;
+ this.shortcutsConfig = { ...DEFAULT_SHORTCUTS_CONFIG };
this.saveShortcutsConfig();
}
}
diff --git a/apps/desktop/src/main/core/ui/__tests__/ShortcutManager.test.ts b/apps/desktop/src/main/core/ui/__tests__/ShortcutManager.test.ts
index 6a68a2667fe..b62737c8e54 100644
--- a/apps/desktop/src/main/core/ui/__tests__/ShortcutManager.test.ts
+++ b/apps/desktop/src/main/core/ui/__tests__/ShortcutManager.test.ts
@@ -175,11 +175,17 @@ describe('ShortcutManager', () => {
expect(result.errorType).toBe('INVALID_ID');
});
- it('should reject empty accelerator', () => {
+ it('should clear shortcut when accelerator is empty', () => {
const result = shortcutManager.updateShortcutConfig('showApp', '');
- expect(result.success).toBe(false);
- expect(result.errorType).toBe('INVALID_FORMAT');
+ expect(result.success).toBe(true);
+ expect(result.errorType).toBeUndefined();
+ expect(mockStoreManager.set).toHaveBeenCalledWith(
+ 'shortcuts',
+ expect.objectContaining({
+ showApp: '',
+ }),
+ );
});
it('should reject accelerator without modifier keys', () => {
diff --git a/locales/zh-CN/setting.json b/locales/zh-CN/setting.json
index a31952f5ea9..b80316c79d9 100644
--- a/locales/zh-CN/setting.json
+++ b/locales/zh-CN/setting.json
@@ -207,6 +207,7 @@
"header.sessionDesc": "助理档案与会话偏好",
"header.sessionWithName": "会话设置 · {{name}}",
"header.title": "设置",
+ "hotkey.clearBinding": "取消绑定",
"hotkey.conflicts": "与现有快捷键冲突",
"hotkey.errors.CONFLICT": "快捷键冲突:该快捷键已被其他功能占用",
"hotkey.errors.INVALID_FORMAT": "快捷键格式无效:请使用正确的格式(如 CommandOrControl+E)",
diff --git a/package.json b/package.json
index 635f7c326b5..3de1efe45a9 100644
--- a/package.json
+++ b/package.json
@@ -239,7 +239,7 @@
"@lobehub/icons": "^5.0.0",
"@lobehub/market-sdk": "^0.31.3",
"@lobehub/tts": "^5.1.2",
- "@lobehub/ui": "^5.0.0",
+ "@lobehub/ui": "^5.4.0",
"@modelcontextprotocol/sdk": "^1.26.0",
"@napi-rs/canvas": "^0.1.88",
"@neondatabase/serverless": "^1.0.2",
diff --git a/src/locales/default/setting.ts b/src/locales/default/setting.ts
index 9a49437ef1f..8882eac77db 100644
--- a/src/locales/default/setting.ts
+++ b/src/locales/default/setting.ts
@@ -218,6 +218,7 @@ export default {
'header.sessionDesc': 'Agent Profile and session preferences',
'header.sessionWithName': 'Session Settings · {{name}}',
'header.title': 'Settings',
+ 'hotkey.clearBinding': 'Clear binding',
'hotkey.conflicts': 'Conflicts with existing hotkeys',
'hotkey.errors.CONFLICT': 'Hotkey conflict: This hotkey is already assigned to another function',
'hotkey.errors.INVALID_FORMAT':
diff --git a/src/routes/(main)/settings/hotkey/features/Conversation.tsx b/src/routes/(main)/settings/hotkey/features/Conversation.tsx
index e9beb30edb3..bb50266e0b9 100644
--- a/src/routes/(main)/settings/hotkey/features/Conversation.tsx
+++ b/src/routes/(main)/settings/hotkey/features/Conversation.tsx
@@ -25,6 +25,19 @@ const HotkeySetting = memo(() => {
if (!isUserStateInit) return <Skeleton active paragraph={{ rows: 5 }} title={false} />;
+ const clearHotkeyBinding = async (id: HotkeyItem['id']) => {
+ if (!hotkey[id]) return;
+
+ setLoading(true);
+ form.setFieldValue(id, '');
+
+ try {
+ await setSettings({ hotkey: { [id]: '' } });
+ } finally {
+ setLoading(false);
+ }
+ };
+
const mapHotkeyItem = (item: HotkeyItem) => {
const hotkeyConflicts = Object.entries(hotkey)
.map(([key, value]) => {
@@ -36,10 +49,13 @@ const HotkeySetting = memo(() => {
return {
children: (
<HotkeyInput
+ allowClear={!item.nonEditable}
disabled={item.nonEditable}
hotkeyConflicts={hotkeyConflicts}
placeholder={t('hotkey.record')}
resetValue={item.keys}
+ texts={{ clear: t('hotkey.clearBinding') }}
+ onClear={() => void clearHotkeyBinding(item.id)}
/>
),
desc: hotkeyMeta[`${item.id}.desc`] ? t(`${item.id}.desc`, { ns: 'hotkey' }) : undefined,
@@ -66,8 +82,11 @@ const HotkeySetting = memo(() => {
variant={'filled'}
onValuesChange={async (values) => {
setLoading(true);
- await setSettings({ hotkey: values });
- setLoading(false);
+ try {
+ await setSettings({ hotkey: values });
+ } finally {
+ setLoading(false);
+ }
}}
{...FORM_STYLE}
/>
diff --git a/src/routes/(main)/settings/hotkey/features/Desktop.tsx b/src/routes/(main)/settings/hotkey/features/Desktop.tsx
index 427c79570bc..50b1a6472af 100644
--- a/src/routes/(main)/settings/hotkey/features/Desktop.tsx
+++ b/src/routes/(main)/settings/hotkey/features/Desktop.tsx
@@ -34,30 +34,34 @@ const HotkeySetting = memo(() => {
if (!isHotkeysInit) return <Skeleton active paragraph={{ rows: 5 }} title={false} />;
+ const updateHotkey = async (id: DesktopHotkeyItem['id'], value: string) => {
+ setLoading(true);
+ try {
+ const result = await updateDesktopHotkey(id, value);
+ if (result.success) {
+ message.success(t('hotkey.updateSuccess', { ns: 'setting' }));
+ } else {
+ // Show the appropriate error message based on error type
+ message.error(t(`hotkey.errors.${result.errorType}` as any, { ns: 'setting' }));
+ }
+ } catch {
+ message.error(t('hotkey.updateError', { ns: 'setting' }));
+ } finally {
+ setLoading(false);
+ }
+ };
+
const mapHotkeyItem = (item: DesktopHotkeyItem) => ({
children: (
<HotkeyInput
+ allowClear={!item.nonEditable}
disabled={item.nonEditable}
placeholder={t('hotkey.record')}
resetValue={item.keys}
+ texts={{ clear: t('hotkey.clearBinding') }}
value={hotkeys[item.id]}
- onChange={async (value) => {
- setLoading(true);
- try {
- const result = await updateDesktopHotkey(item.id, value);
- if (result.success) {
- message.success(t('hotkey.updateSuccess', { ns: 'setting' }));
- } else {
- // Show the appropriate error message based on error type
-
- message.error(t(`hotkey.errors.${result.errorType}` as any, { ns: 'setting' }));
- }
- } catch {
- message.error(t('hotkey.updateError', { ns: 'setting' }));
- } finally {
- setLoading(false);
- }
- }}
+ onChange={(value) => void updateHotkey(item.id, value)}
+ onClear={() => void updateHotkey(item.id, '')}
/>
),
diff --git a/src/routes/(main)/settings/hotkey/features/Essential.tsx b/src/routes/(main)/settings/hotkey/features/Essential.tsx
index f2700dc5e2a..73b1ac82bdb 100644
--- a/src/routes/(main)/settings/hotkey/features/Essential.tsx
+++ b/src/routes/(main)/settings/hotkey/features/Essential.tsx
@@ -25,6 +25,19 @@ const HotkeySetting = memo(() => {
if (!isUserStateInit) return <Skeleton active paragraph={{ rows: 5 }} title={false} />;
+ const clearHotkeyBinding = async (id: HotkeyItem['id']) => {
+ if (!hotkey[id]) return;
+
+ setLoading(true);
+ form.setFieldValue(id, '');
+
+ try {
+ await setSettings({ hotkey: { [id]: '' } });
+ } finally {
+ setLoading(false);
+ }
+ };
+
const mapHotkeyItem = (item: HotkeyItem) => {
const hotkeyConflicts = Object.entries(hotkey)
.map(([key, value]) => {
@@ -36,10 +49,13 @@ const HotkeySetting = memo(() => {
return {
children: (
<HotkeyInput
+ allowClear={!item.nonEditable}
disabled={item.nonEditable}
hotkeyConflicts={hotkeyConflicts}
placeholder={t('hotkey.record')}
resetValue={item.keys}
+ texts={{ clear: t('hotkey.clearBinding') }}
+ onClear={() => void clearHotkeyBinding(item.id)}
/>
),
desc: hotkeyMeta[`${item.id}.desc`] ? t(`${item.id}.desc`, { ns: 'hotkey' }) : undefined,
@@ -66,8 +82,11 @@ const HotkeySetting = memo(() => {
variant={'filled'}
onValuesChange={async (values) => {
setLoading(true);
- await setSettings({ hotkey: values });
- setLoading(false);
+ try {
+ await setSettings({ hotkey: values });
+ } finally {
+ setLoading(false);
+ }
}}
{...FORM_STYLE}
/>