diff --git a/packages/flutter/lib/src/widgets/selectable_region.dart b/packages/flutter/lib/src/widgets/selectable_region.dart index 8fbdb763a7..7806527095 100644 --- a/packages/flutter/lib/src/widgets/selectable_region.dart +++ b/packages/flutter/lib/src/widgets/selectable_region.dart @@ -331,6 +331,12 @@ class SelectableRegionState extends State with TextSelectionDe @visibleForTesting SelectionOverlay? get selectionOverlay => _selectionOverlay; + /// The text processing service used to retrieve the native text processing actions. + final ProcessTextService _processTextService = DefaultProcessTextService(); + + /// The list of native text processing actions provided by the engine. + final List _processTextActions = []; + @override void initState() { super.initState(); @@ -359,6 +365,14 @@ class SelectableRegionState extends State with TextSelectionDe instance.onSecondaryTapDown = _handleRightClickDown; }, ); + _initProcessTextActions(); + } + + /// Query the engine to initialize the list of text processing actions to show + /// in the text selection toolbar. + Future _initProcessTextActions() async { + _processTextActions.clear(); + _processTextActions.addAll(await _processTextService.queryTextActions()); } @override @@ -1203,7 +1217,29 @@ class SelectableRegionState extends State with TextSelectionDe hideToolbar(); } }, - ); + )..addAll(_textProcessingActionButtonItems); + } + + List get _textProcessingActionButtonItems { + final List buttonItems = []; + final SelectedContent? data = _selectable?.getSelectedContent(); + if (data == null) { + return buttonItems; + } + + for (final ProcessTextAction action in _processTextActions) { + buttonItems.add(ContextMenuButtonItem( + label: action.label, + onPressed: () async { + final String selectedText = data.plainText; + if (selectedText.isNotEmpty) { + await _processTextService.processTextAction(action.id, selectedText, true); + hideToolbar(); + } + }, + )); + } + return buttonItems; } /// The line height at the start of the current selection. diff --git a/packages/flutter/test/material/selection_area_test.dart b/packages/flutter/test/material/selection_area_test.dart index b26f17eaa6..9967576c88 100644 --- a/packages/flutter/test/material/selection_area_test.dart +++ b/packages/flutter/test/material/selection_area_test.dart @@ -7,8 +7,10 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; +import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; +import '../widgets/process_text_utils.dart'; Offset textOffsetToPosition(RenderParagraph paragraph, int offset) { const Rect caret = Rect.fromLTWH(0.0, 0.0, 2.0, 20.0); @@ -200,6 +202,54 @@ void main() { skip: kIsWeb, // [intended] ); + testWidgets('Text processing actions are added to the toolbar', (WidgetTester tester) async { + final MockProcessTextHandler mockProcessTextHandler = MockProcessTextHandler(); + TestWidgetsFlutterBinding.ensureInitialized().defaultBinaryMessenger + .setMockMethodCallHandler(SystemChannels.processText, mockProcessTextHandler.handleMethodCall); + addTearDown(() => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(SystemChannels.processText, null)); + + final FocusNode focusNode = FocusNode(); + addTearDown(focusNode.dispose); + + await tester.pumpWidget( + MaterialApp( + home: SelectionArea( + focusNode: focusNode, + child: const Text('How are you?'), + ), + ), + ); + await tester.pumpAndSettle(); + + expect(find.byType(AdaptiveTextSelectionToolbar), findsNothing); + + final RenderParagraph paragraph = tester.renderObject( + find.descendant( + of: find.text('How are you?'), + matching: find.byType(RichText), + ), + ); + final TestGesture gesture = await tester.startGesture(textOffsetToPosition(paragraph, 6)); // at the 'r' + addTearDown(gesture.removePointer); + await tester.pump(const Duration(milliseconds: 500)); + // `are` is selected. + expect(paragraph.selections[0], const TextSelection(baseOffset: 4, extentOffset: 7)); + + await gesture.up(); + await tester.pumpAndSettle(); + + // The toolbar is visible. + expect(find.byType(AdaptiveTextSelectionToolbar), findsOneWidget); + + // The text processing actions are visible on Android only. + final bool areTextActionsSupported = defaultTargetPlatform == TargetPlatform.android; + expect(find.text(fakeAction1Label), areTextActionsSupported ? findsOneWidget : findsNothing); + expect(find.text(fakeAction2Label), areTextActionsSupported ? findsOneWidget : findsNothing); + }, + variant: TargetPlatformVariant.all(), + skip: kIsWeb, // [intended] + ); + testWidgets('onSelectionChange is called when the selection changes', (WidgetTester tester) async { SelectedContent? content; diff --git a/packages/flutter/test/widgets/selectable_region_test.dart b/packages/flutter/test/widgets/selectable_region_test.dart index 6a2b295d7f..698e49dbe0 100644 --- a/packages/flutter/test/widgets/selectable_region_test.dart +++ b/packages/flutter/test/widgets/selectable_region_test.dart @@ -11,6 +11,7 @@ import 'package:flutter_test/flutter_test.dart'; import 'clipboard_utils.dart'; import 'keyboard_utils.dart'; +import 'process_text_utils.dart'; import 'semantics_tester.dart'; Offset textOffsetToPosition(RenderParagraph paragraph, int offset) { @@ -3368,6 +3369,60 @@ void main() { skip: kIsWeb, // [intended] ); + testWidgets('Text processing actions are added to the toolbar', (WidgetTester tester) async { + final MockProcessTextHandler mockProcessTextHandler = MockProcessTextHandler(); + TestWidgetsFlutterBinding.ensureInitialized().defaultBinaryMessenger + .setMockMethodCallHandler(SystemChannels.processText, mockProcessTextHandler.handleMethodCall); + addTearDown(() => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(SystemChannels.processText, null)); + + Set buttonLabels = {}; + final FocusNode focusNode = FocusNode(); + addTearDown(focusNode.dispose); + + await tester.pumpWidget( + MaterialApp( + home: SelectableRegion( + focusNode: focusNode, + selectionControls: materialTextSelectionHandleControls, + contextMenuBuilder: ( + BuildContext context, + SelectableRegionState selectableRegionState, + ) { + buttonLabels = selectableRegionState.contextMenuButtonItems + .map((ContextMenuButtonItem buttonItem) => buttonItem.label) + .toSet(); + return const SizedBox.shrink(); + }, + child: const Text('How are you?'), + ), + ), + ); + await tester.pumpAndSettle(); + + final RenderParagraph paragraph = tester.renderObject( + find.descendant( + of: find.text('How are you?'), + matching: find.byType(RichText), + ), + ); + final TestGesture gesture = await tester.startGesture(textOffsetToPosition(paragraph, 6)); // at the 'r' + addTearDown(gesture.removePointer); + await tester.pump(const Duration(milliseconds: 500)); + // `are` is selected. + expect(paragraph.selections[0], const TextSelection(baseOffset: 4, extentOffset: 7)); + + await gesture.up(); + await tester.pumpAndSettle(); + + // The text processing actions are available on Android only. + final bool areTextActionsSupported = defaultTargetPlatform == TargetPlatform.android; + expect(buttonLabels.contains(fakeAction1Label), areTextActionsSupported); + expect(buttonLabels.contains(fakeAction2Label), areTextActionsSupported); + }, + variant: TargetPlatformVariant.all(), + skip: kIsWeb, // [intended] + ); + testWidgets('onSelectionChange is called when the selection changes through gestures', (WidgetTester tester) async { SelectedContent? content; final FocusNode focusNode = FocusNode();