Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[locker] Polish the scanner capture animation

laurenspriemec5d47441629
diff --git a/mobile/apps/locker/lib/ui/pages/scanner/capture_flight.dart b/mobile/apps/locker/lib/ui/pages/scanner/capture_flight.dart
index dce82223aa..be007c7db1 100644
--- a/mobile/apps/locker/lib/ui/pages/scanner/capture_flight.dart
+++ b/mobile/apps/locker/lib/ui/pages/scanner/capture_flight.dart
@@ -101,6 +101,57 @@ List<Offset> lerpCorners(List<Offset> a, List<Offset> b, double t) => [
for (var i = 0; i < a.length; i++) Offset.lerp(a[i], b[i], t)!,
];
+Offset centroid(List<Offset> corners) {
+ var sum = Offset.zero;
+ for (final c in corners) {
+ sum += c;
+ }
+ return sum / corners.length.toDouble();
+}
+
+List<Offset> poseCorners(
+ List<Offset> corners, {
+ required Offset about,
+ required Offset offset,
+ required double angle,
+ required double scale,
+}) {
+ final cosA = math.cos(angle);
+ final sinA = math.sin(angle);
+ return [
+ for (final c in corners)
+ () {
+ final d = (c - about) * scale;
+ return about +
+ offset +
+ Offset(d.dx * cosA - d.dy * sinA, d.dx * sinA + d.dy * cosA);
+ }(),
+ ];
+}
+
+const developContrast = 1.16;
+const developBrightness = 12.0;
+const developSaturation = 0.88;
+
+List<double> developMatrix(double t) {
+ final c = ui.lerpDouble(1, developContrast, t)!;
+ final b = ui.lerpDouble(0, developBrightness, t)!;
+ final s = ui.lerpDouble(1, developSaturation, t)!;
+ const lr = 0.2126;
+ const lg = 0.7152;
+ const lb = 0.0722;
+ final sr = (1 - s) * lr;
+ final sg = (1 - s) * lg;
+ final sb = (1 - s) * lb;
+ final offset = 128 * (1 - c) + b;
+ return [
+ c * (sr + s), c * sg, c * sb, 0, offset, //
+ c * sr, c * (sg + s), c * sb, 0, offset, //
+ c * sr, c * sg, c * (sb + s), 0, offset, //
+ 0, 0, 0, 1, 0, //
+ ];
+}
+
Path roundedPolygonPath(List<Offset> corners, double radius) {
final path = Path();
if (radius <= 0.01) return path..addPolygon(corners, true);
@@ -127,6 +178,10 @@ Path roundedPolygonPath(List<Offset> corners, double radius) {
return path..close();
}
+const flightArcHeight = 0.08;
+const flightTilt = -3 * math.pi / 180;
+const flightLift = 0.02;
+
class CaptureFlightCard extends StatefulWidget {
const CaptureFlightCard({
super.key,
@@ -201,9 +256,30 @@ class _FlightPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final t = progress.value;
- final clip = lerpCorners(spec.sourceCorners, spec.clipEnd, t);
+ final swing = math.sin(math.pi * t);
+ final travel = spec.target.center - centroid(spec.sourceCorners);
+ final pose = (
+ offset: Offset(0, -travel.distance * flightArcHeight * swing),
+ angle: flightTilt * swing,
+ scale: 1 + flightLift * math.sin(math.pi * (t / 0.35).clamp(0.0, 1.0)),
+ );
+ final rawClip = lerpCorners(spec.sourceCorners, spec.clipEnd, t);
+ final about = centroid(rawClip);
+ final clip = poseCorners(
+ rawClip,
+ about: about,
+ offset: pose.offset,
+ angle: pose.angle,
+ scale: pose.scale,
+ );
final sourceQuad = lerpCorners(spec.sourceCorners, spec.zoomedCorners, t);
- final imageQuad = lerpCorners(spec.sourceCorners, spec.imageEnd, t);
+ final imageQuad = poseCorners(
+ lerpCorners(spec.sourceCorners, spec.imageEnd, t),
+ about: about,
+ offset: pose.offset,
+ angle: pose.angle,
+ scale: pose.scale,
+ );
final path = roundedPolygonPath(clip, spec.targetRadius * t);
final elevation = 18 * math.sin(math.pi * t) + 3 * t;
if (elevation > 0) {
@@ -212,7 +288,7 @@ class _FlightPainter extends CustomPainter {
canvas
..save()
..clipPath(path);
- paintMappedSnapshot(canvas, spec, sourceQuad, imageQuad);
+ paintMappedSnapshot(canvas, spec, sourceQuad, imageQuad, develop: t);
final borderAlpha = ((t - 0.7) / 0.3).clamp(0.0, 1.0);
if (borderAlpha > 0) {
canvas.drawPath(
@@ -235,8 +311,9 @@ void paintMappedSnapshot(
Canvas canvas,
CaptureFlightSpec spec,
List<Offset> sourceQuad,
- List<Offset> imageQuad,
-) {
+ List<Offset> imageQuad, {
+ required double develop,
+}) {
canvas.save();
final homography = homographyMatrix(sourceQuad, imageQuad);
if (homography != null) {
@@ -249,13 +326,13 @@ void paintMappedSnapshot(
..scale(to.width / from.width, to.height / from.height)
..translate(-from.left, -from.top);
}
+ final paint = Paint()..filterQuality = FilterQuality.medium;
+ if (develop > 0) {
+ paint.colorFilter = ColorFilter.matrix(developMatrix(develop));
+ }
canvas
..transform(spec.imageToLayer)
- ..drawImage(
- spec.image,
- Offset.zero,
- Paint()..filterQuality = FilterQuality.medium,
- )
+ ..drawImage(spec.image, Offset.zero, paint)
..restore();
}
@@ -302,7 +379,13 @@ class _SnapshotThumbnailPainter extends CustomPainter {
..save()
..clipRect(Offset.zero & size)
..translate(-origin.dx, -origin.dy);
- paintMappedSnapshot(canvas, spec, spec.zoomedCorners, spec.imageEnd);
+ paintMappedSnapshot(
+ canvas,
+ spec,
+ spec.zoomedCorners,
+ spec.imageEnd,
+ develop: 1,
+ );
canvas.restore();
}
diff --git a/mobile/apps/locker/lib/ui/pages/scanner/scanner_capture_page.dart b/mobile/apps/locker/lib/ui/pages/scanner/scanner_capture_page.dart
index 6cdae3ab18..978bb92f50 100644
--- a/mobile/apps/locker/lib/ui/pages/scanner/scanner_capture_page.dart
+++ b/mobile/apps/locker/lib/ui/pages/scanner/scanner_capture_page.dart
@@ -239,10 +239,7 @@ class _ScannerCapturePageState extends State<ScannerCapturePage>
}
if (mounted) {
setState(() => _stableQuad = stable);
- if (fire) {
- unawaited(HapticFeedback.lightImpact());
- unawaited(_capture());
- }
+ if (fire) unawaited(_capture());
}
} catch (_) {
} finally {
@@ -255,6 +252,7 @@ class _ScannerCapturePageState extends State<ScannerCapturePage>
if (camera == null || _takingPicture) return;
final quad = _stableQuad ?? ScanQuad.fullFrame();
_autoCapture.notifyCaptureStarted();
+ unawaited(HapticFeedback.mediumImpact());
setState(() {
_takingPicture = true;
_stableQuad = null;
@@ -365,6 +363,7 @@ class _ScannerCapturePageState extends State<ScannerCapturePage>
void _onFlightLanded(_ActiveFlight flight) {
if (!mounted) return;
+ unawaited(HapticFeedback.selectionClick());
setState(() {
_flights.remove(flight);
flight.capture.landed = true;
@@ -411,7 +410,7 @@ class _ScannerCapturePageState extends State<ScannerCapturePage>
}
}
- ({int count, Widget? thumbnail}) _pagesButtonState() {
+ ({int count, Widget? thumbnail, File? heroFile}) _pagesButtonState() {
final hiddenPages = _pending
.where((capture) => capture.processed && !capture.landed)
.length;
@@ -424,6 +423,9 @@ class _ScannerCapturePageState extends State<ScannerCapturePage>
if (capture.landed && !capture.failed && capture.spec != null) capture,
];
Widget? thumbnail;
+ final heroFile = shownPages > 0
+ ? _session.pages[shownPages - 1].processedJpeg
+ : null;
if (showingSnapshot.isNotEmpty) {
final capture = showingSnapshot.last;
thumbnail = CaptureSnapshotThumbnail(
@@ -437,7 +439,11 @@ class _ScannerCapturePageState extends State<ScannerCapturePage>
page: page,
);
}
- return (count: shownPages + landedUnprocessed, thumbnail: thumbnail);
+ return (
+ count: shownPages + landedUnprocessed,
+ thumbnail: thumbnail,
+ heroFile: heroFile,
+ );
}
void _toggleAutoMode() {
@@ -460,6 +466,13 @@ class _ScannerCapturePageState extends State<ScannerCapturePage>
Future<void> _openReview() async {
if (_session.pageCount == 0 && !_session.isProcessing) return;
final navigator = Navigator.of(context);
+ if (_session.pageCount > 0) {
+ await precacheImage(
+ FileImage(_session.pages.last.processedJpeg),
+ context,
+ );
+ if (!mounted) return;
+ }
await _pauseCamera();
final saved = await navigator.push<bool>(
MaterialPageRoute(
@@ -538,8 +551,6 @@ class _ScannerCapturePageState extends State<ScannerCapturePage>
_status == _CameraStatus.ready &&
!_takingPicture &&
_session.isServiceReady,
- armingProgress: _autoMode ? _autoCapture.progress : 0,
- armingColor: colors.primary,
onTap: _capture,
),
Align(
@@ -548,6 +559,7 @@ class _ScannerCapturePageState extends State<ScannerCapturePage>
key: _pagesButtonKey,
count: pages.count,
thumbnail: pages.thumbnail,
+ heroFile: pages.heroFile,
accent: colors.primary,
onTap: _openReview,
),
@@ -631,6 +643,7 @@ class _ScannerCapturePageState extends State<ScannerCapturePage>
? null
: _stableQuad,
color: colors.primary,
+ armingProgress: _autoMode ? _autoCapture.progress : 0,
),
CaptureSnapOverlay(
quad: _snapQuad,
@@ -777,62 +790,53 @@ class _ChromeButton extends StatelessWidget {
}
}
-class _ShutterButton extends StatelessWidget {
- const _ShutterButton({
- required this.enabled,
- required this.armingProgress,
- required this.armingColor,
- required this.onTap,
- });
+class _ShutterButton extends StatefulWidget {
+ const _ShutterButton({required this.enabled, required this.onTap});
final bool enabled;
-
- final double armingProgress;
- final Color armingColor;
final Future<void> Function() onTap;
+ @override
+ State<_ShutterButton> createState() => _ShutterButtonState();
+}
+
+class _ShutterButtonState extends State<_ShutterButton> {
+ bool _pressed = false;
+
+ void _setPressed(bool pressed) {
+ if (_pressed != pressed) setState(() => _pressed = pressed);
+ }
+
@override
Widget build(BuildContext context) {
final colors = context.componentColors;
final white = colors.specialWhite;
return GestureDetector(
- onTap: enabled ? () => onTap() : null,
+ onTapDown: widget.enabled ? (_) => _setPressed(true) : null,
+ onTapUp: (_) => _setPressed(false),
+ onTapCancel: () => _setPressed(false),
+ onTap: widget.enabled ? () => widget.onTap() : null,
child: AnimatedOpacity(
duration: const Duration(milliseconds: 150),
- opacity: enabled ? 1 : 0.5,
- child: SizedBox(
- width: 76,
- height: 76,
- child: Stack(
- fit: StackFit.expand,
- children: [
- Container(
- decoration: BoxDecoration(
- shape: BoxShape.circle,
- border: Border.all(color: white, width: 4),
- ),
- child: Center(
- child: Container(
- width: 60,
- height: 60,
- decoration: BoxDecoration(
- shape: BoxShape.circle,
- color: white,
- ),
- ),
- ),
- ),
- TweenAnimationBuilder<double>(
- tween: Tween(begin: 0, end: armingProgress.clamp(0.0, 1.0)),
- duration: Motion.quick,
- builder: (context, value, _) => CustomPaint(
- painter: _ArmingArcPainter(
- progress: value,
- color: armingColor,
- ),
- ),
+ opacity: widget.enabled ? 1 : 0.5,
+ child: AnimatedScale(
+ scale: _pressed ? 0.9 : 1,
+ duration: _pressed ? Motion.quick : Motion.slow,
+ curve: _pressed ? Curves.easeOut : Curves.easeOutBack,
+ child: Container(
+ width: 76,
+ height: 76,
+ decoration: BoxDecoration(
+ shape: BoxShape.circle,
+ border: Border.all(color: white, width: 4),
+ ),
+ child: Center(
+ child: Container(
+ width: 60,
+ height: 60,
+ decoration: BoxDecoration(shape: BoxShape.circle, color: white),
),
- ],
+ ),
),
),
),
@@ -840,36 +844,6 @@ class _ShutterButton extends StatelessWidget {
}
}
-class _ArmingArcPainter extends CustomPainter {
- const _ArmingArcPainter({required this.progress, required this.color});
-
- final double progress;
- final Color color;
-
- @override
- void paint(Canvas canvas, Size size) {
- if (progress <= 0 || size.isEmpty) return;
- canvas.drawArc(
- Rect.fromCircle(
- center: size.center(Offset.zero),
- radius: size.shortestSide / 2 - 2,
- ),
- -math.pi / 2,
- progress * 2 * math.pi,
- false,
- Paint()
- ..color = color
- ..style = PaintingStyle.stroke
- ..strokeWidth = 4
- ..strokeCap = StrokeCap.round,
- );
- }
-
- @override
- bool shouldRepaint(_ArmingArcPainter oldDelegate) =>
- oldDelegate.progress != progress || oldDelegate.color != color;
-}
-
class _AutoToggle extends StatelessWidget {
const _AutoToggle({required this.active, required this.onTap});
@@ -957,14 +931,17 @@ class _PagesButton extends StatefulWidget {
super.key,
required this.count,
required this.thumbnail,
+ required this.heroFile,
required this.accent,
required this.onTap,
});
static const borderWidth = 2.0;
+ static const maxStackedSheets = 2;
final int count;
final Widget? thumbnail;
+ final File? heroFile;
final Color accent;
final VoidCallback onTap;
@@ -1017,6 +994,44 @@ class _PagesButtonState extends State<_PagesButton>
static Widget _expandedLayout(Widget? current, List<Widget> previous) =>
Stack(fit: StackFit.expand, children: [...previous, ?current]);
+ Widget _stackedSheet(int depth, ColorTokens colors) {
+ final visible = widget.count > depth;
+ final angle = (depth.isOdd ? -5.0 : 4.0) * math.pi / 180;
+ final shift = Offset(depth.isOdd ? -1 : 2, -2.0 * depth);
+ return Positioned.fill(
+ child: IgnorePointer(
+ child: AnimatedOpacity(
+ duration: Motion.standard,
+ opacity: visible ? 1 : 0,
+ child: AnimatedScale(
+ duration: Motion.standard,
+ curve: Curves.easeOutBack,
+ scale: visible ? 1 : 0.8,
+ child: Transform(
+ alignment: Alignment.center,
+ transform: Matrix4.translationValues(shift.dx, shift.dy, 0)
+ ..rotateZ(angle),
+ child: Container(
+ decoration: BoxDecoration(
+ borderRadius: BorderRadius.circular(Radii.md),
+ color: colors.specialWhite.withValues(
+ alpha: depth == 1 ? 0.85 : 0.6,
+ ),
+ boxShadow: [
+ BoxShadow(
+ color: Colors.black.withValues(alpha: 0.3),
+ blurRadius: 6,
+ ),
+ ],
+ ),
+ ),
+ ),
+ ),
+ ),
+ ),
+ );
+ }
+
@override
Widget build(BuildContext context) {
final colors = context.componentColors;
@@ -1030,6 +1045,8 @@ class _PagesButtonState extends State<_PagesButton>
child: Stack(
clipBehavior: Clip.none,
children: [
+ for (var depth = _PagesButton.maxStackedSheets; depth >= 1; depth--)
+ _stackedSheet(depth, colors),
Positioned.fill(
child: Container(
decoration: BoxDecoration(
@@ -1046,16 +1063,20 @@ class _PagesButtonState extends State<_PagesButton>
),
],
),
- child: ClipRRect(
- borderRadius: BorderRadius.circular(
- Radii.md - _PagesButton.borderWidth,
- ),
- child: AnimatedSwitcher(
- duration: _switchDuration,
- layoutBuilder: _expandedLayout,
- child:
- widget.thumbnail ??
- const SizedBox.expand(key: ValueKey('empty')),
+ child: ScannerPageHero(
+ file: widget.heroFile,
+ primary: false,
+ child: ClipRRect(
+ borderRadius: BorderRadius.circular(
+ Radii.md - _PagesButton.borderWidth,
+ ),
+ child: AnimatedSwitcher(
+ duration: _switchDuration,
+ layoutBuilder: _expandedLayout,
+ child:
+ widget.thumbnail ??
+ const SizedBox.expand(key: ValueKey('empty')),
+ ),
),
),
),