Skip to content

Commit

Permalink
feat: HoverRegion should prevent onExit while scrolling (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
knopp authored Nov 16, 2023
1 parent 37c4400 commit d459521
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
34 changes: 21 additions & 13 deletions lib/src/hover_region.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,24 @@ class _HoverRegionState extends State<HoverRegion> {
bool get _preventNotifications => _scrolling;

void _flush() {
if (_pendingEnter != null && !_preventNotifications) {
widget.onEnter?.call(_pendingEnter!);
_inside = true;
if (_pendingHover != null) {
widget.onHover?.call(_pendingHover!);
if (!_preventNotifications) {
assert(_pendingEnter == null || _pendingExit == null);
if (_pendingEnter != null) {
widget.onEnter?.call(_pendingEnter!);
_inside = true;
if (_pendingHover != null) {
widget.onHover?.call(_pendingHover!);
}
_pendingEnter = null;
_pendingHover = null;
} else if (_pendingExit != null) {
widget.onExit?.call(_pendingExit!);
_inside = false;
_pendingExit = null;
}
_pendingEnter = null;
_pendingHover = null;
// Refresh mouse cursor
setState(() {});
}
// Refresh mouse cursor
setState(() {});
}

void _resetScrollPosition() {
Expand Down Expand Up @@ -261,10 +268,8 @@ class _HoverRegionState extends State<HoverRegion> {
return;
}

if (_pendingExitPointer == event.pointer) {
_pendingExitPointer = null;
_pendingExit = null;
}
_pendingExitPointer = null;
_pendingExit = null;
if (!_inside && event.down) {
_ignoredEnterPointer = event.pointer;
} else if (!_inside) {
Expand Down Expand Up @@ -327,6 +332,9 @@ class _HoverRegionState extends State<HoverRegion> {
if (event.down) {
_pendingExitPointer = event.pointer;
_pendingExit = event;
} else if (_preventNotifications) {
_pendingExitPointer = null;
_pendingExit = event;
} else {
_inside = false;
widget.onExit?.call(event);
Expand Down
12 changes: 6 additions & 6 deletions test/hover_region_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,11 @@ void main() {

await tester.sendEventToBinding(pointer.scroll(const Offset(0, 100)));
await tester.pumpAndSettle();
expect(region1.isInside, false);
expect(region1.isInside, true);
expect(region2.isInside, false);

async.elapse(const Duration(milliseconds: 50));
expect(region1.isInside, false);
expect(region1.isInside, true);
expect(region2.isInside, false);

async.elapse(const Duration(milliseconds: 50));
Expand Down Expand Up @@ -284,26 +284,26 @@ void main() {

await tester.sendEventToBinding(pointer.scroll(const Offset(0, 100)));
await tester.pumpAndSettle();
expect(region1.isInside, false);
expect(region1.isInside, true);
expect(region2.isInside, false);

async.elapse(const Duration(milliseconds: 50));
expect(region1.isInside, false);
expect(region1.isInside, true);
expect(region2.isInside, false);

for (int i = 0; i < 5; ++i) {
await tester.sendEventToBinding(pointer.scroll(const Offset(0, 1)));
await tester.pumpAndSettle();

async.elapse(const Duration(milliseconds: 50));
expect(region1.isInside, false);
expect(region1.isInside, true);
expect(region2.isInside, false);

await tester.sendEventToBinding(pointer.scroll(const Offset(0, 1)));
await tester.pumpAndSettle();

async.elapse(const Duration(milliseconds: 50));
expect(region1.isInside, false);
expect(region1.isInside, true);
expect(region2.isInside, false);
}

Expand Down

0 comments on commit d459521

Please sign in to comment.