updateFloatingCursor method
Updates the floating cursor position and state.
See also:
- FloatingCursorDragState, which explains the floating cursor feature in detail.
Implementation
@override
void updateFloatingCursor(RawFloatingCursorPoint point) {
switch (point.state) {
case FloatingCursorDragState.Start:
if (floatingCursorResetController.isAnimating) {
floatingCursorResetController.stop();
onFloatingCursorResetTick();
}
// We want to send in points that are centered around a (0,0) origin, so
// we cache the position.
_pointOffsetOrigin = point.offset;
final currentTextPosition = TextPosition(offset: renderEditor.selection.baseOffset);
_startCaretRect = renderEditor.getLocalRectForCaret(currentTextPosition);
_lastBoundedOffset = _startCaretRect!.center - _floatingCursorOffset(currentTextPosition);
_lastTextPosition = currentTextPosition;
renderEditor.setFloatingCursor(point.state, _lastBoundedOffset!, _lastTextPosition!);
break;
case FloatingCursorDragState.Update:
assert(_lastTextPosition != null, 'Last text position was not set');
final floatingCursorOffset = _floatingCursorOffset(_lastTextPosition!);
final centeredPoint = point.offset! - _pointOffsetOrigin!;
final rawCursorOffset = _startCaretRect!.center + centeredPoint - floatingCursorOffset;
final preferredLineHeight = renderEditor.preferredLineHeight(_lastTextPosition!);
_lastBoundedOffset = renderEditor.calculateBoundedFloatingCursorOffset(
rawCursorOffset,
preferredLineHeight,
);
_lastTextPosition =
renderEditor.getPositionForOffset(renderEditor.localToGlobal(_lastBoundedOffset! + floatingCursorOffset));
renderEditor.setFloatingCursor(point.state, _lastBoundedOffset!, _lastTextPosition!);
final newSelection =
TextSelection.collapsed(offset: _lastTextPosition!.offset, affinity: _lastTextPosition!.affinity);
// Setting selection as floating cursor moves will have scroll view
// bring background cursor into view
renderEditor.onSelectionChanged(newSelection, SelectionChangedCause.forcePress);
break;
case FloatingCursorDragState.End:
// We skip animation if no update has happened.
if (_lastTextPosition != null && _lastBoundedOffset != null) {
floatingCursorResetController
..value = 0.0
..animateTo(1, duration: _floatingCursorResetTime, curve: Curves.decelerate);
}
break;
}
}