| 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 22 matching lines...) Expand all Loading... |
| 33 } | 33 } |
| 34 | 34 |
| 35 ScrollbarAnimationController::ScrollbarAnimationController( | 35 ScrollbarAnimationController::ScrollbarAnimationController( |
| 36 ElementId scroll_element_id, | 36 ElementId scroll_element_id, |
| 37 ScrollbarAnimationControllerClient* client, | 37 ScrollbarAnimationControllerClient* client, |
| 38 base::TimeDelta fade_delay, | 38 base::TimeDelta fade_delay, |
| 39 base::TimeDelta fade_duration) | 39 base::TimeDelta fade_duration) |
| 40 : client_(client), | 40 : client_(client), |
| 41 fade_delay_(fade_delay), | 41 fade_delay_(fade_delay), |
| 42 fade_duration_(fade_duration), | 42 fade_duration_(fade_duration), |
| 43 need_trigger_scrollbar_show_(false), | 43 need_trigger_scrollbar_fade_in_(false), |
| 44 is_animating_(false), | 44 is_animating_(false), |
| 45 animation_change_(NONE), | 45 animation_change_(NONE), |
| 46 scroll_element_id_(scroll_element_id), | 46 scroll_element_id_(scroll_element_id), |
| 47 currently_scrolling_(false), | 47 currently_scrolling_(false), |
| 48 show_in_fast_scroll_(false), | 48 show_in_fast_scroll_(false), |
| 49 opacity_(0.0f), | 49 opacity_(0.0f), |
| 50 show_scrollbars_on_scroll_gesture_(false), | 50 show_scrollbars_on_scroll_gesture_(false), |
| 51 need_thinning_animation_(false), | 51 need_thinning_animation_(false), |
| 52 weak_factory_(this) { | 52 weak_factory_(this) { |
| 53 ApplyOpacityToScrollbars(0.0f); | 53 ApplyOpacityToScrollbars(0.0f); |
| 54 } | 54 } |
| 55 | 55 |
| 56 ScrollbarAnimationController::ScrollbarAnimationController( | 56 ScrollbarAnimationController::ScrollbarAnimationController( |
| 57 ElementId scroll_element_id, | 57 ElementId scroll_element_id, |
| 58 ScrollbarAnimationControllerClient* client, | 58 ScrollbarAnimationControllerClient* client, |
| 59 base::TimeDelta fade_delay, | 59 base::TimeDelta fade_delay, |
| 60 base::TimeDelta fade_duration, | 60 base::TimeDelta fade_duration, |
| 61 base::TimeDelta thinning_duration) | 61 base::TimeDelta thinning_duration) |
| 62 : client_(client), | 62 : client_(client), |
| 63 fade_delay_(fade_delay), | 63 fade_delay_(fade_delay), |
| 64 fade_duration_(fade_duration), | 64 fade_duration_(fade_duration), |
| 65 need_trigger_scrollbar_show_(false), | 65 need_trigger_scrollbar_fade_in_(false), |
| 66 is_animating_(false), | 66 is_animating_(false), |
| 67 animation_change_(NONE), | 67 animation_change_(NONE), |
| 68 scroll_element_id_(scroll_element_id), | 68 scroll_element_id_(scroll_element_id), |
| 69 currently_scrolling_(false), | 69 currently_scrolling_(false), |
| 70 show_in_fast_scroll_(false), | 70 show_in_fast_scroll_(false), |
| 71 opacity_(0.0f), | 71 opacity_(0.0f), |
| 72 show_scrollbars_on_scroll_gesture_(true), | 72 show_scrollbars_on_scroll_gesture_(true), |
| 73 need_thinning_animation_(true), | 73 need_thinning_animation_(true), |
| 74 weak_factory_(this) { | 74 weak_factory_(this) { |
| 75 vertical_controller_ = SingleScrollbarAnimationControllerThinning::Create( | 75 vertical_controller_ = SingleScrollbarAnimationControllerThinning::Create( |
| 76 scroll_element_id, ScrollbarOrientation::VERTICAL, client, | 76 scroll_element_id, ScrollbarOrientation::VERTICAL, client, |
| 77 thinning_duration); | 77 thinning_duration); |
| 78 horizontal_controller_ = SingleScrollbarAnimationControllerThinning::Create( | 78 horizontal_controller_ = SingleScrollbarAnimationControllerThinning::Create( |
| 79 scroll_element_id, ScrollbarOrientation::HORIZONTAL, client, | 79 scroll_element_id, ScrollbarOrientation::HORIZONTAL, client, |
| 80 thinning_duration); | 80 thinning_duration); |
| 81 |
| 81 ApplyOpacityToScrollbars(0.0f); | 82 ApplyOpacityToScrollbars(0.0f); |
| 82 } | 83 } |
| 83 | 84 |
| 84 ScrollbarAnimationController::~ScrollbarAnimationController() {} | 85 ScrollbarAnimationController::~ScrollbarAnimationController() {} |
| 85 | 86 |
| 86 ScrollbarSet ScrollbarAnimationController::Scrollbars() const { | 87 ScrollbarSet ScrollbarAnimationController::Scrollbars() const { |
| 87 return client_->ScrollbarsFor(scroll_element_id_); | 88 return client_->ScrollbarsFor(scroll_element_id_); |
| 88 } | 89 } |
| 89 | 90 |
| 90 SingleScrollbarAnimationControllerThinning& | 91 SingleScrollbarAnimationControllerThinning& |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 } | 242 } |
| 242 | 243 |
| 243 void ScrollbarAnimationController::DidMouseLeave() { | 244 void ScrollbarAnimationController::DidMouseLeave() { |
| 244 if (!need_thinning_animation_) | 245 if (!need_thinning_animation_) |
| 245 return; | 246 return; |
| 246 | 247 |
| 247 vertical_controller_->DidMouseLeave(); | 248 vertical_controller_->DidMouseLeave(); |
| 248 horizontal_controller_->DidMouseLeave(); | 249 horizontal_controller_->DidMouseLeave(); |
| 249 | 250 |
| 250 delayed_scrollbar_animation_.Cancel(); | 251 delayed_scrollbar_animation_.Cancel(); |
| 251 need_trigger_scrollbar_show_ = false; | 252 need_trigger_scrollbar_fade_in_ = false; |
| 252 | 253 |
| 253 if (ScrollbarsHidden() || Captured()) | 254 if (ScrollbarsHidden() || Captured()) |
| 254 return; | 255 return; |
| 255 | 256 |
| 256 PostDelayedAnimation(FADE_OUT); | 257 PostDelayedAnimation(FADE_OUT); |
| 257 } | 258 } |
| 258 | 259 |
| 259 void ScrollbarAnimationController::DidMouseMoveNear( | 260 void ScrollbarAnimationController::DidMouseMove( |
| 260 ScrollbarOrientation orientation, | 261 const gfx::PointF& device_viewport_point) { |
| 261 float distance) { | |
| 262 if (!need_thinning_animation_) | 262 if (!need_thinning_animation_) |
| 263 return; | 263 return; |
| 264 | 264 |
| 265 bool need_trigger_scrollbar_show_before = need_trigger_scrollbar_show_; | 265 bool need_trigger_scrollbar_fade_in_before = need_trigger_scrollbar_fade_in_; |
| 266 | 266 |
| 267 GetScrollbarAnimationController(orientation).DidMouseMoveNear(distance); | 267 vertical_controller_->DidMouseMove(device_viewport_point); |
| 268 horizontal_controller_->DidMouseMove(device_viewport_point); |
| 268 | 269 |
| 269 need_trigger_scrollbar_show_ = | 270 need_trigger_scrollbar_fade_in_ = MouseIsNearAnyScrollbar(); |
| 270 CalcNeedTriggerScrollbarShow(orientation, distance); | |
| 271 | 271 |
| 272 if (Captured()) | 272 if (Captured()) |
| 273 return; | 273 return; |
| 274 | 274 |
| 275 if (ScrollbarsHidden()) { | 275 if (ScrollbarsHidden()) { |
| 276 if (need_trigger_scrollbar_show_before != need_trigger_scrollbar_show_) { | 276 if (need_trigger_scrollbar_fade_in_before != |
| 277 if (need_trigger_scrollbar_show_) { | 277 need_trigger_scrollbar_fade_in_) { |
| 278 if (need_trigger_scrollbar_fade_in_) { |
| 278 PostDelayedAnimation(FADE_IN); | 279 PostDelayedAnimation(FADE_IN); |
| 279 } else { | 280 } else { |
| 280 delayed_scrollbar_animation_.Cancel(); | 281 delayed_scrollbar_animation_.Cancel(); |
| 281 } | 282 } |
| 282 } | 283 } |
| 283 } else { | 284 } else { |
| 284 if (MouseIsNearAnyScrollbar()) { | 285 if (MouseIsNearAnyScrollbar()) { |
| 285 Show(); | 286 Show(); |
| 286 StopAnimation(); | 287 StopAnimation(); |
| 287 } else if (!is_animating_) { | 288 } else if (!is_animating_) { |
| 288 PostDelayedAnimation(FADE_OUT); | 289 PostDelayedAnimation(FADE_OUT); |
| 289 } | 290 } |
| 290 } | 291 } |
| 291 } | 292 } |
| 292 | 293 |
| 293 bool ScrollbarAnimationController::CalcNeedTriggerScrollbarShow( | 294 bool ScrollbarAnimationController::MouseIsOverScrollbarThumb( |
| 294 ScrollbarOrientation orientation, | 295 ScrollbarOrientation orientation) const { |
| 295 float distance) const { | |
| 296 DCHECK(need_thinning_animation_); | 296 DCHECK(need_thinning_animation_); |
| 297 | 297 return GetScrollbarAnimationController(orientation) |
| 298 if (vertical_controller_->mouse_is_over_scrollbar() || | 298 .mouse_is_over_scrollbar_thumb(); |
| 299 horizontal_controller_->mouse_is_over_scrollbar()) | |
| 300 return true; | |
| 301 | |
| 302 for (ScrollbarLayerImplBase* scrollbar : Scrollbars()) { | |
| 303 if (scrollbar->orientation() != orientation) | |
| 304 continue; | |
| 305 | |
| 306 if (distance < kMouseMoveDistanceToTriggerFadeIn) | |
| 307 return true; | |
| 308 } | |
| 309 | |
| 310 return false; | |
| 311 } | 299 } |
| 312 | 300 |
| 313 bool ScrollbarAnimationController::MouseIsOverScrollbar( | 301 bool ScrollbarAnimationController::MouseIsNearScrollbarThumb( |
| 314 ScrollbarOrientation orientation) const { | 302 ScrollbarOrientation orientation) const { |
| 315 DCHECK(need_thinning_animation_); | 303 DCHECK(need_thinning_animation_); |
| 316 return GetScrollbarAnimationController(orientation).mouse_is_over_scrollbar(); | 304 return GetScrollbarAnimationController(orientation) |
| 305 .mouse_is_near_scrollbar_thumb(); |
| 317 } | 306 } |
| 318 | 307 |
| 319 bool ScrollbarAnimationController::MouseIsNearScrollbar( | 308 bool ScrollbarAnimationController::MouseIsNearScrollbar( |
| 320 ScrollbarOrientation orientation) const { | 309 ScrollbarOrientation orientation) const { |
| 321 DCHECK(need_thinning_animation_); | 310 DCHECK(need_thinning_animation_); |
| 322 return GetScrollbarAnimationController(orientation).mouse_is_near_scrollbar(); | 311 return GetScrollbarAnimationController(orientation) |
| 312 .mouse_is_near_scrollbar_track(); |
| 323 } | 313 } |
| 324 | 314 |
| 325 bool ScrollbarAnimationController::MouseIsNearAnyScrollbar() const { | 315 bool ScrollbarAnimationController::MouseIsNearAnyScrollbar() const { |
| 326 DCHECK(need_thinning_animation_); | 316 DCHECK(need_thinning_animation_); |
| 327 return vertical_controller_->mouse_is_near_scrollbar() || | 317 return vertical_controller_->mouse_is_near_scrollbar_track() || |
| 328 horizontal_controller_->mouse_is_near_scrollbar(); | 318 horizontal_controller_->mouse_is_near_scrollbar_track(); |
| 329 } | 319 } |
| 330 | 320 |
| 331 bool ScrollbarAnimationController::ScrollbarsHidden() const { | 321 bool ScrollbarAnimationController::ScrollbarsHidden() const { |
| 332 return opacity_ == 0.0f; | 322 return opacity_ == 0.0f; |
| 333 } | 323 } |
| 334 | 324 |
| 335 bool ScrollbarAnimationController::Captured() const { | 325 bool ScrollbarAnimationController::Captured() const { |
| 336 DCHECK(need_thinning_animation_); | 326 DCHECK(need_thinning_animation_); |
| 337 return vertical_controller_->captured() || horizontal_controller_->captured(); | 327 return GetScrollbarAnimationController(VERTICAL).captured() || |
| 328 GetScrollbarAnimationController(HORIZONTAL).captured(); |
| 338 } | 329 } |
| 339 | 330 |
| 340 void ScrollbarAnimationController::Show() { | 331 void ScrollbarAnimationController::Show() { |
| 341 delayed_scrollbar_animation_.Cancel(); | 332 delayed_scrollbar_animation_.Cancel(); |
| 342 ApplyOpacityToScrollbars(1.0f); | 333 ApplyOpacityToScrollbars(1.0f); |
| 343 } | 334 } |
| 344 | 335 |
| 345 void ScrollbarAnimationController::ApplyOpacityToScrollbars(float opacity) { | 336 void ScrollbarAnimationController::ApplyOpacityToScrollbars(float opacity) { |
| 346 for (ScrollbarLayerImplBase* scrollbar : Scrollbars()) { | 337 for (ScrollbarLayerImplBase* scrollbar : Scrollbars()) { |
| 347 if (!scrollbar->is_overlay_scrollbar()) | 338 DCHECK(scrollbar->is_overlay_scrollbar()); |
| 348 continue; | |
| 349 float effective_opacity = scrollbar->CanScrollOrientation() ? opacity : 0; | 339 float effective_opacity = scrollbar->CanScrollOrientation() ? opacity : 0; |
| 350 scrollbar->SetOverlayScrollbarLayerOpacityAnimated(effective_opacity); | 340 scrollbar->SetOverlayScrollbarLayerOpacityAnimated(effective_opacity); |
| 351 } | 341 } |
| 352 | 342 |
| 353 bool previouslyVisible = opacity_ > 0.0f; | 343 bool previouslyVisible = opacity_ > 0.0f; |
| 354 bool currentlyVisible = opacity > 0.0f; | 344 bool currentlyVisible = opacity > 0.0f; |
| 355 | 345 |
| 356 if (opacity_ != opacity) | 346 if (opacity_ != opacity) |
| 357 client_->SetNeedsRedrawForScrollbarAnimation(); | 347 client_->SetNeedsRedrawForScrollbarAnimation(); |
| 358 | 348 |
| 359 opacity_ = opacity; | 349 opacity_ = opacity; |
| 360 | 350 |
| 361 if (previouslyVisible != currentlyVisible) | 351 if (previouslyVisible != currentlyVisible) |
| 362 client_->DidChangeScrollbarVisibility(); | 352 client_->DidChangeScrollbarVisibility(); |
| 363 } | 353 } |
| 364 | 354 |
| 365 } // namespace cc | 355 } // namespace cc |
| OLD | NEW |