Skip to content

Commit 3e46a69

Browse files
committed
fix(player): resolve spacebar shortcut not working after clicking to pause (#182)
* fix(player): resolve spacebar shortcut not working after clicking to pause - Remove duplicate onKeyDown handler from VideoSurface component that was intercepting space key events and preventing global shortcuts from working - Add enhanced debug logging to track shortcut trigger events and focus state - Ensure consistent play/pause behavior regardless of focus location Fixes issue where clicking VideoSurface to pause would prevent spacebar from resuming playback * test: fix Windows timing race conditions in VideoLibraryDAO tests - Replace multiple Date.now() calls with fixed timestamps to prevent 1ms differences - Use consistent FIXED_NOW and FIXED_FIRST_PLAYED_AT constants across all mock data - Fix failing tests on Windows where Date.now() calls had microsecond variations Resolves test failures where expected timestamps differed by 1ms from actual values
1 parent 9a9d932 commit 3e46a69

3 files changed

Lines changed: 22 additions & 17 deletions

File tree

src/main/db/dao/__tests__/VideoLibraryDAO.test.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ describe('VideoLibraryDAO', () => {
1212
let dao: VideoLibraryDAO
1313
let mockKysely: any
1414

15+
// Fixed timestamps to avoid race conditions between Date.now() calls
16+
const FIXED_NOW = 1758017326806
17+
const FIXED_FIRST_PLAYED_AT = FIXED_NOW - 86400000 // 1 day ago
18+
1519
// Mock data for insertion (uses boolean, will be converted to number by schema)
1620
const mockVideoRecordFromDBForInsert = {
1721
fileId: 'test-file-id-1',
1822
currentTime: 120.5,
1923
duration: 3600,
20-
playedAt: Date.now(),
21-
firstPlayedAt: Date.now() - 86400000, // 1 day ago
24+
playedAt: FIXED_NOW,
25+
firstPlayedAt: FIXED_FIRST_PLAYED_AT,
2226
playCount: 3,
2327
isFinished: false, // Boolean for insert
2428
isFavorite: true, // Boolean for insert
@@ -30,8 +34,8 @@ describe('VideoLibraryDAO', () => {
3034
fileId: 'test-file-id-1',
3135
currentTime: 120.5,
3236
duration: 3600,
33-
playedAt: Date.now(),
34-
firstPlayedAt: Date.now() - 86400000, // 1 day ago
37+
playedAt: FIXED_NOW,
38+
firstPlayedAt: FIXED_FIRST_PLAYED_AT,
3539
playCount: 3,
3640
isFinished: 0, // Number from DB (0 for false)
3741
isFavorite: 1, // Number from DB (1 for true)
@@ -43,8 +47,8 @@ describe('VideoLibraryDAO', () => {
4347
fileId: 'test-file-id-1',
4448
currentTime: 120.5,
4549
duration: 3600,
46-
playedAt: Date.now(),
47-
firstPlayedAt: Date.now() - 86400000, // 1 day ago
50+
playedAt: FIXED_NOW,
51+
firstPlayedAt: FIXED_FIRST_PLAYED_AT,
4852
playCount: 3,
4953
isFinished: false, // Converted to boolean
5054
isFavorite: true, // Converted to boolean
@@ -155,7 +159,8 @@ describe('VideoLibraryDAO', () => {
155159

156160
describe('getRecentlyPlayed', () => {
157161
it('应该获取最近播放的视频(默认限制)', async () => {
158-
const now = Date.now()
162+
// Use fixed timestamps to avoid race conditions
163+
const now = FIXED_NOW
159164
// Mock database returns number format
160165
const mockDBResults = [
161166
{ id: 1, ...mockVideoRecordFromDBFromDB, playedAt: now },
@@ -262,7 +267,7 @@ describe('VideoLibraryDAO', () => {
262267
it('应该更新播放进度', async () => {
263268
const mockResult = { numUpdatedRows: 1 }
264269
mockKysely.execute.mockResolvedValue(mockResult)
265-
const now = Date.now()
270+
const now = FIXED_NOW
266271
vi.spyOn(Date, 'now').mockReturnValue(now)
267272

268273
const result = await dao.updatePlayProgress(1, 150.5)
@@ -279,7 +284,7 @@ describe('VideoLibraryDAO', () => {
279284
it('应该更新播放进度并设置完成状态', async () => {
280285
const mockResult = { numUpdatedRows: 1 }
281286
mockKysely.execute.mockResolvedValue(mockResult)
282-
const now = Date.now()
287+
const now = FIXED_NOW
283288
vi.spyOn(Date, 'now').mockReturnValue(now)
284289

285290
const result = await dao.updatePlayProgress(1, 3600, true)
@@ -295,7 +300,7 @@ describe('VideoLibraryDAO', () => {
295300
it('应该更新播放进度但不改变完成状态', async () => {
296301
const mockResult = { numUpdatedRows: 1 }
297302
mockKysely.execute.mockResolvedValue(mockResult)
298-
const now = Date.now()
303+
const now = FIXED_NOW
299304
vi.spyOn(Date, 'now').mockReturnValue(now)
300305

301306
const result = await dao.updatePlayProgress(1, 150.5, undefined)

src/renderer/src/pages/player/components/VideoSurface.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,6 @@ function VideoSurface({ src, onLoadedMetadata, onError }: VideoSurfaceProps) {
208208
data-testid="video-surface"
209209
onClick={handleSurfaceClick}
210210
tabIndex={0}
211-
onKeyDown={(e) => {
212-
if (e.key === 'Enter' || e.key === ' ') {
213-
e.preventDefault()
214-
handleSurfaceClick()
215-
}
216-
}}
217211
>
218212
<StyledVideo
219213
ref={handleVideoRef}

src/renderer/src/pages/player/hooks/usePlayerShortcuts.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,14 @@ export function usePlayerShortcuts() {
9797
}, [currentSubtitle, displayMode, t])
9898

9999
useShortcut('play_pause', () => {
100+
logger.debug('空格键快捷键触发', {
101+
activeElement: document.activeElement?.tagName,
102+
activeElementClass: document.activeElement?.className,
103+
activeElementId: document.activeElement?.id,
104+
timestamp: Date.now()
105+
})
100106
cmd.playPause()
101-
logger.info('Shortcut play_pause')
107+
logger.info('Shortcut play_pause executed')
102108
})
103109

104110
// 重播当前字幕

0 commit comments

Comments
 (0)