Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
[Windows] Handled visibilitychange to avoid camera freeze on minimize
Browse files Browse the repository at this point in the history
Extended checkCancelled to check for BarcodeReader.suspended to handle edge case of early suspension before reader initialized
  • Loading branch information
daserge committed Jul 13, 2016
1 parent 11dd6b5 commit 005c303
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions src/windows/BarcodeScannerProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ module.exports = {
}

function checkCancelled() {
if (BarcodeReader.scanCancelled) {
if (BarcodeReader.scanCancelled || BarcodeReader.suspended) {
throw new Error('Canceled');
}
}
Expand Down Expand Up @@ -587,8 +587,12 @@ module.exports = {
cancelled: !result
});
}, function (error) {
destroyPreview();
// Suppress null result (cancel) on suspending
if (BarcodeReader.suspended) {
return;
}

destroyPreview();
if (error.message == 'Canceled') {
success({
cancelled: true
Expand Down Expand Up @@ -622,20 +626,46 @@ function waitForScanEnd() {
return BarcodeReader.scanPromise || WinJS.Promise.as();
}

function suspend(args) {
BarcodeReader.suspended = true;
if (args) {
args.setPromise(BarcodeReader.destroyPreview()
.then(waitForScanEnd, waitForScanEnd));
} else {
BarcodeReader.destroyPreview();
}
}

function resume() {
BarcodeReader.suspended = false;
module.exports.scan(BarcodeReader.scanCallArgs.success, BarcodeReader.scanCallArgs.fail, BarcodeReader.scanCallArgs.args);
}

function onVisibilityChanged() {
if (document.visibilityState === 'hidden'
&& BarcodeReader.videoPreviewIsVisible && BarcodeReader.videoPreviewIsVisible() && BarcodeReader.destroyPreview) {
suspend();
} else if (BarcodeReader.suspended) {
resume();
}
}

// Windows 8.1 projects
document.addEventListener('msvisibilitychange', onVisibilityChanged);
// Windows 10 projects
document.addEventListener('visibilitychange', onVisibilityChanged);

// About to be suspended
app.addEventListener('checkpoint', function (args) {
if (BarcodeReader.videoPreviewIsVisible && BarcodeReader.videoPreviewIsVisible() && BarcodeReader.destroyPreview) {
BarcodeReader.suspended = true;
args.setPromise(BarcodeReader.destroyPreview()
.then(waitForScanEnd, waitForScanEnd));
suspend(args);
}
});

// Resuming from a user suspension
Windows.UI.WebUI.WebUIApplication.addEventListener("resuming", function () {
if (BarcodeReader.suspended) {
BarcodeReader.suspended = false;
module.exports.scan(BarcodeReader.scanCallArgs.success, BarcodeReader.scanCallArgs.fail, BarcodeReader.scanCallArgs.args);
resume();
}
}, false);

Expand Down

0 comments on commit 005c303

Please sign in to comment.