| OLD | NEW |
| (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 "content/browser/android/overscroll_refresh.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 | |
| 9 namespace content { | |
| 10 namespace { | |
| 11 | |
| 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; | |
| 15 | |
| 16 } // namespace | |
| 17 | |
| 18 OverscrollRefresh::OverscrollRefresh(OverscrollRefreshHandler* handler) | |
| 19 : scrolled_to_top_(true), | |
| 20 overflow_y_hidden_(false), | |
| 21 scroll_consumption_state_(DISABLED), | |
| 22 handler_(handler) { | |
| 23 DCHECK(handler); | |
| 24 } | |
| 25 | |
| 26 OverscrollRefresh::~OverscrollRefresh() { | |
| 27 } | |
| 28 | |
| 29 void OverscrollRefresh::Reset() { | |
| 30 scroll_consumption_state_ = DISABLED; | |
| 31 handler_->PullReset(); | |
| 32 } | |
| 33 | |
| 34 void OverscrollRefresh::OnScrollBegin() { | |
| 35 ReleaseWithoutActivation(); | |
| 36 if (scrolled_to_top_ && !overflow_y_hidden_) | |
| 37 scroll_consumption_state_ = AWAITING_SCROLL_UPDATE_ACK; | |
| 38 } | |
| 39 | |
| 40 void OverscrollRefresh::OnScrollEnd(const gfx::Vector2dF& scroll_velocity) { | |
| 41 bool allow_activation = scroll_velocity.y() > kMinFlingVelocityForActivation; | |
| 42 Release(allow_activation); | |
| 43 } | |
| 44 | |
| 45 void OverscrollRefresh::OnScrollUpdateAck(bool was_consumed) { | |
| 46 if (scroll_consumption_state_ != AWAITING_SCROLL_UPDATE_ACK) | |
| 47 return; | |
| 48 | |
| 49 if (was_consumed) { | |
| 50 scroll_consumption_state_ = DISABLED; | |
| 51 return; | |
| 52 } | |
| 53 | |
| 54 scroll_consumption_state_ = handler_->PullStart() ? ENABLED : DISABLED; | |
| 55 } | |
| 56 | |
| 57 bool OverscrollRefresh::WillHandleScrollUpdate( | |
| 58 const gfx::Vector2dF& scroll_delta) { | |
| 59 switch (scroll_consumption_state_) { | |
| 60 case DISABLED: | |
| 61 return false; | |
| 62 | |
| 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; | |
| 67 return false; | |
| 68 | |
| 69 case ENABLED: | |
| 70 handler_->PullUpdate(scroll_delta.y()); | |
| 71 return true; | |
| 72 } | |
| 73 | |
| 74 NOTREACHED() << "Invalid overscroll state: " << scroll_consumption_state_; | |
| 75 return false; | |
| 76 } | |
| 77 | |
| 78 void OverscrollRefresh::ReleaseWithoutActivation() { | |
| 79 bool allow_activation = false; | |
| 80 Release(allow_activation); | |
| 81 } | |
| 82 | |
| 83 bool OverscrollRefresh::IsActive() const { | |
| 84 return scroll_consumption_state_ == ENABLED; | |
| 85 } | |
| 86 | |
| 87 bool OverscrollRefresh::IsAwaitingScrollUpdateAck() const { | |
| 88 return scroll_consumption_state_ == AWAITING_SCROLL_UPDATE_ACK; | |
| 89 } | |
| 90 | |
| 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; | |
| 96 } | |
| 97 | |
| 98 void OverscrollRefresh::Release(bool allow_refresh) { | |
| 99 if (scroll_consumption_state_ == ENABLED) | |
| 100 handler_->PullRelease(allow_refresh); | |
| 101 scroll_consumption_state_ = DISABLED; | |
| 102 } | |
| 103 | |
| 104 } // namespace content | |
| OLD | NEW |