Skip to content

Commit

Permalink
Long press will select a word on mobile platform #52
Browse files Browse the repository at this point in the history
  • Loading branch information
MegatronKing committed Nov 4, 2024
1 parent fb36150 commit 1c09c87
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions lib/src/_code_selection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class _CodeSelectionGestureDetector extends StatefulWidget {

class _CodeSelectionGestureDetectorState extends State<_CodeSelectionGestureDetector> {
Offset? _dragPosition;
bool _dragging = false;
DateTime? _pointerTapTimestamp;
Offset? _pointerTapPosition;
bool? _handleByNextEvent;
Expand All @@ -45,14 +46,18 @@ class _CodeSelectionGestureDetectorState extends State<_CodeSelectionGestureDete
if (_longPressOnSelection == true) {
return;
}
if (details.localOffsetFromOrigin.distance < 1) {
return;
}
_dragging = true;
_onLongPressMove(details);
},
onLongPressStart: (details) {
_dragPosition = details.globalPosition;
widget.inputController.ensureInput();
_longPressOnSelection = _isPositionOnSelection(details.globalPosition);
if (_longPressOnSelection != true) {
_onMobileTapDown(details.globalPosition);
_onMobileLongPressedStart(details.globalPosition);
_autoScrollWhenDragging();
} else {
widget.selectionOverlayController.showToolbar(context, details.globalPosition);
Expand All @@ -65,11 +70,13 @@ class _CodeSelectionGestureDetectorState extends State<_CodeSelectionGestureDete
}
_dragPosition = null;
_longPressOnSelection = false;
_dragging = false;
widget.selectionOverlayController.showHandle(context);
},
onLongPressCancel: () {
_dragPosition = null;
_longPressOnSelection = false;
_dragging = false;
widget.selectionOverlayController.hideToolbar();
widget.selectionOverlayController.hideHandle();
},
Expand All @@ -96,26 +103,32 @@ class _CodeSelectionGestureDetectorState extends State<_CodeSelectionGestureDete
return;
}
_dragPosition = details.globalPosition;
_dragging = true;
_autoScrollWhenDragging();
},
onVerticalDragEnd: (_) {
_dragPosition = null;
_dragging = false;
},
onVerticalDragCancel: () {
_dragPosition = null;
_dragging = false;
},
onHorizontalDragStart: (details) {
if (!_tapping) {
return;
}
_dragPosition = details.globalPosition;
_dragging = true;
_autoScrollWhenDragging();
},
onHorizontalDragEnd: (_) {
_dragPosition = null;
_dragging = false;
},
onHorizontalDragCancel: () {
_dragPosition = null;
_dragging = false;
},
behavior: widget.behavior,
onSecondaryTapDown: (detail) {
Expand Down Expand Up @@ -179,6 +192,23 @@ class _CodeSelectionGestureDetectorState extends State<_CodeSelectionGestureDete
widget.inputController.ensureInput();
}

void _onMobileLongPressedStart(Offset position) {
final CodeLineRange? range = render.selectWord(
position: position,
);
if (range == null) {
return;
}
final CodeLineSelection selection = CodeLineSelection.fromRange(
range: range
);
widget.controller.selection = selection;
widget.controller.makeCursorVisible();
_anchorSelection = selection;
widget.selectionOverlayController.hideHandle();
widget.selectionOverlayController.hideToolbar();
}

void _onDesktopTapDown(Offset position) {
if (widget.controller.isComposing) {
return;
Expand Down Expand Up @@ -348,8 +378,10 @@ class _CodeSelectionGestureDetectorState extends State<_CodeSelectionGestureDete
if (_dragPosition == null || position == null) {
return;
}
render.autoScrollWhenDragging(_dragPosition!);
_extendSelection(_dragPosition!, _SelectionChangedCause.drag);
if (_dragging) {
render.autoScrollWhenDragging(_dragPosition!);
_extendSelection(_dragPosition!, _SelectionChangedCause.drag);
}
_autoScrollWhenDragging();
}));
}
Expand Down

0 comments on commit 1c09c87

Please sign in to comment.