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