Skip to content

Commit 3c1a144

Browse files
committed
✨ feat: 添加“收藏”功能至托盘菜单
1 parent 8ed13d4 commit 3c1a144

File tree

4 files changed

+56
-10
lines changed

4 files changed

+56
-10
lines changed

src/i18n/lang/en-US/common.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ export default {
4747
prev: 'Previous',
4848
next: 'Next',
4949
pause: 'Pause',
50-
play: 'Play'
50+
play: 'Play',
51+
favorite: 'Favorite'
5152
},
5253
language: 'Language'
5354
};

src/i18n/lang/zh-CN/common.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export default {
4747
prev: '上一首',
4848
next: '下一首',
4949
pause: '暂停',
50-
play: '播放'
50+
play: '播放',
51+
favorite: '收藏'
5152
}
5253
};

src/main/modules/tray.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,18 @@ export function updateTrayMenu(mainWindow: BrowserWindow) {
174174
})
175175
);
176176

177+
// 收藏
178+
menu.append(
179+
new MenuItem({
180+
label: i18n.global.t('common.tray.favorite'),
181+
type: 'normal',
182+
click: () => {
183+
console.log('[Tray] 发送收藏命令 - macOS菜单');
184+
mainWindow.webContents.send('global-shortcut', 'toggleFavorite');
185+
}
186+
})
187+
);
188+
177189
menu.append(
178190
new MenuItem({
179191
label: i18n.global.t('common.tray.next'),
@@ -253,6 +265,14 @@ export function updateTrayMenu(mainWindow: BrowserWindow) {
253265
mainWindow.show();
254266
}
255267
},
268+
{
269+
label: i18n.global.t('common.tray.favorite'),
270+
type: 'normal',
271+
click: () => {
272+
console.log('[Tray] 发送收藏命令 - Windows/Linux菜单');
273+
mainWindow.webContents.send('global-shortcut', 'toggleFavorite');
274+
}
275+
},
256276
{ type: 'separator' },
257277
{
258278
label: i18n.global.t('common.tray.prev'),

src/renderer/utils/appShortcuts.ts

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ import { showShortcutToast } from './shortcutToast';
1111
let actionTimeout: NodeJS.Timeout | null = null;
1212
const ACTION_DELAY = 300; // 毫秒
1313

14+
// 添加一个操作锁,记录最后一次操作的时间和动作
15+
let lastActionInfo = {
16+
action: '',
17+
timestamp: 0
18+
};
19+
1420
interface ShortcutConfig {
1521
key: string;
1622
enabled: boolean;
@@ -31,17 +37,33 @@ let appShortcuts: ShortcutsConfig = {};
3137
* @param action 快捷键动作
3238
*/
3339
export async function handleShortcutAction(action: string) {
40+
const now = Date.now();
41+
3442
// 如果存在未完成的动作,则忽略当前请求
3543
if (actionTimeout) {
36-
console.log('忽略快速连续的动作请求:', action);
44+
console.log('[AppShortcuts] 忽略快速连续的动作请求:', action);
45+
return;
46+
}
47+
48+
// 检查是否是同一个动作的重复触发(300ms内)
49+
if (lastActionInfo.action === action && now - lastActionInfo.timestamp < ACTION_DELAY) {
50+
console.log(`[AppShortcuts] 忽略重复的 ${action} 动作,距上次仅 ${now - lastActionInfo.timestamp}ms`);
3751
return;
3852
}
53+
54+
// 更新最后一次操作信息
55+
lastActionInfo = {
56+
action,
57+
timestamp: now
58+
};
3959

4060
// 设置防抖锁
4161
actionTimeout = setTimeout(() => {
4262
actionTimeout = null;
4363
}, ACTION_DELAY);
4464

65+
console.log(`[AppShortcuts] 执行动作: ${action}, 时间戳: ${now}`);
66+
4567
const playerStore = usePlayerStore();
4668
const settingsStore = useSettingsStore();
4769

@@ -93,16 +115,19 @@ export async function handleShortcutAction(action: string) {
93115
case 'toggleFavorite': {
94116
const isFavorite = playerStore.favoriteList.includes(Number(playerStore.playMusic.id));
95117
const numericId = Number(playerStore.playMusic.id);
118+
console.log(`[AppShortcuts] toggleFavorite 当前状态: ${isFavorite}, ID: ${numericId}`);
96119
if (isFavorite) {
97120
playerStore.removeFromFavorite(numericId);
121+
console.log(`[AppShortcuts] 已从收藏中移除: ${numericId}`);
98122
} else {
99123
playerStore.addToFavorite(numericId);
124+
console.log(`[AppShortcuts] 已添加到收藏: ${numericId}`);
100125
}
101126
showToast(
102127
isFavorite
103-
? t('player.playBar.favorite', { name: playerStore.playMusic.name })
104-
: t('player.playBar.unFavorite', { name: playerStore.playMusic.name }),
105-
isFavorite ? 'ri-heart-fill' : 'ri-heart-line'
128+
? t('player.playBar.unFavorite', { name: playerStore.playMusic.name })
129+
: t('player.playBar.favorite', { name: playerStore.playMusic.name }),
130+
isFavorite ? 'ri-heart-line' : 'ri-heart-fill'
106131
);
107132
break;
108133
}
@@ -114,10 +139,9 @@ export async function handleShortcutAction(action: string) {
114139
console.error(`执行快捷键动作 ${action} 时出错:`, error);
115140
} finally {
116141
// 确保在出错时也能清除超时
117-
if (actionTimeout) {
118-
clearTimeout(actionTimeout);
119-
actionTimeout = null;
120-
}
142+
clearTimeout(actionTimeout);
143+
actionTimeout = null;
144+
console.log(`[AppShortcuts] 动作完成: ${action}, 时间戳: ${Date.now()}, 耗时: ${Date.now() - now}ms`);
121145
}
122146
}
123147

0 commit comments

Comments
 (0)