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.
5 #include "content/browser/android/overscroll_refresh.h"
7 #include "base/logging.h"
12 // Experimentally determined constant used to allow activation even if touch
13 // release results in a small upward fling (quite common during a slow scroll).
14 const float kMinFlingVelocityForActivation
= -500.f
;
18 OverscrollRefresh::OverscrollRefresh(OverscrollRefreshHandler
* handler
)
19 : scrolled_to_top_(true),
20 overflow_y_hidden_(false),
21 scroll_consumption_state_(DISABLED
),
26 OverscrollRefresh::~OverscrollRefresh() {
29 void OverscrollRefresh::Reset() {
30 scroll_consumption_state_
= DISABLED
;
31 handler_
->PullReset();
34 void OverscrollRefresh::OnScrollBegin() {
35 ReleaseWithoutActivation();
36 if (scrolled_to_top_
&& !overflow_y_hidden_
)
37 scroll_consumption_state_
= AWAITING_SCROLL_UPDATE_ACK
;
40 void OverscrollRefresh::OnScrollEnd(const gfx::Vector2dF
& scroll_velocity
) {
41 bool allow_activation
= scroll_velocity
.y() > kMinFlingVelocityForActivation
;
42 Release(allow_activation
);
45 void OverscrollRefresh::OnScrollUpdateAck(bool was_consumed
) {
46 if (scroll_consumption_state_
!= AWAITING_SCROLL_UPDATE_ACK
)
50 scroll_consumption_state_
= DISABLED
;
54 scroll_consumption_state_
= handler_
->PullStart() ? ENABLED
: DISABLED
;
57 bool OverscrollRefresh::WillHandleScrollUpdate(
58 const gfx::Vector2dF
& scroll_delta
) {
59 switch (scroll_consumption_state_
) {
63 case AWAITING_SCROLL_UPDATE_ACK
:
64 // If the initial scroll motion is downward, never allow activation.
65 if (scroll_delta
.y() <= 0)
66 scroll_consumption_state_
= DISABLED
;
70 handler_
->PullUpdate(scroll_delta
.y());
74 NOTREACHED() << "Invalid overscroll state: " << scroll_consumption_state_
;
78 void OverscrollRefresh::ReleaseWithoutActivation() {
79 bool allow_activation
= false;
80 Release(allow_activation
);
83 bool OverscrollRefresh::IsActive() const {
84 return scroll_consumption_state_
== ENABLED
;
87 bool OverscrollRefresh::IsAwaitingScrollUpdateAck() const {
88 return scroll_consumption_state_
== AWAITING_SCROLL_UPDATE_ACK
;
91 void OverscrollRefresh::OnFrameUpdated(
92 const gfx::Vector2dF
& content_scroll_offset
,
93 bool root_overflow_y_hidden
) {
94 scrolled_to_top_
= content_scroll_offset
.y() == 0;
95 overflow_y_hidden_
= root_overflow_y_hidden
;
98 void OverscrollRefresh::Release(bool allow_refresh
) {
99 if (scroll_consumption_state_
== ENABLED
)
100 handler_
->PullRelease(allow_refresh
);
101 scroll_consumption_state_
= DISABLED
;
104 } // namespace content