From 005c30309d3c0b371c5fc4ceceb2ab8f7f2ffb40 Mon Sep 17 00:00:00 2001 From: daserge Date: Tue, 12 Jul 2016 18:33:46 +0300 Subject: [PATCH] [Windows] Handled visibilitychange to avoid camera freeze on minimize Extended checkCancelled to check for BarcodeReader.suspended to handle edge case of early suspension before reader initialized --- src/windows/BarcodeScannerProxy.js | 44 +++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/src/windows/BarcodeScannerProxy.js b/src/windows/BarcodeScannerProxy.js index e1aff4e6..b9b29caf 100644 --- a/src/windows/BarcodeScannerProxy.js +++ b/src/windows/BarcodeScannerProxy.js @@ -551,7 +551,7 @@ module.exports = { } function checkCancelled() { - if (BarcodeReader.scanCancelled) { + if (BarcodeReader.scanCancelled || BarcodeReader.suspended) { throw new Error('Canceled'); } } @@ -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 @@ -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);