Skip to content
This repository was archived by the owner on Apr 29, 2021. It is now read-only.

Commit bca5137

Browse files
committed
[Fix] TextField maxLength wrong IME input behavior
1 parent fed0017 commit bca5137

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

Runtime/service/keyboard.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ public bool imeRequired() {
6969
return true;
7070
}
7171

72+
bool isimeInput = false;
73+
7274
public void OnGUI() {
7375
if (TouchScreenKeyboard.isSupported) {
7476
return;
@@ -101,15 +103,24 @@ public void OnGUI() {
101103
if (_validateCharacter(ch)) {
102104
this._value = this._value.insert(new string(ch, 1));
103105
}
104-
} else if (!string.IsNullOrEmpty(Input.compositionString)) {
106+
}
107+
else if (!string.IsNullOrEmpty(Input.compositionString)) {
108+
this.isimeInput = true;
105109
this._value = this._value.compose(Input.compositionString);
106110
}
107111

108112
currentEvent.Use();
109113
}
110114

111115
if (this._value != oldValue) {
112-
Window.instance.run(() => { TextInput._updateEditingState(this._client, this._value); });
116+
if (this.isimeInput) {
117+
var imeInput = this.isimeInput;
118+
Window.instance.run(() => { TextInput._updateEditingState(this._client, this._value, imeInput); });
119+
this.isimeInput = false;
120+
}
121+
else {
122+
Window.instance.run(() => { TextInput._updateEditingState(this._client, this._value); });
123+
}
113124
}
114125
}
115126

Runtime/service/text_formatter.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ public LengthLimitingTextInputFormatter(int? maxLength) {
6060

6161
public override TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
6262
if (this.maxLength != null && this.maxLength > 0 && newValue.text.Length > this.maxLength) {
63+
if (Input.compositionString.Length > 0) {
64+
return newValue;
65+
}
66+
6367
TextSelection newSelection = newValue.selection.copyWith(
6468
baseOffset: Mathf.Min(newValue.selection.start, this.maxLength.Value),
6569
extentOffset: Mathf.Min(newValue.selection.end, this.maxLength.Value)
@@ -72,9 +76,10 @@ public override TextEditingValue formatEditUpdate(TextEditingValue oldValue, Tex
7276
composing: TextRange.empty
7377
);
7478
}
79+
7580
return newValue;
7681
}
77-
}
82+
}
7883

7984
static class Util {
8085
internal static TextEditingValue _selectionAwareTextManipulation(TextEditingValue value,

Runtime/service/text_input.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ public interface TextSelectionDelegate {
439439
}
440440

441441
public interface TextInputClient {
442-
void updateEditingValue(TextEditingValue value);
442+
void updateEditingValue(TextEditingValue value, bool imeInput);
443443

444444
void performAction(TextInputAction action);
445445

@@ -608,7 +608,7 @@ internal static void OnGUI() {
608608
}
609609
}
610610

611-
internal static void _updateEditingState(int client, TextEditingValue value) {
611+
internal static void _updateEditingState(int client, TextEditingValue value, bool imeInput = false) {
612612
if (_currentConnection == null) {
613613
return;
614614
}
@@ -617,7 +617,7 @@ internal static void _updateEditingState(int client, TextEditingValue value) {
617617
return;
618618
}
619619

620-
_currentConnection._client.updateEditingValue(value);
620+
_currentConnection._client.updateEditingValue(value, imeInput);
621621
}
622622

623623
internal static void _performAction(int client, TextInputAction action) {

Runtime/widgets/editable_text.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ public override void dispose() {
341341

342342
TextEditingValue _lastKnownRemoteTextEditingValue;
343343

344-
public void updateEditingValue(TextEditingValue value) {
344+
public void updateEditingValue(TextEditingValue value, bool imeInput) {
345345
if (value.text != this._value.text) {
346346
this._hideSelectionOverlayIfNeeded();
347347
this._showCaretOnScreen();
@@ -352,7 +352,7 @@ public void updateEditingValue(TextEditingValue value) {
352352
}
353353

354354
this._lastKnownRemoteTextEditingValue = value;
355-
this._formatAndSetValue(value);
355+
this._formatAndSetValue(value, imeInput);
356356

357357
this._stopCursorTimer(resetCharTicks: false);
358358
this._startCursorTimer();
@@ -723,8 +723,8 @@ public IPromise<bool> didPushRoute(string route) {
723723
return Promise<bool>.Resolved(false);
724724
}
725725

726-
void _formatAndSetValue(TextEditingValue value) {
727-
var textChanged = this._value?.text != value?.text;
726+
void _formatAndSetValue(TextEditingValue value, bool imeInput = false) {
727+
var textChanged = this._value?.text != value?.text || imeInput;
728728
if (textChanged && this.widget.inputFormatters != null && this.widget.inputFormatters.isNotEmpty()) {
729729
foreach (var formatter in this.widget.inputFormatters) {
730730
value = formatter.formatEditUpdate(this._value, value);

0 commit comments

Comments
 (0)