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 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 mouse_is_near_scrollbar_ = mouse_is_near_scrollbar; | |
|
bokan
2017/01/20 19:58:32
This is set at the end, just use the argument in t
| |
| 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 |