OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/renderer_host/input/gestures/snap_scroll_controller.h" |
| 6 |
| 7 #include <math.h> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "content/browser/renderer_host/input/gestures/motion_event.h" |
| 11 #include "content/browser/renderer_host/input/gestures/zoom_manager.h" |
| 12 |
| 13 namespace content { |
| 14 namespace { |
| 15 const int kSnapBound = 16; |
| 16 } // namespace |
| 17 |
| 18 SnapScrollController::Config::Config() |
| 19 : screen_width_pixels(1), |
| 20 screen_height_pixels(1), |
| 21 density_dpi(1), |
| 22 density(1) {} |
| 23 |
| 24 SnapScrollController::Config::~Config() {} |
| 25 |
| 26 SnapScrollController::SnapScrollController(Config config, |
| 27 ZoomManager* zoom_manager) |
| 28 : zoom_manager_(zoom_manager), |
| 29 channel_distance_(16.f), |
| 30 snap_scroll_mode_(SNAP_NONE), |
| 31 first_touch_x_(-1), |
| 32 first_touch_y_(-1), |
| 33 distance_x_(0), |
| 34 distance_y_(0) { |
| 35 DCHECK(zoom_manager_); |
| 36 CalculateChannelDistance(config); |
| 37 } |
| 38 |
| 39 SnapScrollController::~SnapScrollController() {} |
| 40 |
| 41 void SnapScrollController::UpdateSnapScrollMode(float distance_x, |
| 42 float distance_y) { |
| 43 if (snap_scroll_mode_ == SNAP_HORIZ || snap_scroll_mode_ == SNAP_VERT) { |
| 44 distance_x_ += std::abs(distance_x); |
| 45 distance_y_ += std::abs(distance_y); |
| 46 if (snap_scroll_mode_ == SNAP_HORIZ) { |
| 47 if (distance_y_ > channel_distance_) { |
| 48 snap_scroll_mode_ = SNAP_NONE; |
| 49 } else if (distance_x_ > channel_distance_) { |
| 50 distance_x_ = 0; |
| 51 distance_y_ = 0; |
| 52 } |
| 53 } else { |
| 54 if (distance_x_ > channel_distance_) { |
| 55 snap_scroll_mode_ = SNAP_NONE; |
| 56 } else if (distance_y_ > channel_distance_) { |
| 57 distance_x_ = 0; |
| 58 distance_y_ = 0; |
| 59 } |
| 60 } |
| 61 } |
| 62 } |
| 63 |
| 64 void SnapScrollController::SetSnapScrollingMode(const MotionEvent& event) { |
| 65 switch (event.GetActionMasked()) { |
| 66 case MotionEvent::ACTION_DOWN: |
| 67 snap_scroll_mode_ = SNAP_NONE; |
| 68 first_touch_x_ = (int)event.GetX(); |
| 69 first_touch_y_ = (int)event.GetY(); |
| 70 break; |
| 71 // Set scrolling mode to SNAP_X if scroll towards x-axis exceeds kSnapBound |
| 72 // and movement towards y-axis is trivial. |
| 73 // Set scrolling mode to SNAP_Y if scroll towards y-axis exceeds kSnapBound |
| 74 // and movement towards x-axis is trivial. |
| 75 // Scrolling mode will remain in SNAP_NONE for other conditions. |
| 76 case MotionEvent::ACTION_MOVE: |
| 77 if (!zoom_manager_->IsScaleGestureDetectionInProgress() && |
| 78 snap_scroll_mode_ == SNAP_NONE) { |
| 79 int x_diff = (int)std::abs(event.GetX() - first_touch_x_); |
| 80 int y_diff = (int)std::abs(event.GetY() - first_touch_y_); |
| 81 if (x_diff > kSnapBound && y_diff < kSnapBound) { |
| 82 snap_scroll_mode_ = SNAP_HORIZ; |
| 83 } else if (x_diff < kSnapBound && y_diff > kSnapBound) { |
| 84 snap_scroll_mode_ = SNAP_VERT; |
| 85 } |
| 86 } |
| 87 break; |
| 88 case MotionEvent::ACTION_UP: |
| 89 case MotionEvent::ACTION_CANCEL: |
| 90 first_touch_x_ = -1; |
| 91 first_touch_y_ = -1; |
| 92 distance_x_ = 0; |
| 93 distance_y_ = 0; |
| 94 break; |
| 95 default: |
| 96 break; |
| 97 } |
| 98 } |
| 99 |
| 100 void SnapScrollController::CalculateChannelDistance(Config config) { |
| 101 const float screen_size = |
| 102 hypot((float)config.screen_width_pixels / config.density_dpi, |
| 103 (float)config.screen_height_pixels / config.density_dpi); |
| 104 if (screen_size < 3.f) { |
| 105 channel_distance_ = 16.f; |
| 106 } else if (screen_size < 5.f) { |
| 107 channel_distance_ = 22.f; |
| 108 } else if (screen_size < 7.f) { |
| 109 channel_distance_ = 28.f; |
| 110 } else { |
| 111 channel_distance_ = 34.f; |
| 112 } |
| 113 channel_distance_ = channel_distance_ * config.density; |
| 114 if (channel_distance_ < 16.f) |
| 115 channel_distance_ = 16.f; |
| 116 } |
| 117 |
| 118 } // namespace content |
OLD | NEW |