summaryrefslogtreecommitdiffstats
path: root/ui/events/gesture_detection/snap_scroll_controller.cc
blob: bde5a35602d6579cef87173f6fe7ed44c6755cf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// 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 "ui/events/gesture_detection/snap_scroll_controller.h"

#include <cmath>

#include "ui/events/gesture_detection/motion_event.h"
#include "ui/gfx/display.h"

namespace ui {
namespace {
const int kSnapBound = 16;
const float kMinSnapChannelDistance = kSnapBound;
const float kMaxSnapChannelDistance = kMinSnapChannelDistance * 3.f;
const float kSnapChannelDipsPerScreenDip = kMinSnapChannelDistance / 480.f;

float CalculateChannelDistance(const gfx::Display& display) {
  if (display.bounds().IsEmpty())
    return kMinSnapChannelDistance;

  float screen_size =
      std::abs(hypot(static_cast<float>(display.bounds().width()),
                     static_cast<float>(display.bounds().height())));

  float snap_channel_distance = screen_size * kSnapChannelDipsPerScreenDip;
  return std::max(kMinSnapChannelDistance,
                  std::min(kMaxSnapChannelDistance, snap_channel_distance));
}

}  // namespace


SnapScrollController::SnapScrollController(const gfx::Display& display)
    : channel_distance_(CalculateChannelDistance(display)),
      snap_scroll_mode_(SNAP_NONE),
      first_touch_x_(-1),
      first_touch_y_(-1),
      distance_x_(0),
      distance_y_(0) {}

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,
    bool is_scale_gesture_detection_in_progress) {
  switch (event.GetAction()) {
    case MotionEvent::ACTION_DOWN:
      snap_scroll_mode_ = SNAP_NONE;
      first_touch_x_ = event.GetX();
      first_touch_y_ = 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 (!is_scale_gesture_detection_in_progress &&
          snap_scroll_mode_ == SNAP_NONE) {
        int x_diff = static_cast<int>(std::abs(event.GetX() - first_touch_x_));
        int y_diff = static_cast<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;
  }
}

}  // namespace ui