From 15ceca93a4aac8d827b6f1899fe4664abfe71161 Mon Sep 17 00:00:00 2001 From: Ian Hickson Date: Mon, 22 Jan 2024 16:49:05 -0800 Subject: [PATCH] Enable contextMenuBuilder in the absence of selectionControls (#141810) --- packages/flutter/lib/src/gestures/events.dart | 4 +- .../lib/src/widgets/editable_text.dart | 1 - .../lib/src/widgets/text_selection.dart | 2 +- .../test/widgets/text_selection_test.dart | 47 ++++++++++++++++++- 4 files changed, 49 insertions(+), 5 deletions(-) diff --git a/packages/flutter/lib/src/gestures/events.dart b/packages/flutter/lib/src/gestures/events.dart index c78f324e72..a8ef8c665c 100644 --- a/packages/flutter/lib/src/gestures/events.dart +++ b/packages/flutter/lib/src/gestures/events.dart @@ -39,7 +39,7 @@ const int kPrimaryButton = 0x01; /// /// It is equivalent to: /// -/// * [kPrimaryStylusButton]: The stylus contacts the screen. +/// * [kPrimaryStylusButton]: The stylus' primary button. /// * [kSecondaryMouseButton]: The secondary mouse button. /// /// See also: @@ -97,7 +97,7 @@ const int kPrimaryStylusButton = kSecondaryButton; /// /// It is equivalent to: /// -/// * [kMiddleMouseButton]: The tertiary mouseButton. +/// * [kMiddleMouseButton]: The tertiary mouse button. /// * [kSecondaryStylusButton]: The secondary button on a stylus. This is considered /// a tertiary button as the primary button of a stylus already corresponds to a /// "secondary operation" (where stylus contact is the primary operation). diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index 1dbccddbfe..deb47718ea 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -4320,7 +4320,6 @@ class EditableTextState extends State with AutomaticKeepAliveClien /// Toggles the visibility of the toolbar. void toggleToolbar([bool hideHandles = true]) { final TextSelectionOverlay selectionOverlay = _selectionOverlay ??= _createSelectionOverlay(); - if (selectionOverlay.toolbarIsVisible) { hideToolbar(hideHandles); } else { diff --git a/packages/flutter/lib/src/widgets/text_selection.dart b/packages/flutter/lib/src/widgets/text_selection.dart index 00eabb5e7f..bb751a639c 100644 --- a/packages/flutter/lib/src/widgets/text_selection.dart +++ b/packages/flutter/lib/src/widgets/text_selection.dart @@ -448,7 +448,7 @@ class TextSelectionOverlay { void showToolbar() { _updateSelectionOverlay(); - if (selectionControls is! TextSelectionHandleControls) { + if (selectionControls != null && selectionControls is! TextSelectionHandleControls) { _selectionOverlay.showToolbar(); return; } diff --git a/packages/flutter/test/widgets/text_selection_test.dart b/packages/flutter/test/widgets/text_selection_test.dart index f2c4e18b39..ec77ba33cc 100644 --- a/packages/flutter/test/widgets/text_selection_test.dart +++ b/packages/flutter/test/widgets/text_selection_test.dart @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'package:flutter/foundation.dart' show ValueListenable, defaultTargetPlatform; +import 'package:flutter/foundation.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; @@ -1781,6 +1781,51 @@ void main() { ); }); }); + + testWidgets('Context menus', (WidgetTester tester) async { + final TextEditingController controller = TextEditingController( + text: 'You make wine from sour grapes', + ); + addTearDown(controller.dispose); + final GlobalKey editableTextKey = GlobalKey(); + final FakeTextSelectionGestureDetectorBuilderDelegate delegate = FakeTextSelectionGestureDetectorBuilderDelegate( + editableTextKey: editableTextKey, + forcePressEnabled: false, + selectionEnabled: true, + ); + final TextSelectionGestureDetectorBuilder provider = + TextSelectionGestureDetectorBuilder(delegate: delegate); + final FocusNode focusNode = FocusNode(); + addTearDown(focusNode.dispose); + + await tester.pumpWidget( + MaterialApp( + home: provider.buildGestureDetector( + behavior: HitTestBehavior.translucent, + child: EditableText( + key: editableTextKey, + controller: controller, + focusNode: focusNode, + backgroundCursorColor: Colors.white, + cursorColor: Colors.white, + style: const TextStyle(), + contextMenuBuilder: ( + BuildContext context, + EditableTextState editableTextState, + ) { + return const Placeholder(); + }, + ), + ), + ), + ); + + final Offset position = textOffsetToPosition(tester, 0); + expect(find.byType(Placeholder), findsNothing); + await tester.tapAt(position, buttons: kSecondaryMouseButton, kind: PointerDeviceKind.mouse); + await tester.pump(); + expect(find.byType(Placeholder), findsOneWidget); + }, skip: kIsWeb); // [intended] On web, we use native context menus for text fields. } class FakeTextSelectionGestureDetectorBuilderDelegate implements TextSelectionGestureDetectorBuilderDelegate {