From 3f1f0aa592799d7a6f9bcd7d8e3938e1ba0b787b Mon Sep 17 00:00:00 2001 From: Anindya Chatterjee <696662+anidotnet@users.noreply.github.com> Date: Tue, 5 Dec 2023 02:02:21 +0530 Subject: [PATCH] Enable TapRegion to detect all mouse button click (#136799) With this PR, TapRegion would be able to detect any kind of mouse button events. Earlier, TapRegion used to detect only left mouse button event. Fixes #136706 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. [Contributor Guide]: https://github.com/flutter/flutter/wiki/Tree-hygiene#overview [Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene [test-exempt]: https://github.com/flutter/flutter/wiki/Tree-hygiene#tests [Flutter Style Guide]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo [Features we expect every widget to implement]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/wiki/Chat --- .../flutter/lib/src/widgets/tap_region.dart | 2 +- .../flutter/test/widgets/tap_region_test.dart | 574 ++++++++++++++++++ 2 files changed, 575 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/widgets/tap_region.dart b/packages/flutter/lib/src/widgets/tap_region.dart index 721b8ee480..f4bdf8d89e 100644 --- a/packages/flutter/lib/src/widgets/tap_region.dart +++ b/packages/flutter/lib/src/widgets/tap_region.dart @@ -234,7 +234,7 @@ class RenderTapRegionSurface extends RenderProxyBoxWithHitTestBehavior implement return true; }(), 'A RenderTapRegion was registered when it was disabled.'); - if (event is! PointerDownEvent || event.buttons != kPrimaryButton) { + if (event is! PointerDownEvent) { return; } diff --git a/packages/flutter/test/widgets/tap_region_test.dart b/packages/flutter/test/widgets/tap_region_test.dart index 4f31a9e845..c53983b778 100644 --- a/packages/flutter/test/widgets/tap_region_test.dart +++ b/packages/flutter/test/widgets/tap_region_test.dart @@ -480,4 +480,578 @@ void main() { ])); tappedOutside.clear(); }); + + testWidgetsWithLeakTracking('TapRegionSurface detects outside right click', (WidgetTester tester) async { + final Set tappedOutside = {}; + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Column( + children: [ + const Text('Outside Surface'), + TapRegionSurface( + child: Row( + children: [ + const Text('Outside'), + TapRegion( + onTapOutside: (PointerEvent event) { + tappedOutside.add('No Group'); + }, + child: const Text('No Group'), + ), + TapRegion( + groupId: 1, + onTapOutside: (PointerEvent event) { + tappedOutside.add('Group 1 A'); + }, + child: const Text('Group 1 A'), + ), + TapRegion( + groupId: 1, + onTapOutside: (PointerEvent event) { + tappedOutside.add('Group 1 B'); + }, + child: const Text('Group 1 B'), + ), + ], + ), + ), + ], + ), + ), + ); + + await tester.pump(); + + Future click(Finder finder) async { + final TestGesture gesture = await tester.startGesture( + tester.getCenter(finder), + kind: PointerDeviceKind.mouse, + buttons: kSecondaryButton, + ); + await gesture.up(); + await gesture.removePointer(); + } + + expect(tappedOutside, isEmpty); + + await click(find.text('No Group')); + expect( + tappedOutside, + unorderedEquals({ + 'Group 1 A', + 'Group 1 B', + })); + tappedOutside.clear(); + + await click(find.text('Group 1 A')); + expect( + tappedOutside, + equals({ + 'No Group', + })); + tappedOutside.clear(); + + await click(find.text('Group 1 B')); + expect( + tappedOutside, + equals({ + 'No Group', + })); + tappedOutside.clear(); + + await click(find.text('Outside')); + expect( + tappedOutside, + unorderedEquals({ + 'No Group', + 'Group 1 A', + 'Group 1 B', + })); + tappedOutside.clear(); + + await click(find.text('Outside Surface')); + expect(tappedOutside, isEmpty); + }); + + testWidgetsWithLeakTracking('TapRegionSurface detects outside middle click', (WidgetTester tester) async { + final Set tappedOutside = {}; + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Column( + children: [ + const Text('Outside Surface'), + TapRegionSurface( + child: Row( + children: [ + const Text('Outside'), + TapRegion( + onTapOutside: (PointerEvent event) { + tappedOutside.add('No Group'); + }, + child: const Text('No Group'), + ), + TapRegion( + groupId: 1, + onTapOutside: (PointerEvent event) { + tappedOutside.add('Group 1 A'); + }, + child: const Text('Group 1 A'), + ), + TapRegion( + groupId: 1, + onTapOutside: (PointerEvent event) { + tappedOutside.add('Group 1 B'); + }, + child: const Text('Group 1 B'), + ), + ], + ), + ), + ], + ), + ), + ); + + await tester.pump(); + + Future click(Finder finder) async { + final TestGesture gesture = await tester.startGesture( + tester.getCenter(finder), + kind: PointerDeviceKind.mouse, + buttons: kTertiaryButton, + ); + await gesture.up(); + await gesture.removePointer(); + } + + expect(tappedOutside, isEmpty); + + await click(find.text('No Group')); + expect( + tappedOutside, + unorderedEquals({ + 'Group 1 A', + 'Group 1 B', + })); + tappedOutside.clear(); + + await click(find.text('Group 1 A')); + expect( + tappedOutside, + equals({ + 'No Group', + })); + tappedOutside.clear(); + + await click(find.text('Group 1 B')); + expect( + tappedOutside, + equals({ + 'No Group', + })); + tappedOutside.clear(); + + await click(find.text('Outside')); + expect( + tappedOutside, + unorderedEquals({ + 'No Group', + 'Group 1 A', + 'Group 1 B', + })); + tappedOutside.clear(); + + await click(find.text('Outside Surface')); + expect(tappedOutside, isEmpty); + }); + + testWidgetsWithLeakTracking('TapRegionSurface consumes outside right click when asked', (WidgetTester tester) async { + final Set tappedOutside = {}; + int propagatedTaps = 0; + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Column( + children: [ + const Text('Outside Surface'), + TapRegionSurface( + child: Row( + children: [ + GestureDetector( + onTap: () { + propagatedTaps += 1; + }, + child: const Text('Outside'), + ), + TapRegion( + consumeOutsideTaps: true, + onTapOutside: (PointerEvent event) { + tappedOutside.add('No Group'); + }, + child: const Text('No Group'), + ), + TapRegion( + groupId: 1, + onTapOutside: (PointerEvent event) { + tappedOutside.add('Group 1 A'); + }, + child: const Text('Group 1 A'), + ), + TapRegion( + groupId: 1, + consumeOutsideTaps: true, + onTapOutside: (PointerEvent event) { + tappedOutside.add('Group 1 B'); + }, + child: const Text('Group 1 B'), + ), + ], + ), + ), + ], + ), + ), + ); + + await tester.pump(); + + Future click(Finder finder) async { + final TestGesture gesture = await tester.startGesture( + tester.getCenter(finder), + kind: PointerDeviceKind.mouse, + buttons: kSecondaryButton + ); + await gesture.up(); + await gesture.removePointer(); + } + + expect(tappedOutside, isEmpty); + expect(propagatedTaps, equals(0)); + + await click(find.text('No Group')); + expect( + tappedOutside, + unorderedEquals({ + 'Group 1 A', + 'Group 1 B', + })); + expect(propagatedTaps, equals(0)); + tappedOutside.clear(); + + await click(find.text('Group 1 A')); + expect( + tappedOutside, + equals({ + 'No Group', + })); + expect(propagatedTaps, equals(0)); + tappedOutside.clear(); + + await click(find.text('Group 1 B')); + expect( + tappedOutside, + equals({ + 'No Group', + })); + expect(propagatedTaps, equals(0)); + tappedOutside.clear(); + + await click(find.text('Outside')); + expect( + tappedOutside, + unorderedEquals({ + 'No Group', + 'Group 1 A', + 'Group 1 B', + })); + expect(propagatedTaps, equals(0)); + tappedOutside.clear(); + + await click(find.text('Outside Surface')); + expect(tappedOutside, isEmpty); + }); + + testWidgetsWithLeakTracking('TapRegionSurface consumes outside middle click when asked', (WidgetTester tester) async { + final Set tappedOutside = {}; + int propagatedTaps = 0; + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Column( + children: [ + const Text('Outside Surface'), + TapRegionSurface( + child: Row( + children: [ + GestureDetector( + onTap: () { + propagatedTaps += 1; + }, + child: const Text('Outside'), + ), + TapRegion( + consumeOutsideTaps: true, + onTapOutside: (PointerEvent event) { + tappedOutside.add('No Group'); + }, + child: const Text('No Group'), + ), + TapRegion( + groupId: 1, + onTapOutside: (PointerEvent event) { + tappedOutside.add('Group 1 A'); + }, + child: const Text('Group 1 A'), + ), + TapRegion( + groupId: 1, + consumeOutsideTaps: true, + onTapOutside: (PointerEvent event) { + tappedOutside.add('Group 1 B'); + }, + child: const Text('Group 1 B'), + ), + ], + ), + ), + ], + ), + ), + ); + + await tester.pump(); + + Future click(Finder finder) async { + final TestGesture gesture = await tester.startGesture( + tester.getCenter(finder), + kind: PointerDeviceKind.mouse, + buttons: kTertiaryButton, + ); + await gesture.up(); + await gesture.removePointer(); + } + + expect(tappedOutside, isEmpty); + expect(propagatedTaps, equals(0)); + + await click(find.text('No Group')); + expect( + tappedOutside, + unorderedEquals({ + 'Group 1 A', + 'Group 1 B', + })); + expect(propagatedTaps, equals(0)); + tappedOutside.clear(); + + await click(find.text('Group 1 A')); + expect( + tappedOutside, + equals({ + 'No Group', + })); + expect(propagatedTaps, equals(0)); + tappedOutside.clear(); + + await click(find.text('Group 1 B')); + expect( + tappedOutside, + equals({ + 'No Group', + })); + expect(propagatedTaps, equals(0)); + tappedOutside.clear(); + + await click(find.text('Outside')); + expect( + tappedOutside, + unorderedEquals({ + 'No Group', + 'Group 1 A', + 'Group 1 B', + })); + expect(propagatedTaps, equals(0)); + tappedOutside.clear(); + + await click(find.text('Outside Surface')); + expect(tappedOutside, isEmpty); + }); + + testWidgetsWithLeakTracking('TapRegionSurface detects inside right click', (WidgetTester tester) async { + final Set tappedInside = {}; + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Column( + children: [ + const Text('Outside Surface'), + TapRegionSurface( + child: Row( + children: [ + const Text('Outside'), + TapRegion( + onTapInside: (PointerEvent event) { + tappedInside.add('No Group'); + }, + child: const Text('No Group'), + ), + TapRegion( + groupId: 1, + onTapInside: (PointerEvent event) { + tappedInside.add('Group 1 A'); + }, + child: const Text('Group 1 A'), + ), + TapRegion( + groupId: 1, + onTapInside: (PointerEvent event) { + tappedInside.add('Group 1 B'); + }, + child: const Text('Group 1 B'), + ), + ], + ), + ), + ], + ), + ), + ); + + await tester.pump(); + + Future click(Finder finder) async { + final TestGesture gesture = await tester.startGesture( + tester.getCenter(finder), + kind: PointerDeviceKind.mouse, + buttons: kSecondaryButton, + ); + await gesture.up(); + await gesture.removePointer(); + } + + expect(tappedInside, isEmpty); + + await click(find.text('No Group')); + expect( + tappedInside, + unorderedEquals({ + 'No Group', + })); + tappedInside.clear(); + + await click(find.text('Group 1 A')); + expect( + tappedInside, + equals({ + 'Group 1 A', + 'Group 1 B', + })); + tappedInside.clear(); + + await click(find.text('Group 1 B')); + expect( + tappedInside, + equals({ + 'Group 1 A', + 'Group 1 B', + })); + tappedInside.clear(); + + await click(find.text('Outside')); + expect(tappedInside, isEmpty); + tappedInside.clear(); + + await click(find.text('Outside Surface')); + expect(tappedInside, isEmpty); + }); + + testWidgetsWithLeakTracking('TapRegionSurface detects inside middle click', (WidgetTester tester) async { + final Set tappedInside = {}; + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Column( + children: [ + const Text('Outside Surface'), + TapRegionSurface( + child: Row( + children: [ + const Text('Outside'), + TapRegion( + onTapInside: (PointerEvent event) { + tappedInside.add('No Group'); + }, + child: const Text('No Group'), + ), + TapRegion( + groupId: 1, + onTapInside: (PointerEvent event) { + tappedInside.add('Group 1 A'); + }, + child: const Text('Group 1 A'), + ), + TapRegion( + groupId: 1, + onTapInside: (PointerEvent event) { + tappedInside.add('Group 1 B'); + }, + child: const Text('Group 1 B'), + ), + ], + ), + ), + ], + ), + ), + ); + + await tester.pump(); + + Future click(Finder finder) async { + final TestGesture gesture = await tester.startGesture( + tester.getCenter(finder), + kind: PointerDeviceKind.mouse, + buttons: kTertiaryButton, + ); + await gesture.up(); + await gesture.removePointer(); + } + + expect(tappedInside, isEmpty); + + await click(find.text('No Group')); + expect( + tappedInside, + unorderedEquals({ + 'No Group', + })); + tappedInside.clear(); + + await click(find.text('Group 1 A')); + expect( + tappedInside, + equals({ + 'Group 1 A', + 'Group 1 B', + })); + tappedInside.clear(); + + await click(find.text('Group 1 B')); + expect( + tappedInside, + equals({ + 'Group 1 A', + 'Group 1 B', + })); + tappedInside.clear(); + + await click(find.text('Outside')); + expect(tappedInside, isEmpty); + tappedInside.clear(); + + await click(find.text('Outside Surface')); + expect(tappedInside, isEmpty); + }); }