Skip to content

Commit

Permalink
[SuperEditor][SuperReader] Do not stop scroll momentum on mouse move (R…
Browse files Browse the repository at this point in the history
…esolves #1946) (#1966)
  • Loading branch information
angelosilvestre committed Apr 29, 2024
1 parent bb92266 commit 1085a3b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -577,13 +577,6 @@ class _DocumentMouseInteractorState extends State<DocumentMouseInteractor> with
}
}

/// Beginning with Flutter 3.3.3, we are responsible for starting and
/// stopping scroll momentum. This method cancels any scroll momentum
/// in our scroll controller.
void _cancelScrollMomentum() {
widget.autoScroller.goIdle();
}

void _updateDragSelection() {
if (_dragEndGlobal == null) {
// User isn't dragging. No need to update drag selection.
Expand Down Expand Up @@ -729,7 +722,6 @@ Updating drag selection:
}

void _onMouseMove(PointerHoverEvent event) {
_cancelScrollMomentum();
_updateMouseCursor(event.position);
_lastHoverOffset = event.position;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ class _ReadOnlyDocumentMouseInteractorState extends State<ReadOnlyDocumentMouseI
}

void _onMouseMove(PointerHoverEvent event) {
_cancelScrollMomentum();
_updateMouseCursor(event.position);
_lastHoverOffset = event.position;
}
Expand Down Expand Up @@ -464,13 +463,6 @@ class _ReadOnlyDocumentMouseInteractorState extends State<ReadOnlyDocumentMouseI
_updateDragSelection();
}

/// Beginning with Flutter 3.3.3, we are responsible for starting and
/// stopping scroll momentum. This method cancels any scroll momentum
/// in our scroll controller.
void _cancelScrollMomentum() {
widget.autoScroller.goIdle();
}

void _updateDragSelection() {
if (_dragEndGlobal == null) {
// User isn't dragging. No need to update drag selection.
Expand Down
31 changes: 31 additions & 0 deletions super_editor/test/super_editor/supereditor_scrolling_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,37 @@ void main() {
expect(SuperEditorInspector.findDocumentSelection(), isNull);
});

testWidgetsOnArbitraryDesktop("does not stop momentum on mouse move", (tester) async {
final scrollController = ScrollController();

// Pump an editor with a small size to make it scrollable.
await tester //
.createDocument() //
.withLongDoc() //
.withScrollController(scrollController) //
.withEditorSize(const Size(300, 300))
.pump();

// Fling scroll with the trackpad to generate momentum.
await tester.trackpadFling(
find.byType(SuperEditor),
const Offset(0.0, -300),
300.0,
);

final scrollOffsetInMiddleOfMomentum = scrollController.offset;

// Move the mouse around.
final gesture = await tester.createGesture();
await gesture.moveTo(tester.getTopLeft(find.byType(SuperEditor)));

// Let any momentum run.
await tester.pumpAndSettle();

// Ensure that the momentum didn't stop due to mouse movement.
expect(scrollOffsetInMiddleOfMomentum, lessThan(scrollController.offset));
});

testWidgetsOnAndroid("doesn't overscroll when dragging down", (tester) async {
final scrollController = ScrollController();

Expand Down
31 changes: 31 additions & 0 deletions super_editor/test/super_reader/super_reader_scrolling_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,37 @@ void main() {
expect(scrollController.offset, scrollController.position.maxScrollExtent);
});

testWidgetsOnArbitraryDesktop("does not stop momentum on mouse move", (tester) async {
final scrollController = ScrollController();

// Pump a reader with a small size to make it scrollable.
await tester //
.createDocument() //
.withCustomContent(longTextDoc()) //
.withScrollController(scrollController) //
.withEditorSize(const Size(300, 300))
.pump();

// Fling scroll with the trackpad to generate momentum.
await tester.trackpadFling(
find.byType(SuperReader),
const Offset(0.0, -300),
300.0,
);

final scrollOffsetInMiddleOfMomentum = scrollController.offset;

// Move the mouse around.
final gesture = await tester.createGesture();
await gesture.moveTo(tester.getTopLeft(find.byType(SuperReader)));

// Let any momentum run.
await tester.pumpAndSettle();

// Ensure that the momentum didn't stop due to mouse movement.
expect(scrollOffsetInMiddleOfMomentum, lessThan(scrollController.offset));
});

group("when all content fits in the viewport", () {
testWidgetsOnDesktop(
"trackpad doesn't scroll content",
Expand Down

0 comments on commit 1085a3b

Please sign in to comment.