Mouse drag should not show selection handles (#69101)
Fixes a bug where selection handles sometimes showed on web.
This commit is contained in:
@@ -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).
|
||||
|
||||
@@ -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<void>('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<void>('onStart', () => onStart!(details));
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user