From 91ab73ab23345cc412cf198eab85d7749bd113c1 Mon Sep 17 00:00:00 2001 From: xubaolin Date: Thu, 29 Oct 2020 02:24:46 +0800 Subject: [PATCH] Mouse drag should not show selection handles (#69101) Fixes a bug where selection handles sometimes showed on web. --- .../lib/src/gestures/drag_details.dart | 6 ++++- .../flutter/lib/src/gestures/monodrag.dart | 5 ++-- .../lib/src/widgets/text_selection.dart | 9 +++++-- .../test/widgets/text_selection_test.dart | 25 +++++++++++++++++++ 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/packages/flutter/lib/src/gestures/drag_details.dart b/packages/flutter/lib/src/gestures/drag_details.dart index ce8282313b..fd3554a69e 100644 --- a/packages/flutter/lib/src/gestures/drag_details.dart +++ b/packages/flutter/lib/src/gestures/drag_details.dart @@ -3,7 +3,7 @@ // found in the LICENSE file. -import 'dart:ui' show Offset; +import 'dart:ui' show Offset, PointerDeviceKind; import 'package:flutter/foundation.dart'; @@ -71,6 +71,7 @@ class DragStartDetails { this.sourceTimeStamp, this.globalPosition = Offset.zero, Offset? localPosition, + this.kind, }) : assert(globalPosition != null), localPosition = localPosition ?? globalPosition; @@ -96,6 +97,9 @@ class DragStartDetails { /// Defaults to [globalPosition] if not specified in the constructor. final Offset localPosition; + /// The kind of the device that initiated the event. + final PointerDeviceKind? kind; + // TODO(ianh): Expose the current position, so that you can have a no-jump // drag even when disambiguating (though of course it would lag the finger // instead). diff --git a/packages/flutter/lib/src/gestures/monodrag.dart b/packages/flutter/lib/src/gestures/monodrag.dart index 41dd1719dd..6afad9f23c 100644 --- a/packages/flutter/lib/src/gestures/monodrag.dart +++ b/packages/flutter/lib/src/gestures/monodrag.dart @@ -336,7 +336,7 @@ abstract class DragGestureRecognizer extends OneSequenceGestureRecognizer { _pendingDragOffset = OffsetPair.zero; _lastPendingEventTimestamp = null; _lastTransform = null; - _checkStart(timestamp); + _checkStart(timestamp, pointer); if (localUpdateDelta != Offset.zero && onUpdate != null) { final Matrix4? localToGlobal = transform != null ? Matrix4.tryInvert(transform) : null; final Offset correctedLocalPosition = _initialPosition.local + localUpdateDelta; @@ -404,12 +404,13 @@ abstract class DragGestureRecognizer extends OneSequenceGestureRecognizer { invokeCallback('onDown', () => onDown!(details)); } - void _checkStart(Duration timestamp) { + void _checkStart(Duration timestamp, int pointer) { assert(_initialButtons == kPrimaryButton); final DragStartDetails details = DragStartDetails( sourceTimeStamp: timestamp, globalPosition: _initialPosition.global, localPosition: _initialPosition.local, + kind: getKindForPointer(pointer), ); if (onStart != null) invokeCallback('onStart', () => onStart!(details)); diff --git a/packages/flutter/lib/src/widgets/text_selection.dart b/packages/flutter/lib/src/widgets/text_selection.dart index 62d02f840b..9118a1e50f 100644 --- a/packages/flutter/lib/src/widgets/text_selection.dart +++ b/packages/flutter/lib/src/widgets/text_selection.dart @@ -922,8 +922,8 @@ class TextSelectionGestureDetectorBuilder { // For backwards-compatibility, we treat a null kind the same as touch. final PointerDeviceKind? kind = details.kind; _shouldShowSelectionToolbar = kind == null - || kind == PointerDeviceKind.touch - || kind == PointerDeviceKind.stylus; + || kind == PointerDeviceKind.touch + || kind == PointerDeviceKind.stylus; } /// Handler for [TextSelectionGestureDetector.onForcePressStart]. @@ -1077,6 +1077,11 @@ class TextSelectionGestureDetectorBuilder { /// this callback. @protected void onDragSelectionStart(DragStartDetails details) { + final PointerDeviceKind? kind = details.kind; + _shouldShowSelectionToolbar = kind == null + || kind == PointerDeviceKind.touch + || kind == PointerDeviceKind.stylus; + renderEditable.selectPositionAt( from: details.globalPosition, cause: SelectionChangedCause.drag, diff --git a/packages/flutter/test/widgets/text_selection_test.dart b/packages/flutter/test/widgets/text_selection_test.dart index 9af368d577..67f5067f64 100644 --- a/packages/flutter/test/widgets/text_selection_test.dart +++ b/packages/flutter/test/widgets/text_selection_test.dart @@ -557,6 +557,31 @@ void main() { expect(renderEditable.selectWordsInRangeCalled, isTrue); }); + testWidgets('Mouse drag does not show handles nor toolbar', (WidgetTester tester) async { + // Regressing test for https://github.com/flutter/flutter/issues/69001 + await tester.pumpWidget( + const MaterialApp( + home: Scaffold( + body: SelectableText('I love Flutter!'), + ), + ), + ); + + final Offset textFieldStart = tester.getTopLeft(find.byType(SelectableText)); + + final TestGesture gesture = await tester.startGesture(textFieldStart, kind: PointerDeviceKind.mouse); + addTearDown(gesture.removePointer); + await tester.pump(); + await gesture.moveTo(textFieldStart + const Offset(50.0, 0)); + await tester.pump(); + await gesture.up(); + await tester.pumpAndSettle(); + + final EditableTextState editableText = tester.state(find.byType(EditableText)); + expect(editableText.selectionOverlay!.handlesAreVisible, isFalse); + expect(editableText.selectionOverlay!.toolbarIsVisible, isFalse); + }); + testWidgets('test TextSelectionGestureDetectorBuilder selection disabled', (WidgetTester tester) async { await pumpTextSelectionGestureDetectorBuilder(tester, selectionEnabled: false); final TestGesture gesture = await tester.startGesture(