Skip to content

Commit

Permalink
Rework interaction gesture handling
Browse files Browse the repository at this point in the history
  • Loading branch information
knopp committed Sep 16, 2024
1 parent 36376a5 commit a4cdeac
Show file tree
Hide file tree
Showing 13 changed files with 313 additions and 654 deletions.
65 changes: 22 additions & 43 deletions super_editor/lib/src/default_editor/document_gestures_mouse.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import 'package:super_editor/src/document_operations/selection_operations.dart';
import 'package:super_editor/src/infrastructure/_logging.dart';
import 'package:super_editor/src/infrastructure/flutter/flutter_scheduler.dart';
import 'package:super_editor/src/infrastructure/multi_tap_gesture.dart';
import 'package:super_editor/src/infrastructure/sliver_hybrid_stack.dart';

import '../infrastructure/document_gestures_interaction_overrides.dart';

Expand Down Expand Up @@ -51,7 +52,8 @@ class DocumentMouseInteractor extends StatefulWidget {
this.contentTapHandler,
required this.autoScroller,
this.showDebugPaint = false,
this.child,
required this.child,
required this.fillViewport,
}) : super(key: key);

final FocusNode? focusNode;
Expand All @@ -69,12 +71,14 @@ class DocumentMouseInteractor extends StatefulWidget {
/// Auto-scrolling delegate.
final AutoScrollController autoScroller;

final bool fillViewport;

/// Paints some extra visual ornamentation to help with
/// debugging, when `true`.
final bool showDebugPaint;

/// The document to display within this [DocumentMouseInteractor].
final Widget? child;
final Widget child;

@override
State createState() => _DocumentMouseInteractorState();
Expand Down Expand Up @@ -510,16 +514,6 @@ class _DocumentMouseInteractorState extends State<DocumentMouseInteractor> with
editorGesturesLog
.info("Pan update on document, global offset: ${details.globalPosition}, device: $_panGestureDevice");

if (_panGestureDevice == PointerDeviceKind.trackpad) {
// The user dragged using two fingers on a trackpad.
// Scroll the document and keep the selection unchanged.
// We multiply by -1 because the scroll should be in the opposite
// direction of the drag, e.g., dragging up on a trackpad scrolls
// the document to downstream direction.
_scrollVertically(details.delta.dy * -1);
return;
}

setState(() {
_dragEndGlobal = details.globalPosition;

Expand All @@ -533,13 +527,6 @@ class _DocumentMouseInteractorState extends State<DocumentMouseInteractor> with

void _onPanEnd(DragEndDetails details) {
editorGesturesLog.info("Pan end on document, device: $_panGestureDevice");

if (_panGestureDevice == PointerDeviceKind.trackpad) {
// The user ended a pan gesture with two fingers on a trackpad.
// We already scrolled the document.
widget.autoScroller.goBallistic(-details.velocity.pixelsPerSecond.dy);
return;
}
_onDragEnd();
}

Expand All @@ -561,22 +548,6 @@ class _DocumentMouseInteractorState extends State<DocumentMouseInteractor> with
widget.autoScroller.disableAutoScrolling();
}

/// Scrolls the document vertically by [delta] pixels.
void _scrollVertically(double delta) {
widget.autoScroller.jumpBy(delta);
_updateDragSelection();
}

/// We prevent SingleChildScrollView from processing mouse events because
/// it scrolls by drag by default, which we don't want. However, we do
/// still want mouse scrolling. This method re-implements a primitive
/// form of mouse scrolling.
void _scrollOnMouseWheel(PointerSignalEvent event) {
if (event is PointerScrollEvent) {
_scrollVertically(event.scrollDelta.dy);
}
}

void _updateDragSelection() {
if (_dragEndGlobal == null) {
// User isn't dragging. No need to update drag selection.
Expand Down Expand Up @@ -747,14 +718,19 @@ Updating drag selection:

@override
Widget build(BuildContext context) {
return Listener(
onPointerSignal: _scrollOnMouseWheel,
onPointerHover: _onMouseMove,
child: _buildCursorStyle(
child: _buildGestureInput(
child: widget.child ?? const SizedBox(),
return SliverHybridStack(
fillViewport: widget.fillViewport,
children: [
Listener(
onPointerHover: _onMouseMove,
child: _buildCursorStyle(
child: _buildGestureInput(
child: const SizedBox(),
),
),
),
),
widget.child,
],
);
}

Expand Down Expand Up @@ -794,7 +770,10 @@ Updating drag selection:
},
),
PanGestureRecognizer: GestureRecognizerFactoryWithHandlers<PanGestureRecognizer>(
() => PanGestureRecognizer(),
() => PanGestureRecognizer(supportedDevices: {
PointerDeviceKind.mouse,
PointerDeviceKind.touch,
}),
(PanGestureRecognizer recognizer) {
recognizer
..onStart = _onPanStart
Expand Down
Loading

0 comments on commit a4cdeac

Please sign in to comment.