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 "cc/layers/ui_resource_layer.h" |
| 8 #include "cc/trees/layer_tree_host.h" |
| 9 #include "content/browser/android/animation_utils.h" |
| 10 #include "ui/base/android/system_ui_resource_manager.h" |
| 11 |
| 12 using std::max; |
| 13 using std::min; |
| 14 |
| 15 namespace content { |
| 16 namespace { |
| 17 |
| 18 const ui::SystemUIResourceType kIdleResourceType = ui::OVERSCROLL_REFRESH_IDLE; |
| 19 const ui::SystemUIResourceType kActiveResourceType = |
| 20 ui::OVERSCROLL_REFRESH_ACTIVE; |
| 21 |
| 22 // Animation duration after the effect is released without triggering a refresh. |
| 23 const int kRecedeTimeMs = 300; |
| 24 |
| 25 // Animation duration after the effect is released and triggers a refresh. |
| 26 const int kActivationTimeMs = 1000; |
| 27 |
| 28 // Max animation duration after the effect is released and triggers a refresh. |
| 29 const int kMaxActivationTimeMs = kActivationTimeMs * 3; |
| 30 |
| 31 // Animation duration after the refresh activated phase has completed. |
| 32 const int kActivationRecedeTimeMs = 300; |
| 33 |
| 34 // Input threshold required to activate the refresh. |
| 35 const float kPullActivationThreshold = .35f; |
| 36 |
| 37 // Input threshold required to start glowing. |
| 38 const float kGlowActivationThreshold = kPullActivationThreshold * 0.85f; |
| 39 |
| 40 // Useful for avoiding accidental triggering when a scroll janks (is delayed), |
| 41 // capping the impulse per event. |
| 42 const float kMaxNormalizedDeltaPerPull = kPullActivationThreshold / 4.f; |
| 43 |
| 44 // Maximum offset of the effect relative to the content size. |
| 45 const float kMaxRelativeOffset = .3f; |
| 46 |
| 47 // Minimum alpha for the effect layer. |
| 48 const float kMinAlpha = 0.25f; |
| 49 |
| 50 // Controls spin velocity. |
| 51 const float kPullRotationMultiplier = 180.f * (1.f / kPullActivationThreshold); |
| 52 |
| 53 // Experimentally determined constant used to allow activation even if touch |
| 54 // release results in a small upward fling (quite common during a slow scroll). |
| 55 const float kMinFlingVelocityForActivation = -500.f; |
| 56 |
| 57 const float kEpsilon = 0.005f; |
| 58 |
| 59 void UpdateLayer(cc::UIResourceLayer* layer, |
| 60 cc::Layer* parent, |
| 61 cc::UIResourceId res_id, |
| 62 const gfx::SizeF& viewport_size, |
| 63 float relative_offset, |
| 64 float opacity, |
| 65 float rotation) { |
| 66 if (layer->parent() != parent) |
| 67 parent->AddChild(layer); |
| 68 |
| 69 if (!layer->layer_tree_host()) |
| 70 return; |
| 71 |
| 72 // An empty window size, while meaningless, is also relatively harmless, and |
| 73 // will simply prevent any drawing of the layers. |
| 74 if (viewport_size.IsEmpty()) { |
| 75 layer->SetIsDrawable(false); |
| 76 return; |
| 77 } |
| 78 |
| 79 if (!res_id) { |
| 80 layer->SetIsDrawable(false); |
| 81 return; |
| 82 } |
| 83 |
| 84 if (opacity == 0) { |
| 85 layer->SetIsDrawable(false); |
| 86 layer->SetOpacity(0); |
| 87 return; |
| 88 } |
| 89 |
| 90 gfx::Size image_size = layer->layer_tree_host()->GetUIResourceSize(res_id); |
| 91 layer->SetUIResourceId(res_id); |
| 92 layer->SetIsDrawable(true); |
| 93 layer->SetTransformOrigin( |
| 94 gfx::Point3F(image_size.width() * 0.5f, image_size.height() * 0.5f, 0)); |
| 95 layer->SetBounds(image_size); |
| 96 layer->SetContentsOpaque(false); |
| 97 layer->SetOpacity(Clamp(opacity, 0.f, 1.f)); |
| 98 |
| 99 float min_viewport_size = min(viewport_size.width(), viewport_size.height()); |
| 100 float offset_x = (viewport_size.width() - image_size.width()) * 0.5f; |
| 101 float offset_y = |
| 102 Damp(relative_offset, 1.2f) * min_viewport_size * kMaxRelativeOffset - |
| 103 image_size.height(); |
| 104 gfx::Transform transform; |
| 105 transform.Translate(offset_x, offset_y); |
| 106 transform.Rotate(rotation); |
| 107 layer->SetTransform(transform); |
| 108 } |
| 109 |
| 110 } // namespace |
| 111 |
| 112 class OverscrollRefresh::Effect { |
| 113 public: |
| 114 Effect(ui::SystemUIResourceManager* resource_manager) |
| 115 : resource_manager_(resource_manager), |
| 116 idle_layer_(cc::UIResourceLayer::Create()), |
| 117 active_layer_(cc::UIResourceLayer::Create()), |
| 118 idle_alpha_(0), |
| 119 active_alpha_(0), |
| 120 offset_(0), |
| 121 rotation_(0), |
| 122 idle_alpha_start_(0), |
| 123 idle_alpha_finish_(0), |
| 124 active_alpha_start_(0), |
| 125 active_alpha_finish_(0), |
| 126 offset_start_(0), |
| 127 offset_finish_(0), |
| 128 rotation_start_(0), |
| 129 rotation_finish_(0), |
| 130 state_(STATE_IDLE) { |
| 131 idle_layer_->SetIsDrawable(false); |
| 132 active_layer_->SetIsDrawable(false); |
| 133 } |
| 134 |
| 135 ~Effect() { Detach(); } |
| 136 |
| 137 void Pull(float normalized_delta) { |
| 138 if (state_ != STATE_PULL) |
| 139 offset_ = 0; |
| 140 |
| 141 state_ = STATE_PULL; |
| 142 |
| 143 normalized_delta = Clamp(normalized_delta, -kMaxNormalizedDeltaPerPull, |
| 144 kMaxNormalizedDeltaPerPull); |
| 145 |
| 146 offset_ += normalized_delta; |
| 147 offset_ = Clamp(offset_, 0.f, 1.f); |
| 148 |
| 149 idle_alpha_ = |
| 150 kMinAlpha + (1.f - kMinAlpha) * offset_ / kGlowActivationThreshold; |
| 151 active_alpha_ = (offset_ - kGlowActivationThreshold) / |
| 152 (kPullActivationThreshold - kGlowActivationThreshold); |
| 153 idle_alpha_ = Clamp(idle_alpha_, 0.f, 1.f); |
| 154 active_alpha_ = Clamp(active_alpha_, 0.f, 1.f); |
| 155 |
| 156 rotation_ = kPullRotationMultiplier * Damp(offset_, 1.f); |
| 157 } |
| 158 |
| 159 bool Animate(base::TimeTicks current_time, bool still_refreshing) { |
| 160 if (IsFinished()) |
| 161 return false; |
| 162 |
| 163 if (state_ == STATE_PULL) |
| 164 return true; |
| 165 |
| 166 const double dt = (current_time - start_time_).InMilliseconds(); |
| 167 const double t = min(dt / duration_.InMilliseconds(), 1.); |
| 168 const float interp = static_cast<float>(Damp(t, 1.)); |
| 169 |
| 170 idle_alpha_ = Lerp(idle_alpha_start_, idle_alpha_finish_, interp); |
| 171 active_alpha_ = Lerp(active_alpha_start_, active_alpha_finish_, interp); |
| 172 offset_ = Lerp(offset_start_, offset_finish_, interp); |
| 173 rotation_ = Lerp(rotation_start_, rotation_finish_, interp); |
| 174 |
| 175 if (t < 1.f - kEpsilon) |
| 176 return true; |
| 177 |
| 178 switch (state_) { |
| 179 case STATE_IDLE: |
| 180 case STATE_PULL: |
| 181 NOTREACHED() << "Invalidate state for animation."; |
| 182 break; |
| 183 case STATE_ACTIVATED: |
| 184 start_time_ = current_time; |
| 185 if (still_refreshing && |
| 186 (current_time - activated_start_time_ < |
| 187 base::TimeDelta::FromMilliseconds(kMaxActivationTimeMs))) { |
| 188 offset_start_ = offset_finish_ = offset_; |
| 189 rotation_start_ = rotation_; |
| 190 rotation_finish_ = rotation_start_ + 360.f; |
| 191 break; |
| 192 } |
| 193 state_ = STATE_ACTIVATED_RECEDE; |
| 194 duration_ = base::TimeDelta::FromMilliseconds(kActivationRecedeTimeMs); |
| 195 idle_alpha_start_ = idle_alpha_; |
| 196 active_alpha_start_ = active_alpha_; |
| 197 idle_alpha_finish_ = 0; |
| 198 active_alpha_finish_ = 0; |
| 199 rotation_start_ = rotation_finish_ = rotation_; |
| 200 offset_start_ = offset_finish_ = offset_; |
| 201 break; |
| 202 case STATE_ACTIVATED_RECEDE: |
| 203 Finish(); |
| 204 break; |
| 205 case STATE_RECEDE: |
| 206 Finish(); |
| 207 break; |
| 208 }; |
| 209 |
| 210 return !IsFinished(); |
| 211 } |
| 212 |
| 213 bool Release(base::TimeTicks current_time, bool allow_activation) { |
| 214 if (state_ != STATE_ACTIVATED && state_ != STATE_PULL) |
| 215 return false; |
| 216 |
| 217 if (state_ == STATE_ACTIVATED && allow_activation) |
| 218 return false; |
| 219 |
| 220 start_time_ = current_time; |
| 221 idle_alpha_start_ = idle_alpha_; |
| 222 active_alpha_start_ = active_alpha_; |
| 223 offset_start_ = offset_; |
| 224 rotation_start_ = rotation_; |
| 225 |
| 226 if (offset_ < kPullActivationThreshold || !allow_activation) { |
| 227 state_ = STATE_RECEDE; |
| 228 duration_ = base::TimeDelta::FromMilliseconds(kRecedeTimeMs); |
| 229 idle_alpha_finish_ = 0; |
| 230 active_alpha_finish_ = 0; |
| 231 offset_finish_ = 0; |
| 232 rotation_finish_ = rotation_start_ - 180.f; |
| 233 return false; |
| 234 } |
| 235 |
| 236 state_ = STATE_ACTIVATED; |
| 237 duration_ = base::TimeDelta::FromMilliseconds(kActivationTimeMs); |
| 238 activated_start_time_ = current_time; |
| 239 idle_alpha_finish_ = idle_alpha_start_; |
| 240 active_alpha_finish_ = active_alpha_start_; |
| 241 offset_finish_ = kPullActivationThreshold; |
| 242 rotation_finish_ = rotation_start_ + 360.f; |
| 243 return true; |
| 244 } |
| 245 |
| 246 void Finish() { |
| 247 Detach(); |
| 248 idle_layer_->SetIsDrawable(false); |
| 249 active_layer_->SetIsDrawable(false); |
| 250 offset_ = 0; |
| 251 idle_alpha_ = 0; |
| 252 active_alpha_ = 0; |
| 253 rotation_ = 0; |
| 254 state_ = STATE_IDLE; |
| 255 } |
| 256 |
| 257 void ApplyToLayers(const gfx::SizeF& size, cc::Layer* parent) { |
| 258 if (IsFinished()) |
| 259 return; |
| 260 |
| 261 UpdateLayer(idle_layer_.get(), |
| 262 parent, |
| 263 resource_manager_->GetUIResourceId(kIdleResourceType), |
| 264 size, |
| 265 offset_, |
| 266 idle_alpha_, |
| 267 rotation_); |
| 268 UpdateLayer(active_layer_.get(), |
| 269 parent, |
| 270 resource_manager_->GetUIResourceId(kActiveResourceType), |
| 271 size, |
| 272 offset_, |
| 273 active_alpha_, |
| 274 rotation_); |
| 275 } |
| 276 |
| 277 bool IsFinished() const { return state_ == STATE_IDLE; } |
| 278 |
| 279 private: |
| 280 enum State { |
| 281 STATE_IDLE = 0, |
| 282 STATE_PULL, |
| 283 STATE_ACTIVATED, |
| 284 STATE_ACTIVATED_RECEDE, |
| 285 STATE_RECEDE |
| 286 }; |
| 287 |
| 288 void Detach() { |
| 289 idle_layer_->RemoveFromParent(); |
| 290 active_layer_->RemoveFromParent(); |
| 291 } |
| 292 |
| 293 ui::SystemUIResourceManager* const resource_manager_; |
| 294 |
| 295 scoped_refptr<cc::UIResourceLayer> idle_layer_; |
| 296 scoped_refptr<cc::UIResourceLayer> active_layer_; |
| 297 |
| 298 float idle_alpha_; |
| 299 float active_alpha_; |
| 300 float offset_; |
| 301 float rotation_; |
| 302 |
| 303 float idle_alpha_start_; |
| 304 float idle_alpha_finish_; |
| 305 float active_alpha_start_; |
| 306 float active_alpha_finish_; |
| 307 float offset_start_; |
| 308 float offset_finish_; |
| 309 float rotation_start_; |
| 310 float rotation_finish_; |
| 311 |
| 312 base::TimeTicks start_time_; |
| 313 base::TimeTicks activated_start_time_; |
| 314 base::TimeDelta duration_; |
| 315 |
| 316 State state_; |
| 317 }; |
| 318 |
| 319 OverscrollRefresh::OverscrollRefresh( |
| 320 ui::SystemUIResourceManager* resource_manager, |
| 321 OverscrollRefreshClient* client) |
| 322 : client_(client), |
| 323 scrolled_to_top_(true), |
| 324 scroll_consumption_state_(DISABLED), |
| 325 effect_(new Effect(resource_manager)) { |
| 326 DCHECK(client); |
| 327 } |
| 328 |
| 329 OverscrollRefresh::~OverscrollRefresh() { |
| 330 } |
| 331 |
| 332 void OverscrollRefresh::Reset() { |
| 333 scroll_consumption_state_ = DISABLED; |
| 334 effect_->Finish(); |
| 335 } |
| 336 |
| 337 void OverscrollRefresh::OnScrollBegin() { |
| 338 bool allow_activation = false; |
| 339 Release(allow_activation); |
| 340 if (scrolled_to_top_) |
| 341 scroll_consumption_state_ = AWAITING_SCROLL_UPDATE_ACK; |
| 342 } |
| 343 |
| 344 void OverscrollRefresh::OnScrollEnd(const gfx::Vector2dF& scroll_velocity) { |
| 345 bool allow_activation = scroll_velocity.y() > kMinFlingVelocityForActivation; |
| 346 Release(allow_activation); |
| 347 } |
| 348 |
| 349 void OverscrollRefresh::OnScrollUpdateAck(bool was_consumed) { |
| 350 if (scroll_consumption_state_ != AWAITING_SCROLL_UPDATE_ACK) |
| 351 return; |
| 352 |
| 353 scroll_consumption_state_ = was_consumed ? DISABLED : ENABLED; |
| 354 } |
| 355 |
| 356 bool OverscrollRefresh::WillHandleScrollUpdate( |
| 357 const gfx::Vector2dF& scroll_delta) { |
| 358 if (viewport_size_.IsEmpty()) |
| 359 return false; |
| 360 |
| 361 switch (scroll_consumption_state_) { |
| 362 case DISABLED: |
| 363 return false; |
| 364 |
| 365 case AWAITING_SCROLL_UPDATE_ACK: |
| 366 // If the initial scroll motion is downward, never allow activation. |
| 367 if (scroll_delta.y() <= 0) |
| 368 scroll_consumption_state_ = DISABLED; |
| 369 return false; |
| 370 |
| 371 case ENABLED: { |
| 372 float normalized_delta = scroll_delta.y() / min(viewport_size_.height(), |
| 373 viewport_size_.width()); |
| 374 effect_->Pull(normalized_delta); |
| 375 return true; |
| 376 } |
| 377 } |
| 378 |
| 379 NOTREACHED() << "Invalid overscroll state: " << scroll_consumption_state_; |
| 380 return false; |
| 381 } |
| 382 |
| 383 bool OverscrollRefresh::Animate(base::TimeTicks current_time, |
| 384 cc::Layer* parent_layer) { |
| 385 DCHECK(parent_layer); |
| 386 if (effect_->IsFinished()) |
| 387 return false; |
| 388 |
| 389 if (effect_->Animate(current_time, client_->IsStillRefreshing())) |
| 390 effect_->ApplyToLayers(viewport_size_, parent_layer); |
| 391 |
| 392 return !effect_->IsFinished(); |
| 393 } |
| 394 |
| 395 bool OverscrollRefresh::IsActive() const { |
| 396 return scroll_consumption_state_ == ENABLED || !effect_->IsFinished(); |
| 397 } |
| 398 |
| 399 bool OverscrollRefresh::IsAwaitingScrollUpdateAck() const { |
| 400 return scroll_consumption_state_ == AWAITING_SCROLL_UPDATE_ACK; |
| 401 } |
| 402 |
| 403 void OverscrollRefresh::UpdateDisplay( |
| 404 const gfx::SizeF& viewport_size, |
| 405 const gfx::Vector2dF& content_scroll_offset) { |
| 406 viewport_size_ = viewport_size; |
| 407 scrolled_to_top_ = content_scroll_offset.y() == 0; |
| 408 } |
| 409 |
| 410 void OverscrollRefresh::Release(bool allow_activation) { |
| 411 if (scroll_consumption_state_ == ENABLED) { |
| 412 if (effect_->Release(base::TimeTicks::Now(), allow_activation)) |
| 413 client_->TriggerRefresh(); |
| 414 } |
| 415 scroll_consumption_state_ = DISABLED; |
| 416 } |
| 417 |
| 418 } // namespace content |
OLD | NEW |