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