Commit 74f7d9ef authored by Adam Barth's avatar Adam Barth

Move Drawer to GestureDetector

This fixes an issue in the stocks app in horizontal mode where you could both
scroll and drag the drawer at the same time.
parent 4c79cc22
......@@ -15,14 +15,14 @@ enum DragState {
}
typedef void GestureDragStartCallback();
typedef void GestureDragUpdateCallback(double scrollDelta);
typedef void GestureDragUpdateCallback(double delta);
typedef void GestureDragEndCallback(sky.Offset velocity);
typedef void GesturePanStartCallback();
typedef void GesturePanUpdateCallback(sky.Offset scrollDelta);
typedef void GesturePanUpdateCallback(sky.Offset delta);
typedef void GesturePanEndCallback(sky.Offset velocity);
typedef void _GesturePolymorphicUpdateCallback<T>(T scrollDelta);
typedef void _GesturePolymorphicUpdateCallback<T>(T delta);
int _eventTime(sky.PointerEvent event) => (event.timeStamp * 1000.0).toInt(); // microseconds
......@@ -121,8 +121,7 @@ class VerticalDragGestureRecognizer extends _DragGestureRecognizer<double> {
}) : super(router: router, onStart: onStart, onUpdate: onUpdate, onEnd: onEnd);
double get _initialPendingDragDelta => 0.0;
// Notice that we negate dy because scroll offsets go in the opposite direction.
double _getDragDelta(sky.PointerEvent event) => -event.dy;
double _getDragDelta(sky.PointerEvent event) => event.dy;
bool get _hasSufficientPendingDragDeltaToAccept => _pendingDragDelta.abs() > kTouchSlop;
}
......@@ -135,7 +134,7 @@ class HorizontalDragGestureRecognizer extends _DragGestureRecognizer<double> {
}) : super(router: router, onStart: onStart, onUpdate: onUpdate, onEnd: onEnd);
double get _initialPendingDragDelta => 0.0;
double _getDragDelta(sky.PointerEvent event) => -event.dx;
double _getDragDelta(sky.PointerEvent event) => event.dx;
bool get _hasSufficientPendingDragDeltaToAccept => _pendingDragDelta.abs() > kTouchSlop;
}
......@@ -148,8 +147,7 @@ class PanGestureRecognizer extends _DragGestureRecognizer<sky.Offset> {
}) : super(router: router, onStart: onStart, onUpdate: onUpdate, onEnd: onEnd);
sky.Offset get _initialPendingDragDelta => sky.Offset.zero;
// Notice that we negate dy because scroll offsets go in the opposite direction.
sky.Offset _getDragDelta(sky.PointerEvent event) => new sky.Offset(event.dx, -event.dy);
sky.Offset _getDragDelta(sky.PointerEvent event) => new sky.Offset(event.dx, event.dy);
bool get _hasSufficientPendingDragDeltaToAccept {
return _pendingDragDelta.dx.abs() > kTouchSlop || _pendingDragDelta.dy.abs() > kTouchSlop;
}
......
......@@ -119,7 +119,7 @@ class Dismissable extends StatefulComponent {
});
}
void _handleDragUpdate(double scrollOffset) {
void _handleDragUpdate(double delta) {
if (!_isActive || _fadePerformance.isAnimating)
return;
......@@ -127,19 +127,19 @@ class Dismissable extends StatefulComponent {
switch(direction) {
case DismissDirection.horizontal:
case DismissDirection.vertical:
_dragExtent -= scrollOffset;
_dragExtent += delta;
break;
case DismissDirection.up:
case DismissDirection.left:
if (_dragExtent - scrollOffset < 0)
_dragExtent -= scrollOffset;
if (_dragExtent + delta < 0)
_dragExtent += delta;
break;
case DismissDirection.down:
case DismissDirection.right:
if (_dragExtent - scrollOffset > 0)
_dragExtent -= scrollOffset;
if (_dragExtent + delta > 0)
_dragExtent += delta;
break;
}
......
......@@ -109,13 +109,11 @@ class Drawer extends StatefulComponent {
)
);
return new Listener(
child: new Stack([ mask, content ]),
onPointerDown: handlePointerDown,
onPointerMove: handlePointerMove,
onPointerUp: handlePointerUp,
onPointerCancel: handlePointerCancel,
onGestureFlingStart: handleFlingStart
return new GestureDetector(
onHorizontalDragStart: _performance.stop,
onHorizontalDragUpdate: _handleDragUpdate,
onHorizontalDragEnd: _handleDragEnd,
child: new Stack([ mask, content ])
);
}
......@@ -132,41 +130,17 @@ class Drawer extends StatefulComponent {
void _settle() { _isMostlyClosed ? _performance.reverse() : _performance.play(); }
// TODO(mpcomplete): Figure out how to generalize these handlers on a
// "PannableThingy" interface.
EventDisposition handlePointerDown(_) {
_performance.stop();
return EventDisposition.processed;
}
EventDisposition handlePointerMove(sky.PointerEvent event) {
if (_performance.isAnimating)
return EventDisposition.ignored;
_performance.progress += event.dx / _kWidth;
return EventDisposition.processed;
void _handleDragUpdate(double delta) {
_performance.progress += delta / _kWidth;
}
EventDisposition handlePointerUp(_) {
if (!_performance.isAnimating) {
void _handleDragEnd(Offset velocity) {
if (velocity.dx.abs() >= _kMinFlingVelocity) {
_performance.fling(velocity: velocity.dx * _kFlingVelocityScale);
return;
} else {
_settle();
return EventDisposition.processed;
}
return EventDisposition.ignored;
}
EventDisposition handlePointerCancel(_) {
if (!_performance.isAnimating) {
_settle();
return EventDisposition.processed;
}
return EventDisposition.ignored;
}
EventDisposition handleFlingStart(event) {
if (event.velocityX.abs() >= _kMinFlingVelocity) {
_performance.fling(velocity: event.velocityX * _kFlingVelocityScale);
return EventDisposition.processed;
}
return EventDisposition.ignored;
}
}
......@@ -83,7 +83,7 @@ abstract class Scrollable extends StatefulComponent {
GestureDragUpdateCallback _getDragUpdateHandler(ScrollDirection direction) {
if (scrollDirection != direction || !scrollBehavior.isScrollable)
return null;
return scrollBy;
return _handleDragUpdate;
}
GestureDragEndCallback _getDragEndHandler(ScrollDirection direction) {
......@@ -181,6 +181,12 @@ abstract class Scrollable extends StatefulComponent {
return EventDisposition.processed;
}
void _handleDragUpdate(double delta) {
// We negate the delta here because a positive scroll offset moves the
// the content up (or to the left) rather than down (or the right).
scrollBy(-delta);
}
void _handleDragEnd(Offset velocity) {
if (velocity != Offset.zero) {
_startToEndAnimation(velocity: _scrollVelocity(velocity));
......
......@@ -53,14 +53,14 @@ void main() {
router.route(pointer.move(new Point(20.0, 20.0)));
expect(didStartPan, isTrue);
didStartPan = false;
expect(updatedScrollDelta, new sky.Offset(10.0, -10.0));
expect(updatedScrollDelta, new sky.Offset(10.0, 10.0));
updatedScrollDelta = null;
expect(didEndPan, isFalse);
expect(didTap, isFalse);
router.route(pointer.move(new Point(20.0, 25.0)));
expect(didStartPan, isFalse);
expect(updatedScrollDelta, new sky.Offset(0.0, -5.0));
expect(updatedScrollDelta, new sky.Offset(0.0, 5.0));
updatedScrollDelta = null;
expect(didEndPan, isFalse);
expect(didTap, isFalse);
......
......@@ -43,7 +43,7 @@ void main() {
Point secondLocation = new Point(10.0, 9.0);
tester.dispatchEvent(pointer.move(secondLocation), firstLocation);
expect(didStartDrag, isFalse);
expect(updatedDragDelta, 1.0);
expect(updatedDragDelta, -1.0);
updatedDragDelta = null;
expect(didEndDrag, isFalse);
......@@ -86,6 +86,6 @@ void main() {
tester.dispatchEvent(pointer.up(), downLocation);
expect(gestureCount, 2);
expect(dragDistance, -20.0);
expect(dragDistance, 20.0);
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment