From 70bda95422b0b7d68c573057f8c30215dbe6923f Mon Sep 17 00:00:00 2001 From: Renzo Olivares Date: Wed, 31 Jul 2024 12:58:07 -0700 Subject: [PATCH] Shift + click gesture support for SelectionArea on desktop platforms (#148574) Shift + Click to move the selection end edge on desktop platforms. This is consistent with native Linux and Windows. On macOS the behavior moves the selection edge closest to the tapped position (will implement in a later PR). Part of: #129583 --- .../lib/src/widgets/selectable_region.dart | 25 +++++ .../test/widgets/selectable_region_test.dart | 97 +++++++++++++++++++ 2 files changed, 122 insertions(+) diff --git a/packages/flutter/lib/src/widgets/selectable_region.dart b/packages/flutter/lib/src/widgets/selectable_region.dart index b113afc54b..295813662d 100644 --- a/packages/flutter/lib/src/widgets/selectable_region.dart +++ b/packages/flutter/lib/src/widgets/selectable_region.dart @@ -475,6 +475,10 @@ class SelectableRegionState extends State with TextSelectionDe // gestures. + /// Whether the Shift key was pressed when the most recent [PointerDownEvent] + /// was tracked by the [BaseTapAndDragGestureRecognizer]. + bool _isShiftPressed = false; + // The position of the most recent secondary tap down event on this // SelectableRegion. Offset? _lastSecondaryTapDownPosition; @@ -548,6 +552,8 @@ class SelectableRegionState extends State with TextSelectionDe ), (TapAndPanGestureRecognizer instance) { instance + ..onTapTrackStart = _onTapTrackStart + ..onTapTrackReset = _onTapTrackReset ..onTapDown = _startNewMouseSelectionGesture ..onTapUp = _handleMouseTapUp ..onDragStart = _handleMouseDragStart @@ -559,6 +565,16 @@ class SelectableRegionState extends State with TextSelectionDe ); } + void _onTapTrackStart() { + _isShiftPressed = HardwareKeyboard.instance.logicalKeysPressed + .intersection({LogicalKeyboardKey.shiftLeft, LogicalKeyboardKey.shiftRight}) + .isNotEmpty; + } + + void _onTapTrackReset() { + _isShiftPressed = false; + } + void _initTouchGestureRecognizer() { // A [TapAndHorizontalDragGestureRecognizer] is used on non-precise pointer devices // like PointerDeviceKind.touch so [SelectableRegion] gestures do not conflict with @@ -623,6 +639,15 @@ class SelectableRegionState extends State with TextSelectionDe case TargetPlatform.linux: case TargetPlatform.windows: hideToolbar(); + // It is impossible to extend the selection when the shift key is + // pressed and the start of the selection has not been initialized. + // In this case we fallback on collapsing the selection to first + // initialize the selection. + final bool isShiftPressedValid = _isShiftPressed && _selectionDelegate.value.startSelectionPoint != null; + if (isShiftPressedValid) { + _selectEndTo(offset: details.globalPosition); + return; + } _collapseSelectionAt(offset: details.globalPosition); } case 2: diff --git a/packages/flutter/test/widgets/selectable_region_test.dart b/packages/flutter/test/widgets/selectable_region_test.dart index 430d5e74cc..47e7b0554d 100644 --- a/packages/flutter/test/widgets/selectable_region_test.dart +++ b/packages/flutter/test/widgets/selectable_region_test.dart @@ -1971,6 +1971,103 @@ void main() { await gesture.up(); }); + testWidgets('mouse shift + click holds the selection start in place and moves the end', (WidgetTester tester) async { + final FocusNode focusNode = FocusNode(); + addTearDown(focusNode.dispose); + + await tester.pumpWidget( + MaterialApp( + home: SelectableRegion( + focusNode: focusNode, + selectionControls: materialTextSelectionControls, + child: const Column( + children: [ + Text('How are you?'), + Text('Good, and you?'), + Text('Fine, thank you.'), + ], + ), + ), + ), + ); + final RenderParagraph paragraph1 = tester.renderObject(find.descendant(of: find.text('How are you?'), matching: find.byType(RichText))); + final TestGesture gesture = await tester.startGesture(textOffsetToPosition(paragraph1, 9), kind: PointerDeviceKind.mouse); + addTearDown(gesture.removePointer); + await tester.pump(); + await gesture.up(); + await tester.pumpAndSettle(); + expect(paragraph1.selections[0], const TextSelection.collapsed(offset: 9)); + + final RenderParagraph paragraph2 = tester.renderObject(find.descendant(of: find.text('Good, and you?'), matching: find.byType(RichText))); + await tester.sendKeyDownEvent(LogicalKeyboardKey.shift); + await gesture.down(textOffsetToPosition(paragraph2, 5)); + await tester.pump(); + await gesture.up(); + await tester.pumpAndSettle(); + expect(paragraph1.selections[0], const TextSelection(baseOffset: 9, extentOffset: 12)); + expect(paragraph2.selections[0], const TextSelection(baseOffset: 0, extentOffset: 5)); + + final RenderParagraph paragraph3 = tester.renderObject(find.descendant(of: find.text('Fine, thank you.'), matching: find.byType(RichText))); + await gesture.down(textOffsetToPosition(paragraph3, 13)); + await tester.pump(); + await gesture.up(); + await tester.pumpAndSettle(); + expect(paragraph1.selections[0], const TextSelection(baseOffset: 9, extentOffset: 12)); + expect(paragraph2.selections[0], const TextSelection(baseOffset: 0, extentOffset: 14)); + expect(paragraph3.selections[0], const TextSelection(baseOffset: 0, extentOffset: 13)); + + await gesture.down(textOffsetToPosition(paragraph1, 4)); + await tester.pump(); + await gesture.up(); + await tester.pumpAndSettle(); + expect(paragraph1.selections[0], const TextSelection(baseOffset: 9, extentOffset: 4)); + expect(paragraph2.selections.isEmpty, isTrue); + expect(paragraph3.selections.isEmpty, isTrue); + + await gesture.down(textOffsetToPosition(paragraph1, 0)); + await tester.pump(); + await gesture.up(); + await tester.pumpAndSettle(); + expect(paragraph1.selections[0], const TextSelection(baseOffset: 9, extentOffset: 0)); + expect(paragraph2.selections.isEmpty, isTrue); + expect(paragraph3.selections.isEmpty, isTrue); + await tester.sendKeyUpEvent(LogicalKeyboardKey.shift); + }, variant: TargetPlatformVariant.desktop()); + + testWidgets('mouse shift + click collapses the selection when it has not been initialized', (WidgetTester tester) async { + final FocusNode focusNode = FocusNode(); + addTearDown(focusNode.dispose); + + await tester.pumpWidget( + MaterialApp( + home: SelectableRegion( + focusNode: focusNode, + selectionControls: materialTextSelectionControls, + child: const Column( + children: [ + Text('How are you?'), + Text('Good, and you?'), + Text('Fine, thank you.'), + ], + ), + ), + ), + ); + final RenderParagraph paragraph1 = tester.renderObject(find.descendant(of: find.text('How are you?'), matching: find.byType(RichText))); + final RenderParagraph paragraph2 = tester.renderObject(find.descendant(of: find.text('Good, and you?'), matching: find.byType(RichText))); + final RenderParagraph paragraph3 = tester.renderObject(find.descendant(of: find.text('Fine, thank you.'), matching: find.byType(RichText))); + await tester.sendKeyDownEvent(LogicalKeyboardKey.shift); + final TestGesture gesture = await tester.startGesture(textOffsetToPosition(paragraph1, 9), kind: PointerDeviceKind.mouse); + addTearDown(gesture.removePointer); + await tester.pump(); + await gesture.up(); + await tester.pumpAndSettle(); + expect(paragraph1.selections[0], const TextSelection.collapsed(offset: 9)); + expect(paragraph2.selections.isEmpty, isTrue); + expect(paragraph3.selections.isEmpty, isTrue); + await tester.sendKeyUpEvent(LogicalKeyboardKey.shift); + }, variant: TargetPlatformVariant.desktop()); + testWidgets('collapsing selection should clear selection of all other selectables', (WidgetTester tester) async { final FocusNode focusNode = FocusNode(); addTearDown(focusNode.dispose);