| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.content.browser; | 5 package org.chromium.content.browser; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.util.DisplayMetrics; | 8 import android.util.DisplayMetrics; |
| 9 import android.view.MotionEvent; | 9 import android.view.MotionEvent; |
| 10 | 10 |
| 11 /** | 11 /** |
| 12 * This objects controls the scroll snapping behavior based on scroll updates. | 12 * This objects controls the scroll snapping behavior based on scroll updates. |
| 13 */ | 13 */ |
| 14 class SnapScrollController { | 14 class SnapScrollController { |
| 15 private static final String TAG = "SnapScrollController"; | 15 private static final String TAG = "SnapScrollController"; |
| 16 private static final int SNAP_NONE = 0; | 16 private static final int SNAP_NONE = 0; |
| 17 private static final int SNAP_HORIZ = 1; | 17 private static final int SNAP_HORIZ = 1; |
| 18 private static final int SNAP_VERT = 2; | 18 private static final int SNAP_VERT = 2; |
| 19 private static final int SNAP_BOUND = 16; | 19 private static final int SNAP_BOUND = 16; |
| 20 | 20 |
| 21 private float mChannelDistance = 16f; | 21 private float mChannelDistance = 16f; |
| 22 private int mSnapScrollMode = SNAP_NONE; | 22 private int mSnapScrollMode = SNAP_NONE; |
| 23 private int mFirstTouchX = -1; | 23 private int mFirstTouchX = -1; |
| 24 private int mFirstTouchY = -1; | 24 private int mFirstTouchY = -1; |
| 25 private float mDistanceX = 0; | 25 private float mDistanceX = 0; |
| 26 private float mDistanceY = 0; | 26 private float mDistanceY = 0; |
| 27 private ZoomManager mZoomManager; | |
| 28 | 27 |
| 29 SnapScrollController(Context context, ZoomManager zoomManager) { | 28 SnapScrollController(Context context) { |
| 30 calculateChannelDistance(context); | 29 calculateChannelDistance(context); |
| 31 mZoomManager = zoomManager; | |
| 32 } | 30 } |
| 33 | 31 |
| 34 /** | 32 /** |
| 35 * Updates the snap scroll mode based on the given X and Y distance to be mo
ved on scroll. | 33 * Updates the snap scroll mode based on the given X and Y distance to be mo
ved on scroll. |
| 36 * If the scroll update is above a threshold, the snapping behavior is reset
. | 34 * If the scroll update is above a threshold, the snapping behavior is reset
. |
| 37 * @param distanceX X distance for the current scroll update. | 35 * @param distanceX X distance for the current scroll update. |
| 38 * @param distanceY Y distance for the current scroll update. | 36 * @param distanceY Y distance for the current scroll update. |
| 39 */ | 37 */ |
| 40 void updateSnapScrollMode(float distanceX, float distanceY) { | 38 void updateSnapScrollMode(float distanceX, float distanceY) { |
| 41 if (mSnapScrollMode == SNAP_HORIZ || mSnapScrollMode == SNAP_VERT) { | 39 if (mSnapScrollMode == SNAP_HORIZ || mSnapScrollMode == SNAP_VERT) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 56 mDistanceY = 0; | 54 mDistanceY = 0; |
| 57 } | 55 } |
| 58 } | 56 } |
| 59 } | 57 } |
| 60 } | 58 } |
| 61 | 59 |
| 62 /** | 60 /** |
| 63 * Sets the snap scroll mode based on the event type. | 61 * Sets the snap scroll mode based on the event type. |
| 64 * @param event The received MotionEvent. | 62 * @param event The received MotionEvent. |
| 65 */ | 63 */ |
| 66 void setSnapScrollingMode(MotionEvent event) { | 64 void setSnapScrollingMode(MotionEvent event, boolean isScaleGestureDetection
InProgress) { |
| 67 switch(event.getAction()) { | 65 switch(event.getAction()) { |
| 68 case MotionEvent.ACTION_DOWN: | 66 case MotionEvent.ACTION_DOWN: |
| 69 mSnapScrollMode = SNAP_NONE; | 67 mSnapScrollMode = SNAP_NONE; |
| 70 mFirstTouchX = (int) event.getX(); | 68 mFirstTouchX = (int) event.getX(); |
| 71 mFirstTouchY = (int) event.getY(); | 69 mFirstTouchY = (int) event.getY(); |
| 72 break; | 70 break; |
| 73 // Set scrolling mode to SNAP_X if scroll towards x-axis exceeds SN
AP_BOUND | 71 // Set scrolling mode to SNAP_X if scroll towards x-axis exceeds SN
AP_BOUND |
| 74 // and movement towards y-axis is trivial. | 72 // and movement towards y-axis is trivial. |
| 75 // Set scrolling mode to SNAP_Y if scroll towards y-axis exceeds SN
AP_BOUND | 73 // Set scrolling mode to SNAP_Y if scroll towards y-axis exceeds SN
AP_BOUND |
| 76 // and movement towards x-axis is trivial. | 74 // and movement towards x-axis is trivial. |
| 77 // Scrolling mode will remain in SNAP_NONE for other conditions. | 75 // Scrolling mode will remain in SNAP_NONE for other conditions. |
| 78 case MotionEvent.ACTION_MOVE: | 76 case MotionEvent.ACTION_MOVE: |
| 79 if (!mZoomManager.isScaleGestureDetectionInProgress() && | 77 if (!isScaleGestureDetectionInProgress && |
| 80 mSnapScrollMode == SNAP_NONE) { | 78 mSnapScrollMode == SNAP_NONE) { |
| 81 int xDiff = (int) Math.abs(event.getX() - mFirstTouchX); | 79 int xDiff = (int) Math.abs(event.getX() - mFirstTouchX); |
| 82 int yDiff = (int) Math.abs(event.getY() - mFirstTouchY); | 80 int yDiff = (int) Math.abs(event.getY() - mFirstTouchY); |
| 83 if (xDiff > SNAP_BOUND && yDiff < SNAP_BOUND) { | 81 if (xDiff > SNAP_BOUND && yDiff < SNAP_BOUND) { |
| 84 mSnapScrollMode = SNAP_HORIZ; | 82 mSnapScrollMode = SNAP_HORIZ; |
| 85 } else if (xDiff < SNAP_BOUND && yDiff > SNAP_BOUND) { | 83 } else if (xDiff < SNAP_BOUND && yDiff > SNAP_BOUND) { |
| 86 mSnapScrollMode = SNAP_VERT; | 84 mSnapScrollMode = SNAP_VERT; |
| 87 } | 85 } |
| 88 } | 86 } |
| 89 break; | 87 break; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 return mSnapScrollMode == SNAP_HORIZ; | 129 return mSnapScrollMode == SNAP_HORIZ; |
| 132 } | 130 } |
| 133 | 131 |
| 134 /** | 132 /** |
| 135 * @return Whether currently snapping scrolls. | 133 * @return Whether currently snapping scrolls. |
| 136 */ | 134 */ |
| 137 boolean isSnappingScrolls() { | 135 boolean isSnappingScrolls() { |
| 138 return mSnapScrollMode != SNAP_NONE; | 136 return mSnapScrollMode != SNAP_NONE; |
| 139 } | 137 } |
| 140 } | 138 } |
| OLD | NEW |