Unverified Commit 52299fa3 authored by Pierre-Louis's avatar Pierre-Louis Committed by GitHub

Cache floating cursor location on start event instead of update (#62415)

parent 1513b971
...@@ -1623,7 +1623,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien ...@@ -1623,7 +1623,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
// cursor. // cursor.
TextPosition _lastTextPosition; TextPosition _lastTextPosition;
// The offset of the floating cursor as determined from the first update call. // The offset of the floating cursor as determined from the start call.
Offset _pointOffsetOrigin; Offset _pointOffsetOrigin;
// The most recent position of the floating cursor. // The most recent position of the floating cursor.
...@@ -1642,22 +1642,24 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien ...@@ -1642,22 +1642,24 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
_floatingCursorResetController.stop(); _floatingCursorResetController.stop();
_onFloatingCursorResetTick(); _onFloatingCursorResetTick();
} }
// We want to send in points that are centered around a (0,0) origin, so
// we cache the position.
_pointOffsetOrigin = point.offset;
final TextPosition currentTextPosition = TextPosition(offset: renderEditable.selection.baseOffset); final TextPosition currentTextPosition = TextPosition(offset: renderEditable.selection.baseOffset);
_startCaretRect = renderEditable.getLocalRectForCaret(currentTextPosition); _startCaretRect = renderEditable.getLocalRectForCaret(currentTextPosition);
renderEditable.setFloatingCursor(point.state, _startCaretRect.center - _floatingCursorOffset, currentTextPosition);
_lastBoundedOffset = _startCaretRect.center - _floatingCursorOffset;
_lastTextPosition = currentTextPosition;
renderEditable.setFloatingCursor(point.state, _lastBoundedOffset, _lastTextPosition);
break; break;
case FloatingCursorDragState.Update: case FloatingCursorDragState.Update:
// We want to send in points that are centered around a (0,0) origin, so we cache the final Offset centeredPoint = point.offset - _pointOffsetOrigin;
// position on the first update call. final Offset rawCursorOffset = _startCaretRect.center + centeredPoint - _floatingCursorOffset;
if (_pointOffsetOrigin != null) {
final Offset centeredPoint = point.offset - _pointOffsetOrigin; _lastBoundedOffset = renderEditable.calculateBoundedFloatingCursorOffset(rawCursorOffset);
final Offset rawCursorOffset = _startCaretRect.center + centeredPoint - _floatingCursorOffset; _lastTextPosition = renderEditable.getPositionForPoint(renderEditable.localToGlobal(_lastBoundedOffset + _floatingCursorOffset));
_lastBoundedOffset = renderEditable.calculateBoundedFloatingCursorOffset(rawCursorOffset); renderEditable.setFloatingCursor(point.state, _lastBoundedOffset, _lastTextPosition);
_lastTextPosition = renderEditable.getPositionForPoint(renderEditable.localToGlobal(_lastBoundedOffset + _floatingCursorOffset));
renderEditable.setFloatingCursor(point.state, _lastBoundedOffset, _lastTextPosition);
} else {
_pointOffsetOrigin = point.offset;
}
break; break;
case FloatingCursorDragState.End: case FloatingCursorDragState.End:
// We skip animation if no update has happened. // We skip animation if no update has happened.
......
...@@ -398,13 +398,9 @@ void main() { ...@@ -398,13 +398,9 @@ void main() {
expect(controller.selection.baseOffset, 29); expect(controller.selection.baseOffset, 29);
final EditableTextState editableTextState = tester.firstState(find.byType(EditableText)); final EditableTextState editableTextState = tester.firstState(find.byType(EditableText));
editableTextState.updateFloatingCursor(RawFloatingCursorPoint(state: FloatingCursorDragState.Start));
expect(controller.selection.baseOffset, 29);
// Sets the origin. // Sets the origin.
editableTextState.updateFloatingCursor(RawFloatingCursorPoint(state: FloatingCursorDragState.Update, editableTextState.updateFloatingCursor(RawFloatingCursorPoint(state: FloatingCursorDragState.Start, offset: const Offset(20, 20)));
offset: const Offset(20, 20)));
expect(controller.selection.baseOffset, 29); expect(controller.selection.baseOffset, 29);
...@@ -427,10 +423,9 @@ void main() { ...@@ -427,10 +423,9 @@ void main() {
expect(controller.selection.baseOffset, 8); expect(controller.selection.baseOffset, 8);
// Go in the other direction. // Go in the other direction.
editableTextState.updateFloatingCursor(RawFloatingCursorPoint(state: FloatingCursorDragState.Start));
// Sets the origin. // Sets the origin.
editableTextState.updateFloatingCursor(RawFloatingCursorPoint(state: FloatingCursorDragState.Update, editableTextState.updateFloatingCursor(RawFloatingCursorPoint(state: FloatingCursorDragState.Start, offset: const Offset(20, 20)));
offset: const Offset(20, 20)));
editableTextState.updateFloatingCursor(RawFloatingCursorPoint(state: FloatingCursorDragState.Update, editableTextState.updateFloatingCursor(RawFloatingCursorPoint(state: FloatingCursorDragState.Update,
offset: const Offset(-5000, 20))); offset: const Offset(-5000, 20)));
...@@ -482,13 +477,9 @@ void main() { ...@@ -482,13 +477,9 @@ void main() {
expect(controller.selection.baseOffset, 29); expect(controller.selection.baseOffset, 29);
final EditableTextState editableTextState = tester.firstState(find.byType(EditableText)); final EditableTextState editableTextState = tester.firstState(find.byType(EditableText));
editableTextState.updateFloatingCursor(RawFloatingCursorPoint(state: FloatingCursorDragState.Start));
expect(controller.selection.baseOffset, 29);
// Sets the origin. // Sets the origin.
editableTextState.updateFloatingCursor(RawFloatingCursorPoint(state: FloatingCursorDragState.Update, editableTextState.updateFloatingCursor(RawFloatingCursorPoint(state: FloatingCursorDragState.Start, offset: const Offset(20, 20)));
offset: const Offset(20, 20)));
expect(controller.selection.baseOffset, 29); expect(controller.selection.baseOffset, 29);
...@@ -583,13 +574,9 @@ void main() { ...@@ -583,13 +574,9 @@ void main() {
expect(controller.selection.baseOffset, 29); expect(controller.selection.baseOffset, 29);
final EditableTextState editableTextState = tester.firstState(find.byType(EditableText)); final EditableTextState editableTextState = tester.firstState(find.byType(EditableText));
editableTextState.updateFloatingCursor(RawFloatingCursorPoint(state: FloatingCursorDragState.Start));
expect(controller.selection.baseOffset, 29);
// Sets the origin. // Sets the origin.
editableTextState.updateFloatingCursor(RawFloatingCursorPoint(state: FloatingCursorDragState.Update, editableTextState.updateFloatingCursor(RawFloatingCursorPoint(state: FloatingCursorDragState.Start, offset: const Offset(20, 20)));
offset: const Offset(20, 20)));
expect(controller.selection.baseOffset, 29); expect(controller.selection.baseOffset, 29);
...@@ -603,13 +590,11 @@ void main() { ...@@ -603,13 +590,11 @@ void main() {
editableTextState.updateFloatingCursor(RawFloatingCursorPoint(state: FloatingCursorDragState.End)); editableTextState.updateFloatingCursor(RawFloatingCursorPoint(state: FloatingCursorDragState.End));
// Immediately start a new floating cursor, in the same way as happens when // Immediately start a new floating cursor, in the same way as happens when
// the user tries to select text in trackpad mode. // the user tries to select text in trackpad mode.
editableTextState.updateFloatingCursor(RawFloatingCursorPoint(state: FloatingCursorDragState.Start)); editableTextState.updateFloatingCursor(RawFloatingCursorPoint(state: FloatingCursorDragState.Start, offset: const Offset(20, 20)));
await tester.pumpAndSettle(); await tester.pumpAndSettle();
// Set and move the second cursor like a selection. Previously, the second // Set and move the second cursor like a selection. Previously, the second
// Update here caused a crash. // Update here caused a crash.
editableTextState.updateFloatingCursor(RawFloatingCursorPoint(state: FloatingCursorDragState.Update,
offset: const Offset(20, 20)));
editableTextState.updateFloatingCursor(RawFloatingCursorPoint(state: FloatingCursorDragState.Update, editableTextState.updateFloatingCursor(RawFloatingCursorPoint(state: FloatingCursorDragState.Update,
offset: const Offset(-250, 20))); offset: const Offset(-250, 20)));
editableTextState.updateFloatingCursor(RawFloatingCursorPoint(state: FloatingCursorDragState.End)); editableTextState.updateFloatingCursor(RawFloatingCursorPoint(state: FloatingCursorDragState.End));
...@@ -676,13 +661,7 @@ void main() { ...@@ -676,13 +661,7 @@ void main() {
final EditableTextState editableTextState = tester.firstState(find.byType(EditableText)); final EditableTextState editableTextState = tester.firstState(find.byType(EditableText));
editableTextState.updateFloatingCursor( editableTextState.updateFloatingCursor(
RawFloatingCursorPoint(state: FloatingCursorDragState.Start), RawFloatingCursorPoint(state: FloatingCursorDragState.Start, offset: const Offset(20, 20)),
);
editableTextState.updateFloatingCursor(
RawFloatingCursorPoint(
state: FloatingCursorDragState.Update,
offset: const Offset(20, 20),
),
); );
await tester.pump(); await tester.pump();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment