π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Improve QR code overlay and bottom sheet UX
Amrithesh Kakkoth Ente07005f71bd6e
diff --git a/mobile/apps/auth/plugins/qr/android/src/main/kotlin/io/ente/auth/ente_qr/EnteQrPlugin.kt b/mobile/apps/auth/plugins/qr/android/src/main/kotlin/io/ente/auth/ente_qr/EnteQrPlugin.ktindex 0b1cd484bf..68110ca9be 100644--- a/mobile/apps/auth/plugins/qr/android/src/main/kotlin/io/ente/auth/ente_qr/EnteQrPlugin.kt+++ b/mobile/apps/auth/plugins/qr/android/src/main/kotlin/io/ente/auth/ente_qr/EnteQrPlugin.kt@@ -163,20 +163,25 @@ class EnteQrPlugin: FlutterPlugin, MethodCallHandler {)}- var bitmap = BitmapFactory.decodeFile(imagePath)- if (bitmap == null) {+ val origBitmap = BitmapFactory.decodeFile(imagePath)+ if (origBitmap == null) {return mapOf("success" to false,"error" to "Unable to decode image file")}+ val origWidth = origBitmap.width.toDouble()+ val origHeight = origBitmap.height.toDouble()+ var bitmap = origBitmap+for (i in 0..2) {if (i != 0) {- val newWidth = bitmap.width / (i * 2)- val newHeight = bitmap.height / (i * 2)+ val newWidth = origBitmap.width / (i * 2)+ val newHeight = origBitmap.height / (i * 2)if (newWidth > 0 && newHeight > 0) {- bitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true)+ if (bitmap !== origBitmap) bitmap.recycle()+ bitmap = Bitmap.createScaledBitmap(origBitmap, newWidth, newHeight, true)}}@@ -202,9 +207,6 @@ class EnteQrPlugin: FlutterPlugin, MethodCallHandler {val detections = mutableListOf<Map<String, Any>>()val scaleX = if (i == 0) 1.0 else (i * 2).toDouble()val scaleY = if (i == 0) 1.0 else (i * 2).toDouble()- val origBitmap = BitmapFactory.decodeFile(imagePath)- val origWidth = origBitmap.width.toDouble()- val origHeight = origBitmap.height.toDouble()for (qrResult in results) {val points = qrResult.resultPoints@@ -212,8 +214,8 @@ class EnteQrPlugin: FlutterPlugin, MethodCallHandler {var minX = Float.MAX_VALUEvar minY = Float.MAX_VALUE- var maxX = Float.MIN_VALUE- var maxY = Float.MIN_VALUE+ var maxX = -Float.MAX_VALUE+ var maxY = -Float.MAX_VALUEfor (point in points) {if (point == null) continue@@ -243,6 +245,8 @@ class EnteQrPlugin: FlutterPlugin, MethodCallHandler {}if (detections.isNotEmpty()) {+ if (bitmap !== origBitmap) bitmap.recycle()+ origBitmap.recycle()return mapOf("success" to true,"detections" to detections@@ -253,6 +257,9 @@ class EnteQrPlugin: FlutterPlugin, MethodCallHandler {}}+ if (bitmap !== origBitmap) bitmap.recycle()+ origBitmap.recycle()+return mapOf("success" to false,"error" to "No QR code found in image"diff --git a/mobile/apps/auth/plugins/qr/ios/Classes/EnteQrPlugin.swift b/mobile/apps/auth/plugins/qr/ios/Classes/EnteQrPlugin.swiftindex 629daa67d4..7a94d2fbe5 100644--- a/mobile/apps/auth/plugins/qr/ios/Classes/EnteQrPlugin.swift+++ b/mobile/apps/auth/plugins/qr/ios/Classes/EnteQrPlugin.swift@@ -1,6 +1,5 @@import Flutterimport UIKit-import AVFoundationpublic class EnteQrPlugin: NSObject, FlutterPlugin {public static func register(with registrar: FlutterPluginRegistrar) {@@ -137,11 +136,19 @@ public class EnteQrPlugin: NSObject, FlutterPlugin {let minYci = min(qrFeature.bottomLeft.y, qrFeature.bottomRight.y)let maxYci = max(qrFeature.topLeft.y, qrFeature.topRight.y)+ // Add padding around finder patterns (matching Android's 15%)+ let padX = (maxX - minX) * 0.15+ let padY = (maxYci - minYci) * 0.15+ let paddedMinX = max(minX - padX, 0)+ let paddedMaxX = min(maxX + padX, imageWidth)+ let paddedMinYci = max(minYci - padY, 0)+ let paddedMaxYci = min(maxYci + padY, imageHeight)+// Flip y: top-left origin- let normX = minX / imageWidth- let normY = 1.0 - (maxYci / imageHeight)- let normW = (maxX - minX) / imageWidth- let normH = (maxYci - minYci) / imageHeight+ let normX = paddedMinX / imageWidth+ let normY = 1.0 - (paddedMaxYci / imageHeight)+ let normW = (paddedMaxX - paddedMinX) / imageWidth+ let normH = (paddedMaxYci - paddedMinYci) / imageHeightdetections.append(["content": messageString,diff --git a/mobile/apps/photos/lib/l10n/intl_en.arb b/mobile/apps/photos/lib/l10n/intl_en.arbindex 0b053cfe27..d8887daedf 100644--- a/mobile/apps/photos/lib/l10n/intl_en.arb+++ b/mobile/apps/photos/lib/l10n/intl_en.arb@@ -964,6 +964,8 @@"favorite": "Favorite","removeFromFavorite": "Remove from favorites","shareLink": "Share link",+ "openLink": "Open link",+ "pay": "Pay","createCollage": "Create collage","saveCollage": "Save collage","collageSaved": "Collage saved to gallery",diff --git a/mobile/apps/photos/lib/ui/viewer/file/qr_code_content_sheet.dart b/mobile/apps/photos/lib/ui/viewer/file/qr_code_content_sheet.dartindex 91c4a0de62..3c7850648e 100644--- a/mobile/apps/photos/lib/ui/viewer/file/qr_code_content_sheet.dart+++ b/mobile/apps/photos/lib/ui/viewer/file/qr_code_content_sheet.dart@@ -4,6 +4,7 @@ import "package:flutter/services.dart";import "package:photos/generated/l10n.dart";import "package:photos/theme/ente_theme.dart";import "package:photos/ui/components/base_bottom_sheet.dart";+import "package:photos/ui/components/buttons/button_widget_v2.dart";import "package:photos/ui/notification/toast.dart";import "package:photos/utils/share_util.dart";import "package:url_launcher/url_launcher.dart";@@ -11,178 +12,125 @@ import "package:url_launcher/url_launcher.dart";Future<void> showQrCodeContentSheet(BuildContext context, {required List<QrDetection> detections,- int initialIndex = 0,}) {return showBaseBottomSheet(context,title: AppLocalizations.of(context).qrCode,- child: detections.length == 1- ? _SingleQrContent(content: detections.first.content)- : _MultiQrContent(- detections: detections,- initialIndex: initialIndex,- ),+ child: Column(+ mainAxisSize: MainAxisSize.min,+ crossAxisAlignment: CrossAxisAlignment.stretch,+ children: [+ for (int i = 0; i < detections.length; i++) ...[+ if (i > 0) const SizedBox(height: 16),+ _QrContentEntry(content: detections[i].content),+ ],+ ],+ ),);}-class _SingleQrContent extends StatelessWidget {+class _QrContentEntry extends StatelessWidget {final String content;- const _SingleQrContent({required this.content});-- @override- Widget build(BuildContext context) {- return _QrContentEntry(content: content);- }-}--class _MultiQrContent extends StatelessWidget {- final List<QrDetection> detections;- final int initialIndex;-- const _MultiQrContent({- required this.detections,- required this.initialIndex,- });+ const _QrContentEntry({required this.content});@overrideWidget build(BuildContext context) {final colorScheme = getEnteColorScheme(context);final textTheme = getEnteTextTheme(context);+ final l10n = AppLocalizations.of(context);+ final uri = Uri.tryParse(content);+ final isUrl =+ uri != null && (uri.scheme == "http" || uri.scheme == "https");+ final isUpi = uri != null && uri.scheme == "upi";return Column(mainAxisSize: MainAxisSize.min,crossAxisAlignment: CrossAxisAlignment.stretch,children: [- for (int i = 0; i < detections.length; i++) ...[- if (i > 0) Divider(color: colorScheme.strokeFaint, height: 24),- Row(- crossAxisAlignment: CrossAxisAlignment.start,+ if (!isUpi) ...[+ Stack(children: [Container(- width: 24,- height: 24,- alignment: Alignment.center,+ width: double.infinity,+ padding: const EdgeInsets.only(+ left: 16,+ top: 16,+ bottom: 16,+ right: 48,+ ),decoration: BoxDecoration(- color: colorScheme.primary700,- shape: BoxShape.circle,+ color: colorScheme.fillFaint,+ borderRadius: BorderRadius.circular(12),),child: Text(- "${i + 1}",- style: textTheme.mini.copyWith(color: Colors.white),+ content,+ style: textTheme.body.copyWith(color: colorScheme.textBase),),),- const SizedBox(width: 12),- Expanded(- child: _QrContentEntry(content: detections[i].content),+ Positioned(+ top: 12,+ right: 12,+ child: GestureDetector(+ onTap: () async {+ await Clipboard.setData(+ ClipboardData(text: content),+ );+ if (context.mounted) {+ showShortToast(context, l10n.copied);+ }+ },+ child: Icon(+ Icons.copy_rounded,+ size: 20,+ color: colorScheme.textMuted,+ ),+ ),),],),+ const SizedBox(height: 16),],- ],- );- }-}--class _QrContentEntry extends StatelessWidget {- final String content;-- const _QrContentEntry({required this.content});-- @override- Widget build(BuildContext context) {- final colorScheme = getEnteColorScheme(context);- final textTheme = getEnteTextTheme(context);- final l10n = AppLocalizations.of(context);- final Uri? parsedUri = Uri.tryParse(content);- final bool isUrl = parsedUri != null &&- (parsedUri.scheme == "http" || parsedUri.scheme == "https");-- return Column(- mainAxisSize: MainAxisSize.min,- crossAxisAlignment: CrossAxisAlignment.stretch,- children: [- Container(- padding: const EdgeInsets.all(16),- decoration: BoxDecoration(- color: colorScheme.fillDark,- borderRadius: BorderRadius.circular(12),+ if (isUrl) ...[+ ButtonWidgetV2(+ buttonType: ButtonTypeV2.primary,+ labelText: l10n.openLink,+ onTap: () async {+ await launchUrl(uri, mode: LaunchMode.externalApplication);+ },),- child: SelectableText(- content,- style: textTheme.body.copyWith(color: colorScheme.textBase),+ const SizedBox(height: 8),+ ButtonWidgetV2(+ buttonType: ButtonTypeV2.secondary,+ labelText: l10n.shareLink,+ onTap: () async {+ await shareText(content, context: context);+ },),- ),- const SizedBox(height: 16),- Row(- children: [- _ActionButton(- icon: Icons.copy,- label: l10n.copyLink,- onTap: () async {- await Clipboard.setData(ClipboardData(text: content));+ ],+ if (isUpi)+ ButtonWidgetV2(+ buttonType: ButtonTypeV2.primary,+ labelText: l10n.pay,+ onTap: () async {+ try {+ await launchUrl(uri, mode: LaunchMode.externalApplication);+ } catch (_) {if (context.mounted) {- showShortToast(context, l10n.copied);+ showShortToast(context, "No payment app found");}- },- ),- if (isUrl) ...[- const SizedBox(width: 12),- _ActionButton(- icon: Icons.open_in_new,- label: l10n.openFile,- onTap: () => launchUrl(parsedUri),- ),- ],- const SizedBox(width: 12),- _ActionButton(- icon: Icons.share,- label: l10n.share,- onTap: () => shareText(content, context: context),- ),- ],- ),+ }+ },+ ),+ if (!isUrl && !isUpi)+ ButtonWidgetV2(+ buttonType: ButtonTypeV2.primary,+ labelText: l10n.shareLink,+ onTap: () async {+ await shareText(content, context: context);+ },+ ),],);}}--class _ActionButton extends StatelessWidget {- final IconData icon;- final String label;- final VoidCallback onTap;-- const _ActionButton({- required this.icon,- required this.label,- required this.onTap,- });-- @override- Widget build(BuildContext context) {- final colorScheme = getEnteColorScheme(context);- final textTheme = getEnteTextTheme(context);-- return GestureDetector(- onTap: onTap,- child: Container(- padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),- decoration: BoxDecoration(- color: colorScheme.fillDark,- borderRadius: BorderRadius.circular(8),- ),- child: Row(- mainAxisSize: MainAxisSize.min,- children: [- Icon(icon, size: 18, color: colorScheme.textMuted),- const SizedBox(width: 6),- Text(- label,- style: textTheme.mini.copyWith(color: colorScheme.textMuted),- ),- ],- ),- ),- );- }-}diff --git a/mobile/apps/photos/lib/ui/viewer/file/qr_code_detection_helper.dart b/mobile/apps/photos/lib/ui/viewer/file/qr_code_detection_helper.dartindex 34adb65873..c8b6232184 100644--- a/mobile/apps/photos/lib/ui/viewer/file/qr_code_detection_helper.dart+++ b/mobile/apps/photos/lib/ui/viewer/file/qr_code_detection_helper.dart@@ -1,3 +1,4 @@+import "dart:async";import "dart:io";import "package:ente_qr/ente_qr.dart";@@ -9,6 +10,8 @@ import "package:photos/utils/file_util.dart";class QrCodeDetectionHelper {static final Map<String, _DetectionResult> _cache = {};+ static const _debounceDuration = Duration(milliseconds: 500);+final Logger _logger = Logger("QrCodeDetectionHelper");final EnteQr _enteQr = EnteQr();@@ -17,8 +20,11 @@ class QrCodeDetectionHelper {int _requestId = 0;bool _disposed = false;+ Timer? _debounceTimer;Future<void> evaluateFile(EnteFile file) async {+ _debounceTimer?.cancel();+final bool isEligible = _isFileEligible(file);final int requestId = ++_requestId;@@ -27,6 +33,7 @@ class QrCodeDetectionHelper {return;}+ // Return cached results immediately without debouncefinal String cacheKey = _cacheKey(file);final _DetectionResult? cachedResult = _cache[cacheKey];if (cachedResult != null) {@@ -37,6 +44,19 @@ class QrCodeDetectionHelper {qrDetectionsNotifier.value = const [];+ // Debounce uncached scans so swiping doesn't trigger work+ _debounceTimer = Timer(_debounceDuration, () {+ _scanFile(file, cacheKey, requestId);+ });+ }++ Future<void> _scanFile(+ EnteFile file,+ String cacheKey,+ int requestId,+ ) async {+ if (_disposed || requestId != _requestId) return;+try {final File? localFile = await getFile(file);if (_disposed || requestId != _requestId) return;@@ -86,6 +106,7 @@ class QrCodeDetectionHelper {void dispose() {_disposed = true;+ _debounceTimer?.cancel();qrDetectionsNotifier.dispose();}}diff --git a/mobile/apps/photos/lib/ui/viewer/file/qr_code_highlight_overlay.dart b/mobile/apps/photos/lib/ui/viewer/file/qr_code_highlight_overlay.dartindex 6387c50650..ef64f2a275 100644--- a/mobile/apps/photos/lib/ui/viewer/file/qr_code_highlight_overlay.dart+++ b/mobile/apps/photos/lib/ui/viewer/file/qr_code_highlight_overlay.dart@@ -1,3 +1,5 @@+import "dart:async";+import "package:ente_qr/ente_qr.dart";import "package:flutter/foundation.dart";import "package:flutter/material.dart";@@ -56,7 +58,6 @@ class QrCodeHighlightOverlay extends StatelessWidget {for (final detection in detections)_QrTapRegion(detection: detection,- allDetections: detections,offsetX: offsetX,offsetY: offsetY,displayWidth: displayWidth,@@ -71,9 +72,8 @@ class QrCodeHighlightOverlay extends StatelessWidget {}}-class _QrTapRegion extends StatelessWidget {+class _QrTapRegion extends StatefulWidget {final QrDetection detection;- final List<QrDetection> allDetections;final double offsetX;final double offsetY;final double displayWidth;@@ -81,41 +81,92 @@ class _QrTapRegion extends StatelessWidget {const _QrTapRegion({required this.detection,- required this.allDetections,required this.offsetX,required this.offsetY,required this.displayWidth,required this.displayHeight,});+ @override+ State<_QrTapRegion> createState() => _QrTapRegionState();+}++class _QrTapRegionState extends State<_QrTapRegion> {+ static const _longPressDuration = Duration(milliseconds: 500);+ static const _moveThreshold = 20.0;+ Timer? _timer;+ Offset? _downPosition;++ @override+ void dispose() {+ _timer?.cancel();+ super.dispose();+ }++ Future<void> _onLongPress() async {+ await HapticFeedback.lightImpact();+ final uri = Uri.tryParse(widget.detection.content);+ final isUpi = uri != null && uri.scheme == "upi";+ if (isUpi) {+ try {+ final launched =+ await launchUrl(uri, mode: LaunchMode.externalApplication);+ if (!launched && mounted) {+ await showQrCodeContentSheet(+ context,+ detections: [widget.detection],+ );+ }+ } catch (_) {+ if (mounted) {+ await showQrCodeContentSheet(+ context,+ detections: [widget.detection],+ );+ }+ }+ } else {+ await showQrCodeContentSheet(+ context,+ detections: [widget.detection],+ );+ }+ }+@overrideWidget build(BuildContext context) {- final screenX = offsetX + detection.x * displayWidth;- final screenY = offsetY + detection.y * displayHeight;- final screenW = detection.width * displayWidth;- final screenH = detection.height * displayHeight;+ final rawW = widget.detection.width * widget.displayWidth;+ final rawH = widget.detection.height * widget.displayHeight;+ const double minTapTarget = 48.0;+ final screenW = rawW < minTapTarget ? minTapTarget : rawW;+ final screenH = rawH < minTapTarget ? minTapTarget : rawH;+ final screenX = widget.offsetX ++ widget.detection.x * widget.displayWidth -+ (screenW - rawW) / 2;+ final screenY = widget.offsetY ++ widget.detection.y * widget.displayHeight -+ (screenH - rawH) / 2;return Positioned(left: screenX,top: screenY,width: screenW,height: screenH,- child: GestureDetector(- behavior: HitTestBehavior.opaque,- onLongPress: () {- HapticFeedback.lightImpact();- final uri = Uri.tryParse(detection.content);- final isUrl =- uri != null && (uri.scheme == "http" || uri.scheme == "https");- if (isUrl) {- launchUrl(uri);- } else {- showQrCodeContentSheet(- context,- detections: [detection],- );+ child: Listener(+ behavior: HitTestBehavior.translucent,+ onPointerDown: (event) {+ _downPosition = event.position;+ _timer?.cancel();+ _timer = Timer(_longPressDuration, _onLongPress);+ },+ onPointerMove: (event) {+ if (_downPosition != null &&+ (event.position - _downPosition!).distance > _moveThreshold) {+ _timer?.cancel();}},+ onPointerUp: (_) => _timer?.cancel(),+ onPointerCancel: (_) => _timer?.cancel(),child: const SizedBox.expand(),),);