From 21273fde8e5c24b156f75e347de0b110568e17b7 Mon Sep 17 00:00:00 2001 From: LongCatIsLooong <31859944+LongCatIsLooong@users.noreply.github.com> Date: Mon, 7 Jun 2021 19:14:04 -0700 Subject: [PATCH] [TextSelectionControls] move the tap gesture callback into _TextSelectionHandlePainter, remove `_TransparentTapGestureRecognizer`. (#83639) --- .../src/cupertino/desktop_text_selection.dart | 2 +- .../lib/src/cupertino/text_selection.dart | 4 +- .../src/material/desktop_text_selection.dart | 2 +- .../lib/src/material/text_selection.dart | 6 +- .../lib/src/widgets/text_selection.dart | 80 +++++++------------ .../test/cupertino/text_field_test.dart | 2 +- .../test/cupertino/text_selection_test.dart | 1 + .../test/material/text_field_test.dart | 41 +++++++++- .../test/material/text_selection_test.dart | 2 +- .../material/text_selection_theme_test.dart | 3 + .../test/widgets/editable_text_test.dart | 12 +-- 11 files changed, 91 insertions(+), 64 deletions(-) diff --git a/packages/flutter/lib/src/cupertino/desktop_text_selection.dart b/packages/flutter/lib/src/cupertino/desktop_text_selection.dart index 5971492f84..bc4c839484 100644 --- a/packages/flutter/lib/src/cupertino/desktop_text_selection.dart +++ b/packages/flutter/lib/src/cupertino/desktop_text_selection.dart @@ -67,7 +67,7 @@ class _CupertinoDesktopTextSelectionControls extends TextSelectionControls { /// Builds the text selection handles, but desktop has none. @override - Widget buildHandle(BuildContext context, TextSelectionHandleType type, double textLineHeight) { + Widget buildHandle(BuildContext context, TextSelectionHandleType type, double textLineHeight, VoidCallback? onTap) { return const SizedBox.shrink(); } diff --git a/packages/flutter/lib/src/cupertino/text_selection.dart b/packages/flutter/lib/src/cupertino/text_selection.dart index 26c8a78fb7..1e1cb42ccf 100644 --- a/packages/flutter/lib/src/cupertino/text_selection.dart +++ b/packages/flutter/lib/src/cupertino/text_selection.dart @@ -247,7 +247,9 @@ class CupertinoTextSelectionControls extends TextSelectionControls { /// Builder for iOS text selection edges. @override - Widget buildHandle(BuildContext context, TextSelectionHandleType type, double textLineHeight) { + Widget buildHandle(BuildContext context, TextSelectionHandleType type, double textLineHeight, VoidCallback? onTap) { + // iOS selection handles do not respond to taps. + // We want a size that's a vertical line the height of the text plus a 18.0 // padding in every direction that will constitute the selection drag area. final Size desiredSize = getHandleSize(textLineHeight); diff --git a/packages/flutter/lib/src/material/desktop_text_selection.dart b/packages/flutter/lib/src/material/desktop_text_selection.dart index e54c2e4f94..a93711fd54 100644 --- a/packages/flutter/lib/src/material/desktop_text_selection.dart +++ b/packages/flutter/lib/src/material/desktop_text_selection.dart @@ -53,7 +53,7 @@ class _DesktopTextSelectionControls extends TextSelectionControls { /// Builds the text selection handles, but desktop has none. @override - Widget buildHandle(BuildContext context, TextSelectionHandleType type, double textLineHeight) { + Widget buildHandle(BuildContext context, TextSelectionHandleType type, double textLineHeight, VoidCallback? onTap) { return const SizedBox.shrink(); } diff --git a/packages/flutter/lib/src/material/text_selection.dart b/packages/flutter/lib/src/material/text_selection.dart index 9beff7cb1e..2776ed2dbc 100644 --- a/packages/flutter/lib/src/material/text_selection.dart +++ b/packages/flutter/lib/src/material/text_selection.dart @@ -54,7 +54,7 @@ class MaterialTextSelectionControls extends TextSelectionControls { /// Builder for material-style text selection handles. @override - Widget buildHandle(BuildContext context, TextSelectionHandleType type, double textHeight) { + Widget buildHandle(BuildContext context, TextSelectionHandleType type, double textHeight, VoidCallback? onTap) { final ThemeData theme = Theme.of(context); final Color handleColor = TextSelectionTheme.of(context).selectionHandleColor ?? theme.colorScheme.primary; final Widget handle = SizedBox( @@ -64,6 +64,10 @@ class MaterialTextSelectionControls extends TextSelectionControls { painter: _TextSelectionHandlePainter( color: handleColor, ), + child: GestureDetector( + onTap: onTap, + behavior: HitTestBehavior.translucent, + ), ), ); diff --git a/packages/flutter/lib/src/widgets/text_selection.dart b/packages/flutter/lib/src/widgets/text_selection.dart index 4c4f2591a2..c558bf2484 100644 --- a/packages/flutter/lib/src/widgets/text_selection.dart +++ b/packages/flutter/lib/src/widgets/text_selection.dart @@ -110,11 +110,16 @@ class ToolbarItemsParentData extends ContainerBoxParentData { /// /// Override text operations such as [handleCut] if needed. abstract class TextSelectionControls { - /// Builds a selection handle of the given type. + /// Builds a selection handle of the given `type`. /// /// The top left corner of this widget is positioned at the bottom of the /// selection position. - Widget buildHandle(BuildContext context, TextSelectionHandleType type, double textLineHeight); + /// + /// The supplied [onTap] should be invoked when the handle is tapped, if such + /// interaction is allowed. As a counterexample, the default selection handle + /// on iOS [cupertinoTextSelectionControls] does not call [onTap] at all, + /// since its handles are not meant to be tapped. + Widget buildHandle(BuildContext context, TextSelectionHandleType type, double textLineHeight, VoidCallback? onTap); /// Get the anchor point of the handle relative to itself. The anchor point is /// the point that is aligned with a specific point in the text. A handle @@ -401,11 +406,19 @@ class TextSelectionOverlay { final DragStartBehavior dragStartBehavior; /// {@template flutter.widgets.TextSelectionOverlay.onSelectionHandleTapped} - /// A callback that's invoked when a selection handle is tapped. + /// A callback that's optionally invoked when a selection handle is tapped. /// - /// Both regular taps and long presses invoke this callback, but a drag - /// gesture won't. + /// The [TextSelectionControls.buildHandle] implementation the text field + /// uses decides where the the handle's tap "hotspot" is, or whether the + /// selection handle supports tap gestures at all. For instance, + /// [MaterialTextSelectionControls] calls [onSelectionHandleTapped] when the + /// selection handle's "knob" is tapped, while + /// [CupertinoTextSelectionControls] builds a handle that's not sufficiently + /// large for tapping (as it's not meant to be tapped) so it does not call + /// [onSelectionHandleTapped] even when tapped. /// {@endtemplate} + // See https://github.com/flutter/flutter/issues/39376#issuecomment-848406415 + // for provenance. final VoidCallback? onSelectionHandleTapped; /// Maintains the status of the clipboard for determining if its contents can @@ -568,7 +581,8 @@ class TextSelectionOverlay { } Widget _buildHandle(BuildContext context, _TextSelectionHandlePosition position) { - Widget handle; + final Widget handle; + final TextSelectionControls? selectionControls = this.selectionControls; if ((_selection.isCollapsed && position == _TextSelectionHandlePosition.end) || selectionControls == null) handle = Container(); // hide the second handle when collapsed @@ -690,7 +704,7 @@ class _TextSelectionHandleOverlay extends StatefulWidget { final RenderEditable renderObject; final ValueChanged onSelectionHandleChanged; final VoidCallback? onSelectionHandleTapped; - final TextSelectionControls? selectionControls; + final TextSelectionControls selectionControls; final DragStartBehavior dragStartBehavior; @override @@ -747,7 +761,7 @@ class _TextSelectionHandleOverlayState } void _handleDragStart(DragStartDetails details) { - final Size handleSize = widget.selectionControls!.getHandleSize( + final Size handleSize = widget.selectionControls.getHandleSize( widget.renderObject.preferredLineHeight, ); _dragPosition = details.globalPosition + Offset(0.0, -handleSize.height); @@ -784,10 +798,6 @@ class _TextSelectionHandleOverlayState widget.onSelectionHandleChanged(newSelection); } - void _handleTap() { - widget.onSelectionHandleTapped?.call(); - } - @override Widget build(BuildContext context) { final LayerLink layerLink; @@ -814,11 +824,11 @@ class _TextSelectionHandleOverlayState break; } - final Offset handleAnchor = widget.selectionControls!.getHandleAnchor( + final Offset handleAnchor = widget.selectionControls.getHandleAnchor( type, widget.renderObject.preferredLineHeight, ); - final Size handleSize = widget.selectionControls!.getHandleSize( + final Size handleSize = widget.selectionControls.getHandleSize( widget.renderObject.preferredLineHeight, ); @@ -855,7 +865,6 @@ class _TextSelectionHandleOverlayState dragStartBehavior: widget.dragStartBehavior, onPanStart: _handleDragStart, onPanUpdate: _handleDragUpdate, - onTap: _handleTap, child: Padding( padding: EdgeInsets.only( left: padding.left, @@ -863,10 +872,11 @@ class _TextSelectionHandleOverlayState right: padding.right, bottom: padding.bottom, ), - child: widget.selectionControls!.buildHandle( + child: widget.selectionControls.buildHandle( context, type, widget.renderObject.preferredLineHeight, + widget.onSelectionHandleTapped, ), ), ), @@ -1520,12 +1530,9 @@ class _TextSelectionGestureDetectorState extends State gestures = {}; - // Use _TransparentTapGestureRecognizer so that TextSelectionGestureDetector - // can receive the same tap events that a selection handle placed visually - // on top of it also receives. - gestures[_TransparentTapGestureRecognizer] = GestureRecognizerFactoryWithHandlers<_TransparentTapGestureRecognizer>( - () => _TransparentTapGestureRecognizer(debugOwner: this), - (_TransparentTapGestureRecognizer instance) { + gestures[TapGestureRecognizer] = GestureRecognizerFactoryWithHandlers( + () => TapGestureRecognizer(debugOwner: this), + (TapGestureRecognizer instance) { instance ..onSecondaryTap = widget.onSecondaryTap ..onSecondaryTapDown = widget.onSecondaryTapDown @@ -1588,35 +1595,6 @@ class _TextSelectionGestureDetectorState extends State( find.descendant( of: find.byType(CompositedTransformFollower), - matching: find.byType(GestureDetector), + matching: find.byType(Padding), ), ), ); @@ -722,7 +722,7 @@ void main() { tester.renderObjectList( find.descendant( of: find.byType(CompositedTransformFollower), - matching: find.byType(GestureDetector), + matching: find.byType(Padding), ), ), ); @@ -3907,7 +3907,7 @@ void main() { tester.renderObjectList( find.descendant( of: find.byType(CompositedTransformFollower), - matching: find.byType(GestureDetector), + matching: find.byType(Padding), ), ), ); @@ -4027,7 +4027,7 @@ void main() { tester.renderObjectList( find.descendant( of: find.byType(CompositedTransformFollower), - matching: find.byType(GestureDetector), + matching: find.byType(Padding), ), ), ); @@ -5058,7 +5058,7 @@ void main() { tester.renderObjectList( find.descendant( of: find.byType(CompositedTransformFollower), - matching: find.byType(GestureDetector), + matching: find.byType(Padding), ), ), ); @@ -7569,7 +7569,7 @@ class MockTextSelectionControls extends Fake implements TextSelectionControls { } @override - Widget buildHandle(BuildContext context, TextSelectionHandleType type, double textLineHeight) { + Widget buildHandle(BuildContext context, TextSelectionHandleType type, double textLineHeight, VoidCallback? onTap) { return Container(); }