Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(294)

Side by Side Diff: ui/events/gesture_detection/snap_scroll_controller.cc

Issue 128613003: [Tracking Patch] Unified gesture detection (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Cleanup Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "ui/events/gesture_detection/snap_scroll_controller.h"
6
7 #include <math.h>
8
9 #include "ui/events/gesture_detection/motion_event.h"
10
11 namespace ui {
12 namespace {
13 const int kSnapBound = 16;
14 } // namespace
15
16 SnapScrollController::Config::Config()
17 : screen_width_pixels(1),
18 screen_height_pixels(1),
19 device_scale_factor(1) {}
20
21 SnapScrollController::Config::~Config() {}
22
23 SnapScrollController::SnapScrollController(const Config& config)
24 : channel_distance_(CalculateChannelDistance(config)),
25 snap_scroll_mode_(SNAP_NONE),
26 first_touch_x_(-1),
27 first_touch_y_(-1),
28 distance_x_(0),
29 distance_y_(0) {}
30
31 SnapScrollController::~SnapScrollController() {}
32
33 void SnapScrollController::UpdateSnapScrollMode(float distance_x,
34 float distance_y) {
35 if (snap_scroll_mode_ == SNAP_HORIZ || snap_scroll_mode_ == SNAP_VERT) {
36 distance_x_ += std::abs(distance_x);
37 distance_y_ += std::abs(distance_y);
38 if (snap_scroll_mode_ == SNAP_HORIZ) {
39 if (distance_y_ > channel_distance_) {
40 snap_scroll_mode_ = SNAP_NONE;
41 } else if (distance_x_ > channel_distance_) {
42 distance_x_ = 0;
43 distance_y_ = 0;
44 }
45 } else {
46 if (distance_x_ > channel_distance_) {
47 snap_scroll_mode_ = SNAP_NONE;
48 } else if (distance_y_ > channel_distance_) {
49 distance_x_ = 0;
50 distance_y_ = 0;
51 }
52 }
53 }
54 }
55
56 void SnapScrollController::SetSnapScrollingMode(
57 const MotionEvent& event,
58 bool is_scale_gesture_detection_in_progress) {
59 switch (event.GetAction()) {
60 case MotionEvent::ACTION_DOWN:
61 snap_scroll_mode_ = SNAP_NONE;
62 first_touch_x_ = (int)event.GetX();
63 first_touch_y_ = (int)event.GetY();
64 break;
65 // Set scrolling mode to SNAP_X if scroll towards x-axis exceeds kSnapBound
66 // and movement towards y-axis is trivial.
67 // Set scrolling mode to SNAP_Y if scroll towards y-axis exceeds kSnapBound
68 // and movement towards x-axis is trivial.
69 // Scrolling mode will remain in SNAP_NONE for other conditions.
70 case MotionEvent::ACTION_MOVE:
71 if (!is_scale_gesture_detection_in_progress &&
72 snap_scroll_mode_ == SNAP_NONE) {
73 int x_diff = (int)std::abs(event.GetX() - first_touch_x_);
74 int y_diff = (int)std::abs(event.GetY() - first_touch_y_);
75 if (x_diff > kSnapBound && y_diff < kSnapBound) {
76 snap_scroll_mode_ = SNAP_HORIZ;
77 } else if (x_diff < kSnapBound && y_diff > kSnapBound) {
78 snap_scroll_mode_ = SNAP_VERT;
79 }
80 }
81 break;
82 case MotionEvent::ACTION_UP:
83 case MotionEvent::ACTION_CANCEL:
84 first_touch_x_ = -1;
85 first_touch_y_ = -1;
86 distance_x_ = 0;
87 distance_y_ = 0;
88 break;
89 default:
90 break;
91 }
92 }
93
94 // static
95 float SnapScrollController::CalculateChannelDistance(const Config& config) {
96 float channel_distance = 16.f;
97
98 const float screen_size = std::abs(
99 hypot((float)config.screen_width_pixels / config.device_scale_factor,
100 (float)config.screen_height_pixels / config.device_scale_factor));
101 if (screen_size < 3.f) {
102 channel_distance = 16.f;
103 } else if (screen_size < 5.f) {
104 channel_distance = 22.f;
105 } else if (screen_size < 7.f) {
106 channel_distance = 28.f;
107 } else {
108 channel_distance = 34.f;
109 }
110 channel_distance = channel_distance * config.device_scale_factor;
111 if (channel_distance < 16.f)
112 channel_distance = 16.f;
113
114 return channel_distance;
115 }
116
117 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698