| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "cc/input/scrollbar_animation_controller.h" | 5 #include "cc/input/scrollbar_animation_controller.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
| 10 #include "cc/trees/layer_tree_impl.h" | 10 #include "cc/trees/layer_tree_impl.h" |
| 11 | 11 |
| 12 namespace cc { | 12 namespace cc { |
| 13 | 13 |
| 14 ScrollbarAnimationController::ScrollbarAnimationController( | 14 ScrollbarAnimationController::ScrollbarAnimationController( |
| 15 int scroll_layer_id, | 15 int scroll_layer_id, |
| 16 ScrollbarAnimationControllerClient* client, | 16 ScrollbarAnimationControllerClient* client, |
| 17 base::TimeDelta delay_before_starting, | 17 base::TimeDelta delay_before_starting, |
| 18 base::TimeDelta resize_delay_before_starting) | 18 base::TimeDelta resize_delay_before_starting) |
| 19 : client_(client), | 19 : client_(client), |
| 20 delay_before_starting_(delay_before_starting), | 20 delay_before_starting_(delay_before_starting), |
| 21 resize_delay_before_starting_(resize_delay_before_starting), | 21 resize_delay_before_starting_(resize_delay_before_starting), |
| 22 is_animating_(false), | 22 is_animating_(false), |
| 23 scroll_layer_id_(scroll_layer_id), | 23 scroll_layer_id_(scroll_layer_id), |
| 24 currently_scrolling_(false), | 24 currently_scrolling_(false), |
| 25 scroll_gesture_has_scrolled_(false), | 25 scroll_gesture_has_scrolled_(false), |
| 26 weak_factory_(this) {} | 26 weak_factory_(this) { |
| 27 } |
| 27 | 28 |
| 28 ScrollbarAnimationController::~ScrollbarAnimationController() {} | 29 ScrollbarAnimationController::~ScrollbarAnimationController() {} |
| 29 | 30 |
| 31 SingleScrollbarAnimationControllerThinning& |
| 32 ScrollbarAnimationController::GetScrollbarAnimationController( |
| 33 ScrollbarOrientation orientation) const { |
| 34 DCHECK(NeedThinningAnimation()); |
| 35 if (orientation == ScrollbarOrientation::VERTICAL) |
| 36 return *(vertical_controller_.get()); |
| 37 else |
| 38 return *(horizontal_controller_.get()); |
| 39 } |
| 40 |
| 30 bool ScrollbarAnimationController::Animate(base::TimeTicks now) { | 41 bool ScrollbarAnimationController::Animate(base::TimeTicks now) { |
| 31 if (!is_animating_) | 42 bool animated = false; |
| 32 return false; | |
| 33 | 43 |
| 34 if (last_awaken_time_.is_null()) | 44 if (is_animating_) { |
| 35 last_awaken_time_ = now; | 45 if (last_awaken_time_.is_null()) |
| 46 last_awaken_time_ = now; |
| 36 | 47 |
| 37 float progress = AnimationProgressAtTime(now); | 48 float progress = AnimationProgressAtTime(now); |
| 38 RunAnimationFrame(progress); | 49 RunAnimationFrame(progress); |
| 39 | 50 |
| 40 if (is_animating_) | 51 if (is_animating_) |
| 41 client_->SetNeedsAnimateForScrollbarAnimation(); | 52 client_->SetNeedsAnimateForScrollbarAnimation(); |
| 42 return true; | 53 animated = true; |
| 54 } |
| 55 |
| 56 if (NeedThinningAnimation()) { |
| 57 animated |= vertical_controller_->Animate(now); |
| 58 animated |= horizontal_controller_->Animate(now); |
| 59 } |
| 60 |
| 61 return animated; |
| 43 } | 62 } |
| 44 | 63 |
| 45 bool ScrollbarAnimationController::ScrollbarsHidden() const { | 64 bool ScrollbarAnimationController::ScrollbarsHidden() const { |
| 46 return false; | 65 return false; |
| 47 } | 66 } |
| 48 | 67 |
| 68 bool ScrollbarAnimationController::NeedThinningAnimation() const { |
| 69 return false; |
| 70 } |
| 71 |
| 49 float ScrollbarAnimationController::AnimationProgressAtTime( | 72 float ScrollbarAnimationController::AnimationProgressAtTime( |
| 50 base::TimeTicks now) { | 73 base::TimeTicks now) { |
| 51 base::TimeDelta delta = now - last_awaken_time_; | 74 base::TimeDelta delta = now - last_awaken_time_; |
| 52 float progress = delta.InSecondsF() / Duration().InSecondsF(); | 75 float progress = delta.InSecondsF() / Duration().InSecondsF(); |
| 53 return std::max(std::min(progress, 1.f), 0.f); | 76 return std::max(std::min(progress, 1.f), 0.f); |
| 54 } | 77 } |
| 55 | 78 |
| 56 void ScrollbarAnimationController::DidScrollBegin() { | 79 void ScrollbarAnimationController::DidScrollBegin() { |
| 57 currently_scrolling_ = true; | 80 currently_scrolling_ = true; |
| 58 } | 81 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 70 | 93 |
| 71 void ScrollbarAnimationController::DidScrollEnd() { | 94 void ScrollbarAnimationController::DidScrollEnd() { |
| 72 if (scroll_gesture_has_scrolled_) { | 95 if (scroll_gesture_has_scrolled_) { |
| 73 PostDelayedAnimationTask(false); | 96 PostDelayedAnimationTask(false); |
| 74 scroll_gesture_has_scrolled_ = false; | 97 scroll_gesture_has_scrolled_ = false; |
| 75 } | 98 } |
| 76 | 99 |
| 77 currently_scrolling_ = false; | 100 currently_scrolling_ = false; |
| 78 } | 101 } |
| 79 | 102 |
| 103 void ScrollbarAnimationController::DidMouseDown() { |
| 104 if (!NeedThinningAnimation() || ScrollbarsHidden()) |
| 105 return; |
| 106 |
| 107 vertical_controller_->DidMouseDown(); |
| 108 horizontal_controller_->DidMouseDown(); |
| 109 } |
| 110 |
| 111 void ScrollbarAnimationController::DidMouseUp() { |
| 112 if (!NeedThinningAnimation()) |
| 113 return; |
| 114 |
| 115 vertical_controller_->DidMouseUp(); |
| 116 horizontal_controller_->DidMouseUp(); |
| 117 |
| 118 if (!mouse_is_near_any_scrollbar()) |
| 119 PostDelayedAnimationTask(false); |
| 120 } |
| 121 |
| 122 void ScrollbarAnimationController::DidMouseLeave() { |
| 123 if (!NeedThinningAnimation()) |
| 124 return; |
| 125 |
| 126 vertical_controller_->DidMouseLeave(); |
| 127 horizontal_controller_->DidMouseLeave(); |
| 128 } |
| 129 |
| 130 void ScrollbarAnimationController::DidMouseMoveNear( |
| 131 ScrollbarOrientation orientation, |
| 132 float distance) { |
| 133 if (!NeedThinningAnimation()) |
| 134 return; |
| 135 |
| 136 GetScrollbarAnimationController(orientation).DidMouseMoveNear(distance); |
| 137 |
| 138 if (ScrollbarsHidden() || Captured()) |
| 139 return; |
| 140 |
| 141 if (mouse_is_near_any_scrollbar()) { |
| 142 ApplyOpacityToScrollbars(1); |
| 143 StopAnimation(); |
| 144 } else if (!animating_fade()) { |
| 145 PostDelayedAnimationTask(false); |
| 146 } |
| 147 } |
| 148 |
| 149 bool ScrollbarAnimationController::mouse_is_over_scrollbar( |
| 150 ScrollbarOrientation orientation) const { |
| 151 DCHECK(NeedThinningAnimation()); |
| 152 return GetScrollbarAnimationController(orientation).mouse_is_over_scrollbar(); |
| 153 } |
| 154 |
| 155 bool ScrollbarAnimationController::mouse_is_near_scrollbar( |
| 156 ScrollbarOrientation orientation) const { |
| 157 DCHECK(NeedThinningAnimation()); |
| 158 return GetScrollbarAnimationController(orientation).mouse_is_near_scrollbar(); |
| 159 } |
| 160 |
| 161 bool ScrollbarAnimationController::mouse_is_near_any_scrollbar() const { |
| 162 DCHECK(NeedThinningAnimation()); |
| 163 return vertical_controller_->mouse_is_near_scrollbar() || |
| 164 horizontal_controller_->mouse_is_near_scrollbar(); |
| 165 } |
| 166 |
| 167 bool ScrollbarAnimationController::Captured() const { |
| 168 DCHECK(NeedThinningAnimation()); |
| 169 return vertical_controller_->captured() || horizontal_controller_->captured(); |
| 170 } |
| 171 |
| 80 void ScrollbarAnimationController::PostDelayedAnimationTask(bool on_resize) { | 172 void ScrollbarAnimationController::PostDelayedAnimationTask(bool on_resize) { |
| 81 base::TimeDelta delay = | 173 base::TimeDelta delay = |
| 82 on_resize ? resize_delay_before_starting_ : delay_before_starting_; | 174 on_resize ? resize_delay_before_starting_ : delay_before_starting_; |
| 83 delayed_scrollbar_fade_.Reset( | 175 delayed_scrollbar_fade_.Reset( |
| 84 base::Bind(&ScrollbarAnimationController::StartAnimation, | 176 base::Bind(&ScrollbarAnimationController::StartAnimation, |
| 85 weak_factory_.GetWeakPtr())); | 177 weak_factory_.GetWeakPtr())); |
| 86 client_->PostDelayedScrollbarAnimationTask(delayed_scrollbar_fade_.callback(), | 178 client_->PostDelayedScrollbarAnimationTask(delayed_scrollbar_fade_.callback(), |
| 87 delay); | 179 delay); |
| 88 } | 180 } |
| 89 | 181 |
| 90 void ScrollbarAnimationController::StartAnimation() { | 182 void ScrollbarAnimationController::StartAnimation() { |
| 91 delayed_scrollbar_fade_.Cancel(); | 183 delayed_scrollbar_fade_.Cancel(); |
| 92 is_animating_ = true; | 184 is_animating_ = true; |
| 93 last_awaken_time_ = base::TimeTicks(); | 185 last_awaken_time_ = base::TimeTicks(); |
| 94 client_->SetNeedsAnimateForScrollbarAnimation(); | 186 client_->SetNeedsAnimateForScrollbarAnimation(); |
| 95 } | 187 } |
| 96 | 188 |
| 97 void ScrollbarAnimationController::StopAnimation() { | 189 void ScrollbarAnimationController::StopAnimation() { |
| 98 delayed_scrollbar_fade_.Cancel(); | 190 delayed_scrollbar_fade_.Cancel(); |
| 99 is_animating_ = false; | 191 is_animating_ = false; |
| 100 } | 192 } |
| 101 | 193 |
| 102 ScrollbarSet ScrollbarAnimationController::Scrollbars() const { | 194 ScrollbarSet ScrollbarAnimationController::Scrollbars() const { |
| 103 return client_->ScrollbarsFor(scroll_layer_id_); | 195 return client_->ScrollbarsFor(scroll_layer_id_); |
| 104 } | 196 } |
| 105 | 197 |
| 198 void ScrollbarAnimationController::set_mouse_move_distance_for_test( |
| 199 float distance) { |
| 200 vertical_controller_->set_mouse_move_distance_for_test(distance); |
| 201 horizontal_controller_->set_mouse_move_distance_for_test(distance); |
| 202 } |
| 203 |
| 106 } // namespace cc | 204 } // namespace cc |
| OLD | NEW |