-
Notifications
You must be signed in to change notification settings - Fork 626
Prevent external updates during composition/focus #6237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -22,6 +22,8 @@ class _CodeEditorControlState extends State<CodeEditorControl> { | |||||||||||||||||||||||||
| TextSelection? _selection; | ||||||||||||||||||||||||||
| String _value = ""; | ||||||||||||||||||||||||||
| String? _languageName; | ||||||||||||||||||||||||||
| String? _lastControlValue; | ||||||||||||||||||||||||||
| TextSelection? _lastControlSelection; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| @override | ||||||||||||||||||||||||||
| void initState() { | ||||||||||||||||||||||||||
|
|
@@ -31,6 +33,12 @@ class _CodeEditorControlState extends State<CodeEditorControl> { | |||||||||||||||||||||||||
| _controller = _createController(); | ||||||||||||||||||||||||||
| _value = _readValue(); | ||||||||||||||||||||||||||
| _selection = _controller.selection; | ||||||||||||||||||||||||||
| _lastControlValue = widget.control.getString("value"); | ||||||||||||||||||||||||||
| _lastControlSelection = widget.control.getTextSelection( | ||||||||||||||||||||||||||
| "selection", | ||||||||||||||||||||||||||
| minOffset: 0, | ||||||||||||||||||||||||||
| maxOffset: _controller.text.length, | ||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||
| _controller.addListener(_handleControllerChange); | ||||||||||||||||||||||||||
| widget.control.addInvokeMethodListener(_invokeMethod); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
@@ -97,6 +105,20 @@ class _CodeEditorControlState extends State<CodeEditorControl> { | |||||||||||||||||||||||||
| _controller.fullText = value; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| bool get _isComposing { | ||||||||||||||||||||||||||
| final composing = _controller.value.composing; | ||||||||||||||||||||||||||
| return composing.isValid && !composing.isCollapsed; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| bool _shouldApplyExternalState({ | ||||||||||||||||||||||||||
| required bool incomingChanged, | ||||||||||||||||||||||||||
| }) { | ||||||||||||||||||||||||||
| if (!_focusNode.hasFocus) { | ||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| return incomingChanged && !_isComposing; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| void _handleControllerChange() { | ||||||||||||||||||||||||||
| final value = _readValue(); | ||||||||||||||||||||||||||
| final selection = _controller.selection; | ||||||||||||||||||||||||||
|
|
@@ -160,7 +182,11 @@ class _CodeEditorControlState extends State<CodeEditorControl> { | |||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| final value = widget.control.getString("value"); | ||||||||||||||||||||||||||
| final controllerValue = _readValue(); | ||||||||||||||||||||||||||
| if (value != null && value != controllerValue) { | ||||||||||||||||||||||||||
| final valueChangedInControl = value != _lastControlValue; | ||||||||||||||||||||||||||
| _lastControlValue = value; | ||||||||||||||||||||||||||
| if (value != null && | ||||||||||||||||||||||||||
| value != controllerValue && | ||||||||||||||||||||||||||
| _shouldApplyExternalState(incomingChanged: valueChangedInControl)) { | ||||||||||||||||||||||||||
| _setValue(value); | ||||||||||||||||||||||||||
| _value = value; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
@@ -170,7 +196,11 @@ class _CodeEditorControlState extends State<CodeEditorControl> { | |||||||||||||||||||||||||
| minOffset: 0, | ||||||||||||||||||||||||||
| maxOffset: _controller.text.length, | ||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||
| if (explicitSelection != null && explicitSelection != _controller.selection) { | ||||||||||||||||||||||||||
| final selectionChangedInControl = explicitSelection != _lastControlSelection; | ||||||||||||||||||||||||||
| _lastControlSelection = explicitSelection; | ||||||||||||||||||||||||||
| if (explicitSelection != null && | ||||||||||||||||||||||||||
| explicitSelection != _controller.selection && | ||||||||||||||||||||||||||
| _shouldApplyExternalState(incomingChanged: selectionChangedInControl)) { | ||||||||||||||||||||||||||
| _controller.selection = explicitSelection; | ||||||||||||||||||||||||||
|
Comment on lines
+200
to
204
|
||||||||||||||||||||||||||
| _lastControlSelection = explicitSelection; | |
| if (explicitSelection != null && | |
| explicitSelection != _controller.selection && | |
| _shouldApplyExternalState(incomingChanged: selectionChangedInControl)) { | |
| _controller.selection = explicitSelection; | |
| if (explicitSelection == null) { | |
| // Control explicitly cleared selection; record this immediately. | |
| _lastControlSelection = null; | |
| } else if (explicitSelection != _controller.selection && | |
| _shouldApplyExternalState(incomingChanged: selectionChangedInControl)) { | |
| _controller.selection = explicitSelection; | |
| _lastControlSelection = explicitSelection; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_lastControlValue is updated before the external value is applied. If a control-side value change arrives while the editor is focused and composing,
_shouldApplyExternalStateblocks applying it, but_lastControlValuestill advances—so on subsequent buildsvalueChangedInControlbecomes false and the external update can be dropped permanently (e.g., a programmaticvalueupdate during IME composition will never apply even after composition ends/blur). Consider tracking the last applied control value (or keeping a pending external value) and only advancing_lastControlValueafter the update is actually applied / when not composing, so suppressed updates can be applied later when it’s safe.