Chromium Code Reviews| 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 ApplyOpacity(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 SingleScrollbarAnimationControllerThinning& | |
| 53 ScrollbarAnimationControllerThinning::GetScrollbarAnimationController( | |
| 54 ScrollbarOrientation orientation) const { | |
| 55 if (orientation == ScrollbarOrientation::VERTICAL) | |
| 56 return *(vertical_controller_.get()); | |
| 57 else | |
| 58 return *(horizontal_controller_.get()); | |
| 59 } | |
| 60 | |
| 61 bool ScrollbarAnimationControllerThinning::mouse_is_over_scrollbar( | |
| 62 ScrollbarOrientation orientation) const { | |
| 63 return GetScrollbarAnimationController(orientation).mouse_is_over_scrollbar(); | |
| 64 } | |
| 65 | |
| 66 bool ScrollbarAnimationControllerThinning::mouse_is_near_scrollbar( | |
| 67 ScrollbarOrientation orientation) const { | |
| 68 return GetScrollbarAnimationController(orientation).mouse_is_near_scrollbar(); | |
| 69 } | |
| 70 | |
| 71 bool ScrollbarAnimationControllerThinning::mouse_is_near_any_scrollbar() const { | |
| 72 return vertical_controller_->mouse_is_near_scrollbar() || | |
| 73 horizontal_controller_->mouse_is_near_scrollbar(); | |
| 74 } | |
| 75 | |
| 76 bool ScrollbarAnimationControllerThinning::Captured() const { | |
| 77 return vertical_controller_->captured() || horizontal_controller_->captured(); | |
| 78 } | |
| 79 | |
| 80 bool ScrollbarAnimationControllerThinning::ScrollbarsHidden() const { | |
| 81 return opacity_ == 0.0f; | |
| 82 } | |
| 83 | |
| 84 void ScrollbarAnimationControllerThinning::set_mouse_move_distance_for_test( | |
| 85 float distance) { | |
| 86 vertical_controller_->set_mouse_move_distance_for_test(distance); | |
| 87 horizontal_controller_->set_mouse_move_distance_for_test(distance); | |
| 88 } | |
| 89 | |
| 90 bool ScrollbarAnimationControllerThinning::Animate(base::TimeTicks now) { | |
| 91 bool animated = ScrollbarAnimationController::Animate(now); | |
| 92 | |
| 93 animated |= vertical_controller_->Animate(now); | |
| 94 animated |= horizontal_controller_->Animate(now); | |
| 95 | |
| 96 if (animated && !animating() && !ScrollbarsHidden() && !Captured() && | |
| 97 !currently_scrolling() && !mouse_is_near_any_scrollbar()) | |
|
bokan
2016/12/21 15:56:02
This shouldn't be necessary. Why do we need to kee
| |
| 98 PostDelayedAnimationTask(false); | |
| 99 | |
| 100 return animated; | |
| 101 } | |
| 102 | |
| 60 void ScrollbarAnimationControllerThinning::RunAnimationFrame(float progress) { | 103 void ScrollbarAnimationControllerThinning::RunAnimationFrame(float progress) { |
| 61 if (captured_) | 104 ApplyOpacity(1.f - progress); |
| 62 return; | 105 if (progress == 1.f) |
| 63 | 106 StopAnimation(); |
| 64 if (current_animating_property_ == OPACITY) | |
| 65 ApplyOpacity(1.f - progress); | |
| 66 else | 107 else |
| 67 ApplyThumbThicknessScale(ThumbThicknessScaleAt(progress)); | 108 client_->SetNeedsAnimateForScrollbarAnimation(); |
|
bokan
2016/12/21 15:56:02
Why do you need this?
| |
| 68 | |
| 69 client_->SetNeedsRedrawForScrollbarAnimation(); | |
| 70 if (progress == 1.f) { | |
| 71 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 } | 109 } |
| 80 | 110 |
| 81 const base::TimeDelta& ScrollbarAnimationControllerThinning::Duration() { | 111 const base::TimeDelta& ScrollbarAnimationControllerThinning::Duration() { |
| 82 if (current_animating_property_ == OPACITY) | 112 return fade_duration_; |
| 83 return fade_duration_; | |
| 84 else | |
| 85 return thinning_duration_; | |
| 86 } | 113 } |
| 87 | 114 |
| 88 void ScrollbarAnimationControllerThinning::DidMouseDown() { | 115 void ScrollbarAnimationControllerThinning::DidMouseDown() { |
| 89 if (!mouse_is_over_scrollbar_ || opacity_ == 0.0f) | 116 vertical_controller_->DidMouseDown(); |
| 90 return; | 117 horizontal_controller_->DidMouseDown(); |
| 91 | |
| 92 StopAnimation(); | |
| 93 captured_ = true; | |
| 94 ApplyOpacity(1.f); | |
| 95 ApplyThumbThicknessScale(1.f); | |
| 96 } | 118 } |
| 97 | 119 |
| 98 void ScrollbarAnimationControllerThinning::DidMouseUp() { | 120 void ScrollbarAnimationControllerThinning::DidMouseUp() { |
| 99 if (!captured_ || opacity_ == 0.0f) | 121 vertical_controller_->DidMouseUp(); |
| 100 return; | 122 horizontal_controller_->DidMouseUp(); |
| 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 } | 123 } |
| 113 | 124 |
| 114 void ScrollbarAnimationControllerThinning::DidMouseLeave() { | 125 void ScrollbarAnimationControllerThinning::DidMouseLeave() { |
| 115 if (!mouse_is_over_scrollbar_ && !mouse_is_near_scrollbar_) | 126 vertical_controller_->DidMouseLeave(); |
| 116 return; | 127 horizontal_controller_->DidMouseLeave(); |
| 128 } | |
| 117 | 129 |
| 118 mouse_is_over_scrollbar_ = false; | 130 void ScrollbarAnimationControllerThinning::DidMouseMoveNear( |
| 119 mouse_is_near_scrollbar_ = false; | 131 ScrollbarOrientation orientation, |
| 120 | 132 float distance) { |
| 121 if (captured_ || opacity_ == 0.0f) | 133 GetScrollbarAnimationController(orientation).DidMouseMoveNear(distance); |
| 122 return; | 134 FadeInIfNeeded(); |
| 123 | |
| 124 thickness_change_ = DECREASE; | |
| 125 SetCurrentAnimatingProperty(THICKNESS); | |
| 126 StartAnimation(); | |
| 127 } | 135 } |
| 128 | 136 |
| 129 void ScrollbarAnimationControllerThinning::DidScrollUpdate(bool on_resize) { | 137 void ScrollbarAnimationControllerThinning::DidScrollUpdate(bool on_resize) { |
| 130 if (captured_) | 138 if (Captured()) |
| 131 return; | 139 return; |
| 132 | 140 |
| 133 ScrollbarAnimationController::DidScrollUpdate(on_resize); | 141 ScrollbarAnimationController::DidScrollUpdate(on_resize); |
| 134 ApplyOpacity(1.f); | |
| 135 ApplyThumbThicknessScale(mouse_is_near_scrollbar_ ? 1.f | |
| 136 : kIdleThicknessScale); | |
| 137 SetCurrentAnimatingProperty(OPACITY); | |
| 138 | 142 |
| 139 // Don't fade out the scrollbar when mouse is near. | 143 ApplyOpacity(1); |
| 140 if (mouse_is_near_scrollbar_) | 144 vertical_controller_->UpdateThumbThicknessScale(); |
| 145 horizontal_controller_->UpdateThumbThicknessScale(); | |
| 146 | |
| 147 if (mouse_is_near_any_scrollbar()) | |
| 141 StopAnimation(); | 148 StopAnimation(); |
|
bokan
2016/12/21 15:56:02
This seems wrong. Why would we stop the animation
chaopeng
2016/12/21 16:57:47
because we post delay task in ScrollbarAnimationCo
bokan
2016/12/21 23:06:09
Oh yes, that's obvious now that you say it.
Pleas
| |
| 142 } | 149 } |
| 143 | 150 |
| 144 void ScrollbarAnimationControllerThinning::DidScrollEnd() { | 151 void ScrollbarAnimationControllerThinning::DidScrollEnd() { |
| 145 ScrollbarAnimationController::DidScrollEnd(); | 152 ScrollbarAnimationController::DidScrollEnd(); |
| 146 | 153 |
| 147 // Don't fade out the scrollbar when mouse is near. | 154 // Don't fade out the scrollbar when mouse is near. |
| 148 if (mouse_is_near_scrollbar_) | 155 if (mouse_is_near_any_scrollbar()) |
| 149 StopAnimation(); | 156 StopAnimation(); |
| 150 } | 157 } |
| 151 | 158 |
| 152 void ScrollbarAnimationControllerThinning::DidMouseMoveNear(float distance) { | 159 void ScrollbarAnimationControllerThinning::FadeInIfNeeded() { |
| 153 bool mouse_is_over_scrollbar = distance == 0.0f; | 160 if (!mouse_is_near_any_scrollbar() || ScrollbarsHidden()) |
| 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; | 161 return; |
| 166 | 162 |
| 167 if (mouse_is_over_scrollbar_ != mouse_is_over_scrollbar) | 163 ApplyOpacity(1); |
| 168 mouse_is_over_scrollbar_ = mouse_is_over_scrollbar; | 164 StopAnimation(); |
| 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 } | 165 } |
| 210 | 166 |
| 211 void ScrollbarAnimationControllerThinning::ApplyOpacity(float opacity) { | 167 void ScrollbarAnimationControllerThinning::ApplyOpacity(float opacity) { |
| 212 for (ScrollbarLayerImplBase* scrollbar : Scrollbars()) { | 168 for (ScrollbarLayerImplBase* scrollbar : Scrollbars()) { |
| 213 if (!scrollbar->is_overlay_scrollbar()) | 169 if (!scrollbar->is_overlay_scrollbar()) |
| 214 continue; | 170 continue; |
| 215 float effective_opacity = scrollbar->CanScrollOrientation() ? opacity : 0; | 171 float effective_opacity = scrollbar->CanScrollOrientation() ? opacity : 0; |
| 216 PropertyTrees* property_trees = | 172 PropertyTrees* property_trees = |
| 217 scrollbar->layer_tree_impl()->property_trees(); | 173 scrollbar->layer_tree_impl()->property_trees(); |
| 218 // If this method is called during LayerImpl::PushPropertiesTo, we may not | 174 // If this method is called during LayerImpl::PushPropertiesTo, we may not |
| 219 // yet have valid effect_id_to_index_map entries as property trees are | 175 // yet have valid effect_id_to_index_map entries as property trees are |
| 220 // pushed after layers during activation. We can skip updating opacity in | 176 // pushed after layers during activation. We can skip updating opacity in |
| 221 // that case as we are only registering a scrollbar and because opacity will | 177 // that case as we are only registering a scrollbar and because opacity will |
| 222 // be overwritten anyway when property trees are pushed. | 178 // be overwritten anyway when property trees are pushed. |
| 223 if (property_trees->IsInIdToIndexMap(PropertyTrees::TreeType::EFFECT, | 179 if (property_trees->IsInIdToIndexMap(PropertyTrees::TreeType::EFFECT, |
| 224 scrollbar->id())) { | 180 scrollbar->id())) { |
| 225 property_trees->effect_tree.OnOpacityAnimated( | 181 property_trees->effect_tree.OnOpacityAnimated( |
| 226 effective_opacity, | 182 effective_opacity, |
| 227 property_trees->effect_id_to_index_map[scrollbar->id()], | 183 property_trees->effect_id_to_index_map[scrollbar->id()], |
| 228 scrollbar->layer_tree_impl()); | 184 scrollbar->layer_tree_impl()); |
| 229 } | 185 } |
| 230 } | 186 } |
| 231 | 187 |
| 232 bool previouslyVisible = opacity_ > 0.0f; | 188 bool previouslyVisible = opacity_ > 0.0f; |
| 233 bool currentlyVisible = opacity > 0.0f; | 189 bool currentlyVisible = opacity > 0.0f; |
| 234 | 190 |
| 235 opacity_ = opacity; | 191 opacity_ = opacity; |
| 192 vertical_controller_->setHidden(!currentlyVisible); | |
| 193 horizontal_controller_->setHidden(!currentlyVisible); | |
| 236 | 194 |
| 237 if (previouslyVisible != currentlyVisible) | 195 if (previouslyVisible != currentlyVisible) |
| 238 client_->DidChangeScrollbarVisibility(); | 196 client_->DidChangeScrollbarVisibility(); |
| 239 } | 197 } |
| 240 | 198 |
| 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 | 199 } // namespace cc |
| OLD | NEW |