Skip to content

Commit

Permalink
Stop video playback on close to fix signalapp#6661
Browse files Browse the repository at this point in the history
Timeout is subject to change
  • Loading branch information
Ross-Sherlock committed Nov 20, 2023
1 parent ab187ab commit 1fcce52
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
39 changes: 26 additions & 13 deletions app/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -932,19 +932,32 @@ async function createWindow() {
// Prevent the shutdown
e.preventDefault();

/**
* if the user is in fullscreen mode and closes the window, not the
* application, we need them leave fullscreen first before closing it to
* prevent a black screen.
*
* issue: https://github.com/signalapp/Signal-Desktop/issues/4348
*/

if (mainWindow.isFullScreen()) {
mainWindow.once('leave-full-screen', () => mainWindow?.hide());
mainWindow.setFullScreen(false);
} else {
mainWindow.hide();
mainWindow.webContents

let stopPlaybackConfirmed = false;

const playbackTimeout = setTimeout(() => {
if (!stopPlaybackConfirmed) {
console.log("No response from renderer, hiding mainWindow by timeout");
hideMainWindow();
}
}, 100); // This timeout is based on my testing and subject to change, tried to balance responsiveness and appropriate wait time

// Inform video renderer of coming close to enable it to stop playback
mainWindow.webContents.send('prepare-close');
ipc.once('stop-playback-confirmed', () => {
stopPlaybackConfirmed = true;
clearTimeout(playbackTimeout);
hideMainWindow();
});

function hideMainWindow() {
if (mainWindow?.isFullScreen()) {
mainWindow.once('leave-full-screen', () => mainWindow?.hide());
mainWindow.setFullScreen(false);
} else {
mainWindow?.hide();
}
}

// On Mac, or on other platforms when the tray icon is in use, the window
Expand Down
16 changes: 16 additions & 0 deletions ts/components/Lightbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { isGIF } from '../types/Attachment';
import { useRestoreFocus } from '../hooks/useRestoreFocus';
import { usePrevious } from '../hooks/usePrevious';
import { arrow } from '../util/keyboard';
import { ipcRenderer } from 'electron';

export type PropsType = {
children?: ReactNode;
Expand Down Expand Up @@ -224,6 +225,21 @@ export function Lightbox({
closeLightbox();
};

useEffect(() => {
const prepareClose = () => {
if (videoElement && !videoElement.paused) {
videoElement.pause();
}
ipcRenderer.send('stop-playback-confirmed');
};

ipcRenderer.on('prepare-close', prepareClose);

return () => {
ipcRenderer.removeListener('prepare-close', prepareClose);
};
}, [videoElement]);

const playVideo = useCallback(() => {
if (!videoElement) {
return;
Expand Down

0 comments on commit 1fcce52

Please sign in to comment.