Skip to content

Commit

Permalink
Hide the context menu on tap down (#126295)
Browse files Browse the repository at this point in the history
Desktop text selection toolbar no longer flashes before closing.
  • Loading branch information
justinmc authored May 12, 2023
1 parent 15a7e07 commit 6ffcc9e
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/flutter/lib/src/widgets/text_selection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2108,10 +2108,13 @@ class TextSelectionGestureDetectorBuilder {
switch (defaultTargetPlatform) {
case TargetPlatform.android:
case TargetPlatform.fuchsia:
// On mobile platforms the selection is set on tap up.
editableText.hideToolbar(false);
case TargetPlatform.iOS:
// On mobile platforms the selection is set on tap up.
break;
case TargetPlatform.macOS:
editableText.hideToolbar();
// On macOS, a shift-tapped unfocused field expands from 0, not from the
// previous selection.
if (isShiftPressedValid) {
Expand All @@ -2132,6 +2135,7 @@ class TextSelectionGestureDetectorBuilder {
renderEditable.selectPosition(cause: SelectionChangedCause.tap);
case TargetPlatform.linux:
case TargetPlatform.windows:
editableText.hideToolbar();
if (isShiftPressedValid) {
_extendSelection(details.globalPosition, SelectionChangedCause.tap);
return;
Expand Down Expand Up @@ -2206,18 +2210,16 @@ class TextSelectionGestureDetectorBuilder {
case TargetPlatform.linux:
case TargetPlatform.macOS:
case TargetPlatform.windows:
editableText.hideToolbar();
break;
// On desktop platforms the selection is set on tap down.
case TargetPlatform.android:
editableText.hideToolbar();
if (isShiftPressedValid) {
_extendSelection(details.globalPosition, SelectionChangedCause.tap);
return;
}
renderEditable.selectPosition(cause: SelectionChangedCause.tap);
editableText.showSpellCheckSuggestionsToolbar();
case TargetPlatform.fuchsia:
editableText.hideToolbar();
if (isShiftPressedValid) {
_extendSelection(details.globalPosition, SelectionChangedCause.tap);
return;
Expand Down
45 changes: 45 additions & 0 deletions packages/flutter/test/cupertino/text_field_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9513,4 +9513,49 @@ void main() {
variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS }),
skip: kIsWeb, // [intended]
);

testWidgets('text selection toolbar is hidden on tap down', (WidgetTester tester) async {
final TextEditingController controller = TextEditingController(
text: 'blah1 blah2',
);
await tester.pumpWidget(
CupertinoApp(
home: Center(
child: CupertinoTextField(
controller: controller,
),
),
),
);

expect(find.byType(CupertinoAdaptiveTextSelectionToolbar), findsNothing);

TestGesture gesture = await tester.startGesture(
textOffsetToPosition(tester, 8),
kind: PointerDeviceKind.mouse,
buttons: kSecondaryMouseButton,
);
await tester.pump();
await gesture.up();
await tester.pumpAndSettle();

expect(find.byType(CupertinoAdaptiveTextSelectionToolbar), findsOneWidget);

gesture = await tester.startGesture(
textOffsetToPosition(tester, 2),
kind: PointerDeviceKind.mouse,
);
await tester.pump();

// After the gesture is down but not up, the toolbar is already gone.
expect(find.byType(CupertinoAdaptiveTextSelectionToolbar), findsNothing);

await gesture.up();
await tester.pumpAndSettle();

expect(find.byType(CupertinoAdaptiveTextSelectionToolbar), findsNothing);
},
skip: isContextMenuProvidedByPlatform, // [intended] only applies to platforms where we supply the context menu.
variant: TargetPlatformVariant.all(excluding: <TargetPlatform>{ TargetPlatform.iOS }),
);
}
47 changes: 47 additions & 0 deletions packages/flutter/test/material/text_field_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16315,6 +16315,53 @@ void main() {
expectedConfiguration.spellCheckSuggestionsToolbarBuilder,
);
}, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.android, TargetPlatform.iOS }));

testWidgets('text selection toolbar is hidden on tap down', (WidgetTester tester) async {
final TextEditingController controller = TextEditingController(
text: 'blah1 blah2',
);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Center(
child: TextField(
controller: controller,
),
),
),
),
);

expect(find.byType(AdaptiveTextSelectionToolbar), findsNothing);

TestGesture gesture = await tester.startGesture(
textOffsetToPosition(tester, 8),
kind: PointerDeviceKind.mouse,
buttons: kSecondaryMouseButton,
);
await tester.pump();
await gesture.up();
await tester.pumpAndSettle();

expect(find.byType(AdaptiveTextSelectionToolbar), findsOneWidget);

gesture = await tester.startGesture(
textOffsetToPosition(tester, 2),
kind: PointerDeviceKind.mouse,
);
await tester.pump();

// After the gesture is down but not up, the toolbar is already gone.
expect(find.byType(AdaptiveTextSelectionToolbar), findsNothing);

await gesture.up();
await tester.pumpAndSettle();

expect(find.byType(AdaptiveTextSelectionToolbar), findsNothing);
},
skip: isContextMenuProvidedByPlatform, // [intended] only applies to platforms where we supply the context menu.
variant: TargetPlatformVariant.all(excluding: <TargetPlatform>{ TargetPlatform.iOS }),
);
}

/// A Simple widget for testing the obscure text.
Expand Down

0 comments on commit 6ffcc9e

Please sign in to comment.