From a8471a61f8bf7ebce3673ef437c5b4a8e3f03e3c Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Wed, 20 Jan 2021 16:29:01 -0800 Subject: [PATCH] Material Desktop Context Menu (#74286) Very simple right-click context menu for Windows and Linux in a Material-esque style. --- packages/flutter/lib/material.dart | 1 + .../src/cupertino/desktop_text_selection.dart | 44 +-- .../src/material/desktop_text_selection.dart | 372 ++++++++++++++++++ .../lib/src/material/selectable_text.dart | 11 +- .../flutter/lib/src/material/text_field.dart | 11 +- .../text_selection_toolbar_text_button.dart | 4 + ...ext_selection_toolbar_layout_delegate.dart | 51 +++ packages/flutter/lib/widgets.dart | 1 + .../test/material/text_field_test.dart | 8 +- .../test/material/text_form_field_test.dart | 2 +- .../test/widgets/selectable_text_test.dart | 47 ++- 11 files changed, 498 insertions(+), 54 deletions(-) create mode 100644 packages/flutter/lib/src/material/desktop_text_selection.dart create mode 100644 packages/flutter/lib/src/widgets/desktop_text_selection_toolbar_layout_delegate.dart diff --git a/packages/flutter/lib/material.dart b/packages/flutter/lib/material.dart index 6be74ae151..706b2c3dbe 100644 --- a/packages/flutter/lib/material.dart +++ b/packages/flutter/lib/material.dart @@ -57,6 +57,7 @@ export 'src/material/date.dart'; export 'src/material/date_picker.dart'; export 'src/material/date_picker_deprecated.dart'; export 'src/material/debug.dart'; +export 'src/material/desktop_text_selection.dart'; export 'src/material/dialog.dart'; export 'src/material/dialog_theme.dart'; export 'src/material/divider.dart'; diff --git a/packages/flutter/lib/src/cupertino/desktop_text_selection.dart b/packages/flutter/lib/src/cupertino/desktop_text_selection.dart index 73625b54a1..a3686932ce 100644 --- a/packages/flutter/lib/src/cupertino/desktop_text_selection.dart +++ b/packages/flutter/lib/src/cupertino/desktop_text_selection.dart @@ -307,7 +307,7 @@ class _CupertinoDesktopTextSelectionToolbar extends StatelessWidget { _kToolbarScreenPadding, ), child: CustomSingleChildLayout( - delegate: _DesktopTextSelectionToolbarLayoutDelegate( + delegate: DesktopTextSelectionToolbarLayoutDelegate( anchor: anchor - localAdjustment, ), child: toolbarBuilder(context, Column( @@ -319,48 +319,6 @@ class _CupertinoDesktopTextSelectionToolbar extends StatelessWidget { } } -// Positions the toolbar at [anchor] if it fits, otherwise moves it so that it -// just fits fully on-screen. -// -// See also: -// -// * [CupertinoDesktopTextSelectionToolbar], which uses this to position itself. -// * [TextSelectionToolbarLayoutDelegate], which does a similar layout for -// the mobile text selection toolbars. -class _DesktopTextSelectionToolbarLayoutDelegate extends SingleChildLayoutDelegate { - /// Creates an instance of TextSelectionToolbarLayoutDelegate. - _DesktopTextSelectionToolbarLayoutDelegate({ - required this.anchor, - }); - - /// The point at which to render the menu, if possible. - /// - /// Should be provided in local coordinates. - final Offset anchor; - - @override - BoxConstraints getConstraintsForChild(BoxConstraints constraints) { - return constraints.loosen(); - } - - @override - Offset getPositionForChild(Size size, Size childSize) { - final Offset overhang = Offset( - anchor.dx + childSize.width - size.width, - anchor.dy + childSize.height - size.height, - ); - return Offset( - overhang.dx > 0.0 ? anchor.dx - overhang.dx : anchor.dx, - overhang.dy > 0.0 ? anchor.dy - overhang.dy : anchor.dy, - ); - } - - @override - bool shouldRelayout(_DesktopTextSelectionToolbarLayoutDelegate oldDelegate) { - return anchor != oldDelegate.anchor; - } -} - // These values were measured from a screenshot of TextEdit on MacOS 10.15.7 on // a Macbook Pro. const TextStyle _kToolbarButtonFontStyle = TextStyle( diff --git a/packages/flutter/lib/src/material/desktop_text_selection.dart b/packages/flutter/lib/src/material/desktop_text_selection.dart new file mode 100644 index 0000000000..1f5522827f --- /dev/null +++ b/packages/flutter/lib/src/material/desktop_text_selection.dart @@ -0,0 +1,372 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter/gestures.dart'; +import 'package:flutter/rendering.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter/widgets.dart'; + +import 'colors.dart'; +import 'constants.dart'; +import 'debug.dart'; +import 'material.dart'; +import 'material_localizations.dart'; +import 'text_button.dart'; +import 'text_selection_toolbar.dart'; +import 'theme.dart'; + +const double _kToolbarScreenPadding = 8.0; +const double _kToolbarWidth = 222.0; + +class _DesktopTextSelectionControls extends TextSelectionControls { + /// Desktop has no text selection handles. + @override + Size getHandleSize(double textLineHeight) { + return Size.zero; + } + + /// Builder for the Material-style desktop copy/paste text selection toolbar. + @override + Widget buildToolbar( + BuildContext context, + Rect globalEditableRegion, + double textLineHeight, + Offset selectionMidpoint, + List endpoints, + TextSelectionDelegate delegate, + ClipboardStatusNotifier clipboardStatus, + Offset? lastSecondaryTapDownPosition, + ) { + return _DesktopTextSelectionControlsToolbar( + clipboardStatus: clipboardStatus, + endpoints: endpoints, + globalEditableRegion: globalEditableRegion, + handleCut: canCut(delegate) ? () => handleCut(delegate) : null, + handleCopy: canCopy(delegate) ? () => handleCopy(delegate, clipboardStatus) : null, + handlePaste: canPaste(delegate) ? () => handlePaste(delegate) : null, + handleSelectAll: canSelectAll(delegate) ? () => handleSelectAll(delegate) : null, + selectionMidpoint: selectionMidpoint, + lastSecondaryTapDownPosition: lastSecondaryTapDownPosition, + textLineHeight: textLineHeight, + ); + } + + /// Builds the text selection handles, but desktop has none. + @override + Widget buildHandle(BuildContext context, TextSelectionHandleType type, double textLineHeight) { + return const SizedBox.shrink(); + } + + /// Gets the position for the text selection handles, but desktop has none. + @override + Offset getHandleAnchor(TextSelectionHandleType type, double textLineHeight) { + return Offset.zero; + } + + @override + bool canSelectAll(TextSelectionDelegate delegate) { + // Allow SelectAll when selection is not collapsed, unless everything has + // already been selected. Same behavior as Android. + final TextEditingValue value = delegate.textEditingValue; + return delegate.selectAllEnabled && + value.text.isNotEmpty && + !(value.selection.start == 0 && value.selection.end == value.text.length); + } +} + +/// Text selection controls that loosely follows Material design conventions. +final TextSelectionControls desktopTextSelectionControls = + _DesktopTextSelectionControls(); + +// Generates the child that's passed into DesktopTextSelectionToolbar. +class _DesktopTextSelectionControlsToolbar extends StatefulWidget { + const _DesktopTextSelectionControlsToolbar({ + Key? key, + required this.clipboardStatus, + required this.endpoints, + required this.globalEditableRegion, + required this.handleCopy, + required this.handleCut, + required this.handlePaste, + required this.handleSelectAll, + required this.selectionMidpoint, + required this.textLineHeight, + required this.lastSecondaryTapDownPosition, + }) : super(key: key); + + final ClipboardStatusNotifier? clipboardStatus; + final List endpoints; + final Rect globalEditableRegion; + final VoidCallback? handleCopy; + final VoidCallback? handleCut; + final VoidCallback? handlePaste; + final VoidCallback? handleSelectAll; + final Offset? lastSecondaryTapDownPosition; + final Offset selectionMidpoint; + final double textLineHeight; + + @override + _DesktopTextSelectionControlsToolbarState createState() => _DesktopTextSelectionControlsToolbarState(); +} + +class _DesktopTextSelectionControlsToolbarState extends State<_DesktopTextSelectionControlsToolbar> { + ClipboardStatusNotifier? _clipboardStatus; + + void _onChangedClipboardStatus() { + setState(() { + // Inform the widget that the value of clipboardStatus has changed. + }); + } + + @override + void initState() { + super.initState(); + if (widget.handlePaste != null) { + _clipboardStatus = widget.clipboardStatus ?? ClipboardStatusNotifier(); + _clipboardStatus!.addListener(_onChangedClipboardStatus); + _clipboardStatus!.update(); + } + } + + @override + void didUpdateWidget(_DesktopTextSelectionControlsToolbar oldWidget) { + super.didUpdateWidget(oldWidget); + if (oldWidget.clipboardStatus != widget.clipboardStatus) { + if (_clipboardStatus != null) { + _clipboardStatus!.removeListener(_onChangedClipboardStatus); + _clipboardStatus!.dispose(); + } + _clipboardStatus = widget.clipboardStatus ?? ClipboardStatusNotifier(); + _clipboardStatus!.addListener(_onChangedClipboardStatus); + if (widget.handlePaste != null) { + _clipboardStatus!.update(); + } + } + } + + @override + void dispose() { + super.dispose(); + // When used in an Overlay, this can be disposed after its creator has + // already disposed _clipboardStatus. + if (_clipboardStatus != null && !_clipboardStatus!.disposed) { + _clipboardStatus!.removeListener(_onChangedClipboardStatus); + if (widget.clipboardStatus == null) { + _clipboardStatus!.dispose(); + } + } + } + + @override + Widget build(BuildContext context) { + // Don't render the menu until the state of the clipboard is known. + if (widget.handlePaste != null && _clipboardStatus!.value == ClipboardStatus.unknown) { + return const SizedBox(width: 0.0, height: 0.0); + } + + assert(debugCheckHasMediaQuery(context)); + final MediaQueryData mediaQuery = MediaQuery.of(context); + + final Offset midpointAnchor = Offset( + (widget.selectionMidpoint.dx - widget.globalEditableRegion.left).clamp( + mediaQuery.padding.left, + mediaQuery.size.width - mediaQuery.padding.right, + ), + widget.selectionMidpoint.dy - widget.globalEditableRegion.top, + ); + + assert(debugCheckHasMaterialLocalizations(context)); + final MaterialLocalizations localizations = MaterialLocalizations.of(context); + final List items = []; + + void addToolbarButton( + String text, + VoidCallback onPressed, + ) { + items.add(_DesktopTextSelectionToolbarButton.text( + context: context, + onPressed: onPressed, + text: text, + )); + } + + if (widget.handleCut != null) { + addToolbarButton(localizations.cutButtonLabel, widget.handleCut!); + } + if (widget.handleCopy != null) { + addToolbarButton(localizations.copyButtonLabel, widget.handleCopy!); + } + if (widget.handlePaste != null + && _clipboardStatus!.value == ClipboardStatus.pasteable) { + addToolbarButton(localizations.pasteButtonLabel, widget.handlePaste!); + } + if (widget.handleSelectAll != null) { + addToolbarButton(localizations.selectAllButtonLabel, widget.handleSelectAll!); + } + + // If there is no option available, build an empty widget. + if (items.isEmpty) { + return const SizedBox(width: 0.0, height: 0.0); + } + + return _DesktopTextSelectionToolbar( + anchor: widget.lastSecondaryTapDownPosition ?? midpointAnchor, + children: items, + ); + } +} + +/// A Material-style desktop text selection toolbar. +/// +/// Typically displays buttons for text manipulation, e.g. copying and pasting +/// text. +/// +/// Tries to position itself as closesly as possible to [anchor] while remaining +/// fully on-screen. +/// +/// See also: +/// +/// * [_DesktopTextSelectionControls.buildToolbar], where this is used by +/// default to build a Material-style desktop toolbar. +/// * [TextSelectionToolbar], which is similar, but builds an Android-style +/// toolbar. +class _DesktopTextSelectionToolbar extends StatelessWidget { + /// Creates an instance of _DesktopTextSelectionToolbar. + const _DesktopTextSelectionToolbar({ + Key? key, + required this.anchor, + required this.children, + this.toolbarBuilder = _defaultToolbarBuilder, + }) : assert(children.length > 0), + super(key: key); + + /// The point at which the toolbar will attempt to position itself as closely + /// as possible. + final Offset anchor; + + /// {@macro flutter.material.TextSelectionToolbar.children} + /// + /// See also: + /// * [DesktopTextSelectionToolbarButton], which builds a default + /// Material-style desktop text selection toolbar text button. + final List children; + + /// {@macro flutter.material.TextSelectionToolbar.toolbarBuilder} + /// + /// The given anchor and isAbove can be used to position an arrow, as in the + /// default toolbar. + final ToolbarBuilder toolbarBuilder; + + // Builds a desktop toolbar in the Material style. + static Widget _defaultToolbarBuilder(BuildContext context, Widget child) { + return SizedBox( + width: _kToolbarWidth, + child: Material( + borderRadius: const BorderRadius.all(Radius.circular(7.0)), + clipBehavior: Clip.antiAlias, + elevation: 1.0, + type: MaterialType.card, + child: child, + ), + ); + } + + @override + Widget build(BuildContext context) { + assert(debugCheckHasMediaQuery(context)); + final MediaQueryData mediaQuery = MediaQuery.of(context); + + final double paddingAbove = mediaQuery.padding.top + _kToolbarScreenPadding; + final Offset localAdjustment = Offset(_kToolbarScreenPadding, paddingAbove); + + return Padding( + padding: EdgeInsets.fromLTRB( + _kToolbarScreenPadding, + paddingAbove, + _kToolbarScreenPadding, + _kToolbarScreenPadding, + ), + child: CustomSingleChildLayout( + delegate: DesktopTextSelectionToolbarLayoutDelegate( + anchor: anchor - localAdjustment, + ), + child: toolbarBuilder(context, Column( + mainAxisSize: MainAxisSize.min, + children: children, + )), + ), + ); + } +} + +const TextStyle _kToolbarButtonFontStyle = TextStyle( + inherit: false, + fontSize: 14.0, + letterSpacing: -0.15, + fontWeight: FontWeight.w400, +); + +const EdgeInsets _kToolbarButtonPadding = EdgeInsets.fromLTRB( + 20.0, + 0.0, + 20.0, + 3.0, +); + +/// A [TextButton] for the Material desktop text selection toolbar. +class _DesktopTextSelectionToolbarButton extends StatelessWidget { + /// Creates an instance of DesktopTextSelectionToolbarButton. + const _DesktopTextSelectionToolbarButton({ + Key? key, + required this.onPressed, + required this.child, + }) : super(key: key); + + /// Create an instance of [_DesktopTextSelectionToolbarButton] whose child is + /// a [Text] widget in the style of the Material text selection toolbar. + _DesktopTextSelectionToolbarButton.text({ + Key? key, + required BuildContext context, + required this.onPressed, + required String text, + }) : child = Text( + text, + overflow: TextOverflow.ellipsis, + style: _kToolbarButtonFontStyle.copyWith( + color: Theme.of(context).colorScheme.brightness == Brightness.dark + ? Colors.white + : Colors.black87, + ), + ), + super(key: key); + + /// {@macro flutter.material.TextSelectionToolbarTextButton.onPressed} + final VoidCallback onPressed; + + /// {@macro flutter.material.TextSelectionToolbarTextButton.child} + final Widget child; + + @override + Widget build(BuildContext context) { + // TODO(hansmuller): Should be colorScheme.onSurface + final ThemeData theme = Theme.of(context); + final bool isDark = theme.colorScheme.brightness == Brightness.dark; + final Color primary = isDark ? Colors.white : Colors.black87; + + return SizedBox( + width: double.infinity, + child: TextButton( + style: TextButton.styleFrom( + alignment: Alignment.centerLeft, + primary: primary, + shape: const RoundedRectangleBorder(), + minimumSize: const Size(kMinInteractiveDimension, 36.0), + padding: _kToolbarButtonPadding, + ), + onPressed: onPressed, + child: child, + ), + ); + } +} diff --git a/packages/flutter/lib/src/material/selectable_text.dart b/packages/flutter/lib/src/material/selectable_text.dart index 83c6965301..31ef4ace27 100644 --- a/packages/flutter/lib/src/material/selectable_text.dart +++ b/packages/flutter/lib/src/material/selectable_text.dart @@ -9,6 +9,7 @@ import 'package:flutter/widgets.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/gestures.dart'; +import 'desktop_text_selection.dart'; import 'feedback.dart'; import 'text_selection.dart'; import 'text_selection_theme.dart'; @@ -606,10 +607,18 @@ class _SelectableTextState extends State with AutomaticKeepAlive case TargetPlatform.android: case TargetPlatform.fuchsia: + forcePressEnabled = false; + textSelectionControls ??= materialTextSelectionControls; + paintCursorAboveText = false; + cursorOpacityAnimates = false; + cursorColor ??= selectionTheme.cursorColor ?? theme.colorScheme.primary; + selectionColor = selectionTheme.selectionColor ?? theme.colorScheme.primary.withOpacity(0.40); + break; + case TargetPlatform.linux: case TargetPlatform.windows: forcePressEnabled = false; - textSelectionControls ??= materialTextSelectionControls; + textSelectionControls ??= desktopTextSelectionControls; paintCursorAboveText = false; cursorOpacityAnimates = false; cursorColor ??= selectionTheme.cursorColor ?? theme.colorScheme.primary; diff --git a/packages/flutter/lib/src/material/text_field.dart b/packages/flutter/lib/src/material/text_field.dart index 2701e25823..aa60b566c1 100644 --- a/packages/flutter/lib/src/material/text_field.dart +++ b/packages/flutter/lib/src/material/text_field.dart @@ -13,6 +13,7 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/gestures.dart'; import 'debug.dart'; +import 'desktop_text_selection.dart'; import 'feedback.dart'; import 'input_decorator.dart'; import 'material.dart'; @@ -1169,10 +1170,18 @@ class _TextFieldState extends State with RestorationMixin implements case TargetPlatform.android: case TargetPlatform.fuchsia: + forcePressEnabled = false; + textSelectionControls ??= materialTextSelectionControls; + paintCursorAboveText = false; + cursorOpacityAnimates = false; + cursorColor ??= selectionTheme.cursorColor ?? theme.colorScheme.primary; + selectionColor = selectionTheme.selectionColor ?? theme.colorScheme.primary.withOpacity(0.40); + break; + case TargetPlatform.linux: case TargetPlatform.windows: forcePressEnabled = false; - textSelectionControls ??= materialTextSelectionControls; + textSelectionControls ??= desktopTextSelectionControls; paintCursorAboveText = false; cursorOpacityAnimates = false; cursorColor ??= selectionTheme.cursorColor ?? theme.colorScheme.primary; diff --git a/packages/flutter/lib/src/material/text_selection_toolbar_text_button.dart b/packages/flutter/lib/src/material/text_selection_toolbar_text_button.dart index dae5787439..ec865f26ee 100644 --- a/packages/flutter/lib/src/material/text_selection_toolbar_text_button.dart +++ b/packages/flutter/lib/src/material/text_selection_toolbar_text_button.dart @@ -38,12 +38,16 @@ class TextSelectionToolbarTextButton extends StatelessWidget { static const double _kMiddlePadding = 9.5; static const double _kEndPadding = 14.5; + /// {@template flutter.material.TextSelectionToolbarTextButton.child} /// The child of this button. /// /// Usually a [Text]. + /// {@endtemplate} final Widget child; + /// {@template flutter.material.TextSelectionToolbarTextButton.onPressed} /// Called when this button is pressed. + /// {@endtemplate} final VoidCallback? onPressed; /// The padding between the button's edge and its child. diff --git a/packages/flutter/lib/src/widgets/desktop_text_selection_toolbar_layout_delegate.dart b/packages/flutter/lib/src/widgets/desktop_text_selection_toolbar_layout_delegate.dart new file mode 100644 index 0000000000..471b0116b8 --- /dev/null +++ b/packages/flutter/lib/src/widgets/desktop_text_selection_toolbar_layout_delegate.dart @@ -0,0 +1,51 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter/rendering.dart'; + + +/// Positions the toolbar at [anchor] if it fits, otherwise moves it so that it +/// just fits fully on-screen. +/// +/// See also: +/// +/// * [desktopTextSelectionControls], which uses this to position +/// itself. +/// * [cupertinoDesktopTextSelectionControls], which uses this to position +/// itself. +/// * [TextSelectionToolbarLayoutDelegate], which does a similar layout for +/// the mobile text selection toolbars. +class DesktopTextSelectionToolbarLayoutDelegate extends SingleChildLayoutDelegate { + /// Creates an instance of TextSelectionToolbarLayoutDelegate. + DesktopTextSelectionToolbarLayoutDelegate({ + required this.anchor, + }); + + /// The point at which to render the menu, if possible. + /// + /// Should be provided in local coordinates. + final Offset anchor; + + @override + BoxConstraints getConstraintsForChild(BoxConstraints constraints) { + return constraints.loosen(); + } + + @override + Offset getPositionForChild(Size size, Size childSize) { + final Offset overhang = Offset( + anchor.dx + childSize.width - size.width, + anchor.dy + childSize.height - size.height, + ); + return Offset( + overhang.dx > 0.0 ? anchor.dx - overhang.dx : anchor.dx, + overhang.dy > 0.0 ? anchor.dy - overhang.dy : anchor.dy, + ); + } + + @override + bool shouldRelayout(DesktopTextSelectionToolbarLayoutDelegate oldDelegate) { + return anchor != oldDelegate.anchor; + } +} diff --git a/packages/flutter/lib/widgets.dart b/packages/flutter/lib/widgets.dart index c67677371b..67efa66563 100644 --- a/packages/flutter/lib/widgets.dart +++ b/packages/flutter/lib/widgets.dart @@ -33,6 +33,7 @@ export 'src/widgets/bottom_navigation_bar_item.dart'; export 'src/widgets/color_filter.dart'; export 'src/widgets/container.dart'; export 'src/widgets/debug.dart'; +export 'src/widgets/desktop_text_selection_toolbar_layout_delegate.dart'; export 'src/widgets/dismissible.dart'; export 'src/widgets/disposable_build_context.dart'; export 'src/widgets/drag_target.dart'; diff --git a/packages/flutter/test/material/text_field_test.dart b/packages/flutter/test/material/text_field_test.dart index 82b2f9bec0..85851893c5 100644 --- a/packages/flutter/test/material/text_field_test.dart +++ b/packages/flutter/test/material/text_field_test.dart @@ -238,7 +238,7 @@ void main() { expect(controller.text, ' blah2blah1'); expect(controller.selection, const TextSelection(baseOffset: 0, extentOffset: 0)); expect(find.byType(CupertinoButton), findsNothing); - }, variant: const TargetPlatformVariant({ TargetPlatform.macOS }), skip: kIsWeb); + }, variant: const TargetPlatformVariant({ TargetPlatform.macOS, TargetPlatform.windows, TargetPlatform.linux }), skip: kIsWeb); testWidgets('TextField passes onEditingComplete to EditableText', (WidgetTester tester) async { final VoidCallback onEditingComplete = () { }; @@ -7296,7 +7296,7 @@ void main() { ); // The text selection toolbar isn't shown on Mac without a right click. expect(find.byType(CupertinoButton), findsNothing); - }, variant: const TargetPlatformVariant({ TargetPlatform.macOS }), skip: kIsWeb); + }, variant: const TargetPlatformVariant({ TargetPlatform.macOS, TargetPlatform.windows, TargetPlatform.linux }), skip: kIsWeb); testWidgets('double tap chains work', (WidgetTester tester) async { final TextEditingController controller = TextEditingController( @@ -7445,7 +7445,7 @@ void main() { const TextSelection(baseOffset: 8, extentOffset: 12), ); expect(find.byType(CupertinoButton), findsNothing); - }, variant: const TargetPlatformVariant({ TargetPlatform.macOS }), skip: kIsWeb); + }, variant: const TargetPlatformVariant({ TargetPlatform.macOS, TargetPlatform.windows, TargetPlatform.linux }), skip: kIsWeb); testWidgets('double tapping a space selects the previous word on iOS', (WidgetTester tester) async { final TextEditingController controller = TextEditingController( @@ -7639,7 +7639,7 @@ void main() { expect(controller.value.selection, isNotNull); expect(controller.value.selection.baseOffset, 0); expect(controller.value.selection.extentOffset, 1); - }, variant: const TargetPlatformVariant({ TargetPlatform.macOS })); + }, variant: const TargetPlatformVariant({ TargetPlatform.macOS, TargetPlatform.windows, TargetPlatform.linux }), skip: kIsWeb); testWidgets('force press does not select a word', (WidgetTester tester) async { final TextEditingController controller = TextEditingController( diff --git a/packages/flutter/test/material/text_form_field_test.dart b/packages/flutter/test/material/text_form_field_test.dart index 8e3cb01bc3..d741bdab73 100644 --- a/packages/flutter/test/material/text_form_field_test.dart +++ b/packages/flutter/test/material/text_form_field_test.dart @@ -112,7 +112,7 @@ void main() { expect(controller.text, ' blah2blah1'); expect(controller.selection, const TextSelection(baseOffset: 0, extentOffset: 0)); expect(find.byType(CupertinoButton), findsNothing); - }, variant: const TargetPlatformVariant({ TargetPlatform.macOS }), skip: kIsWeb); + }, variant: const TargetPlatformVariant({ TargetPlatform.macOS, TargetPlatform.windows, TargetPlatform.linux }), skip: kIsWeb); testWidgets('Passes textAlign to underlying TextField', (WidgetTester tester) async { const TextAlign alignment = TextAlign.center; diff --git a/packages/flutter/test/widgets/selectable_text_test.dart b/packages/flutter/test/widgets/selectable_text_test.dart index d0edaa7cdd..15bd5bb6f6 100644 --- a/packages/flutter/test/widgets/selectable_text_test.dart +++ b/packages/flutter/test/widgets/selectable_text_test.dart @@ -255,7 +255,7 @@ void main() { expect(controller.text, ' blah2blah1'); expect(controller.selection, const TextSelection(baseOffset: 0, extentOffset: 0)); expect(find.byType(CupertinoButton), findsNothing); - }, variant: const TargetPlatformVariant({ TargetPlatform.macOS }), skip: kIsWeb); + }, variant: const TargetPlatformVariant({ TargetPlatform.macOS, TargetPlatform.windows, TargetPlatform.linux }), skip: kIsWeb); testWidgets('has expected defaults', (WidgetTester tester) async { await tester.pumpWidget( @@ -4329,8 +4329,6 @@ void main() { variant: const TargetPlatformVariant({ TargetPlatform.android, TargetPlatform.fuchsia, - TargetPlatform.linux, - TargetPlatform.windows, }), ); @@ -4367,8 +4365,49 @@ void main() { variant: const TargetPlatformVariant({ TargetPlatform.android, TargetPlatform.fuchsia, - TargetPlatform.linux, + }), + ); + + testWidgets('The Select All calls on selection changed with a mouse on windows and linux', (WidgetTester tester) async { + const String string = 'abc def ghi'; + TextSelection? newSelection; + await tester.pumpWidget( + MaterialApp( + home: Material( + child: SelectableText( + string, + onSelectionChanged: (TextSelection selection, SelectionChangedCause? cause) { + expect(newSelection, isNull); + newSelection = selection; + }, + ), + ), + ), + ); + + // Right-click on the 'e' in 'def'. + final Offset ePos = textOffsetToPosition(tester, 5); + final TestGesture gesture = await tester.startGesture( + ePos, + kind: PointerDeviceKind.mouse, + buttons: kSecondaryMouseButton, + ); + addTearDown(gesture.removePointer); + await tester.pump(); + await gesture.up(); + await tester.pumpAndSettle(); + expect(newSelection!.baseOffset, 4); + expect(newSelection!.extentOffset, 7); + newSelection = null; + + await tester.tap(find.text('Select all')); + await tester.pump(); + expect(newSelection!.baseOffset, 0); + expect(newSelection!.extentOffset, 11); + }, + variant: const TargetPlatformVariant({ TargetPlatform.windows, + TargetPlatform.linux, }), );