@@ -7,6 +7,10 @@ import { usePlayerStore, useSettingsStore } from '@/store';
7
7
import { isElectron } from '.' ;
8
8
import { showShortcutToast } from './shortcutToast' ;
9
9
10
+ // 添加一个简单的防抖机制
11
+ let actionTimeout : NodeJS . Timeout | null = null ;
12
+ const ACTION_DELAY = 300 ; // 毫秒
13
+
10
14
interface ShortcutConfig {
11
15
key : string ;
12
16
enabled : boolean ;
@@ -27,6 +31,17 @@ let appShortcuts: ShortcutsConfig = {};
27
31
* @param action 快捷键动作
28
32
*/
29
33
export async function handleShortcutAction ( action : string ) {
34
+ // 如果存在未完成的动作,则忽略当前请求
35
+ if ( actionTimeout ) {
36
+ console . log ( '忽略快速连续的动作请求:' , action ) ;
37
+ return ;
38
+ }
39
+
40
+ // 设置防抖锁
41
+ actionTimeout = setTimeout ( ( ) => {
42
+ actionTimeout = null ;
43
+ } , ACTION_DELAY ) ;
44
+
30
45
const playerStore = usePlayerStore ( ) ;
31
46
const settingsStore = useSettingsStore ( ) ;
32
47
@@ -38,61 +53,71 @@ export async function handleShortcutAction(action: string) {
38
53
showShortcutToast ( message , iconName ) ;
39
54
} ;
40
55
41
- switch ( action ) {
42
- case 'togglePlay' :
43
- if ( playerStore . play ) {
44
- await audioService . pause ( ) ;
45
- showToast ( t ( 'player.playBar.pause' ) , 'ri-pause-circle-line' ) ;
46
- } else {
47
- await audioService . play ( ) ;
48
- showToast ( t ( 'player.playBar.play' ) , 'ri-play-circle-line' ) ;
49
- }
50
- break ;
51
- case 'prevPlay' :
52
- playerStore . prevPlay ( ) ;
53
- showToast ( t ( 'player.playBar.prev' ) , 'ri-skip-back-line' ) ;
54
- break ;
55
- case 'nextPlay' :
56
- playerStore . nextPlay ( ) ;
57
- showToast ( t ( 'player.playBar.next' ) , 'ri-skip-forward-line' ) ;
58
- break ;
59
- case 'volumeUp' :
60
- if ( currentSound && currentSound ?. volume ( ) < 1 ) {
61
- currentSound ?. volume ( ( currentSound ?. volume ( ) || 0 ) + 0.1 ) ;
56
+ try {
57
+ switch ( action ) {
58
+ case 'togglePlay' :
59
+ if ( playerStore . play ) {
60
+ await audioService . pause ( ) ;
61
+ showToast ( t ( 'player.playBar.pause' ) , 'ri-pause-circle-line' ) ;
62
+ } else {
63
+ await audioService . play ( ) ;
64
+ showToast ( t ( 'player.playBar.play' ) , 'ri-play-circle-line' ) ;
65
+ }
66
+ break ;
67
+ case 'prevPlay' :
68
+ await playerStore . prevPlay ( ) ;
69
+ showToast ( t ( 'player.playBar.prev' ) , 'ri-skip-back-line' ) ;
70
+ break ;
71
+ case 'nextPlay' :
72
+ await playerStore . nextPlay ( ) ;
73
+ showToast ( t ( 'player.playBar.next' ) , 'ri-skip-forward-line' ) ;
74
+ break ;
75
+ case 'volumeUp' :
76
+ if ( currentSound && currentSound ?. volume ( ) < 1 ) {
77
+ currentSound ?. volume ( ( currentSound ?. volume ( ) || 0 ) + 0.1 ) ;
78
+ showToast (
79
+ `${ t ( 'player.playBar.volume' ) } ${ Math . round ( ( currentSound ?. volume ( ) || 0 ) * 100 ) } %` ,
80
+ 'ri-volume-up-line'
81
+ ) ;
82
+ }
83
+ break ;
84
+ case 'volumeDown' :
85
+ if ( currentSound && currentSound ?. volume ( ) > 0 ) {
86
+ currentSound ?. volume ( ( currentSound ?. volume ( ) || 0 ) - 0.1 ) ;
87
+ showToast (
88
+ `${ t ( 'player.playBar.volume' ) } ${ Math . round ( ( currentSound ?. volume ( ) || 0 ) * 100 ) } %` ,
89
+ 'ri-volume-down-line'
90
+ ) ;
91
+ }
92
+ break ;
93
+ case 'toggleFavorite' : {
94
+ const isFavorite = playerStore . favoriteList . includes ( Number ( playerStore . playMusic . id ) ) ;
95
+ const numericId = Number ( playerStore . playMusic . id ) ;
96
+ if ( isFavorite ) {
97
+ playerStore . removeFromFavorite ( numericId ) ;
98
+ } else {
99
+ playerStore . addToFavorite ( numericId ) ;
100
+ }
62
101
showToast (
63
- `${ t ( 'player.playBar.volume' ) } ${ Math . round ( ( currentSound ?. volume ( ) || 0 ) * 100 ) } %` ,
64
- 'ri-volume-up-line'
102
+ 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'
65
106
) ;
107
+ break ;
66
108
}
67
- break ;
68
- case 'volumeDown' :
69
- if ( currentSound && currentSound ?. volume ( ) > 0 ) {
70
- currentSound ?. volume ( ( currentSound ?. volume ( ) || 0 ) - 0.1 ) ;
71
- showToast (
72
- `${ t ( 'player.playBar.volume' ) } ${ Math . round ( ( currentSound ?. volume ( ) || 0 ) * 100 ) } %` ,
73
- 'ri-volume-down-line'
74
- ) ;
75
- }
76
- break ;
77
- case 'toggleFavorite' : {
78
- const isFavorite = playerStore . favoriteList . includes ( Number ( playerStore . playMusic . id ) ) ;
79
- const numericId = Number ( playerStore . playMusic . id ) ;
80
- if ( isFavorite ) {
81
- playerStore . removeFromFavorite ( numericId ) ;
82
- } else {
83
- playerStore . addToFavorite ( numericId ) ;
84
- }
85
- showToast (
86
- isFavorite
87
- ? t ( 'player.playBar.favorite' , { name : playerStore . playMusic . name } )
88
- : t ( 'player.playBar.unFavorite' , { name : playerStore . playMusic . name } ) ,
89
- isFavorite ? 'ri-heart-fill' : 'ri-heart-line'
90
- ) ;
91
- break ;
109
+ default :
110
+ console . log ( '未知的快捷键动作:' , action ) ;
111
+ break ;
112
+ }
113
+ } catch ( error ) {
114
+ console . error ( `执行快捷键动作 ${ action } 时出错:` , error ) ;
115
+ } finally {
116
+ // 确保在出错时也能清除超时
117
+ if ( actionTimeout ) {
118
+ clearTimeout ( actionTimeout ) ;
119
+ actionTimeout = null ;
92
120
}
93
- default :
94
- console . log ( '未知的快捷键动作:' , action ) ;
95
- break ;
96
121
}
97
122
}
98
123
0 commit comments