Skip to content

Commit da20edf

Browse files
Fix Tooltip show delay when mouse moves to one Tooltip from another (#141656)
Fixes flutter/flutter#141644
1 parent a10712e commit da20edf

File tree

2 files changed

+64
-11
lines changed

2 files changed

+64
-11
lines changed

packages/flutter/lib/src/material/tooltip.dart

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -665,19 +665,18 @@ class TooltipState extends State<Tooltip> with SingleTickerProviderStateMixin {
665665
// tooltip is the first to be hit in the widget tree's hit testing order.
666666
// See also _ExclusiveMouseRegion for the exact behavior.
667667
_activeHoveringPointerDevices.add(event.device);
668-
final List<TooltipState> openedTooltips = Tooltip._openedTooltips.toList();
669-
bool otherTooltipsDismissed = false;
670-
for (final TooltipState tooltip in openedTooltips) {
668+
// Dismiss other open tooltips unless they're kept visible by other mice.
669+
// The mouse tracker implementation always dispatches all `onExit` events
670+
// before dispatching any `onEnter` events, so `event.device` must have
671+
// already been removed from _activeHoveringPointerDevices of the tooltips
672+
// that are no longer being hovered over.
673+
final List<TooltipState> tooltipsToDismiss = Tooltip._openedTooltips
674+
.where((TooltipState tooltip) => tooltip._activeHoveringPointerDevices.isEmpty).toList();
675+
for (final TooltipState tooltip in tooltipsToDismiss) {
671676
assert(tooltip.mounted);
672-
final Set<int> hoveringDevices = tooltip._activeHoveringPointerDevices;
673-
final bool shouldDismiss = tooltip != this
674-
&& (hoveringDevices.length == 1 && hoveringDevices.single == event.device);
675-
if (shouldDismiss) {
676-
otherTooltipsDismissed = true;
677-
tooltip._scheduleDismissTooltip(withDelay: Duration.zero);
678-
}
677+
tooltip._scheduleDismissTooltip(withDelay: Duration.zero);
679678
}
680-
_scheduleShowTooltip(withDelay: otherTooltipsDismissed ? Duration.zero : _waitDuration);
679+
_scheduleShowTooltip(withDelay: tooltipsToDismiss.isNotEmpty ? Duration.zero : _waitDuration);
681680
}
682681

683682
void _handleMouseExit(PointerExitEvent event) {

packages/flutter/test/material/tooltip_test.dart

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,6 +1508,60 @@ void main() {
15081508
expect(find.text(tooltipText), findsNothing);
15091509
});
15101510

1511+
// Regression test for https://github.com/flutter/flutter/issues/141644.
1512+
// This allows the user to quickly explore the UI via tooltips.
1513+
testWidgets('Tooltip shows without delay when the mouse moves from another tooltip', (WidgetTester tester) async {
1514+
const Duration waitDuration = Durations.extralong1;
1515+
final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
1516+
addTearDown(() async {
1517+
return gesture.removePointer();
1518+
});
1519+
await gesture.addPointer();
1520+
await gesture.moveTo(const Offset(1.0, 1.0));
1521+
await tester.pump();
1522+
await gesture.moveTo(Offset.zero);
1523+
1524+
await tester.pumpWidget(
1525+
const MaterialApp(
1526+
home: Column(
1527+
children: <Widget>[
1528+
Tooltip(
1529+
message: 'first tooltip',
1530+
waitDuration: waitDuration,
1531+
child: SizedBox(
1532+
width: 100.0,
1533+
height: 100.0,
1534+
),
1535+
),
1536+
Tooltip(
1537+
message: 'last tooltip',
1538+
waitDuration: waitDuration,
1539+
child: SizedBox(
1540+
width: 100.0,
1541+
height: 100.0,
1542+
),
1543+
),
1544+
],
1545+
),
1546+
),
1547+
);
1548+
1549+
await gesture.moveTo(Offset.zero);
1550+
await tester.pump();
1551+
await gesture.moveTo(tester.getCenter(find.byType(Tooltip).first));
1552+
await tester.pump();
1553+
// Wait for the first tooltip to appear.
1554+
await tester.pump(waitDuration);
1555+
expect(find.text('first tooltip'), findsOneWidget);
1556+
expect(find.text('last tooltip'), findsNothing);
1557+
1558+
// Move to the second tooltip and expect it to show up immediately.
1559+
await gesture.moveTo(tester.getCenter(find.byType(Tooltip).last));
1560+
await tester.pump();
1561+
expect(find.text('first tooltip'), findsNothing);
1562+
expect(find.text('last tooltip'), findsOneWidget);
1563+
});
1564+
15111565
testWidgets('Tooltip text is also hoverable', (WidgetTester tester) async {
15121566
const Duration waitDuration = Duration.zero;
15131567
final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);

0 commit comments

Comments
 (0)