Skip to content

Commit

Permalink
Fix cases where UI hidden for screenshot does not return
Browse files Browse the repository at this point in the history
  • Loading branch information
killergerbah committed Apr 14, 2024
1 parent 54faabb commit d01df42
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 35 deletions.
24 changes: 12 additions & 12 deletions extension/src/handlers/video/record-media-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ export default class RecordMediaHandler {
Math.min(subtitle.end - subtitle.start, recordMediaCommand.message.imageDelay),
{ maxWidth, maxHeight, rect, frameId }
);
imagePromise.then(() => {
const screenshotTakenCommand: ExtensionToVideoCommand<ScreenshotTakenMessage> = {
sender: 'asbplayer-extension-to-video',
message: {
command: 'screenshot-taken',
},
src: recordMediaCommand.src,
};
chrome.tabs.sendMessage(senderTab.id!, screenshotTakenCommand);
});
}

if (audioPromise) {
Expand All @@ -90,22 +100,12 @@ export default class RecordMediaHandler {
};
}

let imageBase64: string | undefined;

if (imagePromise) {
imageBase64 = await imagePromise;
const screenshotTakenCommand: ExtensionToVideoCommand<ScreenshotTakenMessage> = {
sender: 'asbplayer-extension-to-video',
message: {
command: 'screenshot-taken',
},
src: recordMediaCommand.src,
};
chrome.tabs.sendMessage(senderTab.id!, screenshotTakenCommand);
await imagePromise;

// Use the last screenshot taken to allow user to re-take screenshot while audio is recording
imageModel = {
base64: imageBase64!,
base64: this._imageCapturer.lastImageBase64!,
extension: 'jpeg',
};
}
Expand Down
5 changes: 2 additions & 3 deletions extension/src/services/binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -635,12 +635,11 @@ export default class Binding {
const screenshotTakenMessage = request.message as ScreenshotTakenMessage;
this.subtitleController.forceHideSubtitles = false;
this.mobileVideoOverlayController.forceHide = false;
this.controlsController.show();

if (screenshotTakenMessage.ankiUiState) {
if (!this.recordingMedia && screenshotTakenMessage.ankiUiState) {
this.ankiUiController.showAfterRetakingScreenshot(this, screenshotTakenMessage.ankiUiState);
}

this.controlsController.show();
break;
case 'alert':
// ignore
Expand Down
52 changes: 32 additions & 20 deletions extension/src/services/image-capturer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default class ImageCapturer {
private imageBase64Reject: ((error: any) => void) | undefined;
private lastCaptureTimeoutId?: NodeJS.Timeout;

private _capturing = false;
private _lastImageBase64?: string;

constructor(settings: SettingsProvider) {
Expand All @@ -27,6 +28,10 @@ export default class ImageCapturer {
}

capture(tabId: number, src: string, delay: number, captureParams: ImageCaptureParams): Promise<string> {
if (this._capturing) {
return Promise.reject(new Error('Already capturing visible tab'));
}

this._lastImageBase64 = undefined;

if (
Expand Down Expand Up @@ -56,31 +61,38 @@ export default class ImageCapturer {
reject: (error: any) => void
) {
const timeoutId = setTimeout(() => {
captureVisibleTab(tabId).then(async (dataUrl) => {
try {
if (timeoutId !== this.lastCaptureTimeoutId) {
// The promise was already resolved by another call to capture with a shorter delay
return;
this._capturing = true;
captureVisibleTab(tabId)
.then(async (dataUrl) => {
try {
this._capturing = false;

if (timeoutId !== this.lastCaptureTimeoutId) {
// The promise was already resolved by another call to capture with a shorter delay
return;
}

const croppedDataUrl = await this._cropAndResize(dataUrl, tabId, src, captureParams);

if (timeoutId !== this.lastCaptureTimeoutId) {
// The promise was already resolved by another call to capture with a shorter delay
return;
}

this._lastImageBase64 = croppedDataUrl.substring(croppedDataUrl.indexOf(',') + 1);
resolve(this._lastImageBase64);
} catch (e) {
reject(e);
}

const croppedDataUrl = await this._cropAndResize(dataUrl, tabId, src, captureParams);

if (timeoutId !== this.lastCaptureTimeoutId) {
// The promise was already resolved by another call to capture with a shorter delay
return;
}

this._lastImageBase64 = croppedDataUrl.substring(croppedDataUrl.indexOf(',') + 1);
resolve(this._lastImageBase64);
} catch (e) {
reject(e);
} finally {
})
.catch(reject)
.finally(() => {
this._capturing = false;
this.imageBase64Promise = undefined;
this.imageBase64Resolve = undefined;
this.imageBase64Reject = undefined;
this.lastCaptureTimeoutId = undefined;
}
});
});
}, delay);
this.lastCaptureTimeoutId = timeoutId;
}
Expand Down

0 comments on commit d01df42

Please sign in to comment.