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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
ash / public / cpp / pagination / pagination_controller.cc [blame]
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/public/cpp/pagination/pagination_controller.h"
#include "ash/public/cpp/pagination/pagination_model.h"
#include "base/check.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/vector2d.h"
namespace ash {
namespace {
// Constants for dealing with scroll events.
const int kMinScrollToSwitchPage = 10;
const int kMinHorizVelocityToSwitchPage = 800;
const double kFinishTransitionThreshold = 0.33;
} // namespace
PaginationController::PaginationController(PaginationModel* model,
ScrollAxis scroll_axis,
const RecordMetrics& record_metrics)
: pagination_model_(model),
scroll_axis_(scroll_axis),
record_metrics_(record_metrics) {
DCHECK(pagination_model_);
DCHECK(record_metrics_);
}
PaginationController::~PaginationController() = default;
bool PaginationController::OnScroll(const gfx::Vector2dF& offset,
ui::EventType type) {
float offset_magnitude;
if (scroll_axis_ == SCROLL_AXIS_HORIZONTAL) {
// If the view scrolls horizontally, both horizontal and vertical scroll
// events are valid (since most mouse wheels only have vertical scrolling).
offset_magnitude =
abs(offset.x()) > abs(offset.y()) ? offset.x() : offset.y();
} else {
// If the view scrolls vertically, only vertical scroll events are valid.
offset_magnitude = offset.y();
}
// Do not scroll on very small events.
// TODO(calamity): This should only apply on touchpad scroll but touchpad
// events are coming in as mousewheel events. See https://crbug.com/594264.
if (abs(offset_magnitude) > kMinScrollToSwitchPage &&
!pagination_model_->has_transition()) {
const int delta = offset_magnitude > 0 ? -1 : 1;
SelectPageAndRecordMetric(delta, type);
return true;
}
return false;
}
bool PaginationController::OnGestureEvent(const ui::GestureEvent& event,
const gfx::Rect& bounds) {
const ui::GestureEventDetails& details = event.details();
switch (event.type()) {
case ui::EventType::kGestureScrollBegin: {
float scroll = scroll_axis_ == SCROLL_AXIS_HORIZONTAL
? details.scroll_x_hint()
: details.scroll_y_hint();
return StartDrag(scroll);
}
case ui::EventType::kGestureScrollUpdate: {
float scroll = scroll_axis_ == SCROLL_AXIS_HORIZONTAL
? details.scroll_x()
: details.scroll_y();
return UpdateDrag(scroll, bounds);
}
case ui::EventType::kGestureScrollEnd: {
return EndDrag(event);
}
case ui::EventType::kScrollFlingStart: {
float velocity = scroll_axis_ == SCROLL_AXIS_HORIZONTAL
? details.velocity_x()
: details.velocity_y();
if (fabs(velocity) > kMinHorizVelocityToSwitchPage) {
pagination_model_->EndScroll(true);
const int delta = velocity < 0 ? 1 : -1;
SelectPageAndRecordMetric(delta, event.type());
} else {
// If the gesture ends in a fling below page switch velocity threshold,
// decide whether to switch page depending on the scroll progress (if
// gesture ends with a slow fling after the user has dragged the page
// beyond page switch drag threshold, switch the page).
EndDrag(event);
}
return true;
}
default:
return false;
}
}
void PaginationController::StartMouseDrag(const gfx::Vector2dF& offset) {
float scroll =
scroll_axis_ == SCROLL_AXIS_HORIZONTAL ? offset.x() : offset.y();
StartDrag(scroll);
}
void PaginationController::UpdateMouseDrag(const gfx::Vector2dF& offset,
const gfx::Rect& bounds) {
float scroll =
scroll_axis_ == SCROLL_AXIS_HORIZONTAL ? offset.x() : offset.y();
UpdateDrag(scroll, bounds);
}
void PaginationController::EndMouseDrag(const ui::MouseEvent& event) {
EndDrag(event);
}
bool PaginationController::StartDrag(float scroll) {
if (scroll == 0)
return false;
pagination_model_->StartScroll();
return true;
}
bool PaginationController::UpdateDrag(float scroll, const gfx::Rect& bounds) {
if (!pagination_model_->IsValidPageRelative(scroll < 0 ? 1 : -1) &&
!pagination_model_->has_transition()) {
// scroll > 0 means moving contents right or down. That is,
// transitioning to the previous page. If scrolling to an invalid page,
// ignore the event until movement continues in a valid direction.
return true;
}
int width =
scroll_axis_ == SCROLL_AXIS_HORIZONTAL ? bounds.width() : bounds.height();
pagination_model_->UpdateScroll(scroll / width);
return true;
}
bool PaginationController::EndDrag(const ui::LocatedEvent& event) {
const bool cancel_transition =
pagination_model_->transition().progress < kFinishTransitionThreshold;
pagination_model_->EndScroll(cancel_transition);
if (!cancel_transition)
record_metrics_.Run(event.type());
return true;
}
void PaginationController::SelectPageAndRecordMetric(int delta,
ui::EventType type) {
if (pagination_model_->IsValidPageRelative(delta)) {
record_metrics_.Run(type);
}
pagination_model_->SelectPageRelative(delta, true);
}
} // namespace ash