Index: content/browser/renderer_host/input/gestures/snap_scroll_controller.cc |
diff --git a/content/browser/renderer_host/input/gestures/snap_scroll_controller.cc b/content/browser/renderer_host/input/gestures/snap_scroll_controller.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d31e37dbf4f99587c8ca9bb76834886e5a6598b8 |
--- /dev/null |
+++ b/content/browser/renderer_host/input/gestures/snap_scroll_controller.cc |
@@ -0,0 +1,118 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/browser/renderer_host/input/gestures/snap_scroll_controller.h" |
+ |
+#include <math.h> |
+ |
+#include "base/logging.h" |
+#include "content/browser/renderer_host/input/gestures/motion_event.h" |
+#include "content/browser/renderer_host/input/gestures/zoom_manager.h" |
+ |
+namespace content { |
+namespace { |
+const int kSnapBound = 16; |
+} // namespace |
+ |
+SnapScrollController::Config::Config() |
+ : screen_width_pixels(1), |
+ screen_height_pixels(1), |
+ density_dpi(1), |
+ density(1) {} |
+ |
+SnapScrollController::Config::~Config() {} |
+ |
+SnapScrollController::SnapScrollController(Config config, |
+ ZoomManager* zoom_manager) |
+ : zoom_manager_(zoom_manager), |
+ channel_distance_(16.f), |
+ snap_scroll_mode_(SNAP_NONE), |
+ first_touch_x_(-1), |
+ first_touch_y_(-1), |
+ distance_x_(0), |
+ distance_y_(0) { |
+ DCHECK(zoom_manager_); |
+ CalculateChannelDistance(config); |
+} |
+ |
+SnapScrollController::~SnapScrollController() {} |
+ |
+void SnapScrollController::UpdateSnapScrollMode(float distance_x, |
+ float distance_y) { |
+ if (snap_scroll_mode_ == SNAP_HORIZ || snap_scroll_mode_ == SNAP_VERT) { |
+ distance_x_ += std::abs(distance_x); |
+ distance_y_ += std::abs(distance_y); |
+ if (snap_scroll_mode_ == SNAP_HORIZ) { |
+ if (distance_y_ > channel_distance_) { |
+ snap_scroll_mode_ = SNAP_NONE; |
+ } else if (distance_x_ > channel_distance_) { |
+ distance_x_ = 0; |
+ distance_y_ = 0; |
+ } |
+ } else { |
+ if (distance_x_ > channel_distance_) { |
+ snap_scroll_mode_ = SNAP_NONE; |
+ } else if (distance_y_ > channel_distance_) { |
+ distance_x_ = 0; |
+ distance_y_ = 0; |
+ } |
+ } |
+ } |
+} |
+ |
+void SnapScrollController::SetSnapScrollingMode(const MotionEvent& event) { |
+ switch (event.GetActionMasked()) { |
+ case MotionEvent::ACTION_DOWN: |
+ snap_scroll_mode_ = SNAP_NONE; |
+ first_touch_x_ = (int)event.GetX(); |
+ first_touch_y_ = (int)event.GetY(); |
+ break; |
+ // Set scrolling mode to SNAP_X if scroll towards x-axis exceeds kSnapBound |
+ // and movement towards y-axis is trivial. |
+ // Set scrolling mode to SNAP_Y if scroll towards y-axis exceeds kSnapBound |
+ // and movement towards x-axis is trivial. |
+ // Scrolling mode will remain in SNAP_NONE for other conditions. |
+ case MotionEvent::ACTION_MOVE: |
+ if (!zoom_manager_->IsScaleGestureDetectionInProgress() && |
+ snap_scroll_mode_ == SNAP_NONE) { |
+ int x_diff = (int)std::abs(event.GetX() - first_touch_x_); |
+ int y_diff = (int)std::abs(event.GetY() - first_touch_y_); |
+ if (x_diff > kSnapBound && y_diff < kSnapBound) { |
+ snap_scroll_mode_ = SNAP_HORIZ; |
+ } else if (x_diff < kSnapBound && y_diff > kSnapBound) { |
+ snap_scroll_mode_ = SNAP_VERT; |
+ } |
+ } |
+ break; |
+ case MotionEvent::ACTION_UP: |
+ case MotionEvent::ACTION_CANCEL: |
+ first_touch_x_ = -1; |
+ first_touch_y_ = -1; |
+ distance_x_ = 0; |
+ distance_y_ = 0; |
+ break; |
+ default: |
+ break; |
+ } |
+} |
+ |
+void SnapScrollController::CalculateChannelDistance(Config config) { |
+ const float screen_size = |
+ hypot((float)config.screen_width_pixels / config.density_dpi, |
+ (float)config.screen_height_pixels / config.density_dpi); |
+ if (screen_size < 3.f) { |
+ channel_distance_ = 16.f; |
+ } else if (screen_size < 5.f) { |
+ channel_distance_ = 22.f; |
+ } else if (screen_size < 7.f) { |
+ channel_distance_ = 28.f; |
+ } else { |
+ channel_distance_ = 34.f; |
+ } |
+ channel_distance_ = channel_distance_ * config.density; |
+ if (channel_distance_ < 16.f) |
+ channel_distance_ = 16.f; |
+} |
+ |
+} // namespace content |