Skip to content

armgit / ente

Public Imported from GitHub
0

💚 End-to-end encrypted cloud for everything.

[mobile] Keep Auth codes usable at large text sizes (#14337)

neeraj-pilot0483776fc3e5
diff --git a/mobile/apps/auth/changes/current-code-large-text.md b/mobile/apps/auth/changes/current-code-large-text.md
new file mode 100644
index 0000000000..bba6cf9386
--- /dev/null
+++ b/mobile/apps/auth/changes/current-code-large-text.md
@@ -0,0 +1 @@
+- Fixed the current code being cut off at large text sizes (@yzhou216).
diff --git a/mobile/apps/auth/lib/ui/code_widget.dart b/mobile/apps/auth/lib/ui/code_widget.dart
index ecb7784404..126578cf9c 100644
--- a/mobile/apps/auth/lib/ui/code_widget.dart
+++ b/mobile/apps/auth/lib/ui/code_widget.dart
@@ -392,86 +392,116 @@ class _CodeWidgetState extends State<CodeWidget> {
Widget _getBottomRow(StringsLocalizations l10n) {
return Container(
padding: const EdgeInsets.only(left: 16, right: 16),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.start,
- crossAxisAlignment: CrossAxisAlignment.end,
- children: [
- Expanded(
- child: ValueListenableBuilder<String>(
- valueListenable: _currentCode,
- builder: (context, value, child) {
- return Material(
- type: MaterialType.transparency,
- child: AutoSizeText(
- _getFormattedCode(value),
- style: TextStyle(fontSize: widget.isCompactMode ? 14 : 24),
- maxLines: 1,
- textDirection: TextDirection.ltr,
- ),
- );
- },
- ),
- ),
- const SizedBox(width: 8),
- widget.code.type.isTOTPCompatible
- ? IgnorePointer(
- ignoring:
- CodeDisplayStore.instance.isSelectionModeActive.value,
- child: GestureDetector(
- onTap: () {
- _copyNextToClipboard();
- },
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.end,
- children: [
- Text(
- l10n.nextTotpTitle,
- style: Theme.of(context).textTheme.bodySmall,
+ child: LayoutBuilder(
+ builder: (context, constraints) => Row(
+ mainAxisAlignment: MainAxisAlignment.start,
+ crossAxisAlignment: CrossAxisAlignment.end,
+ children: [
+ Expanded(
+ child: ValueListenableBuilder<String>(
+ valueListenable: _currentCode,
+ builder: (context, value, child) {
+ final code = _getFormattedCode(value);
+ final style = TextStyle(
+ fontSize: widget.isCompactMode ? 14 : 24,
+ );
+ return Material(
+ type: MaterialType.transparency,
+ child: AutoSizeText(
+ code,
+ style: style,
+ maxLines: 1,
+ textDirection: TextDirection.ltr,
+ overflowReplacement: FittedBox(
+ fit: BoxFit.scaleDown,
+ alignment: Alignment.centerLeft,
+ child: Text(
+ code,
+ style: style,
+ maxLines: 1,
+ textDirection: TextDirection.ltr,
),
- ValueListenableBuilder<String>(
- valueListenable: _nextCode,
- builder: (context, value, child) {
- return Material(
- type: MaterialType.transparency,
- child: Text(
- _getFormattedCode(value),
- style: TextStyle(
- fontSize: widget.isCompactMode ? 12 : 18,
- color: Colors.grey,
- ),
- textDirection: TextDirection.ltr,
- ),
- );
- },
- ),
- ],
+ ),
),
+ );
+ },
+ ),
+ ),
+ const SizedBox(width: 8),
+ ConstrainedBox(
+ constraints: BoxConstraints(maxWidth: constraints.maxWidth / 2),
+ child: widget.code.type.isTOTPCompatible
+ ? FittedBox(
+ fit: BoxFit.scaleDown,
+ child: _getNextColumn(l10n),
+ )
+ : _getNextColumn(l10n),
+ ),
+ ],
+ ),
+ ),
+ );
+ }
+
+ Widget _getNextColumn(StringsLocalizations l10n) {
+ return widget.code.type.isTOTPCompatible
+ ? IgnorePointer(
+ ignoring: CodeDisplayStore.instance.isSelectionModeActive.value,
+ child: GestureDetector(
+ onTap: () {
+ _copyNextToClipboard();
+ },
+ child: Column(
+ crossAxisAlignment: CrossAxisAlignment.end,
+ children: [
+ Text(
+ l10n.nextTotpTitle,
+ style: Theme.of(context).textTheme.bodySmall,
),
- )
- : IgnorePointer(
- ignoring:
- CodeDisplayStore.instance.isSelectionModeActive.value,
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.end,
- children: [
- Text(
- l10n.nextTotpTitle,
- style: Theme.of(context).textTheme.bodySmall,
- ),
- InkWell(
- onTap: _onNextHotpTapped,
- child: const Icon(
- Icons.forward_outlined,
- size: 32,
- color: Colors.grey,
+ ValueListenableBuilder<String>(
+ valueListenable: _nextCode,
+ builder: (context, value, child) {
+ return Material(
+ type: MaterialType.transparency,
+ child: Text(
+ _getFormattedCode(value),
+ style: TextStyle(
+ fontSize: widget.isCompactMode ? 12 : 18,
+ color: Colors.grey,
+ ),
+ textDirection: TextDirection.ltr,
),
- ),
- ],
+ );
+ },
+ ),
+ ],
+ ),
+ ),
+ )
+ : IgnorePointer(
+ ignoring: CodeDisplayStore.instance.isSelectionModeActive.value,
+ child: Column(
+ crossAxisAlignment: CrossAxisAlignment.end,
+ children: [
+ FittedBox(
+ fit: BoxFit.scaleDown,
+ alignment: Alignment.centerRight,
+ child: Text(
+ l10n.nextTotpTitle,
+ style: Theme.of(context).textTheme.bodySmall,
),
),
- ],
- ),
- );
+ InkWell(
+ onTap: _onNextHotpTapped,
+ child: const Icon(
+ Icons.forward_outlined,
+ size: 32,
+ color: Colors.grey,
+ ),
+ ),
+ ],
+ ),
+ );
}
Widget _getTopRow({required bool isSelected}) {
diff --git a/mobile/apps/auth/test/ui/code_widget_text_scale_test.dart b/mobile/apps/auth/test/ui/code_widget_text_scale_test.dart
new file mode 100644
index 0000000000..6484ae6c72
--- /dev/null
+++ b/mobile/apps/auth/test/ui/code_widget_text_scale_test.dart
@@ -0,0 +1,127 @@
+import 'dart:io';
+
+import 'package:ente_auth/core/configuration.dart';
+import 'package:ente_auth/ente_theme_data.dart';
+import 'package:ente_auth/models/code.dart';
+import 'package:ente_auth/services/preference_service.dart';
+import 'package:ente_auth/ui/code_widget.dart';
+import 'package:ente_strings/ente_strings.dart';
+import 'package:flutter/material.dart';
+import 'package:flutter/rendering.dart';
+import 'package:flutter/services.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:shared_preferences/shared_preferences.dart';
+
+const _pathProviderChannel = MethodChannel('plugins.flutter.io/path_provider');
+const _secureStorageChannel = MethodChannel(
+ 'plugins.it_nomads.com/flutter_secure_storage',
+);
+final _codeText = find.byWidgetPredicate(
+ (widget) =>
+ widget is Text && RegExp(r'^\d+( \d+)*$').hasMatch(widget.data ?? ''),
+);
+
+void main() {
+ setUpAll(() async {
+ TestWidgetsFlutterBinding.ensureInitialized();
+ final messenger =
+ TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
+ final root = await Directory.systemTemp.createTemp('auth_text_scale_');
+ messenger.setMockMethodCallHandler(
+ _pathProviderChannel,
+ (_) async => root.path,
+ );
+ messenger.setMockMethodCallHandler(_secureStorageChannel, (_) async {
+ return null;
+ });
+ SharedPreferences.setMockInitialValues({});
+ await Configuration.instance.init([]);
+ await PreferenceService.instance.init();
+ });
+
+ for (final isCompactMode in [false, true]) {
+ testWidgets(
+ 'fits every digit at 2x text${isCompactMode ? ' in compact mode' : ''}',
+ (tester) async {
+ tester.view.physicalSize = const Size(960, 1600);
+ tester.view.devicePixelRatio = 2;
+ addTearDown(tester.view.reset);
+
+ await tester.pumpWidget(_app(_code(), isCompactMode: isCompactMode));
+
+ final codes = _codeText.evaluate().toList();
+ expect(codes, hasLength(2));
+ for (final code in codes) {
+ final paragraph = code.renderObject! as RenderParagraph;
+ expect(paragraph.didExceedMaxLines, isFalse);
+ }
+
+ final nextCode = tester.getRect(
+ find.byElementPredicate((element) => element == codes.last),
+ );
+ final card = tester.getRect(find.byType(CodeWidget));
+ expect(nextCode.width, lessThanOrEqualTo(card.width / 2));
+ },
+ );
+ }
+
+ testWidgets('keeps the HOTP action at full size with large translated text', (
+ tester,
+ ) async {
+ tester.view.physicalSize = const Size(640, 1600);
+ tester.view.devicePixelRatio = 2;
+ addTearDown(tester.view.reset);
+
+ await tester.pumpWidget(
+ _app(_hotpCode(), isCompactMode: false, locale: const Locale('de')),
+ );
+
+ expect(find.text('Nächster Code'), findsOneWidget);
+ final action = find.byWidgetPredicate(
+ (widget) =>
+ widget is InkWell &&
+ widget.child is Icon &&
+ (widget.child! as Icon).icon == Icons.forward_outlined,
+ );
+ expect(action, findsOneWidget);
+ expect(tester.getRect(action).size, const Size.square(32));
+ });
+}
+
+Code _code() {
+ return Code.fromAccountAndSecret(
+ Type.totp,
+ 'Ente',
+ 'JBSWY3DPEHPK3PXP',
+ null,
+ 8,
+ );
+}
+
+Code _hotpCode() {
+ return Code.fromAccountAndSecret(
+ Type.hotp,
+ 'Ente',
+ 'JBSWY3DPEHPK3PXP',
+ null,
+ 6,
+ );
+}
+
+Widget _app(Code code, {required bool isCompactMode, Locale? locale}) {
+ return MaterialApp(
+ theme: lightThemeData,
+ locale: locale,
+ localizationsDelegates: StringsLocalizations.localizationsDelegates,
+ supportedLocales: StringsLocalizations.supportedLocales,
+ builder: (context, child) => MediaQuery(
+ data: MediaQuery.of(
+ context,
+ ).copyWith(textScaler: const TextScaler.linear(2)),
+ child: child!,
+ ),
+ home: Scaffold(body: CodeWidget(code, isCompactMode: isCompactMode)),
+ );
+}