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
This commit is contained in:
Renzo Olivares
2024-07-31 12:58:07 -07:00
committed by GitHub
parent 0d154e55d1
commit 70bda95422
2 changed files with 122 additions and 0 deletions

View File

@@ -475,6 +475,10 @@ class SelectableRegionState extends State<SelectableRegion> 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<SelectableRegion> with TextSelectionDe
),
(TapAndPanGestureRecognizer instance) {
instance
..onTapTrackStart = _onTapTrackStart
..onTapTrackReset = _onTapTrackReset
..onTapDown = _startNewMouseSelectionGesture
..onTapUp = _handleMouseTapUp
..onDragStart = _handleMouseDragStart
@@ -559,6 +565,16 @@ class SelectableRegionState extends State<SelectableRegion> with TextSelectionDe
);
}
void _onTapTrackStart() {
_isShiftPressed = HardwareKeyboard.instance.logicalKeysPressed
.intersection(<LogicalKeyboardKey>{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<SelectableRegion> 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:

View File

@@ -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: <Widget>[
Text('How are you?'),
Text('Good, and you?'),
Text('Fine, thank you.'),
],
),
),
),
);
final RenderParagraph paragraph1 = tester.renderObject<RenderParagraph>(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<RenderParagraph>(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<RenderParagraph>(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: <Widget>[
Text('How are you?'),
Text('Good, and you?'),
Text('Fine, thank you.'),
],
),
),
),
);
final RenderParagraph paragraph1 = tester.renderObject<RenderParagraph>(find.descendant(of: find.text('How are you?'), matching: find.byType(RichText)));
final RenderParagraph paragraph2 = tester.renderObject<RenderParagraph>(find.descendant(of: find.text('Good, and you?'), matching: find.byType(RichText)));
final RenderParagraph paragraph3 = tester.renderObject<RenderParagraph>(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);