| 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/input/scrollbar_animation_controller.h" |
| 12 #include "cc/layers/layer_impl.h" |
| 13 #include "cc/layers/scrollbar_layer_impl_base.h" |
| 14 #include "cc/trees/layer_tree_impl.h" |
| 15 |
| 16 namespace { |
| 17 const float kIdleThicknessScale = 0.4f; |
| 18 const float kDefaultMouseMoveDistanceToTriggerAnimation = 25.f; |
| 19 } |
| 20 |
| 21 namespace cc { |
| 22 |
| 23 std::unique_ptr<SingleScrollbarAnimationControllerThinning> |
| 24 SingleScrollbarAnimationControllerThinning::Create( |
| 25 int scroll_layer_id, |
| 26 ScrollbarOrientation orientation, |
| 27 ScrollbarAnimationControllerClient* client, |
| 28 base::TimeDelta thinning_duration) { |
| 29 return base::WrapUnique(new SingleScrollbarAnimationControllerThinning( |
| 30 scroll_layer_id, orientation, client, thinning_duration)); |
| 31 } |
| 32 |
| 33 SingleScrollbarAnimationControllerThinning:: |
| 34 SingleScrollbarAnimationControllerThinning( |
| 35 int scroll_layer_id, |
| 36 ScrollbarOrientation orientation, |
| 37 ScrollbarAnimationControllerClient* client, |
| 38 base::TimeDelta thinning_duration) |
| 39 : client_(client), |
| 40 is_animating_(false), |
| 41 scroll_layer_id_(scroll_layer_id), |
| 42 orientation_(orientation), |
| 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_) |
| 103 return; |
| 104 |
| 105 StopAnimation(); |
| 106 captured_ = true; |
| 107 ApplyThumbThicknessScale(1.f); |
| 108 } |
| 109 |
| 110 void SingleScrollbarAnimationControllerThinning::DidMouseUp() { |
| 111 if (!captured_) |
| 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_) |
| 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_ && mouse_is_near_scrollbar != mouse_is_near_scrollbar_) { |
| 146 thickness_change_ = mouse_is_near_scrollbar ? INCREASE : DECREASE; |
| 147 StartAnimation(); |
| 148 } |
| 149 mouse_is_near_scrollbar_ = mouse_is_near_scrollbar; |
| 150 mouse_is_over_scrollbar_ = mouse_is_over_scrollbar; |
| 151 } |
| 152 |
| 153 float SingleScrollbarAnimationControllerThinning::ThumbThicknessScaleAt( |
| 154 float progress) { |
| 155 if (thickness_change_ == NONE) |
| 156 return mouse_is_near_scrollbar_ ? 1.f : kIdleThicknessScale; |
| 157 float factor = thickness_change_ == INCREASE ? progress : (1.f - progress); |
| 158 return ((1.f - kIdleThicknessScale) * factor) + kIdleThicknessScale; |
| 159 } |
| 160 |
| 161 float SingleScrollbarAnimationControllerThinning::AdjustScale( |
| 162 float new_value, |
| 163 float current_value, |
| 164 AnimationChange animation_change, |
| 165 float min_value, |
| 166 float max_value) { |
| 167 float result; |
| 168 if (animation_change == INCREASE && current_value > new_value) |
| 169 result = current_value; |
| 170 else if (animation_change == DECREASE && current_value < new_value) |
| 171 result = current_value; |
| 172 else |
| 173 result = new_value; |
| 174 if (result > max_value) |
| 175 return max_value; |
| 176 if (result < min_value) |
| 177 return min_value; |
| 178 return result; |
| 179 } |
| 180 |
| 181 void SingleScrollbarAnimationControllerThinning::UpdateThumbThicknessScale() { |
| 182 StopAnimation(); |
| 183 ApplyThumbThicknessScale(mouse_is_near_scrollbar_ ? 1.f |
| 184 : kIdleThicknessScale); |
| 185 } |
| 186 |
| 187 void SingleScrollbarAnimationControllerThinning::ApplyThumbThicknessScale( |
| 188 float thumb_thickness_scale) { |
| 189 for (ScrollbarLayerImplBase* scrollbar : |
| 190 client_->ScrollbarsFor(scroll_layer_id_)) { |
| 191 if (scrollbar->orientation() != orientation_) |
| 192 continue; |
| 193 if (!scrollbar->is_overlay_scrollbar()) |
| 194 continue; |
| 195 |
| 196 float scale = AdjustScale(thumb_thickness_scale, |
| 197 scrollbar->thumb_thickness_scale_factor(), |
| 198 thickness_change_, kIdleThicknessScale, 1); |
| 199 |
| 200 scrollbar->SetThumbThicknessScaleFactor(scale); |
| 201 } |
| 202 } |
| 203 |
| 204 } // namespace cc |
| OLD | NEW |