Skip to content

Commit

Permalink
Merge pull request #977 from navaronbracke/cherry-pick_scan_window_fix
Browse files Browse the repository at this point in the history
fix: port scan window update fix to the new beta release
  • Loading branch information
navaronbracke authored Mar 6, 2024
2 parents ad68fda + 985b25e commit c3989c4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## NEXT

Bugs fixed:
* Fixed an issue where the scan window was not updated when its size was changed. (thanks @navaronbracke !)

## 5.0.0-beta.1

**BREAKING CHANGES:**
Expand Down
64 changes: 57 additions & 7 deletions lib/src/mobile_scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class MobileScanner extends StatefulWidget {
this.overlayBuilder,
this.placeholderBuilder,
this.scanWindow,
this.scanWindowUpdateThreshold = 0.0,
super.key,
});

Expand Down Expand Up @@ -94,6 +95,19 @@ class MobileScanner extends StatefulWidget {
/// ```
final Rect? scanWindow;

/// The threshold for updates to the [scanWindow].
///
/// If the [scanWindow] would be updated,
/// due to new layout constraints for the scanner,
/// and the width or height of the new scan window have not changed by this threshold,
/// then the scan window is not updated.
///
/// It is recommended to set this threshold
/// if scan window updates cause performance issues.
///
/// Defaults to no threshold for scan window updates.
final double scanWindowUpdateThreshold;

@override
State<MobileScanner> createState() => _MobileScannerState();
}
Expand All @@ -109,13 +123,49 @@ class _MobileScannerState extends State<MobileScanner> {
MobileScannerState scannerState,
BoxConstraints constraints,
) {
if (widget.scanWindow != null && scanWindow == null) {
scanWindow = calculateScanWindowRelativeToTextureInPercentage(
widget.fit,
widget.scanWindow!,
textureSize: scannerState.size,
widgetSize: constraints.biggest,
);
if (widget.scanWindow == null) {
return;
}

final Rect newScanWindow = calculateScanWindowRelativeToTextureInPercentage(
widget.fit,
widget.scanWindow!,
textureSize: scannerState.size,
widgetSize: constraints.biggest,
);

// The scan window was never set before.
// Set the initial scan window.
if (scanWindow == null) {
scanWindow = newScanWindow;

unawaited(widget.controller.updateScanWindow(scanWindow));

return;
}

// The scan window did not not change.
// The left, right, top and bottom are the same.
if (scanWindow == newScanWindow) {
return;
}

// The update threshold is not set, allow updating the scan window.
if (widget.scanWindowUpdateThreshold == 0.0) {
scanWindow = newScanWindow;

unawaited(widget.controller.updateScanWindow(scanWindow));

return;
}

final double dx = (newScanWindow.width - scanWindow!.width).abs();
final double dy = (newScanWindow.height - scanWindow!.height).abs();

// The new scan window has changed enough, allow updating the scan window.
if (dx >= widget.scanWindowUpdateThreshold ||
dy >= widget.scanWindowUpdateThreshold) {
scanWindow = newScanWindow;

unawaited(widget.controller.updateScanWindow(scanWindow));
}
Expand Down

0 comments on commit c3989c4

Please sign in to comment.