From 4377083810c32321a0305989cf684063a4934b6f Mon Sep 17 00:00:00 2001 From: jslavitz Date: Mon, 14 Jan 2019 10:29:51 -0800 Subject: [PATCH] Selects a word on force tap (#25683) * adds force press select word functionality --- .../flutter/lib/src/cupertino/text_field.dart | 14 ++++ .../flutter/lib/src/material/text_field.dart | 16 +++++ .../flutter/lib/src/rendering/editable.dart | 23 ++++++- .../lib/src/widgets/gesture_detector.dart | 1 + .../lib/src/widgets/text_selection.dart | 24 +++++++ .../test/cupertino/text_field_test.dart | 32 +++++++++ .../test/material/text_field_test.dart | 65 +++++++++++++++++++ .../test/widgets/text_selection_test.dart | 58 +++++++++++++++++ 8 files changed, 230 insertions(+), 3 deletions(-) diff --git a/packages/flutter/lib/src/cupertino/text_field.dart b/packages/flutter/lib/src/cupertino/text_field.dart index a716a47803..267fc97865 100644 --- a/packages/flutter/lib/src/cupertino/text_field.dart +++ b/packages/flutter/lib/src/cupertino/text_field.dart @@ -451,6 +451,18 @@ class _CupertinoTextFieldState extends State with AutomaticK _renderEditable.handleTapDown(details); } + void _handleForcePressStarted(ForcePressDetails details) { + // The cause is not keyboard press but we would still like to just + // highlight the word without showing any handles or toolbar. + _renderEditable.selectWordsInRange(from: details.globalPosition, cause: SelectionChangedCause.keyboard); + } + + void _handleForcePressEnded(ForcePressDetails details) { + // The cause is not technically double tap, but we would still like to show + // the toolbar and handles. + _renderEditable.selectWordsInRange(from: details.globalPosition, cause: SelectionChangedCause.doubleTap); + } + void _handleSingleTapUp(TapUpDetails details) { _renderEditable.selectWordEdge(cause: SelectionChangedCause.tap); _requestKeyboard(); @@ -648,6 +660,8 @@ class _CupertinoTextFieldState extends State with AutomaticK : CupertinoColors.darkBackgroundGray, child: TextSelectionGestureDetector( onTapDown: _handleTapDown, + onForcePressStart: _handleForcePressStarted, + onForcePressEnd: _handleForcePressEnded, onSingleTapUp: _handleSingleTapUp, onSingleLongTapDown: _handleSingleLongTapDown, onDoubleTapDown: _handleDoubleTapDown, diff --git a/packages/flutter/lib/src/material/text_field.dart b/packages/flutter/lib/src/material/text_field.dart index d5e374f4e2..7adf7105e5 100644 --- a/packages/flutter/lib/src/material/text_field.dart +++ b/packages/flutter/lib/src/material/text_field.dart @@ -543,6 +543,21 @@ class _TextFieldState extends State with AutomaticKeepAliveClientMixi _startSplash(details); } + void _handleForcePressStarted(ForcePressDetails details) { + if (widget.selectionEnabled) { + switch (Theme.of(context).platform) { + case TargetPlatform.iOS: + // The cause is not technically double tap, but we would like to show + // the toolbar. + _renderEditable.selectWordsInRange(from: details.globalPosition, cause: SelectionChangedCause.doubleTap); + break; + case TargetPlatform.android: + case TargetPlatform.fuchsia: + break; + } + } + } + void _handleSingleTapUp(TapUpDetails details) { if (widget.selectionEnabled) { switch (Theme.of(context).platform) { @@ -706,6 +721,7 @@ class _TextFieldState extends State with AutomaticKeepAliveClientMixi ignoring: !(widget.enabled ?? widget.decoration?.enabled ?? true), child: TextSelectionGestureDetector( onTapDown: _handleTapDown, + onForcePressStart: _handleForcePressStarted, onSingleTapUp: _handleSingleTapUp, onSingleTapCancel: _handleSingleTapCancel, onSingleLongTapDown: _handleSingleLongTapDown, diff --git a/packages/flutter/lib/src/rendering/editable.dart b/packages/flutter/lib/src/rendering/editable.dart index 780a43ce3d..10cea2c6e4 100644 --- a/packages/flutter/lib/src/rendering/editable.dart +++ b/packages/flutter/lib/src/rendering/editable.dart @@ -1184,12 +1184,29 @@ class RenderEditable extends RenderBox { /// Select a word around the location of the last tap down. void selectWord({@required SelectionChangedCause cause}) { + selectWordsInRange(from: _lastTapDownPosition, cause: cause); + } + + /// Selects the set words of a paragraph in a given range of global positions. + /// + /// The first and last endpoints of the selection will always be at the + /// beginning and end of a word respectively. + void selectWordsInRange({@required Offset from, Offset to, @required SelectionChangedCause cause}) { assert(cause != null); _layoutText(constraints.maxWidth); - assert(_lastTapDownPosition != null); if (onSelectionChanged != null) { - final TextPosition position = _textPainter.getPositionForOffset(globalToLocal(_lastTapDownPosition)); - onSelectionChanged(_selectWordAtOffset(position), this, cause); + final TextPosition firstPosition = _textPainter.getPositionForOffset(globalToLocal(from + -_paintOffset)); + final TextSelection firstWord = _selectWordAtOffset(firstPosition); + final TextSelection lastWord = to == null ? + firstWord : _selectWordAtOffset(_textPainter.getPositionForOffset(globalToLocal(to + -_paintOffset))); + + onSelectionChanged( + TextSelection( + baseOffset: firstWord.base.offset, + extentOffset: lastWord.extent.offset, + affinity: firstWord.affinity, + ), this, cause, + ); } } diff --git a/packages/flutter/lib/src/widgets/gesture_detector.dart b/packages/flutter/lib/src/widgets/gesture_detector.dart index 119d846174..35b633cee8 100644 --- a/packages/flutter/lib/src/widgets/gesture_detector.dart +++ b/packages/flutter/lib/src/widgets/gesture_detector.dart @@ -36,6 +36,7 @@ export 'package:flutter/gestures.dart' show ScaleEndDetails, TapDownDetails, TapUpDetails, + ForcePressDetails, Velocity; // Examples can assume: diff --git a/packages/flutter/lib/src/widgets/text_selection.dart b/packages/flutter/lib/src/widgets/text_selection.dart index 5d8110e927..fff3f5717d 100644 --- a/packages/flutter/lib/src/widgets/text_selection.dart +++ b/packages/flutter/lib/src/widgets/text_selection.dart @@ -612,6 +612,8 @@ class TextSelectionGestureDetector extends StatefulWidget { const TextSelectionGestureDetector({ Key key, this.onTapDown, + this.onForcePressStart, + this.onForcePressEnd, this.onSingleTapUp, this.onSingleTapCancel, this.onSingleLongTapDown, @@ -626,6 +628,14 @@ class TextSelectionGestureDetector extends StatefulWidget { /// to not qualify as taps (e.g. pans and flings). final GestureTapDownCallback onTapDown; + /// Called when a pointer has tapped down and the force of the pointer has + /// just become greater than [ForcePressGestureDetector.startPressure]. + final GestureForcePressStartCallback onForcePressStart; + + /// Called when a pointer that had previously triggered [onForcePressStart] is + /// lifted off the screen. + final GestureForcePressEndCallback onForcePressEnd; + /// Called for each distinct tap except for every second tap of a double tap. /// For example, if the detector was configured [onSingleTapDown] and /// [onDoubleTapDown], three quick taps would be recognized as a single tap @@ -712,6 +722,18 @@ class _TextSelectionGestureDetectorState extends State pumpGestureDetector(WidgetTester tester) async { @@ -35,6 +41,8 @@ void main() { onSingleTapCancel: _handleSingleTapCancel, onSingleLongTapDown: _handleSingleLongTapDown, onDoubleTapDown: _handleDoubleTapDown, + onForcePressStart: _handleForcePressStart, + onForcePressEnd: _handleForcePressEnd, child: Container(), ), ); @@ -142,4 +150,54 @@ void main() { expect(doubleTapDownCount, 0); expect(singleLongTapDownCount, 0); }); + + testWidgets('a force press intiates a force press', (WidgetTester tester) async { + await pumpGestureDetector(tester); + + const int pointerValue = 1; + TestGesture gesture = await tester.startGesture(const Offset(400.0, 50.0), pointer: pointerValue); + await gesture.updateWithCustomEvent(const PointerMoveEvent(pointer: pointerValue, position: Offset(0.0, 0.0), pressure: 0.5, pressureMin: 0, pressureMax: 1)); + await gesture.up(); + await tester.pumpAndSettle(); + + gesture = await tester.startGesture(const Offset(400.0, 50.0), pointer: pointerValue); + await gesture.updateWithCustomEvent(const PointerMoveEvent(pointer: pointerValue, position: Offset(0.0, 0.0), pressure: 0.5, pressureMin: 0, pressureMax: 1)); + await gesture.up(); + await tester.pump(const Duration(milliseconds: 20)); + + gesture = await tester.startGesture(const Offset(400.0, 50.0), pointer: pointerValue); + await gesture.updateWithCustomEvent(const PointerMoveEvent(pointer: pointerValue, position: Offset(0.0, 0.0), pressure: 0.5, pressureMin: 0, pressureMax: 1)); + await gesture.up(); + await tester.pump(const Duration(milliseconds: 20)); + + gesture = await tester.startGesture(const Offset(400.0, 50.0), pointer: pointerValue); + await gesture.updateWithCustomEvent(const PointerMoveEvent(pointer: pointerValue, position: Offset(0.0, 0.0), pressure: 0.5, pressureMin: 0, pressureMax: 1)); + await gesture.up(); + + expect(forcePressStartCount, 4); + }); + + testWidgets('a tap and then force press intiates a force press and not a double tap', (WidgetTester tester) async { + await pumpGestureDetector(tester); + + const int pointerValue = 1; + TestGesture gesture = await tester.startGesture(const Offset(400.0, 50.0), pointer: pointerValue); + + // Initiate a quick tap. + await gesture.updateWithCustomEvent(const PointerMoveEvent(pointer: pointerValue, position: Offset(0.0, 0.0), pressure: 0.0, pressureMin: 0, pressureMax: 1)); + await tester.pump(const Duration(milliseconds: 50)); + await gesture.up(); + + // Initiate a force tap. + gesture = await tester.startGesture(const Offset(400.0, 50.0), pointer: pointerValue); + await gesture.updateWithCustomEvent(const PointerMoveEvent(pointer: pointerValue, position: Offset(0.0, 0.0), pressure: 0.5, pressureMin: 0, pressureMax: 1)); + expect(forcePressStartCount, 1); + + await tester.pump(const Duration(milliseconds: 50)); + await gesture.up(); + await tester.pumpAndSettle(); + + expect(forcePressEndCount, 1); + expect(doubleTapDownCount, 0); + }); }