From 212d0a64c96002a3c4c2e349efd823ac428a95b1 Mon Sep 17 00:00:00 2001 From: LongCatIsLooong <31859944+LongCatIsLooong@users.noreply.github.com> Date: Tue, 16 Jan 2024 15:33:49 -0800 Subject: [PATCH] Allow selection in composing region (#140516) Fixes https://github.com/flutter/flutter/issues/68547 for macOS. Needs https://github.com/flutter/engine/pull/49314 --- .../lib/src/widgets/editable_text.dart | 19 ++++++++----------- .../test/widgets/editable_text_test.dart | 5 +++-- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index 8f6611370a..0c8d98b09d 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -218,9 +218,9 @@ class TextEditingController extends ValueNotifier { /// actions, not during the build, layout, or paint phases. /// /// This property can be set from a listener added to this - /// [TextEditingController]; however, one should not also set [selection] + /// [TextEditingController]; **however, one should not also set [selection] /// in a separate statement. To change both the [text] and the [selection] - /// change the controller's [value]. + /// change the controller's [value].** set text(String newText) { value = value.copyWith( text: newText, @@ -285,16 +285,13 @@ class TextEditingController extends ValueNotifier { /// in a separate statement. To change both the [text] and the [selection] /// change the controller's [value]. /// - /// If the new selection is of non-zero length, or is outside the composing - /// range, the composing range is cleared. + /// If the new selection is outside the composing range, the composing range is + /// cleared. set selection(TextSelection newSelection) { - if (!isSelectionWithinTextBounds(newSelection)) { + if (!_isSelectionWithinTextBounds(newSelection)) { throw FlutterError('invalid text selection: $newSelection'); } - final TextRange newComposing = - newSelection.isCollapsed && _isSelectionWithinComposingRange(newSelection) - ? value.composing - : TextRange.empty; + final TextRange newComposing = _isSelectionWithinComposingRange(newSelection) ? value.composing : TextRange.empty; value = value.copyWith(selection: newSelection, composing: newComposing); } @@ -326,7 +323,7 @@ class TextEditingController extends ValueNotifier { } /// Check that the [selection] is inside of the bounds of [text]. - bool isSelectionWithinTextBounds(TextSelection selection) { + bool _isSelectionWithinTextBounds(TextSelection selection) { return selection.start <= text.length && selection.end <= text.length; } @@ -3650,7 +3647,7 @@ class EditableTextState extends State with AutomaticKeepAliveClien // We return early if the selection is not valid. This can happen when the // text of [EditableText] is updated at the same time as the selection is // changed by a gesture event. - if (!widget.controller.isSelectionWithinTextBounds(selection)) { + if (!widget.controller._isSelectionWithinTextBounds(selection)) { return; } diff --git a/packages/flutter/test/widgets/editable_text_test.dart b/packages/flutter/test/widgets/editable_text_test.dart index c5b0ad0b5b..d259253c2d 100644 --- a/packages/flutter/test/widgets/editable_text_test.dart +++ b/packages/flutter/test/widgets/editable_text_test.dart @@ -11185,14 +11185,15 @@ void main() { )); expect(state.currentTextEditingValue.composing, const TextRange(start: 4, end: 12)); - // Setting a selection within the composing range clears the composing range. + // Setting a selection within the composing range doesn't clear the composing range. state.updateEditingValue(const TextEditingValue( text: 'foo composing bar', selection: TextSelection.collapsed(offset: 4), composing: TextRange(start: 4, end: 12), )); controller.selection = const TextSelection(baseOffset: 5, extentOffset: 7); - expect(state.currentTextEditingValue.composing, TextRange.empty); + expect(state.currentTextEditingValue.composing, const TextRange(start: 4, end: 12)); + expect(state.currentTextEditingValue.selection, const TextSelection(baseOffset: 5, extentOffset: 7)); // Reset the composing range. state.updateEditingValue(const TextEditingValue(