[go: nahoru, domu]

Skip to content

Commit

Permalink
Fix Tooltip show delay when mouse moves to one Tooltip from another (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
LongCatIsLooong committed Jan 17, 2024
1 parent a10712e commit da20edf
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 11 deletions.
21 changes: 10 additions & 11 deletions packages/flutter/lib/src/material/tooltip.dart
Original file line number Diff line number Diff line change
Expand Up @@ -665,19 +665,18 @@ class TooltipState extends State<Tooltip> with SingleTickerProviderStateMixin {
// tooltip is the first to be hit in the widget tree's hit testing order.
// See also _ExclusiveMouseRegion for the exact behavior.
_activeHoveringPointerDevices.add(event.device);
final List<TooltipState> openedTooltips = Tooltip._openedTooltips.toList();
bool otherTooltipsDismissed = false;
for (final TooltipState tooltip in openedTooltips) {
// Dismiss other open tooltips unless they're kept visible by other mice.
// The mouse tracker implementation always dispatches all `onExit` events
// before dispatching any `onEnter` events, so `event.device` must have
// already been removed from _activeHoveringPointerDevices of the tooltips
// that are no longer being hovered over.
final List<TooltipState> tooltipsToDismiss = Tooltip._openedTooltips
.where((TooltipState tooltip) => tooltip._activeHoveringPointerDevices.isEmpty).toList();
for (final TooltipState tooltip in tooltipsToDismiss) {
assert(tooltip.mounted);
final Set<int> hoveringDevices = tooltip._activeHoveringPointerDevices;
final bool shouldDismiss = tooltip != this
&& (hoveringDevices.length == 1 && hoveringDevices.single == event.device);
if (shouldDismiss) {
otherTooltipsDismissed = true;
tooltip._scheduleDismissTooltip(withDelay: Duration.zero);
}
tooltip._scheduleDismissTooltip(withDelay: Duration.zero);
}
_scheduleShowTooltip(withDelay: otherTooltipsDismissed ? Duration.zero : _waitDuration);
_scheduleShowTooltip(withDelay: tooltipsToDismiss.isNotEmpty ? Duration.zero : _waitDuration);
}

void _handleMouseExit(PointerExitEvent event) {
Expand Down
54 changes: 54 additions & 0 deletions packages/flutter/test/material/tooltip_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1508,6 +1508,60 @@ void main() {
expect(find.text(tooltipText), findsNothing);
});

// Regression test for https://github.com/flutter/flutter/issues/141644.
// This allows the user to quickly explore the UI via tooltips.
testWidgets('Tooltip shows without delay when the mouse moves from another tooltip', (WidgetTester tester) async {
const Duration waitDuration = Durations.extralong1;
final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
addTearDown(() async {
return gesture.removePointer();
});
await gesture.addPointer();
await gesture.moveTo(const Offset(1.0, 1.0));
await tester.pump();
await gesture.moveTo(Offset.zero);

await tester.pumpWidget(
const MaterialApp(
home: Column(
children: <Widget>[
Tooltip(
message: 'first tooltip',
waitDuration: waitDuration,
child: SizedBox(
width: 100.0,
height: 100.0,
),
),
Tooltip(
message: 'last tooltip',
waitDuration: waitDuration,
child: SizedBox(
width: 100.0,
height: 100.0,
),
),
],
),
),
);

await gesture.moveTo(Offset.zero);
await tester.pump();
await gesture.moveTo(tester.getCenter(find.byType(Tooltip).first));
await tester.pump();
// Wait for the first tooltip to appear.
await tester.pump(waitDuration);
expect(find.text('first tooltip'), findsOneWidget);
expect(find.text('last tooltip'), findsNothing);

// Move to the second tooltip and expect it to show up immediately.
await gesture.moveTo(tester.getCenter(find.byType(Tooltip).last));
await tester.pump();
expect(find.text('first tooltip'), findsNothing);
expect(find.text('last tooltip'), findsOneWidget);
});

testWidgets('Tooltip text is also hoverable', (WidgetTester tester) async {
const Duration waitDuration = Duration.zero;
final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
Expand Down

0 comments on commit da20edf

Please sign in to comment.