[web] Allow updating input configuration while connection is active (flutter/engine#21048)
This commit is contained in:
@@ -532,7 +532,7 @@ class InputConfiguration {
|
||||
final bool readOnly;
|
||||
|
||||
/// Whether to hide the text being edited.
|
||||
final bool? obscureText;
|
||||
final bool obscureText;
|
||||
|
||||
/// Whether to enable autocorrection.
|
||||
///
|
||||
@@ -759,20 +759,8 @@ abstract class DefaultTextEditingStrategy implements TextEditingStrategy {
|
||||
}) {
|
||||
assert(!isEnabled);
|
||||
|
||||
this._inputConfiguration = inputConfig;
|
||||
|
||||
_domElement = inputConfig.inputType.createDomElement();
|
||||
if (inputConfig.readOnly) {
|
||||
domElement.setAttribute('readonly', 'readonly');
|
||||
}
|
||||
if (inputConfig.obscureText!) {
|
||||
domElement.setAttribute('type', 'password');
|
||||
}
|
||||
|
||||
inputConfig.autofill?.applyToDomElement(domElement, focusedElement: true);
|
||||
|
||||
final String autocorrectValue = inputConfig.autocorrect ? 'on' : 'off';
|
||||
domElement.setAttribute('autocorrect', autocorrectValue);
|
||||
_applyConfiguration(inputConfig);
|
||||
|
||||
_setStaticStyleAttributes(domElement);
|
||||
_style?.applyToDomElement(domElement);
|
||||
@@ -793,6 +781,25 @@ abstract class DefaultTextEditingStrategy implements TextEditingStrategy {
|
||||
_onAction = onAction;
|
||||
}
|
||||
|
||||
void _applyConfiguration(InputConfiguration config) {
|
||||
_inputConfiguration = config;
|
||||
|
||||
if (config.readOnly) {
|
||||
domElement.setAttribute('readonly', 'readonly');
|
||||
} else {
|
||||
domElement.removeAttribute('readonly');
|
||||
}
|
||||
|
||||
if (config.obscureText) {
|
||||
domElement.setAttribute('type', 'password');
|
||||
}
|
||||
|
||||
config.autofill?.applyToDomElement(domElement, focusedElement: true);
|
||||
|
||||
final String autocorrectValue = config.autocorrect ? 'on' : 'off';
|
||||
domElement.setAttribute('autocorrect', autocorrectValue);
|
||||
}
|
||||
|
||||
@override
|
||||
void initializeElementPlacement() {
|
||||
placeElement();
|
||||
@@ -1278,6 +1285,11 @@ class TextEditingChannel {
|
||||
);
|
||||
break;
|
||||
|
||||
case 'TextInput.updateConfig':
|
||||
final config = InputConfiguration.fromFrameworkMessage(call.arguments);
|
||||
implementation.updateConfig(config);
|
||||
break;
|
||||
|
||||
case 'TextInput.setEditingState':
|
||||
implementation
|
||||
.setEditingState(EditingState.fromFrameworkMessage(call.arguments));
|
||||
@@ -1475,6 +1487,11 @@ class HybridTextEditing {
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
void updateConfig(InputConfiguration configuration) {
|
||||
_configuration = configuration;
|
||||
editingElement?._applyConfiguration(_configuration);
|
||||
}
|
||||
|
||||
/// Responds to the 'TextInput.setEditingState' message.
|
||||
void setEditingState(EditingState state) {
|
||||
editingElement!.setEditingState(state);
|
||||
|
||||
@@ -287,7 +287,8 @@ void testMain() {
|
||||
});
|
||||
|
||||
test('Triggers input action', () {
|
||||
final InputConfiguration config = InputConfiguration(inputAction: 'TextInputAction.done');
|
||||
final InputConfiguration config =
|
||||
InputConfiguration(inputAction: 'TextInputAction.done');
|
||||
editingElement.enable(
|
||||
config,
|
||||
onChange: trackEditingState,
|
||||
@@ -666,6 +667,45 @@ void testMain() {
|
||||
expect(spy.messages, isEmpty);
|
||||
});
|
||||
|
||||
test('setClient, setEditingState, show, updateConfig, clearClient', () {
|
||||
final MethodCall setClient = MethodCall('TextInput.setClient', <dynamic>[
|
||||
123,
|
||||
createFlutterConfig('text', readOnly: true),
|
||||
]);
|
||||
sendFrameworkMessage(codec.encodeMethodCall(setClient));
|
||||
|
||||
const MethodCall setEditingState = MethodCall(
|
||||
'TextInput.setEditingState',
|
||||
<String, dynamic>{
|
||||
'text': 'abcd',
|
||||
'selectionBase': 2,
|
||||
'selectionExtent': 3,
|
||||
},
|
||||
);
|
||||
sendFrameworkMessage(codec.encodeMethodCall(setEditingState));
|
||||
|
||||
const MethodCall show = MethodCall('TextInput.show');
|
||||
sendFrameworkMessage(codec.encodeMethodCall(show));
|
||||
|
||||
final HtmlElement element = textEditing.editingElement.domElement;
|
||||
expect(element.getAttribute('readonly'), 'readonly');
|
||||
|
||||
// Update the read-only config.
|
||||
final MethodCall updateConfig = MethodCall(
|
||||
'TextInput.updateConfig',
|
||||
createFlutterConfig('text', readOnly: false),
|
||||
);
|
||||
sendFrameworkMessage(codec.encodeMethodCall(updateConfig));
|
||||
|
||||
expect(element.hasAttribute('readonly'), isFalse);
|
||||
|
||||
const MethodCall clearClient = MethodCall('TextInput.clearClient');
|
||||
sendFrameworkMessage(codec.encodeMethodCall(clearClient));
|
||||
|
||||
// Confirm that [HybridTextEditing] didn't send any messages.
|
||||
expect(spy.messages, isEmpty);
|
||||
});
|
||||
|
||||
test('do not close connection on blur', () async {
|
||||
final MethodCall setClient = MethodCall(
|
||||
'TextInput.setClient', <dynamic>[123, flutterSinglelineConfig]);
|
||||
@@ -2082,6 +2122,7 @@ void checkTextAreaEditingState(
|
||||
/// simplicity.
|
||||
Map<String, dynamic> createFlutterConfig(
|
||||
String inputType, {
|
||||
bool readOnly = false,
|
||||
bool obscureText = false,
|
||||
bool autocorrect = true,
|
||||
String textCapitalization = 'TextCapitalization.none',
|
||||
@@ -2095,6 +2136,7 @@ Map<String, dynamic> createFlutterConfig(
|
||||
'name': 'TextInputType.$inputType',
|
||||
if (decimal) 'decimal': true,
|
||||
},
|
||||
'readOnly': readOnly,
|
||||
'obscureText': obscureText,
|
||||
'autocorrect': autocorrect,
|
||||
'inputAction': inputAction ?? 'TextInputAction.done',
|
||||
|
||||
Reference in New Issue
Block a user