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