| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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_thinning.h" | 5 #include "cc/input/scrollbar_animation_controller_thinning.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/time/time.h" | 8 #include "base/time/time.h" |
| 9 #include "cc/layers/layer_impl.h" | 9 #include "cc/layers/layer_impl.h" |
| 10 #include "cc/layers/scrollbar_layer_impl_base.h" | 10 #include "cc/layers/scrollbar_layer_impl_base.h" |
| 11 #include "cc/trees/layer_tree_impl.h" | 11 #include "cc/trees/layer_tree_impl.h" |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 const float kIdleThicknessScale = 0.4f; | 14 const float kIdleThicknessScale = 0.4f; |
| 15 const float kIdleOpacity = 0.7f; | |
| 16 const float kDefaultMouseMoveDistanceToTriggerAnimation = 25.f; | 15 const float kDefaultMouseMoveDistanceToTriggerAnimation = 25.f; |
| 17 } | 16 } |
| 18 | 17 |
| 19 namespace cc { | 18 namespace cc { |
| 20 | 19 |
| 21 std::unique_ptr<ScrollbarAnimationControllerThinning> | 20 std::unique_ptr<ScrollbarAnimationControllerThinning> |
| 22 ScrollbarAnimationControllerThinning::Create( | 21 ScrollbarAnimationControllerThinning::Create( |
| 23 int scroll_layer_id, | 22 int scroll_layer_id, |
| 24 ScrollbarAnimationControllerClient* client, | 23 ScrollbarAnimationControllerClient* client, |
| 25 base::TimeDelta delay_before_starting, | 24 base::TimeDelta delay_before_starting, |
| 26 base::TimeDelta resize_delay_before_starting, | 25 base::TimeDelta resize_delay_before_starting, |
| 27 base::TimeDelta duration) { | 26 base::TimeDelta fade_duration, |
| 27 base::TimeDelta thinning_duration) { |
| 28 return base::WrapUnique(new ScrollbarAnimationControllerThinning( | 28 return base::WrapUnique(new ScrollbarAnimationControllerThinning( |
| 29 scroll_layer_id, client, delay_before_starting, | 29 scroll_layer_id, client, delay_before_starting, |
| 30 resize_delay_before_starting, duration)); | 30 resize_delay_before_starting, fade_duration, thinning_duration)); |
| 31 } | 31 } |
| 32 | 32 |
| 33 ScrollbarAnimationControllerThinning::ScrollbarAnimationControllerThinning( | 33 ScrollbarAnimationControllerThinning::ScrollbarAnimationControllerThinning( |
| 34 int scroll_layer_id, | 34 int scroll_layer_id, |
| 35 ScrollbarAnimationControllerClient* client, | 35 ScrollbarAnimationControllerClient* client, |
| 36 base::TimeDelta delay_before_starting, | 36 base::TimeDelta delay_before_starting, |
| 37 base::TimeDelta resize_delay_before_starting, | 37 base::TimeDelta resize_delay_before_starting, |
| 38 base::TimeDelta duration) | 38 base::TimeDelta fade_duration, |
| 39 base::TimeDelta thinning_duration) |
| 39 : ScrollbarAnimationController(scroll_layer_id, | 40 : ScrollbarAnimationController(scroll_layer_id, |
| 40 client, | 41 client, |
| 41 delay_before_starting, | 42 delay_before_starting, |
| 42 resize_delay_before_starting, | 43 resize_delay_before_starting), |
| 43 duration), | 44 opacity_(0.0f), |
| 44 captured_(false), | 45 captured_(false), |
| 45 mouse_is_over_scrollbar_(false), | 46 mouse_is_over_scrollbar_(false), |
| 46 mouse_is_near_scrollbar_(false), | 47 mouse_is_near_scrollbar_(false), |
| 47 thickness_change_(NONE), | 48 thickness_change_(NONE), |
| 48 opacity_change_(NONE), | |
| 49 mouse_move_distance_to_trigger_animation_( | 49 mouse_move_distance_to_trigger_animation_( |
| 50 kDefaultMouseMoveDistanceToTriggerAnimation) { | 50 kDefaultMouseMoveDistanceToTriggerAnimation), |
| 51 ApplyOpacityAndThumbThicknessScale(kIdleOpacity, kIdleThicknessScale); | 51 fade_duration_(fade_duration), |
| 52 thinning_duration_(thinning_duration), |
| 53 current_animating_property_(OPACITY) { |
| 54 ApplyOpacity(0.f); |
| 55 ApplyThumbThicknessScale(kIdleThicknessScale); |
| 52 } | 56 } |
| 53 | 57 |
| 54 ScrollbarAnimationControllerThinning::~ScrollbarAnimationControllerThinning() {} | 58 ScrollbarAnimationControllerThinning::~ScrollbarAnimationControllerThinning() {} |
| 55 | 59 |
| 56 void ScrollbarAnimationControllerThinning::RunAnimationFrame(float progress) { | 60 void ScrollbarAnimationControllerThinning::RunAnimationFrame(float progress) { |
| 57 float opacity = OpacityAtAnimationProgress(progress); | 61 if (captured_) |
| 58 float thumb_thickness_scale = | 62 return; |
| 59 ThumbThicknessScaleAtAnimationProgress(progress); | 63 |
| 60 ApplyOpacityAndThumbThicknessScale(opacity, thumb_thickness_scale); | 64 if (current_animating_property_ == OPACITY) |
| 65 ApplyOpacity(1.f - progress); |
| 66 else |
| 67 ApplyThumbThicknessScale(ThumbThicknessScaleAt(progress)); |
| 68 |
| 61 client_->SetNeedsRedrawForScrollbarAnimation(); | 69 client_->SetNeedsRedrawForScrollbarAnimation(); |
| 62 if (progress == 1.f) { | 70 if (progress == 1.f) { |
| 63 opacity_change_ = NONE; | |
| 64 thickness_change_ = NONE; | |
| 65 StopAnimation(); | 71 StopAnimation(); |
| 72 if (current_animating_property_ == THICKNESS) { |
| 73 thickness_change_ = NONE; |
| 74 SetCurrentAnimatingProperty(OPACITY); |
| 75 PostDelayedAnimationTask(false); |
| 76 } |
| 66 } | 77 } |
| 67 } | 78 } |
| 68 | 79 |
| 80 const base::TimeDelta& ScrollbarAnimationControllerThinning::Duration() { |
| 81 if (current_animating_property_ == OPACITY) |
| 82 return fade_duration_; |
| 83 else |
| 84 return thinning_duration_; |
| 85 } |
| 86 |
| 69 void ScrollbarAnimationControllerThinning::DidCaptureScrollbarBegin() { | 87 void ScrollbarAnimationControllerThinning::DidCaptureScrollbarBegin() { |
| 88 if (opacity_ == 0.0f) |
| 89 return; |
| 90 StopAnimation(); |
| 70 captured_ = true; | 91 captured_ = true; |
| 71 ApplyOpacityAndThumbThicknessScale(1, 1.f); | 92 ApplyOpacity(1.f); |
| 93 ApplyThumbThicknessScale(1.f); |
| 72 } | 94 } |
| 73 | 95 |
| 74 void ScrollbarAnimationControllerThinning::DidCaptureScrollbarEnd() { | 96 void ScrollbarAnimationControllerThinning::DidCaptureScrollbarEnd() { |
| 97 if (opacity_ == 0.0f) |
| 98 return; |
| 75 captured_ = false; | 99 captured_ = false; |
| 100 StopAnimation(); |
| 76 | 101 |
| 77 if (!mouse_is_over_scrollbar_) | 102 if (!mouse_is_near_scrollbar_) { |
| 78 opacity_change_ = DECREASE; | 103 SetCurrentAnimatingProperty(THICKNESS); |
| 79 if (!mouse_is_near_scrollbar_) | |
| 80 thickness_change_ = DECREASE; | 104 thickness_change_ = DECREASE; |
| 81 StartAnimation(); | 105 StartAnimation(); |
| 106 } else { |
| 107 SetCurrentAnimatingProperty(OPACITY); |
| 108 PostDelayedAnimationTask(false); |
| 109 } |
| 82 } | 110 } |
| 83 | 111 |
| 84 void ScrollbarAnimationControllerThinning::DidMouseMoveOffScrollbar() { | 112 void ScrollbarAnimationControllerThinning::DidMouseMoveOffScrollbar() { |
| 85 if (!mouse_is_over_scrollbar_ && !mouse_is_near_scrollbar_) | 113 if (!mouse_is_over_scrollbar_ && !mouse_is_near_scrollbar_) |
| 86 return; | 114 return; |
| 87 | 115 |
| 88 mouse_is_over_scrollbar_ = false; | 116 mouse_is_over_scrollbar_ = false; |
| 89 mouse_is_near_scrollbar_ = false; | 117 mouse_is_near_scrollbar_ = false; |
| 90 | 118 |
| 91 if (captured_) | 119 if (captured_ || opacity_ == 0.0f) |
| 92 return; | 120 return; |
| 93 | 121 |
| 94 opacity_change_ = DECREASE; | |
| 95 thickness_change_ = DECREASE; | 122 thickness_change_ = DECREASE; |
| 123 SetCurrentAnimatingProperty(THICKNESS); |
| 96 StartAnimation(); | 124 StartAnimation(); |
| 97 } | 125 } |
| 98 | 126 |
| 99 void ScrollbarAnimationControllerThinning::DidScrollUpdate(bool on_resize) { | 127 void ScrollbarAnimationControllerThinning::DidScrollUpdate(bool on_resize) { |
| 100 if (captured_) { | 128 if (captured_) |
| 129 return; |
| 130 |
| 131 ScrollbarAnimationController::DidScrollUpdate(on_resize); |
| 132 ApplyOpacity(1.f); |
| 133 ApplyThumbThicknessScale(mouse_is_near_scrollbar_ ? 1.f |
| 134 : kIdleThicknessScale); |
| 135 SetCurrentAnimatingProperty(OPACITY); |
| 136 } |
| 137 |
| 138 void ScrollbarAnimationControllerThinning::DidMouseMoveNear(float distance) { |
| 139 bool mouse_is_over_scrollbar = distance == 0.0f; |
| 140 bool mouse_is_near_scrollbar = |
| 141 distance < mouse_move_distance_to_trigger_animation_; |
| 142 |
| 143 if (captured_ || opacity_ == 0.0f) { |
| 144 mouse_is_near_scrollbar_ = mouse_is_near_scrollbar; |
| 145 mouse_is_over_scrollbar_ = mouse_is_over_scrollbar; |
| 101 return; | 146 return; |
| 102 } | 147 } |
| 103 | 148 |
| 104 ScrollbarAnimationController::DidScrollUpdate(on_resize); | 149 if (mouse_is_over_scrollbar == mouse_is_over_scrollbar_ && |
| 105 ApplyOpacityAndThumbThicknessScale( | 150 mouse_is_near_scrollbar == mouse_is_near_scrollbar_) |
| 106 1, mouse_is_near_scrollbar_ ? 1.f : kIdleThicknessScale); | 151 return; |
| 107 | 152 |
| 108 if (!mouse_is_over_scrollbar_) | 153 if (mouse_is_over_scrollbar_ != mouse_is_over_scrollbar) |
| 109 opacity_change_ = DECREASE; | 154 mouse_is_over_scrollbar_ = mouse_is_over_scrollbar; |
| 155 |
| 156 if (mouse_is_near_scrollbar_ != mouse_is_near_scrollbar) { |
| 157 mouse_is_near_scrollbar_ = mouse_is_near_scrollbar; |
| 158 thickness_change_ = mouse_is_near_scrollbar_ ? INCREASE : DECREASE; |
| 159 } |
| 160 |
| 161 SetCurrentAnimatingProperty(THICKNESS); |
| 162 StartAnimation(); |
| 110 } | 163 } |
| 111 | 164 |
| 112 void ScrollbarAnimationControllerThinning::DidMouseMoveNear(float distance) { | 165 float ScrollbarAnimationControllerThinning::ThumbThicknessScaleAt( |
| 113 bool mouse_is_over_scrollbar = distance == 0.0; | |
| 114 bool mouse_is_near_scrollbar = | |
| 115 distance < mouse_move_distance_to_trigger_animation_; | |
| 116 | |
| 117 if (captured_) { | |
| 118 mouse_is_near_scrollbar_ = mouse_is_near_scrollbar; | |
| 119 mouse_is_over_scrollbar_ = mouse_is_over_scrollbar; | |
| 120 return; | |
| 121 } else { | |
| 122 if (mouse_is_over_scrollbar == mouse_is_over_scrollbar_ && | |
| 123 mouse_is_near_scrollbar == mouse_is_near_scrollbar_) | |
| 124 return; | |
| 125 | |
| 126 if (mouse_is_over_scrollbar_ != mouse_is_over_scrollbar) { | |
| 127 mouse_is_over_scrollbar_ = mouse_is_over_scrollbar; | |
| 128 opacity_change_ = mouse_is_over_scrollbar_ ? INCREASE : DECREASE; | |
| 129 } | |
| 130 | |
| 131 if (mouse_is_near_scrollbar_ != mouse_is_near_scrollbar) { | |
| 132 mouse_is_near_scrollbar_ = mouse_is_near_scrollbar; | |
| 133 thickness_change_ = mouse_is_near_scrollbar_ ? INCREASE : DECREASE; | |
| 134 } | |
| 135 | |
| 136 StartAnimation(); | |
| 137 } | |
| 138 } | |
| 139 | |
| 140 float ScrollbarAnimationControllerThinning::OpacityAtAnimationProgress( | |
| 141 float progress) { | 166 float progress) { |
| 142 if (opacity_change_ == NONE) | |
| 143 return mouse_is_over_scrollbar_ ? 1.f : kIdleOpacity; | |
| 144 float factor = opacity_change_ == INCREASE ? progress : (1.f - progress); | |
| 145 float ret = ((1.f - kIdleOpacity) * factor) + kIdleOpacity; | |
| 146 return ret; | |
| 147 } | |
| 148 | |
| 149 float ScrollbarAnimationControllerThinning:: | |
| 150 ThumbThicknessScaleAtAnimationProgress(float progress) { | |
| 151 if (thickness_change_ == NONE) | 167 if (thickness_change_ == NONE) |
| 152 return mouse_is_near_scrollbar_ ? 1.f : kIdleThicknessScale; | 168 return mouse_is_near_scrollbar_ ? 1.f : kIdleThicknessScale; |
| 153 float factor = thickness_change_ == INCREASE ? progress : (1.f - progress); | 169 float factor = thickness_change_ == INCREASE ? progress : (1.f - progress); |
| 154 return ((1.f - kIdleThicknessScale) * factor) + kIdleThicknessScale; | 170 return ((1.f - kIdleThicknessScale) * factor) + kIdleThicknessScale; |
| 155 } | 171 } |
| 156 | 172 |
| 157 float ScrollbarAnimationControllerThinning::AdjustScale( | 173 float ScrollbarAnimationControllerThinning::AdjustScale( |
| 158 float new_value, | 174 float new_value, |
| 159 float current_value, | 175 float current_value, |
| 160 AnimationChange animation_change, | 176 AnimationChange animation_change, |
| 161 float min_value, | 177 float min_value, |
| 162 float max_value) { | 178 float max_value) { |
| 163 float result; | 179 float result; |
| 164 if (animation_change == INCREASE && current_value > new_value) | 180 if (animation_change == INCREASE && current_value > new_value) |
| 165 result = current_value; | 181 result = current_value; |
| 166 else if (animation_change == DECREASE && current_value < new_value) | 182 else if (animation_change == DECREASE && current_value < new_value) |
| 167 result = current_value; | 183 result = current_value; |
| 168 else | 184 else |
| 169 result = new_value; | 185 result = new_value; |
| 170 if (result > max_value) | 186 if (result > max_value) |
| 171 return max_value; | 187 return max_value; |
| 172 if (result < min_value) | 188 if (result < min_value) |
| 173 return min_value; | 189 return min_value; |
| 174 return result; | 190 return result; |
| 175 } | 191 } |
| 176 | 192 |
| 177 void ScrollbarAnimationControllerThinning::ApplyOpacityAndThumbThicknessScale( | 193 void ScrollbarAnimationControllerThinning::ApplyOpacity(float opacity) { |
| 178 float opacity, | |
| 179 float thumb_thickness_scale) { | |
| 180 for (ScrollbarLayerImplBase* scrollbar : Scrollbars()) { | 194 for (ScrollbarLayerImplBase* scrollbar : Scrollbars()) { |
| 181 if (!scrollbar->is_overlay_scrollbar()) | 195 if (!scrollbar->is_overlay_scrollbar()) |
| 182 continue; | 196 continue; |
| 183 float effective_opacity = | 197 float effective_opacity = scrollbar->CanScrollOrientation() ? opacity : 0; |
| 184 scrollbar->CanScrollOrientation() | |
| 185 ? AdjustScale(opacity, scrollbar->Opacity(), opacity_change_, | |
| 186 kIdleOpacity, 1) | |
| 187 : 0; | |
| 188 PropertyTrees* property_trees = | 198 PropertyTrees* property_trees = |
| 189 scrollbar->layer_tree_impl()->property_trees(); | 199 scrollbar->layer_tree_impl()->property_trees(); |
| 190 // If this method is called during LayerImpl::PushPropertiesTo, we may not | 200 // If this method is called during LayerImpl::PushPropertiesTo, we may not |
| 191 // yet have valid effect_id_to_index_map entries as property trees are | 201 // yet have valid effect_id_to_index_map entries as property trees are |
| 192 // pushed after layers during activation. We can skip updating opacity in | 202 // pushed after layers during activation. We can skip updating opacity in |
| 193 // that case as we are only registering a scrollbar and because opacity will | 203 // that case as we are only registering a scrollbar and because opacity will |
| 194 // be overwritten anyway when property trees are pushed. | 204 // be overwritten anyway when property trees are pushed. |
| 195 if (property_trees->IsInIdToIndexMap(PropertyTrees::TreeType::EFFECT, | 205 if (property_trees->IsInIdToIndexMap(PropertyTrees::TreeType::EFFECT, |
| 196 scrollbar->id())) { | 206 scrollbar->id())) { |
| 197 property_trees->effect_tree.OnOpacityAnimated( | 207 property_trees->effect_tree.OnOpacityAnimated( |
| 198 effective_opacity, | 208 effective_opacity, |
| 199 property_trees->effect_id_to_index_map[scrollbar->id()], | 209 property_trees->effect_id_to_index_map[scrollbar->id()], |
| 200 scrollbar->layer_tree_impl()); | 210 scrollbar->layer_tree_impl()); |
| 201 } | 211 } |
| 212 } |
| 213 |
| 214 opacity_ = opacity; |
| 215 } |
| 216 |
| 217 void ScrollbarAnimationControllerThinning::ApplyThumbThicknessScale( |
| 218 float thumb_thickness_scale) { |
| 219 for (ScrollbarLayerImplBase* scrollbar : Scrollbars()) { |
| 220 if (!scrollbar->is_overlay_scrollbar()) |
| 221 continue; |
| 222 |
| 202 scrollbar->SetThumbThicknessScaleFactor(AdjustScale( | 223 scrollbar->SetThumbThicknessScaleFactor(AdjustScale( |
| 203 thumb_thickness_scale, scrollbar->thumb_thickness_scale_factor(), | 224 thumb_thickness_scale, scrollbar->thumb_thickness_scale_factor(), |
| 204 thickness_change_, kIdleThicknessScale, 1)); | 225 thickness_change_, kIdleThicknessScale, 1)); |
| 205 } | 226 } |
| 206 } | 227 } |
| 207 | 228 |
| 229 void ScrollbarAnimationControllerThinning::SetCurrentAnimatingProperty( |
| 230 AnimatingProperty property) { |
| 231 if (current_animating_property_ == property) |
| 232 return; |
| 233 |
| 234 StopAnimation(); |
| 235 current_animating_property_ = property; |
| 236 if (current_animating_property_ == THICKNESS) |
| 237 ApplyOpacity(1.f); |
| 238 } |
| 239 |
| 208 } // namespace cc | 240 } // namespace cc |
| OLD | NEW |