Skip to content

Commit

Permalink
Fix _TextEditingHistoryState reentrant calls (#120889)
Browse files Browse the repository at this point in the history
Fix text history undo/redo should not add a new entry to the history
  • Loading branch information
bleroux authored Mar 1, 2023
1 parent d3044a6 commit b66a547
Show file tree
Hide file tree
Showing 2 changed files with 249 additions and 187 deletions.
10 changes: 10 additions & 0 deletions packages/flutter/lib/src/widgets/editable_text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5285,6 +5285,10 @@ class _TextEditingHistoryState extends State<_TextEditingHistory> {
late final _Throttled<TextEditingValue> _throttledPush;
Timer? _throttleTimer;

// This is used to prevent a reentrant call to the history (a call to _undo or _redo
// should not call _push to add a new entry in the history).
bool _locked = false;

// This duration was chosen as a best fit for the behavior of Mac, Linux,
// and Windows undo/redo state save durations, but it is not perfect for any
// of them.
Expand All @@ -5305,13 +5309,19 @@ class _TextEditingHistoryState extends State<_TextEditingHistory> {
if (nextValue.text == widget.controller.text) {
return;
}
_locked = true;
widget.onTriggered(widget.controller.value.copyWith(
text: nextValue.text,
selection: nextValue.selection,
));
_locked = false;
}

void _push() {
// Do not try to push a new state when the change is related to an undo or redo.
if (_locked) {
return;
}
if (widget.controller.value == TextEditingValue.empty) {
return;
}
Expand Down
Loading

0 comments on commit b66a547

Please sign in to comment.