From cbe0999d1f211cb29cb9e6fae088a9b700c996d3 Mon Sep 17 00:00:00 2001 From: guolinaileen <2993595+guolinaileen@users.noreply.github.com> Date: Tue, 11 Aug 2020 11:31:02 -0700 Subject: [PATCH] Introduce onAppPrivateCommand to TextField (#62712) --- .../flutter/lib/src/material/text_field.dart | 5 + .../flutter/lib/src/services/text_input.dart | 7 + .../lib/src/widgets/editable_text.dart | 14 ++ .../flutter/test/services/autofill_test.dart | 5 + .../test/services/text_input_test.dart | 151 +++++++++++++++++- 5 files changed, 180 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/material/text_field.dart b/packages/flutter/lib/src/material/text_field.dart index 1622dcebcb..1702fff855 100644 --- a/packages/flutter/lib/src/material/text_field.dart +++ b/packages/flutter/lib/src/material/text_field.dart @@ -351,6 +351,7 @@ class TextField extends StatefulWidget { this.onChanged, this.onEditingComplete, this.onSubmitted, + this.onAppPrivateCommand, this.inputFormatters, this.enabled, this.cursorWidth = 2.0, @@ -632,6 +633,9 @@ class TextField extends StatefulWidget { /// [TextInputAction.previous] for [textInputAction]. final ValueChanged onSubmitted; + /// {@macro flutter.widgets.editableText.onAppPrivateCommand} + final AppPrivateCommandCallback onAppPrivateCommand; + /// {@macro flutter.widgets.editableText.inputFormatters} final List inputFormatters; @@ -1147,6 +1151,7 @@ class _TextFieldState extends State implements TextSelectionGestureDe onSelectionChanged: _handleSelectionChanged, onEditingComplete: widget.onEditingComplete, onSubmitted: widget.onSubmitted, + onAppPrivateCommand: widget.onAppPrivateCommand, onSelectionHandleTapped: _handleSelectionHandleTapped, inputFormatters: formatters, rendererIgnoresPointer: true, diff --git a/packages/flutter/lib/src/services/text_input.dart b/packages/flutter/lib/src/services/text_input.dart index 079c211975..468675be46 100644 --- a/packages/flutter/lib/src/services/text_input.dart +++ b/packages/flutter/lib/src/services/text_input.dart @@ -802,6 +802,9 @@ abstract class TextInputClient { /// Requests that this client perform the given action. void performAction(TextInputAction action); + /// Requests that this client perform the private command. + void performPrivateCommand(String action, Map data); + /// Updates the floating cursor position and state. void updateFloatingCursor(RawFloatingCursorPoint point); @@ -1157,6 +1160,10 @@ class TextInput { case 'TextInputClient.performAction': _currentConnection!._client.performAction(_toTextInputAction(args[1] as String)); break; + case 'TextInputClient.performPrivateCommand': + _currentConnection!._client.performPrivateCommand( + args[1]['action'] as String, args[1]['data'] as Map); + break; case 'TextInputClient.updateFloatingCursor': _currentConnection!._client.updateFloatingCursor(_toTextPoint( _toTextCursorAction(args[1] as String), diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index 3ebad5fbc6..c5c6018c39 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -40,6 +40,9 @@ export 'package:flutter/services.dart' show TextEditingValue, TextSelection, Tex /// (including the cursor location). typedef SelectionChangedCallback = void Function(TextSelection selection, SelectionChangedCause cause); +/// Signature for the callback that reports the app private command results. +typedef AppPrivateCommandCallback = void Function(String, Map); + // The time it takes for the cursor to fade from fully opaque to fully // transparent and vice versa. A full cursor blink, from transparent to opaque // to transparent, is twice this duration. @@ -413,6 +416,7 @@ class EditableText extends StatefulWidget { this.onChanged, this.onEditingComplete, this.onSubmitted, + this.onAppPrivateCommand, this.onSelectionChanged, this.onSelectionHandleTapped, List inputFormatters, @@ -1030,6 +1034,11 @@ class EditableText extends StatefulWidget { /// {@end-tool} final ValueChanged onSubmitted; + /// {@template flutter.widgets.editableText.onAppPrivateCommand} + /// Called when the result of an app private command is received. + /// {@endtemplate} + final AppPrivateCommandCallback onAppPrivateCommand; + /// Called when the user changes the selection of text (including the cursor /// location). final SelectionChangedCallback onSelectionChanged; @@ -1616,6 +1625,11 @@ class EditableTextState extends State with AutomaticKeepAliveClien } } + @override + void performPrivateCommand(String action, Map data) { + widget.onAppPrivateCommand(action, data); + } + // The original position of the caret on FloatingCursorDragState.start. Rect _startCaretRect; diff --git a/packages/flutter/test/services/autofill_test.dart b/packages/flutter/test/services/autofill_test.dart index d1a29224d3..290af272d1 100644 --- a/packages/flutter/test/services/autofill_test.dart +++ b/packages/flutter/test/services/autofill_test.dart @@ -148,6 +148,11 @@ class FakeAutofillClient implements TextInputClient, AutofillClient { latestMethodCall = 'performAction'; } + @override + void performPrivateCommand(String action, Map data) { + latestMethodCall = 'performPrivateCommand'; + } + @override void updateFloatingCursor(RawFloatingCursorPoint point) { latestMethodCall = 'updateFloatingCursor'; diff --git a/packages/flutter/test/services/text_input_test.dart b/packages/flutter/test/services/text_input_test.dart index 7ef6a90e83..a802d6aaa3 100644 --- a/packages/flutter/test/services/text_input_test.dart +++ b/packages/flutter/test/services/text_input_test.dart @@ -5,6 +5,7 @@ // @dart = 2.8 import 'dart:convert' show utf8; +import 'dart:convert' show jsonDecode; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart' show TestWidgetsFlutterBinding; @@ -170,7 +171,147 @@ void main() { expect(client.latestMethodCall, 'connectionClosed'); }); - test('TextInputClient showAutocorrectionPromptRect method is called', () async { + test('TextInputClient performPrivateCommand method is called', () async { + // Assemble a TextInputConnection so we can verify its change in state. + final FakeTextInputClient client = FakeTextInputClient(null); + const TextInputConfiguration configuration = TextInputConfiguration(); + TextInput.attach(client, configuration); + + expect(client.latestMethodCall, isEmpty); + + // Send performPrivateCommand message. + final ByteData messageBytes = const JSONMessageCodec().encodeMessage({ + 'args': [ + 1, + jsonDecode( + '{"action": "actionCommand", "data": {\"input_context\" : \"abcdefg\"}}') + ], + 'method': 'TextInputClient.performPrivateCommand', + }); + await ServicesBinding.instance.defaultBinaryMessenger.handlePlatformMessage( + 'flutter/textinput', + messageBytes, + (ByteData _) {}, + ); + + expect(client.latestMethodCall, 'performPrivateCommand'); + }); + + test('TextInputClient performPrivateCommand method is called with float', + () async { + // Assemble a TextInputConnection so we can verify its change in state. + final FakeTextInputClient client = FakeTextInputClient(null); + const TextInputConfiguration configuration = TextInputConfiguration(); + TextInput.attach(client, configuration); + + expect(client.latestMethodCall, isEmpty); + + // Send performPrivateCommand message. + final ByteData messageBytes = const JSONMessageCodec().encodeMessage({ + 'args': [ + 1, + jsonDecode( + '{"action": "actionCommand", "data": {\"input_context\" : 0.5}}') + ], + 'method': 'TextInputClient.performPrivateCommand', + }); + await ServicesBinding.instance.defaultBinaryMessenger.handlePlatformMessage( + 'flutter/textinput', + messageBytes, + (ByteData _) {}, + ); + + expect(client.latestMethodCall, 'performPrivateCommand'); + }); + + test( + 'TextInputClient performPrivateCommand method is called with CharSequence array', + () async { + // Assemble a TextInputConnection so we can verify its change in state. + final FakeTextInputClient client = FakeTextInputClient(null); + const TextInputConfiguration configuration = TextInputConfiguration(); + TextInput.attach(client, configuration); + + expect(client.latestMethodCall, isEmpty); + + // Send performPrivateCommand message. + final ByteData messageBytes = const JSONMessageCodec().encodeMessage({ + 'args': [ + 1, + jsonDecode( + '{"action": "actionCommand", "data": {\"input_context\" : ["abc", "efg"]}}') + ], + 'method': 'TextInputClient.performPrivateCommand', + }); + await ServicesBinding.instance.defaultBinaryMessenger.handlePlatformMessage( + 'flutter/textinput', + messageBytes, + (ByteData _) {}, + ); + + expect(client.latestMethodCall, 'performPrivateCommand'); + }); + + test( + 'TextInputClient performPrivateCommand method is called with CharSequence', + () async { + // Assemble a TextInputConnection so we can verify its change in state. + final FakeTextInputClient client = FakeTextInputClient(null); + const TextInputConfiguration configuration = TextInputConfiguration(); + TextInput.attach(client, configuration); + + expect(client.latestMethodCall, isEmpty); + + // Send performPrivateCommand message. + final ByteData messageBytes = + const JSONMessageCodec().encodeMessage({ + 'args': [ + 1, + jsonDecode( + '{"action": "actionCommand", "data": {\"input_context\" : "abc"}}') + ], + 'method': 'TextInputClient.performPrivateCommand', + }); + await ServicesBinding.instance.defaultBinaryMessenger.handlePlatformMessage( + 'flutter/textinput', + messageBytes, + (ByteData _) {}, + ); + + expect(client.latestMethodCall, 'performPrivateCommand'); + }); + + test( + 'TextInputClient performPrivateCommand method is called with float array', + () async { + // Assemble a TextInputConnection so we can verify its change in state. + final FakeTextInputClient client = FakeTextInputClient(null); + const TextInputConfiguration configuration = TextInputConfiguration(); + TextInput.attach(client, configuration); + + expect(client.latestMethodCall, isEmpty); + + // Send performPrivateCommand message. + final ByteData messageBytes = + const JSONMessageCodec().encodeMessage({ + 'args': [ + 1, + jsonDecode( + '{"action": "actionCommand", "data": {\"input_context\" : [0.5, 0.8]}}') + ], + 'method': 'TextInputClient.performPrivateCommand', + }); + await ServicesBinding.instance.defaultBinaryMessenger.handlePlatformMessage( + 'flutter/textinput', + messageBytes, + (ByteData _) {}, + ); + + expect(client.latestMethodCall, 'performPrivateCommand'); + }); + + test('TextInputClient showAutocorrectionPromptRect method is called', + () async { // Assemble a TextInputConnection so we can verify its change in state. final FakeTextInputClient client = FakeTextInputClient(null); const TextInputConfiguration configuration = TextInputConfiguration(); @@ -179,7 +320,8 @@ void main() { expect(client.latestMethodCall, isEmpty); // Send onConnectionClosed message. - final ByteData messageBytes = const JSONMessageCodec().encodeMessage({ + final ByteData messageBytes = + const JSONMessageCodec().encodeMessage({ 'args': [1, 0, 1], 'method': 'TextInputClient.showAutocorrectionPromptRect', }); @@ -237,6 +379,11 @@ class FakeTextInputClient implements TextInputClient { latestMethodCall = 'performAction'; } + @override + void performPrivateCommand(String action, Map data) { + latestMethodCall = 'performPrivateCommand'; + } + @override void updateEditingValue(TextEditingValue value) { latestMethodCall = 'updateEditingValue';