Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "cc/input/scrollbar_animation_controller.h" | 5 #include "cc/input/scrollbar_animation_controller.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
| 10 #include "cc/trees/layer_tree_impl.h" | 10 #include "cc/trees/layer_tree_impl.h" |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 23 fade_out_duration)); | 23 fade_out_duration)); |
| 24 } | 24 } |
| 25 | 25 |
| 26 std::unique_ptr<ScrollbarAnimationController> | 26 std::unique_ptr<ScrollbarAnimationController> |
| 27 ScrollbarAnimationController::CreateScrollbarAnimationControllerAuraOverlay( | 27 ScrollbarAnimationController::CreateScrollbarAnimationControllerAuraOverlay( |
| 28 int scroll_layer_id, | 28 int scroll_layer_id, |
| 29 ScrollbarAnimationControllerClient* client, | 29 ScrollbarAnimationControllerClient* client, |
| 30 base::TimeDelta show_delay, | 30 base::TimeDelta show_delay, |
| 31 base::TimeDelta fade_out_delay, | 31 base::TimeDelta fade_out_delay, |
| 32 base::TimeDelta fade_out_resize_delay, | 32 base::TimeDelta fade_out_resize_delay, |
| 33 base::TimeDelta fade_in_duration, | |
| 33 base::TimeDelta fade_out_duration, | 34 base::TimeDelta fade_out_duration, |
| 34 base::TimeDelta thinning_duration) { | 35 base::TimeDelta thinning_duration) { |
| 35 return base::WrapUnique(new ScrollbarAnimationController( | 36 return base::WrapUnique(new ScrollbarAnimationController( |
| 36 scroll_layer_id, client, show_delay, fade_out_delay, | 37 scroll_layer_id, client, show_delay, fade_out_delay, |
| 37 fade_out_resize_delay, fade_out_duration, thinning_duration)); | 38 fade_out_resize_delay, fade_in_duration, fade_out_duration, |
| 39 thinning_duration)); | |
| 38 } | 40 } |
| 39 | 41 |
| 40 ScrollbarAnimationController::ScrollbarAnimationController( | 42 ScrollbarAnimationController::ScrollbarAnimationController( |
| 41 int scroll_layer_id, | 43 int scroll_layer_id, |
| 42 ScrollbarAnimationControllerClient* client, | 44 ScrollbarAnimationControllerClient* client, |
| 43 base::TimeDelta fade_out_delay, | 45 base::TimeDelta fade_out_delay, |
| 44 base::TimeDelta fade_out_resize_delay, | 46 base::TimeDelta fade_out_resize_delay, |
| 45 base::TimeDelta fade_out_duration) | 47 base::TimeDelta fade_out_duration) |
| 46 : client_(client), | 48 : client_(client), |
| 47 fade_out_delay_(fade_out_delay), | 49 fade_out_delay_(fade_out_delay), |
| 48 fade_out_resize_delay_(fade_out_resize_delay), | 50 fade_out_resize_delay_(fade_out_resize_delay), |
| 51 fade_out_duration_(fade_out_duration), | |
| 49 need_trigger_scrollbar_show_(false), | 52 need_trigger_scrollbar_show_(false), |
| 50 is_animating_(false), | 53 is_animating_(false), |
| 54 animation_change_(NONE), | |
| 51 scroll_layer_id_(scroll_layer_id), | 55 scroll_layer_id_(scroll_layer_id), |
| 52 currently_scrolling_(false), | 56 currently_scrolling_(false), |
| 53 show_in_fast_scroll_(false), | 57 show_in_fast_scroll_(false), |
| 54 opacity_(0.0f), | 58 opacity_(0.0f), |
| 55 fade_out_duration_(fade_out_duration), | |
| 56 show_scrollbars_on_scroll_gesture_(false), | 59 show_scrollbars_on_scroll_gesture_(false), |
| 57 need_thinning_animation_(false), | 60 need_thinning_animation_(false), |
| 58 weak_factory_(this) { | 61 weak_factory_(this) { |
| 59 ApplyOpacityToScrollbars(0.0f); | 62 ApplyOpacityToScrollbars(0.0f); |
| 60 } | 63 } |
| 61 | 64 |
| 62 ScrollbarAnimationController::ScrollbarAnimationController( | 65 ScrollbarAnimationController::ScrollbarAnimationController( |
| 63 int scroll_layer_id, | 66 int scroll_layer_id, |
| 64 ScrollbarAnimationControllerClient* client, | 67 ScrollbarAnimationControllerClient* client, |
| 65 base::TimeDelta show_delay, | 68 base::TimeDelta show_delay, |
| 66 base::TimeDelta fade_out_delay, | 69 base::TimeDelta fade_out_delay, |
| 67 base::TimeDelta fade_out_resize_delay, | 70 base::TimeDelta fade_out_resize_delay, |
| 71 base::TimeDelta fade_in_duration, | |
| 68 base::TimeDelta fade_out_duration, | 72 base::TimeDelta fade_out_duration, |
| 69 base::TimeDelta thinning_duration) | 73 base::TimeDelta thinning_duration) |
| 70 : client_(client), | 74 : client_(client), |
| 71 show_delay_(show_delay), | 75 show_delay_(show_delay), |
| 72 fade_out_delay_(fade_out_delay), | 76 fade_out_delay_(fade_out_delay), |
| 73 fade_out_resize_delay_(fade_out_resize_delay), | 77 fade_out_resize_delay_(fade_out_resize_delay), |
| 78 fade_in_duration_(fade_in_duration), | |
| 79 fade_out_duration_(fade_out_duration), | |
| 74 need_trigger_scrollbar_show_(false), | 80 need_trigger_scrollbar_show_(false), |
| 75 is_animating_(false), | 81 is_animating_(false), |
| 82 animation_change_(NONE), | |
| 76 scroll_layer_id_(scroll_layer_id), | 83 scroll_layer_id_(scroll_layer_id), |
| 77 currently_scrolling_(false), | 84 currently_scrolling_(false), |
| 78 show_in_fast_scroll_(false), | 85 show_in_fast_scroll_(false), |
| 79 opacity_(0.0f), | 86 opacity_(0.0f), |
| 80 fade_out_duration_(fade_out_duration), | |
| 81 show_scrollbars_on_scroll_gesture_(true), | 87 show_scrollbars_on_scroll_gesture_(true), |
| 82 need_thinning_animation_(true), | 88 need_thinning_animation_(true), |
| 83 weak_factory_(this) { | 89 weak_factory_(this) { |
| 84 vertical_controller_ = SingleScrollbarAnimationControllerThinning::Create( | 90 vertical_controller_ = SingleScrollbarAnimationControllerThinning::Create( |
| 85 scroll_layer_id, ScrollbarOrientation::VERTICAL, client, | 91 scroll_layer_id, ScrollbarOrientation::VERTICAL, client, |
| 86 thinning_duration); | 92 thinning_duration); |
| 87 horizontal_controller_ = SingleScrollbarAnimationControllerThinning::Create( | 93 horizontal_controller_ = SingleScrollbarAnimationControllerThinning::Create( |
| 88 scroll_layer_id, ScrollbarOrientation::HORIZONTAL, client, | 94 scroll_layer_id, ScrollbarOrientation::HORIZONTAL, client, |
| 89 thinning_duration); | 95 thinning_duration); |
| 90 ApplyOpacityToScrollbars(0.0f); | 96 ApplyOpacityToScrollbars(0.0f); |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 111 delayed_scrollbar_fade_out_.Cancel(); | 117 delayed_scrollbar_fade_out_.Cancel(); |
| 112 is_animating_ = true; | 118 is_animating_ = true; |
| 113 last_awaken_time_ = base::TimeTicks(); | 119 last_awaken_time_ = base::TimeTicks(); |
| 114 client_->SetNeedsAnimateForScrollbarAnimation(); | 120 client_->SetNeedsAnimateForScrollbarAnimation(); |
| 115 } | 121 } |
| 116 | 122 |
| 117 void ScrollbarAnimationController::StopAnimation() { | 123 void ScrollbarAnimationController::StopAnimation() { |
| 118 delayed_scrollbar_show_.Cancel(); | 124 delayed_scrollbar_show_.Cancel(); |
| 119 delayed_scrollbar_fade_out_.Cancel(); | 125 delayed_scrollbar_fade_out_.Cancel(); |
| 120 is_animating_ = false; | 126 is_animating_ = false; |
| 127 animation_change_ = NONE; | |
| 121 } | 128 } |
| 122 | 129 |
| 123 void ScrollbarAnimationController::PostDelayedShow() { | 130 void ScrollbarAnimationController::PostDelayedFadeIn() { |
| 124 DCHECK(delayed_scrollbar_fade_out_.IsCancelled()); | 131 DCHECK(delayed_scrollbar_fade_out_.IsCancelled()); |
| 132 animation_change_ = FADE_IN; | |
| 125 delayed_scrollbar_show_.Cancel(); | 133 delayed_scrollbar_show_.Cancel(); |
| 126 delayed_scrollbar_show_.Reset(base::Bind(&ScrollbarAnimationController::Show, | 134 delayed_scrollbar_show_.Reset( |
|
bokan
2017/04/13 14:38:14
Rename delayed_scrollbar_show too.
chaopeng
2017/04/13 15:15:37
Done.
| |
| 127 weak_factory_.GetWeakPtr())); | 135 base::Bind(&ScrollbarAnimationController::StartAnimation, |
| 136 weak_factory_.GetWeakPtr())); | |
| 128 client_->PostDelayedScrollbarAnimationTask(delayed_scrollbar_show_.callback(), | 137 client_->PostDelayedScrollbarAnimationTask(delayed_scrollbar_show_.callback(), |
| 129 show_delay_); | 138 show_delay_); |
| 130 } | 139 } |
| 131 | 140 |
| 132 void ScrollbarAnimationController::PostDelayedFadeOut(bool on_resize) { | 141 void ScrollbarAnimationController::PostDelayedFadeOut(bool on_resize) { |
| 133 DCHECK(delayed_scrollbar_show_.IsCancelled()); | 142 DCHECK(delayed_scrollbar_show_.IsCancelled()); |
| 143 animation_change_ = FADE_OUT; | |
| 134 base::TimeDelta delay = on_resize ? fade_out_resize_delay_ : fade_out_delay_; | 144 base::TimeDelta delay = on_resize ? fade_out_resize_delay_ : fade_out_delay_; |
| 135 delayed_scrollbar_fade_out_.Cancel(); | 145 delayed_scrollbar_fade_out_.Cancel(); |
| 136 delayed_scrollbar_fade_out_.Reset( | 146 delayed_scrollbar_fade_out_.Reset( |
| 137 base::Bind(&ScrollbarAnimationController::StartAnimation, | 147 base::Bind(&ScrollbarAnimationController::StartAnimation, |
| 138 weak_factory_.GetWeakPtr())); | 148 weak_factory_.GetWeakPtr())); |
| 139 client_->PostDelayedScrollbarAnimationTask( | 149 client_->PostDelayedScrollbarAnimationTask( |
| 140 delayed_scrollbar_fade_out_.callback(), delay); | 150 delayed_scrollbar_fade_out_.callback(), delay); |
| 141 } | 151 } |
| 142 | 152 |
| 143 bool ScrollbarAnimationController::Animate(base::TimeTicks now) { | 153 bool ScrollbarAnimationController::Animate(base::TimeTicks now) { |
| 144 bool animated = false; | 154 bool animated = false; |
| 145 | 155 |
| 146 if (is_animating_) { | 156 if (is_animating_ && animation_change_ != NONE) { |
| 147 if (last_awaken_time_.is_null()) | 157 if (last_awaken_time_.is_null()) |
| 148 last_awaken_time_ = now; | 158 last_awaken_time_ = now; |
| 149 | 159 |
| 150 float progress = AnimationProgressAtTime(now); | 160 float progress = AnimationProgressAtTime(now); |
| 151 RunAnimationFrame(progress); | 161 RunAnimationFrame(progress); |
| 152 | 162 |
| 153 if (is_animating_) | 163 if (is_animating_) |
| 154 client_->SetNeedsAnimateForScrollbarAnimation(); | 164 client_->SetNeedsAnimateForScrollbarAnimation(); |
| 155 animated = true; | 165 animated = true; |
| 156 } | 166 } |
| 157 | 167 |
| 158 if (need_thinning_animation_) { | 168 if (need_thinning_animation_) { |
| 159 animated |= vertical_controller_->Animate(now); | 169 animated |= vertical_controller_->Animate(now); |
| 160 animated |= horizontal_controller_->Animate(now); | 170 animated |= horizontal_controller_->Animate(now); |
| 161 } | 171 } |
| 162 | 172 |
| 163 return animated; | 173 return animated; |
| 164 } | 174 } |
| 165 | 175 |
| 166 float ScrollbarAnimationController::AnimationProgressAtTime( | 176 float ScrollbarAnimationController::AnimationProgressAtTime( |
| 167 base::TimeTicks now) { | 177 base::TimeTicks now) { |
| 168 base::TimeDelta delta = now - last_awaken_time_; | 178 base::TimeDelta delta = now - last_awaken_time_; |
| 169 float progress = delta.InSecondsF() / fade_out_duration_.InSecondsF(); | 179 base::TimeDelta duration = |
| 180 animation_change_ == FADE_IN ? fade_in_duration_ : fade_out_duration_; | |
| 181 float progress = delta.InSecondsF() / duration.InSecondsF(); | |
| 170 return std::max(std::min(progress, 1.f), 0.f); | 182 return std::max(std::min(progress, 1.f), 0.f); |
| 171 } | 183 } |
| 172 | 184 |
| 173 void ScrollbarAnimationController::RunAnimationFrame(float progress) { | 185 void ScrollbarAnimationController::RunAnimationFrame(float progress) { |
| 174 ApplyOpacityToScrollbars(1.f - progress); | 186 float opacity; |
| 187 | |
| 188 if (animation_change_ == FADE_IN) { | |
| 189 opacity = std::max(progress, opacity_); | |
| 190 } else { | |
|
bokan
2017/04/13 14:38:14
DCHECK above that animation_change_ isn't NONE
chaopeng
2017/04/13 15:15:37
Done.
| |
| 191 opacity = std::min(1.f - progress, opacity_); | |
| 192 } | |
| 193 | |
| 194 ApplyOpacityToScrollbars(opacity); | |
| 175 if (progress == 1.f) | 195 if (progress == 1.f) |
| 176 StopAnimation(); | 196 StopAnimation(); |
| 177 } | 197 } |
| 178 | 198 |
| 179 void ScrollbarAnimationController::DidScrollBegin() { | 199 void ScrollbarAnimationController::DidScrollBegin() { |
| 180 currently_scrolling_ = true; | 200 currently_scrolling_ = true; |
| 181 } | 201 } |
| 182 | 202 |
| 183 void ScrollbarAnimationController::DidScrollEnd() { | 203 void ScrollbarAnimationController::DidScrollEnd() { |
| 184 bool has_scrolled = show_in_fast_scroll_; | 204 bool has_scrolled = show_in_fast_scroll_; |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 289 | 309 |
| 290 need_trigger_scrollbar_show_ = | 310 need_trigger_scrollbar_show_ = |
| 291 CalcNeedTriggerScrollbarShow(orientation, distance); | 311 CalcNeedTriggerScrollbarShow(orientation, distance); |
| 292 | 312 |
| 293 if (Captured()) | 313 if (Captured()) |
| 294 return; | 314 return; |
| 295 | 315 |
| 296 if (ScrollbarsHidden()) { | 316 if (ScrollbarsHidden()) { |
| 297 if (need_trigger_scrollbar_show_before != need_trigger_scrollbar_show_) { | 317 if (need_trigger_scrollbar_show_before != need_trigger_scrollbar_show_) { |
| 298 if (need_trigger_scrollbar_show_) { | 318 if (need_trigger_scrollbar_show_) { |
| 299 PostDelayedShow(); | 319 PostDelayedFadeIn(); |
| 300 } else { | 320 } else { |
| 301 delayed_scrollbar_show_.Cancel(); | 321 delayed_scrollbar_show_.Cancel(); |
| 302 } | 322 } |
| 303 } | 323 } |
| 304 } else { | 324 } else { |
| 305 if (MouseIsNearAnyScrollbar()) { | 325 if (MouseIsNearAnyScrollbar()) { |
| 306 Show(); | 326 Show(); |
| 307 StopAnimation(); | 327 StopAnimation(); |
| 308 } else if (!is_animating_) { | 328 } else if (!is_animating_) { |
| 309 PostDelayedFadeOut(false); | 329 PostDelayedFadeOut(false); |
| 310 } | 330 } |
| 311 } | 331 } |
| 312 } | 332 } |
| 313 | 333 |
| 314 bool ScrollbarAnimationController::CalcNeedTriggerScrollbarShow( | 334 bool ScrollbarAnimationController::CalcNeedTriggerScrollbarShow( |
| 315 ScrollbarOrientation orientation, | 335 ScrollbarOrientation orientation, |
| 316 float distance) const { | 336 float distance) const { |
| 317 DCHECK(need_thinning_animation_); | 337 DCHECK(need_thinning_animation_); |
| 318 | 338 |
| 319 if (vertical_controller_->mouse_is_over_scrollbar() || | 339 if (vertical_controller_->mouse_is_over_scrollbar() || |
| 320 horizontal_controller_->mouse_is_over_scrollbar()) | 340 horizontal_controller_->mouse_is_over_scrollbar()) |
| 321 return true; | 341 return true; |
| 322 | 342 |
| 323 for (ScrollbarLayerImplBase* scrollbar : Scrollbars()) { | 343 for (ScrollbarLayerImplBase* scrollbar : Scrollbars()) { |
| 324 if (scrollbar->orientation() != orientation) | 344 if (scrollbar->orientation() != orientation) |
| 325 continue; | 345 continue; |
| 326 | 346 |
| 327 if (distance < kMouseMoveDistanceToTriggerShow) | 347 if (distance < kMouseMoveDistanceToTriggerFadeIn) |
| 328 return true; | 348 return true; |
| 329 } | 349 } |
| 330 | 350 |
| 331 return false; | 351 return false; |
| 332 } | 352 } |
| 333 | 353 |
| 334 bool ScrollbarAnimationController::MouseIsOverScrollbar( | 354 bool ScrollbarAnimationController::MouseIsOverScrollbar( |
| 335 ScrollbarOrientation orientation) const { | 355 ScrollbarOrientation orientation) const { |
| 336 DCHECK(need_thinning_animation_); | 356 DCHECK(need_thinning_animation_); |
| 337 return GetScrollbarAnimationController(orientation).mouse_is_over_scrollbar(); | 357 return GetScrollbarAnimationController(orientation).mouse_is_over_scrollbar(); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 352 bool ScrollbarAnimationController::ScrollbarsHidden() const { | 372 bool ScrollbarAnimationController::ScrollbarsHidden() const { |
| 353 return opacity_ == 0.0f; | 373 return opacity_ == 0.0f; |
| 354 } | 374 } |
| 355 | 375 |
| 356 bool ScrollbarAnimationController::Captured() const { | 376 bool ScrollbarAnimationController::Captured() const { |
| 357 DCHECK(need_thinning_animation_); | 377 DCHECK(need_thinning_animation_); |
| 358 return vertical_controller_->captured() || horizontal_controller_->captured(); | 378 return vertical_controller_->captured() || horizontal_controller_->captured(); |
| 359 } | 379 } |
| 360 | 380 |
| 361 void ScrollbarAnimationController::Show() { | 381 void ScrollbarAnimationController::Show() { |
| 362 delayed_scrollbar_show_.Cancel(); | |
|
bokan
2017/04/13 14:38:14
Why don't we want to cancel this now? If we scroll
chaopeng
2017/04/13 15:15:37
Yes, we should keep this.
| |
| 363 ApplyOpacityToScrollbars(1.0f); | 382 ApplyOpacityToScrollbars(1.0f); |
| 364 } | 383 } |
| 365 | 384 |
| 366 void ScrollbarAnimationController::ApplyOpacityToScrollbars(float opacity) { | 385 void ScrollbarAnimationController::ApplyOpacityToScrollbars(float opacity) { |
| 367 for (ScrollbarLayerImplBase* scrollbar : Scrollbars()) { | 386 for (ScrollbarLayerImplBase* scrollbar : Scrollbars()) { |
| 368 if (!scrollbar->is_overlay_scrollbar()) | 387 if (!scrollbar->is_overlay_scrollbar()) |
| 369 continue; | 388 continue; |
| 370 float effective_opacity = scrollbar->CanScrollOrientation() ? opacity : 0; | 389 float effective_opacity = scrollbar->CanScrollOrientation() ? opacity : 0; |
| 371 scrollbar->SetOverlayScrollbarLayerOpacityAnimated(effective_opacity); | 390 scrollbar->SetOverlayScrollbarLayerOpacityAnimated(effective_opacity); |
| 372 } | 391 } |
| 373 | 392 |
| 374 bool previouslyVisible = opacity_ > 0.0f; | 393 bool previouslyVisible = opacity_ > 0.0f; |
| 375 bool currentlyVisible = opacity > 0.0f; | 394 bool currentlyVisible = opacity > 0.0f; |
| 376 | 395 |
| 377 if (opacity_ != opacity) | 396 if (opacity_ != opacity) |
| 378 client_->SetNeedsRedrawForScrollbarAnimation(); | 397 client_->SetNeedsRedrawForScrollbarAnimation(); |
| 379 | 398 |
| 380 opacity_ = opacity; | 399 opacity_ = opacity; |
| 381 | 400 |
| 382 if (previouslyVisible != currentlyVisible) | 401 if (previouslyVisible != currentlyVisible) |
| 383 client_->DidChangeScrollbarVisibility(); | 402 client_->DidChangeScrollbarVisibility(); |
| 384 } | 403 } |
| 385 | 404 |
| 386 } // namespace cc | 405 } // namespace cc |
| OLD | NEW |