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 #251 from daserge/close-button
Browse files Browse the repository at this point in the history
Windows Mobile 10 - Close button does not work, this fixes #196
  • Loading branch information
Vladimir Kotikov committed Jun 8, 2016
2 parents 5161af4 + 942d1ed commit 128c80f
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/windows/BarcodeScannerProxy.js
Expand Up @@ -284,6 +284,7 @@ module.exports = {
closeButton.className = "app-bar-action action-close";
navigationButtonsDiv.appendChild(closeButton);

BarcodeReader.scanCancelled = false;
closeButton.addEventListener("click", cancelPreview, false);
document.addEventListener('backbutton', cancelPreview, false);

Expand Down Expand Up @@ -472,21 +473,31 @@ module.exports = {
* See https://github.com/phonegap-build/BarcodeScanner#using-the-plugin
*/
function cancelPreview() {
BarcodeReader.scanCancelled = true;
reader && reader.stop();
}

function checkCancelled() {
if (BarcodeReader.scanCancelled) {
throw new Error('Canceled');
}
}

WinJS.Promise.wrap(createPreview())
.then(function () {
checkCancelled();
return startPreview();
})
.then(function (captureSettings) {
checkCancelled();
reader = BarcodeReader.get(captureSettings.capture);
reader.init(captureSettings.capture, captureSettings.width, captureSettings.height);

// Add a small timeout before capturing first frame otherwise
// we would get an 'Invalid state' error from 'getPreviewFrameAsync'
return WinJS.Promise.timeout(200)
.then(function () {
checkCancelled();
return reader.readCode();
});
})
Expand All @@ -499,7 +510,14 @@ module.exports = {
});
}, function (error) {
destroyPreview();
fail(error);

if (error.message == 'Canceled') {
success({
cancelled: true
});
} else {
fail(error);
}
});
},

Expand Down

0 comments on commit 128c80f

Please sign in to comment.