Roll Libjingle 8093:8097
[chromium-blink-merge.git] / cc / animation / scrollbar_animation_controller_thinning.cc
blob0405074b55a311b1c2f5fa5543a6219c5d4cdf20
1 // Copyright 2013 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 "cc/animation/scrollbar_animation_controller_thinning.h"
7 #include "base/time/time.h"
8 #include "cc/layers/layer_impl.h"
9 #include "cc/layers/scrollbar_layer_impl_base.h"
11 namespace {
12 const float kIdleThicknessScale = 0.4f;
13 const float kIdleOpacity = 0.7f;
14 const float kDefaultMouseMoveDistanceToTriggerAnimation = 25.f;
17 namespace cc {
19 scoped_ptr<ScrollbarAnimationControllerThinning>
20 ScrollbarAnimationControllerThinning::Create(
21 LayerImpl* scroll_layer,
22 ScrollbarAnimationControllerClient* client,
23 base::TimeDelta delay_before_starting,
24 base::TimeDelta resize_delay_before_starting,
25 base::TimeDelta duration) {
26 return make_scoped_ptr(
27 new ScrollbarAnimationControllerThinning(scroll_layer,
28 client,
29 delay_before_starting,
30 resize_delay_before_starting,
31 duration));
34 ScrollbarAnimationControllerThinning::ScrollbarAnimationControllerThinning(
35 LayerImpl* scroll_layer,
36 ScrollbarAnimationControllerClient* client,
37 base::TimeDelta delay_before_starting,
38 base::TimeDelta resize_delay_before_starting,
39 base::TimeDelta duration)
40 : ScrollbarAnimationController(client,
41 delay_before_starting,
42 resize_delay_before_starting,
43 duration),
44 scroll_layer_(scroll_layer),
45 mouse_is_over_scrollbar_(false),
46 mouse_is_near_scrollbar_(false),
47 thickness_change_(NONE),
48 opacity_change_(NONE),
49 mouse_move_distance_to_trigger_animation_(
50 kDefaultMouseMoveDistanceToTriggerAnimation) {
51 ApplyOpacityAndThumbThicknessScale(kIdleOpacity, kIdleThicknessScale);
54 ScrollbarAnimationControllerThinning::~ScrollbarAnimationControllerThinning() {
57 void ScrollbarAnimationControllerThinning::RunAnimationFrame(float progress) {
58 float opacity = OpacityAtAnimationProgress(progress);
59 float thumb_thickness_scale = ThumbThicknessScaleAtAnimationProgress(
60 progress);
61 ApplyOpacityAndThumbThicknessScale(opacity, thumb_thickness_scale);
62 if (progress == 1.f) {
63 opacity_change_ = NONE;
64 thickness_change_ = NONE;
65 StopAnimation();
69 void ScrollbarAnimationControllerThinning::DidMouseMoveOffScrollbar() {
70 mouse_is_over_scrollbar_ = false;
71 mouse_is_near_scrollbar_ = false;
72 opacity_change_ = DECREASE;
73 thickness_change_ = DECREASE;
74 StartAnimation();
77 void ScrollbarAnimationControllerThinning::DidScrollUpdate(bool on_resize) {
78 ScrollbarAnimationController::DidScrollUpdate(on_resize);
79 ApplyOpacityAndThumbThicknessScale(
80 1, mouse_is_near_scrollbar_ ? 1.f : kIdleThicknessScale);
82 if (!mouse_is_over_scrollbar_)
83 opacity_change_ = DECREASE;
86 void ScrollbarAnimationControllerThinning::DidMouseMoveNear(float distance) {
87 bool mouse_is_over_scrollbar = distance == 0.0;
88 bool mouse_is_near_scrollbar =
89 distance < mouse_move_distance_to_trigger_animation_;
91 if (mouse_is_over_scrollbar == mouse_is_over_scrollbar_ &&
92 mouse_is_near_scrollbar == mouse_is_near_scrollbar_)
93 return;
95 if (mouse_is_over_scrollbar_ != mouse_is_over_scrollbar) {
96 mouse_is_over_scrollbar_ = mouse_is_over_scrollbar;
97 opacity_change_ = mouse_is_over_scrollbar_ ? INCREASE : DECREASE;
100 if (mouse_is_near_scrollbar_ != mouse_is_near_scrollbar) {
101 mouse_is_near_scrollbar_ = mouse_is_near_scrollbar;
102 thickness_change_ = mouse_is_near_scrollbar_ ? INCREASE : DECREASE;
105 StartAnimation();
108 float ScrollbarAnimationControllerThinning::OpacityAtAnimationProgress(
109 float progress) {
110 if (opacity_change_ == NONE)
111 return mouse_is_over_scrollbar_ ? 1.f : kIdleOpacity;
112 float factor = opacity_change_ == INCREASE ? progress : (1.f - progress);
113 float ret = ((1.f - kIdleOpacity) * factor) + kIdleOpacity;
114 return ret;
117 float
118 ScrollbarAnimationControllerThinning::ThumbThicknessScaleAtAnimationProgress(
119 float progress) {
120 if (thickness_change_ == NONE)
121 return mouse_is_near_scrollbar_ ? 1.f : kIdleThicknessScale;
122 float factor = thickness_change_ == INCREASE ? progress : (1.f - progress);
123 return ((1.f - kIdleThicknessScale) * factor) + kIdleThicknessScale;
126 float ScrollbarAnimationControllerThinning::AdjustScale(
127 float new_value,
128 float current_value,
129 AnimationChange animation_change) {
130 if (animation_change == INCREASE && current_value > new_value)
131 return current_value;
132 if (animation_change == DECREASE && current_value < new_value)
133 return current_value;
134 return new_value;
137 void ScrollbarAnimationControllerThinning::ApplyOpacityAndThumbThicknessScale(
138 float opacity, float thumb_thickness_scale) {
139 if (!scroll_layer_->scrollbars())
140 return;
142 LayerImpl::ScrollbarSet* scrollbars = scroll_layer_->scrollbars();
143 for (LayerImpl::ScrollbarSet::iterator it = scrollbars->begin();
144 it != scrollbars->end();
145 ++it) {
146 ScrollbarLayerImplBase* scrollbar = *it;
147 if (scrollbar->is_overlay_scrollbar()) {
148 float effectiveOpacity =
149 scrollbar->CanScrollOrientation()
150 ? AdjustScale(opacity, scrollbar->opacity(), opacity_change_)
151 : 0;
153 scrollbar->SetOpacity(effectiveOpacity);
154 scrollbar->SetThumbThicknessScaleFactor(
155 AdjustScale(thumb_thickness_scale,
156 scrollbar->thumb_thickness_scale_factor(),
157 thickness_change_));
162 } // namespace cc