From a217bbb6defed97c84bc26372193f5617ea683e3 Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Fri, 18 Mar 2022 10:50:18 -0700 Subject: [PATCH] Hide toolbar after select all on desktop (#100261) --- .../src/cupertino/desktop_text_selection.dart | 6 +++ .../src/material/desktop_text_selection.dart | 6 +++ .../test/material/text_field_test.dart | 50 +++++++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/packages/flutter/lib/src/cupertino/desktop_text_selection.dart b/packages/flutter/lib/src/cupertino/desktop_text_selection.dart index f5faa89a2b..56f5f6de2b 100644 --- a/packages/flutter/lib/src/cupertino/desktop_text_selection.dart +++ b/packages/flutter/lib/src/cupertino/desktop_text_selection.dart @@ -77,6 +77,12 @@ class _CupertinoDesktopTextSelectionControls extends TextSelectionControls { Offset getHandleAnchor(TextSelectionHandleType type, double textLineHeight) { return Offset.zero; } + + @override + void handleSelectAll(TextSelectionDelegate delegate) { + super.handleSelectAll(delegate); + delegate.hideToolbar(); + } } /// Text selection controls that follows Mac design conventions. diff --git a/packages/flutter/lib/src/material/desktop_text_selection.dart b/packages/flutter/lib/src/material/desktop_text_selection.dart index dd92770bf7..ad91020237 100644 --- a/packages/flutter/lib/src/material/desktop_text_selection.dart +++ b/packages/flutter/lib/src/material/desktop_text_selection.dart @@ -73,6 +73,12 @@ class _DesktopTextSelectionControls extends TextSelectionControls { value.text.isNotEmpty && !(value.selection.start == 0 && value.selection.end == value.text.length); } + + @override + void handleSelectAll(TextSelectionDelegate delegate) { + super.handleSelectAll(delegate); + delegate.hideToolbar(); + } } /// Text selection controls that loosely follows Material design conventions. diff --git a/packages/flutter/test/material/text_field_test.dart b/packages/flutter/test/material/text_field_test.dart index 51f7b90dc6..c6ec10363b 100644 --- a/packages/flutter/test/material/text_field_test.dart +++ b/packages/flutter/test/material/text_field_test.dart @@ -10125,6 +10125,56 @@ void main() { expect(cursorRight, inputWidth - kCaretGap); }); + testWidgets('Text selection menu hides after select all on desktop', (WidgetTester tester) async { + final TextEditingController controller = TextEditingController( + text: 'Atwater Peel Sherbrooke Bonaventure', + ); + await tester.pumpWidget( + MaterialApp( + home: Material( + child: TextField( + controller: controller, + ), + ), + ), + ); + + final String selectAll = defaultTargetPlatform == TargetPlatform.macOS + ? 'Select All' + : 'Select all'; + + expect(find.text(selectAll), findsNothing); + expect(find.text('Copy'), findsNothing); + + final TestGesture gesture = await tester.startGesture( + const Offset(10.0, 0.0) + textOffsetToPosition(tester, controller.text.length), + kind: PointerDeviceKind.mouse, + buttons: kSecondaryMouseButton, + ); + addTearDown(gesture.removePointer); + await tester.pump(); + await gesture.up(); + await tester.pumpAndSettle(); + + expect( + controller.value.selection, + TextSelection.collapsed( + offset: controller.text.length, + affinity: TextAffinity.upstream, + ), + ); + expect(find.text(selectAll), findsOneWidget); + + await tester.tapAt(tester.getCenter(find.text(selectAll))); + + await tester.pump(); + expect(find.text(selectAll), findsNothing); + expect(find.text('Copy'), findsNothing); + }, + variant: TargetPlatformVariant.desktop(), + skip: isContextMenuProvidedByPlatform, // [intended] only applies to platforms where we supply the context menu. + ); + // Regressing test for https://github.com/flutter/flutter/issues/70625 testWidgets('TextFields can inherit [FloatingLabelBehaviour] from InputDecorationTheme.', (WidgetTester tester) async { final FocusNode focusNode = FocusNode();