Skip to content

Commit

Permalink
use named parameters; add commas for formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
navaronbracke committed Oct 12, 2023
1 parent 4e3e564 commit cf60fe4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 31 deletions.
4 changes: 2 additions & 2 deletions lib/src/mobile_scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ class _MobileScannerState extends State<MobileScanner>
scanWindow = calculateScanWindowRelativeToTextureInPercentage(
widget.fit,
widget.scanWindow!,
value.size,
constraints.biggest,
textureSize: value.size,
widgetSize: constraints.biggest,
);

_controller.updateScanWindow(scanWindow);
Expand Down
15 changes: 8 additions & 7 deletions lib/src/scan_window_calculation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import 'package:flutter/rendering.dart';
/// Returns a [Rect] that represents the position and size of the scan window in the texture.
Rect calculateScanWindowRelativeToTextureInPercentage(
BoxFit fit,
Rect scanWindow,
Size textureSize,
Size widgetSize,
) {
/// map the texture size to get its new size after fitted to screen
Rect scanWindow, {
required Size textureSize,
required Size widgetSize,
}) {
// Convert the texture size to a size in widget-space, with the box fit applied.
final fittedTextureSize = applyBoxFit(fit, textureSize, widgetSize);

// Get the correct scaling values depending on the given BoxFit mode
Expand Down Expand Up @@ -76,8 +76,9 @@ Rect calculateScanWindowRelativeToTextureInPercentage(

// Clip the scan window in texture coordinates with the texture bounds.
// This prevents percentages outside the range [0; 1].
final clippedScanWndInTexSpace = scanWindowInTexSpace
.intersect(Rect.fromLTWH(0, 0, textureSize.width, textureSize.height));
final clippedScanWndInTexSpace = scanWindowInTexSpace.intersect(
Rect.fromLTWH(0, 0, textureSize.width, textureSize.height),
);

// Compute relative rectangle coordinates,
// with respect to the texture size, i.e. scan image.
Expand Down
67 changes: 45 additions & 22 deletions test/scan_window_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,49 +20,58 @@ void main() {

test('wl tp: BoxFit.none', () {
ctx.testScanWindow(
BoxFit.none, const Rect.fromLTRB(0.275, 0.4, 0.725, 0.6));
BoxFit.none,
const Rect.fromLTRB(0.275, 0.4, 0.725, 0.6),
);
});

test('wl tp: BoxFit.fill', () {
ctx.testScanWindow(
BoxFit.fill, const Rect.fromLTRB(0.25, 0.25, 0.75, 0.75));
BoxFit.fill,
const Rect.fromLTRB(0.25, 0.25, 0.75, 0.75),
);
});

test('wl tp: BoxFit.fitHeight', () {
ctx.testScanWindow(
BoxFit.fitHeight, const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75));
BoxFit.fitHeight,
const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75),
);
});

test('wl tp: BoxFit.fitWidth', () {
ctx.testScanWindow(
BoxFit.fitWidth,
const Rect.fromLTRB(
0.25, 0.38888888888888895, 0.75, 0.6111111111111112));
BoxFit.fitWidth,
const Rect.fromLTRB(0.25, 0.38888888888888895, 0.75, 0.6111111111111112),
);
});

test('wl tp: BoxFit.cover', () {
// equal to fitWidth
ctx.testScanWindow(
BoxFit.cover,
const Rect.fromLTRB(
0.25, 0.38888888888888895, 0.75, 0.6111111111111112));
BoxFit.cover,
const Rect.fromLTRB(0.25, 0.38888888888888895, 0.75, 0.6111111111111112),
);
});

test('wl tp: BoxFit.contain', () {
// equal to fitHeigth
ctx.testScanWindow(
BoxFit.contain, const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75));
BoxFit.contain,
const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75),
);
});

test('wl tp: BoxFit.scaleDown', () {
// equal to fitHeigth, contain
ctx.testScanWindow(
BoxFit.scaleDown, const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75));
BoxFit.scaleDown,
const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75),
);
});
});

group('Widget (landscape) smaller than texture and texture (landscape)',
() {
group('Widget (landscape) smaller than texture and texture (landscape)', () {
const textureSize = Size(640.0, 480.0);
const widgetSize = Size(320.0, 120.0);
final ctx = ScanWindowTestContext(
Expand All @@ -78,40 +87,54 @@ void main() {

test('wl tl: BoxFit.none', () {
ctx.testScanWindow(
BoxFit.none, const Rect.fromLTRB(0.375, 0.4375, 0.625, 0.5625));
BoxFit.none,
const Rect.fromLTRB(0.375, 0.4375, 0.625, 0.5625),
);
});

test('wl tl: BoxFit.fill', () {
ctx.testScanWindow(
BoxFit.fill, const Rect.fromLTRB(0.25, 0.25, 0.75, 0.75));
BoxFit.fill,
const Rect.fromLTRB(0.25, 0.25, 0.75, 0.75),
);
});

test('wl tl: BoxFit.fitHeight', () {
ctx.testScanWindow(
BoxFit.fitHeight, const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75));
BoxFit.fitHeight,
const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75),
);
});

test('wl tl: BoxFit.fitWidth', () {
ctx.testScanWindow(
BoxFit.fitWidth, const Rect.fromLTRB(0.25, 0.375, 0.75, 0.625));
BoxFit.fitWidth,
const Rect.fromLTRB(0.25, 0.375, 0.75, 0.625),
);
});

test('wl tl: BoxFit.cover', () {
// equal to fitWidth
ctx.testScanWindow(
BoxFit.cover, const Rect.fromLTRB(0.25, 0.375, 0.75, 0.625));
BoxFit.cover,
const Rect.fromLTRB(0.25, 0.375, 0.75, 0.625),
);
});

test('wl tl: BoxFit.contain', () {
// equal to fitHeigth
ctx.testScanWindow(
BoxFit.contain, const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75));
BoxFit.contain,
const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75),
);
});

test('wl tl: BoxFit.scaleDown', () {
// equal to fitHeigth, contain
ctx.testScanWindow(
BoxFit.scaleDown, const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75));
BoxFit.scaleDown,
const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75),
);
});
});
});
Expand All @@ -132,8 +155,8 @@ class ScanWindowTestContext {
final actual = calculateScanWindowRelativeToTextureInPercentage(
fit,
scanWindow,
textureSize,
widgetSize,
textureSize: textureSize,
widgetSize: widgetSize,
);

// don't use expect(actual, expected) because Rect.toString() only shows one digit after the comma which can be confusing
Expand Down

0 comments on commit cf60fe4

Please sign in to comment.