Commit 4eb2364b authored by Matthew Jones's avatar Matthew Jones Committed by Commit Bot

ToolbarSwipeHandler now uses more restricted gestures

Swipes left and right on the toolbar are very easily triggered in the
ToolbarSwipeHandler. This causes an issue for the bottom sheet when it
is using the velocity swipe model; the page would shudder because the
toolbar swipe layout would be triggered.

This change only detects directional swipes if the initial direction
is within 25 degrees or left, right, or down. This makes accidentally
triggering a different layout more difficult.

BUG=779615

Change-Id: Ied417f3e71e79431168410d4e305fa29a2f57b9b
Reviewed-on: https://chromium-review.googlesource.com/772830
Commit-Queue: Matthew Jones <mdjones@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Cr-Commit-Position: refs/heads/master@{#517165}
parent 2b344530
......@@ -618,6 +618,15 @@ public class LayoutManagerChrome
* A {@link EdgeSwipeHandler} meant to respond to edge events for the toolbar.
*/
protected class ToolbarSwipeHandler extends EdgeSwipeHandlerLayoutDelegate {
/** The scroll direction of the current gesture. */
private ScrollDirection mScrollDirection;
/**
* The range in degrees that a swipe can be from a particular direction to be considered
* that direction.
*/
private static final float SWIPE_RANGE_DEG = 25;
/**
* Creates an instance of the {@link ToolbarSwipeHandler}.
* @param provider A {@link LayoutProvider} instance.
......@@ -628,13 +637,51 @@ public class LayoutManagerChrome
@Override
public void swipeStarted(ScrollDirection direction, float x, float y) {
if (direction == ScrollDirection.DOWN) {
mScrollDirection = ScrollDirection.UNKNOWN;
}
@Override
public void swipeUpdated(float x, float y, float dx, float dy, float tx, float ty) {
// If scroll direction has been computed, send the event to super.
if (mScrollDirection != ScrollDirection.UNKNOWN) {
super.swipeUpdated(x, y, dx, dy, tx, ty);
return;
}
mScrollDirection = computeScrollDirection(dx, dy);
if (mScrollDirection == ScrollDirection.UNKNOWN) return;
if (mScrollDirection == ScrollDirection.DOWN) {
startShowing(mOverviewLayout, true);
super.swipeStarted(direction, x, y);
} else if (direction == ScrollDirection.LEFT || direction == ScrollDirection.RIGHT) {
} else if (mScrollDirection == ScrollDirection.LEFT
|| mScrollDirection == ScrollDirection.RIGHT) {
startShowing(mToolbarSwipeLayout, true);
super.swipeStarted(direction, x, y);
}
super.swipeStarted(mScrollDirection, x, y);
}
/**
* Compute the direction of the scroll.
* @param dx The distance traveled on the X axis.
* @param dy The distance traveled on the Y axis.
* @return The direction of the scroll.
*/
private ScrollDirection computeScrollDirection(float dx, float dy) {
ScrollDirection direction = ScrollDirection.UNKNOWN;
// Figure out the angle of the swipe. Invert 'dy' so 90 degrees is up.
double swipeAngle = (Math.toDegrees(Math.atan2(-dy, dx)) + 360) % 360;
if (swipeAngle < 180 + SWIPE_RANGE_DEG && swipeAngle > 180 - SWIPE_RANGE_DEG) {
direction = ScrollDirection.LEFT;
} else if (swipeAngle < SWIPE_RANGE_DEG || swipeAngle > 360 - SWIPE_RANGE_DEG) {
direction = ScrollDirection.RIGHT;
} else if (swipeAngle < 270 + SWIPE_RANGE_DEG && swipeAngle > 270 - SWIPE_RANGE_DEG) {
direction = ScrollDirection.DOWN;
}
return direction;
}
@Override
......
......@@ -546,6 +546,9 @@ public class LayoutManagerTest implements MockTabModelDelegate {
final float deltaX = MathUtils.flipSignIf(layoutWidth / 2.f, scrollLeft);
eventHandler.swipeStarted(direction, layoutWidth, 0);
// Call swipeUpdated twice since the handler computes direction in that method.
// TODO(mdjones): Update implementation of EdgeSwipeHandler to work this way by default.
eventHandler.swipeUpdated(deltaX, 0.f, deltaX, 0.f, deltaX, 0.f);
eventHandler.swipeUpdated(deltaX, 0.f, deltaX, 0.f, deltaX, 0.f);
eventHandler.swipeFinished();
......
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