forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
overscroll_refresh.cc
165 lines (139 loc) · 5.12 KB
/
overscroll_refresh.cc
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
164
165
// 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 "ui/android/overscroll_refresh.h"
#include <ostream>
#include "base/check.h"
#include "base/notreached.h"
#include "cc/input/overscroll_behavior.h"
#include "ui/android/overscroll_refresh_handler.h"
#include "ui/gfx/geometry/point_f.h"
namespace ui {
namespace {
// Experimentally determined constant used to allow activation even if touch
// release results in a small upward fling (quite common during a slow scroll).
const float kMinFlingVelocityForActivation = -500.f;
// Weighted value used to determine whether a scroll should trigger vertical
// scroll or horizontal navigation.
const float kWeightAngle30 = 1.73f;
} // namespace
OverscrollRefresh::OverscrollRefresh(OverscrollRefreshHandler* handler,
float edge_width)
: scrolled_to_top_(true),
top_at_scroll_start_(true),
overflow_y_hidden_(false),
scroll_consumption_state_(DISABLED),
edge_width_(edge_width),
handler_(handler) {
DCHECK(handler);
}
OverscrollRefresh::OverscrollRefresh()
: scrolled_to_top_(true),
overflow_y_hidden_(false),
scroll_consumption_state_(DISABLED),
edge_width_(kDefaultNavigationEdgeWidth * 1.f),
handler_(nullptr) {}
OverscrollRefresh::~OverscrollRefresh() {
}
void OverscrollRefresh::Reset() {
scroll_consumption_state_ = DISABLED;
cumulative_scroll_.set_x(0);
cumulative_scroll_.set_y(0);
handler_->PullReset();
}
void OverscrollRefresh::OnScrollBegin(const gfx::PointF& pos) {
scroll_begin_x_ = pos.x();
scroll_begin_y_ = pos.y();
top_at_scroll_start_ = scrolled_to_top_;
ReleaseWithoutActivation();
scroll_consumption_state_ = AWAITING_SCROLL_UPDATE_ACK;
}
void OverscrollRefresh::OnScrollEnd(const gfx::Vector2dF& scroll_velocity) {
bool allow_activation = scroll_velocity.y() > kMinFlingVelocityForActivation;
Release(allow_activation);
}
void OverscrollRefresh::OnOverscrolled(const cc::OverscrollBehavior& behavior) {
if (scroll_consumption_state_ != AWAITING_SCROLL_UPDATE_ACK)
return;
float ydelta = cumulative_scroll_.y();
float xdelta = cumulative_scroll_.x();
bool in_y_direction = std::abs(ydelta) > std::abs(xdelta);
bool in_x_direction = std::abs(ydelta) * kWeightAngle30 < std::abs(xdelta);
OverscrollAction type = OverscrollAction::NONE;
bool navigate_forward = false;
if (ydelta > 0 && in_y_direction) {
// Pull-to-refresh. Check overscroll-behavior-y
if (behavior.y != cc::OverscrollBehavior::Type::kAuto) {
Reset();
return;
}
type = OverscrollAction::PULL_TO_REFRESH;
} else if (in_x_direction &&
(scroll_begin_x_ < edge_width_ ||
viewport_width_ - scroll_begin_x_ < edge_width_)) {
// Swipe-to-navigate. Check overscroll-behavior-x
if (behavior.x != cc::OverscrollBehavior::Type::kAuto) {
Reset();
return;
}
type = OverscrollAction::HISTORY_NAVIGATION;
navigate_forward = xdelta < 0;
}
if (type != OverscrollAction::NONE) {
scroll_consumption_state_ =
handler_->PullStart(type, scroll_begin_x_, scroll_begin_y_,
navigate_forward)
? ENABLED
: DISABLED;
}
}
bool OverscrollRefresh::WillHandleScrollUpdate(
const gfx::Vector2dF& scroll_delta) {
switch (scroll_consumption_state_) {
case DISABLED:
return false;
case AWAITING_SCROLL_UPDATE_ACK:
// Check applies for the pull-to-refresh condition only.
if (std::abs(scroll_delta.y()) > std::abs(scroll_delta.x())) {
// If the initial scroll motion is downward, or we're in other cases
// where activation shouldn't have happened, stop here.
if (scroll_delta.y() <= 0 || !top_at_scroll_start_ ||
overflow_y_hidden_) {
scroll_consumption_state_ = DISABLED;
return false;
}
}
cumulative_scroll_.Add(scroll_delta);
return false;
case ENABLED:
handler_->PullUpdate(scroll_delta.x(), scroll_delta.y());
return true;
}
NOTREACHED() << "Invalid overscroll state: " << scroll_consumption_state_;
return false;
}
void OverscrollRefresh::ReleaseWithoutActivation() {
bool allow_activation = false;
Release(allow_activation);
}
bool OverscrollRefresh::IsActive() const {
return scroll_consumption_state_ == ENABLED;
}
bool OverscrollRefresh::IsAwaitingScrollUpdateAck() const {
return scroll_consumption_state_ == AWAITING_SCROLL_UPDATE_ACK;
}
void OverscrollRefresh::OnFrameUpdated(const gfx::SizeF& viewport_size,
const gfx::PointF& content_scroll_offset,
bool root_overflow_y_hidden) {
viewport_width_ = viewport_size.width();
scrolled_to_top_ = content_scroll_offset.y() == 0;
overflow_y_hidden_ = root_overflow_y_hidden;
}
void OverscrollRefresh::Release(bool allow_refresh) {
if (scroll_consumption_state_ == ENABLED)
handler_->PullRelease(allow_refresh);
scroll_consumption_state_ = DISABLED;
cumulative_scroll_.set_x(0);
cumulative_scroll_.set_y(0);
}
} // namespace ui