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

Commit

Permalink
Merge pull request #285 from daserge/visibilitychange
Browse files Browse the repository at this point in the history
[Windows] Handled visibilitychange to avoid camera freeze on minimize
  • Loading branch information
Sergey Shakhnazarov authored Jul 15, 2016
2 parents 11dd6b5 + 005c303 commit 9d4a961
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 9d4a961

Please sign in to comment.