From 153f3d23c30e65838616e653b8b151205f9a952e Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 12 Nov 2020 00:34:05 +0100 Subject: [PATCH] Fix Editable(Text) shortcuts to respect read-only on desktop (#69891) --- .../flutter/lib/src/rendering/editable.dart | 7 +- .../test/widgets/editable_text_test.dart | 175 ++++++++++++++++++ 2 files changed, 180 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/rendering/editable.dart b/packages/flutter/lib/src/rendering/editable.dart index 219f62a388..a60a1e53a8 100644 --- a/packages/flutter/lib/src/rendering/editable.dart +++ b/packages/flutter/lib/src/rendering/editable.dart @@ -776,7 +776,7 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin { } return; } - if (key == LogicalKeyboardKey.keyX) { + if (key == LogicalKeyboardKey.keyX && !_readOnly) { if (!selection!.isCollapsed) { Clipboard.setData(ClipboardData(text: selection!.textInside(_plainText))); textSelectionDelegate.textEditingValue = TextEditingValue( @@ -787,7 +787,7 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin { } return; } - if (key == LogicalKeyboardKey.keyV) { + if (key == LogicalKeyboardKey.keyV && !_readOnly) { // Snapshot the input before using `await`. // See https://github.com/flutter/flutter/issues/11427 final TextEditingValue value = textSelectionDelegate.textEditingValue; @@ -818,6 +818,9 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin { void _handleDelete({ required bool forward }) { assert(_selection != null); + if (_readOnly) { + return; + } String textBefore = selection!.textBefore(_plainText); String textAfter = selection!.textAfter(_plainText); int cursorPosition = selection!.start; diff --git a/packages/flutter/test/widgets/editable_text_test.dart b/packages/flutter/test/widgets/editable_text_test.dart index 74518774e7..5fabb27e0d 100644 --- a/packages/flutter/test/widgets/editable_text_test.dart +++ b/packages/flutter/test/widgets/editable_text_test.dart @@ -4364,6 +4364,181 @@ void main() { // On web, using keyboard for selection is handled by the browser. }, skip: kIsWeb); + testWidgets('keyboard shortcuts respect read-only', (WidgetTester tester) async { + final String platform = describeEnum(defaultTargetPlatform).toLowerCase(); + final TextEditingController controller = TextEditingController(text: testText); + controller.selection = const TextSelection( + baseOffset: 0, + extentOffset: testText.length ~/2, + affinity: TextAffinity.upstream, + ); + TextSelection? selection; + await tester.pumpWidget(MaterialApp( + home: Align( + alignment: Alignment.topLeft, + child: SizedBox( + width: 400, + child: EditableText( + readOnly: true, + controller: controller, + autofocus: true, + focusNode: FocusNode(), + style: Typography.material2018(platform: TargetPlatform.android).black.subtitle1!, + cursorColor: Colors.blue, + backgroundCursorColor: Colors.grey, + selectionControls: materialTextSelectionControls, + keyboardType: TextInputType.text, + textAlign: TextAlign.right, + onSelectionChanged: (TextSelection newSelection, SelectionChangedCause? newCause) { + selection = newSelection; + }, + ), + ), + ), + )); + + await tester.pump(); // Wait for autofocus to take effect. + + const String clipboardContent = 'read-only'; + await Clipboard.setData(const ClipboardData(text: clipboardContent)); + + // Paste + await sendKeys( + tester, + [ + LogicalKeyboardKey.keyV, + ], + shortcutModifier: true, + platform: platform, + ); + + expect(selection, isNull, reason: 'on $platform'); + expect(controller.text, equals(testText), reason: 'on $platform'); + + // Select All + await sendKeys( + tester, + [ + LogicalKeyboardKey.keyA, + ], + shortcutModifier: true, + platform: platform, + ); + + expect( + selection!, + equals( + const TextSelection( + baseOffset: 0, + extentOffset: testText.length, + affinity: TextAffinity.upstream, + ), + ), + reason: 'on $platform', + ); + expect(controller.text, equals(testText), reason: 'on $platform'); + + // Cut + await sendKeys( + tester, + [ + LogicalKeyboardKey.keyX, + ], + shortcutModifier: true, + platform: platform, + ); + + expect( + selection!, + equals( + const TextSelection( + baseOffset: 0, + extentOffset: testText.length, + affinity: TextAffinity.upstream, + ), + ), + reason: 'on $platform', + ); + expect(controller.text, equals(testText), reason: 'on $platform'); + expect( + (await Clipboard.getData(Clipboard.kTextPlain))!.text, + equals(clipboardContent), + reason: 'on $platform', + ); + + // Copy + await sendKeys( + tester, + [ + LogicalKeyboardKey.keyC, + ], + shortcutModifier: true, + platform: platform, + ); + + expect( + selection!, + equals( + const TextSelection( + baseOffset: 0, + extentOffset: testText.length, + affinity: TextAffinity.upstream, + ), + ), + reason: 'on $platform', + ); + expect(controller.text, equals(testText), reason: 'on $platform'); + expect( + (await Clipboard.getData(Clipboard.kTextPlain))!.text, + equals(testText), + reason: 'on $platform', + ); + + // Delete + await sendKeys( + tester, + [ + LogicalKeyboardKey.delete, + ], + platform: platform, + ); + expect( + selection!, + equals( + const TextSelection( + baseOffset: 0, + extentOffset: testText.length, + affinity: TextAffinity.upstream, + ), + ), + reason: 'on $platform', + ); + expect(controller.text, equals(testText), reason: 'on $platform'); + + // Backspace + await sendKeys( + tester, + [ + LogicalKeyboardKey.backspace, + ], + platform: platform, + ); + expect( + selection!, + equals( + const TextSelection( + baseOffset: 0, + extentOffset: testText.length, + affinity: TextAffinity.upstream, + ), + ), + reason: 'on $platform', + ); + expect(controller.text, equals(testText), reason: 'on $platform'); + }, + skip: kIsWeb, + variant: TargetPlatformVariant.all()); + // Regression test for https://github.com/flutter/flutter/issues/31287 testWidgets('text selection handle visibility', (WidgetTester tester) async { // Text with two separate words to select.