Skip to content

Commit

Permalink
prevent media keys from working while minimized or not focused
Browse files Browse the repository at this point in the history
  • Loading branch information
vaisest committed Sep 16, 2024
1 parent 5521335 commit 6b8f8f6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ const createWindow = async () => {
}
});

mainWindow.on('focus', () => {
mainWindow?.webContents.send('window-focus-status', true);
});

mainWindow.on('blur', () => {
mainWindow?.webContents.send('window-focus-status', false);
});

mainWindow.on('closed', () => {
mainWindow = null;
});
Expand Down
19 changes: 19 additions & 0 deletions src/renderer/VideoPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,25 @@ export const VideoPlayer = (props: IProps) => {
}
};

// By default the window hijacks media keys even when
// the window isn't focused or it is minimized
// so we override the action handlers
useEffect(() => {
ipc.on('window-focus-status', (arg: unknown) => {
const focused = arg as boolean;
if (focused) {
// unset action handlers when focused, making them work like initially
navigator.mediaSession.setActionHandler('play', null);
navigator.mediaSession.setActionHandler('pause', null);
} else {
navigator.mediaSession.setActionHandler('play', () => {});
navigator.mediaSession.setActionHandler('pause', () => {});
}
// note that this kind of solution doesn't work for the stop key for some reason.
// it seems to behave differently and it clears the entire session
});
}, []);

/**
* Handle the user clicking on the rate button by going to the next rate
* option.
Expand Down

0 comments on commit 6b8f8f6

Please sign in to comment.