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