Prevent external updates during composition/focus#6237
Prevent external updates during composition/focus#6237FeodorFitsner wants to merge 1 commit intomainfrom
Conversation
Track the last control-provided value and selection and avoid applying external value/selection updates while the editor has focus and the user is composing text. Added _lastControlValue and _lastControlSelection, an _isComposing getter, and _shouldApplyExternalState to gate updates. Value/selection handlers now only apply control changes when they actually changed in the control and applying them won't interfere with an in-progress composition or focused editing session. Fix #6211
Deploying flet-examples with
|
| Latest commit: |
6b67560
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://c8de6914.flet-examples.pages.dev |
| Branch Preview URL: | https://codeeditor-ime.flet-examples.pages.dev |
There was a problem hiding this comment.
Pull request overview
This PR adjusts the Flutter-side CodeEditorControl state synchronization so IME composition and focused editing sessions aren’t disrupted by externally-provided value/selection updates (addressing #6211).
Changes:
- Track last control-provided
valueandselectionvia_lastControlValue/_lastControlSelection. - Add composition detection (
_isComposing) and a gating helper (_shouldApplyExternalState) to suppress unsafe external state application while focused/composing. - Apply control
value/selectionupdates only when they differ and pass the new gating rules.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| final valueChangedInControl = value != _lastControlValue; | ||
| _lastControlValue = value; | ||
| if (value != null && | ||
| value != controllerValue && | ||
| _shouldApplyExternalState(incomingChanged: valueChangedInControl)) { | ||
| _setValue(value); | ||
| _value = value; | ||
| } |
There was a problem hiding this comment.
_lastControlValue is updated before the external value is applied. If a control-side value change arrives while the editor is focused and composing, _shouldApplyExternalState blocks applying it, but _lastControlValue still advances—so on subsequent builds valueChangedInControl becomes false and the external update can be dropped permanently (e.g., a programmatic value update 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 _lastControlValue after the update is actually applied / when not composing, so suppressed updates can be applied later when it’s safe.
| _lastControlSelection = explicitSelection; | ||
| if (explicitSelection != null && | ||
| explicitSelection != _controller.selection && | ||
| _shouldApplyExternalState(incomingChanged: selectionChangedInControl)) { | ||
| _controller.selection = explicitSelection; |
There was a problem hiding this comment.
Same pattern for selection: _lastControlSelection is updated even when an incoming selection change is suppressed due to focus/composition. That can prevent a queued control-side selection update from ever being applied later (after composition ends or focus is lost). Consider only updating the "last seen/applied" selection marker when the selection is actually applied, or store a pending selection to apply once _isComposing becomes false.
| _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; |
Deploying flet-docs with
|
| Latest commit: |
6b67560
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://b9cc26ee.flet-docs.pages.dev |
| Branch Preview URL: | https://codeeditor-ime.flet-docs.pages.dev |
Track the last control-provided value and selection and avoid applying external value/selection updates while the editor has focus and the user is composing text. Added _lastControlValue and _lastControlSelection, an _isComposing getter, and _shouldApplyExternalState to gate updates. Value/selection handlers now only apply control changes when they actually changed in the control and applying them won't interfere with an in-progress composition or focused editing session.
Fix #6211
Summary by Sourcery
Prevent external value and selection updates from interfering with in-progress text composition or focused editing in the code editor control.
Bug Fixes:
Enhancements: