Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "cc/input/single_scrollbar_animation_controller_thinning.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/memory/ptr_util.h" | |
| 10 #include "base/time/time.h" | |
| 11 #include "cc/layers/layer_impl.h" | |
| 12 #include "cc/layers/scrollbar_layer_impl_base.h" | |
| 13 #include "cc/trees/layer_tree_impl.h" | |
| 14 | |
| 15 namespace { | |
| 16 const float kIdleThicknessScale = 0.4f; | |
| 17 const float kDefaultMouseMoveDistanceToTriggerAnimation = 25.f; | |
| 18 } | |
| 19 | |
| 20 namespace cc { | |
| 21 | |
| 22 std::unique_ptr<SingleScrollbarAnimationControllerThinning> | |
| 23 SingleScrollbarAnimationControllerThinning::Create( | |
| 24 int scroll_layer_id, | |
| 25 ScrollbarOrientation orientation, | |
| 26 ScrollbarAnimationControllerClient* client, | |
| 27 base::CancelableClosure* delayed_scrollbar_fade, | |
| 28 base::TimeDelta thinning_duration) { | |
| 29 return base::WrapUnique(new SingleScrollbarAnimationControllerThinning( | |
| 30 scroll_layer_id, orientation, client, delayed_scrollbar_fade, | |
| 31 thinning_duration)); | |
| 32 } | |
| 33 | |
| 34 SingleScrollbarAnimationControllerThinning:: | |
| 35 SingleScrollbarAnimationControllerThinning( | |
| 36 int scroll_layer_id, | |
| 37 ScrollbarOrientation orientation, | |
| 38 ScrollbarAnimationControllerClient* client, | |
| 39 base::CancelableClosure* delayed_scrollbar_fade, | |
| 40 base::TimeDelta thinning_duration) | |
| 41 : client_(client), | |
| 42 delayed_scrollbar_fade_(delayed_scrollbar_fade), | |
| 43 is_animating_(false), | |
| 44 scroll_layer_id_(scroll_layer_id), | |
| 45 orientation_(orientation), | |
| 46 hidden_(false), | |
| 47 captured_(false), | |
| 48 mouse_is_over_scrollbar_(false), | |
| 49 mouse_is_near_scrollbar_(false), | |
| 50 thickness_change_(NONE), | |
| 51 mouse_move_distance_to_trigger_animation_( | |
| 52 kDefaultMouseMoveDistanceToTriggerAnimation), | |
| 53 thinning_duration_(thinning_duration) { | |
| 54 ApplyThumbThicknessScale(kIdleThicknessScale); | |
| 55 } | |
| 56 | |
| 57 bool SingleScrollbarAnimationControllerThinning::Animate(base::TimeTicks now) { | |
| 58 if (!is_animating_) | |
| 59 return false; | |
| 60 | |
| 61 if (last_awaken_time_.is_null()) | |
| 62 last_awaken_time_ = now; | |
| 63 | |
| 64 float progress = AnimationProgressAtTime(now); | |
| 65 RunAnimationFrame(progress); | |
| 66 | |
| 67 return true; | |
| 68 } | |
| 69 | |
| 70 float SingleScrollbarAnimationControllerThinning::AnimationProgressAtTime( | |
| 71 base::TimeTicks now) { | |
| 72 base::TimeDelta delta = now - last_awaken_time_; | |
| 73 float progress = delta.InSecondsF() / Duration().InSecondsF(); | |
| 74 return std::max(std::min(progress, 1.f), 0.f); | |
| 75 } | |
| 76 | |
| 77 const base::TimeDelta& SingleScrollbarAnimationControllerThinning::Duration() { | |
| 78 return thinning_duration_; | |
| 79 } | |
| 80 | |
| 81 void SingleScrollbarAnimationControllerThinning::RunAnimationFrame( | |
| 82 float progress) { | |
| 83 if (captured_) | |
| 84 return; | |
| 85 | |
| 86 ApplyThumbThicknessScale(ThumbThicknessScaleAt(progress)); | |
| 87 | |
| 88 client_->SetNeedsRedrawForScrollbarAnimation(); | |
| 89 if (progress == 1.f) { | |
| 90 StopAnimation(); | |
| 91 thickness_change_ = NONE; | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 void SingleScrollbarAnimationControllerThinning::StartAnimation() { | |
| 96 delayed_scrollbar_fade_->Cancel(); | |
|
bokan
2016/12/19 16:11:47
This is still splitting responsibility for fade an
| |
| 97 is_animating_ = true; | |
| 98 last_awaken_time_ = base::TimeTicks(); | |
| 99 client_->SetNeedsAnimateForScrollbarAnimation(); | |
| 100 } | |
| 101 | |
| 102 void SingleScrollbarAnimationControllerThinning::StopAnimation() { | |
| 103 delayed_scrollbar_fade_->Cancel(); | |
| 104 is_animating_ = false; | |
| 105 } | |
| 106 | |
| 107 bool SingleScrollbarAnimationControllerThinning::ShouldFadeOut() const { | |
| 108 return !captured_ && !mouse_is_near_scrollbar_ && !is_animating_ && | |
| 109 thickness_change_ == NONE && !hidden_; | |
| 110 } | |
| 111 | |
| 112 void SingleScrollbarAnimationControllerThinning::DidMouseDown() { | |
| 113 if (!mouse_is_over_scrollbar_ || hidden_) | |
| 114 return; | |
| 115 | |
| 116 StopAnimation(); | |
| 117 captured_ = true; | |
| 118 ApplyThumbThicknessScale(1.f); | |
| 119 } | |
| 120 | |
| 121 void SingleScrollbarAnimationControllerThinning::DidMouseUp() { | |
| 122 if (!captured_ || hidden_) | |
| 123 return; | |
| 124 | |
| 125 captured_ = false; | |
| 126 StopAnimation(); | |
| 127 | |
| 128 if (!mouse_is_near_scrollbar_) { | |
| 129 thickness_change_ = DECREASE; | |
| 130 StartAnimation(); | |
| 131 } else { | |
| 132 thickness_change_ = NONE; | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 void SingleScrollbarAnimationControllerThinning::DidMouseLeave() { | |
| 137 if (!mouse_is_over_scrollbar_ && !mouse_is_near_scrollbar_) | |
| 138 return; | |
| 139 | |
| 140 mouse_is_over_scrollbar_ = false; | |
| 141 mouse_is_near_scrollbar_ = false; | |
| 142 | |
| 143 if (captured_ || hidden_) | |
| 144 return; | |
| 145 | |
| 146 thickness_change_ = DECREASE; | |
| 147 StartAnimation(); | |
| 148 } | |
| 149 | |
| 150 void SingleScrollbarAnimationControllerThinning::DidMouseMoveNear( | |
| 151 float distance) { | |
| 152 bool mouse_is_over_scrollbar = distance == 0.0f; | |
| 153 bool mouse_is_near_scrollbar = | |
| 154 distance < mouse_move_distance_to_trigger_animation_; | |
| 155 | |
| 156 if (captured_ || hidden_) { | |
| 157 mouse_is_near_scrollbar_ = mouse_is_near_scrollbar; | |
| 158 mouse_is_over_scrollbar_ = mouse_is_over_scrollbar; | |
| 159 return; | |
| 160 } | |
| 161 | |
| 162 if (mouse_is_over_scrollbar == mouse_is_over_scrollbar_ && | |
| 163 mouse_is_near_scrollbar == mouse_is_near_scrollbar_) | |
| 164 return; | |
| 165 | |
| 166 if (mouse_is_over_scrollbar_ != mouse_is_over_scrollbar) | |
| 167 mouse_is_over_scrollbar_ = mouse_is_over_scrollbar; | |
| 168 | |
| 169 if (mouse_is_near_scrollbar_ != mouse_is_near_scrollbar) { | |
| 170 mouse_is_near_scrollbar_ = mouse_is_near_scrollbar; | |
| 171 thickness_change_ = mouse_is_near_scrollbar_ ? INCREASE : DECREASE; | |
| 172 } | |
| 173 | |
| 174 StartAnimation(); | |
| 175 } | |
| 176 | |
| 177 float SingleScrollbarAnimationControllerThinning::ThumbThicknessScaleAt( | |
| 178 float progress) { | |
| 179 if (thickness_change_ == NONE) | |
| 180 return mouse_is_near_scrollbar_ ? 1.f : kIdleThicknessScale; | |
| 181 float factor = thickness_change_ == INCREASE ? progress : (1.f - progress); | |
| 182 return ((1.f - kIdleThicknessScale) * factor) + kIdleThicknessScale; | |
| 183 } | |
| 184 | |
| 185 float SingleScrollbarAnimationControllerThinning::AdjustScale( | |
| 186 float new_value, | |
| 187 float current_value, | |
| 188 AnimationChange animation_change, | |
| 189 float min_value, | |
| 190 float max_value) { | |
| 191 float result; | |
| 192 if (animation_change == INCREASE && current_value > new_value) | |
| 193 result = current_value; | |
| 194 else if (animation_change == DECREASE && current_value < new_value) | |
| 195 result = current_value; | |
| 196 else | |
| 197 result = new_value; | |
| 198 if (result > max_value) | |
| 199 return max_value; | |
| 200 if (result < min_value) | |
| 201 return min_value; | |
| 202 return result; | |
| 203 } | |
| 204 | |
| 205 void SingleScrollbarAnimationControllerThinning::UpdateThumbThicknessScale() { | |
| 206 ApplyThumbThicknessScale(mouse_is_near_scrollbar_ ? 1.f | |
| 207 : kIdleThicknessScale); | |
| 208 } | |
| 209 | |
| 210 void SingleScrollbarAnimationControllerThinning::ApplyThumbThicknessScale( | |
| 211 float thumb_thickness_scale) { | |
| 212 for (ScrollbarLayerImplBase* scrollbar : | |
| 213 client_->ScrollbarsFor(scroll_layer_id_)) { | |
| 214 if (scrollbar->orientation() != orientation_) | |
| 215 continue; | |
| 216 if (!scrollbar->is_overlay_scrollbar()) | |
| 217 continue; | |
| 218 | |
| 219 float scale = AdjustScale(thumb_thickness_scale, | |
| 220 scrollbar->thumb_thickness_scale_factor(), | |
| 221 thickness_change_, kIdleThicknessScale, 1); | |
| 222 | |
| 223 scrollbar->SetThumbThicknessScaleFactor(scale); | |
| 224 } | |
| 225 } | |
| 226 | |
| 227 } // namespace cc | |
| OLD | NEW |