From 9707001c02346b7d29844e8cf32af3fbcf46df7b Mon Sep 17 00:00:00 2001 From: hellohuanlin <41930132+hellohuanlin@users.noreply.github.com> Date: Thu, 8 Jun 2023 13:03:49 -0700 Subject: [PATCH] [floating_cursor_selection]add more comments on the tricky part (#127227) Many parts of the floating cursor selection feature is pretty tricky. Some took me a while to figure out. So I added some comments to explain a bit for future readers. *List which issues are fixed by this PR. You must list at least one issue.* https://github.com/flutter/flutter/issues/30476 *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].* --- .../flutter/lib/src/rendering/editable.dart | 4 +++- .../lib/src/widgets/editable_text.dart | 19 +++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/packages/flutter/lib/src/rendering/editable.dart b/packages/flutter/lib/src/rendering/editable.dart index 09fea17285..07853bb918 100644 --- a/packages/flutter/lib/src/rendering/editable.dart +++ b/packages/flutter/lib/src/rendering/editable.dart @@ -1269,7 +1269,9 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin, // [assembleSemanticsNode] invocations. LinkedHashMap? _cachedChildNodes; - /// Returns a list of rects that bound the given selection. + /// Returns a list of rects that bound the given selection, and the text + /// direction. The text direction is used by the engine to calculate + /// the closest position to a given point. /// /// See [TextPainter.getBoxesForSelection] for more details. List getBoxesForSelection(TextSelection selection) { diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index 7187bfb58a..607200d9c7 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -2792,6 +2792,7 @@ class EditableTextState extends State with AutomaticKeepAliveClien if (_textInputConnection?.scribbleInProgress ?? false) { cause = SelectionChangedCause.scribble; } else if (_pointOffsetOrigin != null) { + // For floating cursor selection when force pressing the space bar. cause = SelectionChangedCause.forcePress; } else { cause = SelectionChangedCause.keyboard; @@ -2941,8 +2942,22 @@ class EditableTextState extends State with AutomaticKeepAliveClien final Offset finalPosition = renderEditable.getLocalRectForCaret(_lastTextPosition!).centerLeft - _floatingCursorOffset; if (_floatingCursorResetController!.isCompleted) { renderEditable.setFloatingCursor(FloatingCursorDragState.End, finalPosition, _lastTextPosition!); - // Only change if the current selection range is collapsed, to prevent - // overwriting the result of the iOS keyboard selection gesture. + // During a floating cursor's move gesture (1 finger), a cursor is + // animated only visually, without actually updating the selection. + // Only after move gesture is complete, this function will be called + // to actually update the selection to the new cursor location with + // zero selection length. + + // However, During a floating cursor's selection gesture (2 fingers), the + // selection is constantly updated by the engine throughout the gesture. + // Thus when the gesture is complete, we should not update the selection + // to the cursor location with zero selection length, because that would + // overwrite the selection made by floating cursor selection. + + // Here we use `isCollapsed` to distinguish between floating cursor's + // move gesture (1 finger) vs selection gesture (2 fingers), as + // the engine does not provide information other than notifying a + // new selection during with selection gesture (2 fingers). if (renderEditable.selection!.isCollapsed) { // The cause is technically the force cursor, but the cause is listed as tap as the desired functionality is the same. _handleSelectionChanged(TextSelection.fromPosition(_lastTextPosition!), SelectionChangedCause.forcePress);