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" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 base::TimeDelta fade_out_delay, | 43 base::TimeDelta fade_out_delay, |
44 base::TimeDelta fade_out_resize_delay, | 44 base::TimeDelta fade_out_resize_delay, |
45 base::TimeDelta fade_out_duration) | 45 base::TimeDelta fade_out_duration) |
46 : client_(client), | 46 : client_(client), |
47 fade_out_delay_(fade_out_delay), | 47 fade_out_delay_(fade_out_delay), |
48 fade_out_resize_delay_(fade_out_resize_delay), | 48 fade_out_resize_delay_(fade_out_resize_delay), |
49 need_trigger_scrollbar_show_(false), | 49 need_trigger_scrollbar_show_(false), |
50 is_animating_(false), | 50 is_animating_(false), |
51 scroll_layer_id_(scroll_layer_id), | 51 scroll_layer_id_(scroll_layer_id), |
52 currently_scrolling_(false), | 52 currently_scrolling_(false), |
53 scroll_gesture_has_scrolled_(false), | 53 show_in_fast_scroll_(false), |
54 opacity_(0.0f), | 54 opacity_(0.0f), |
55 fade_out_duration_(fade_out_duration), | 55 fade_out_duration_(fade_out_duration), |
| 56 show_scrollbars_on_scroll_gesture_(false), |
56 need_thinning_animation_(false), | 57 need_thinning_animation_(false), |
57 weak_factory_(this) { | 58 weak_factory_(this) { |
58 ApplyOpacityToScrollbars(0.0f); | 59 ApplyOpacityToScrollbars(0.0f); |
59 } | 60 } |
60 | 61 |
61 ScrollbarAnimationController::ScrollbarAnimationController( | 62 ScrollbarAnimationController::ScrollbarAnimationController( |
62 int scroll_layer_id, | 63 int scroll_layer_id, |
63 ScrollbarAnimationControllerClient* client, | 64 ScrollbarAnimationControllerClient* client, |
64 base::TimeDelta show_delay, | 65 base::TimeDelta show_delay, |
65 base::TimeDelta fade_out_delay, | 66 base::TimeDelta fade_out_delay, |
66 base::TimeDelta fade_out_resize_delay, | 67 base::TimeDelta fade_out_resize_delay, |
67 base::TimeDelta fade_out_duration, | 68 base::TimeDelta fade_out_duration, |
68 base::TimeDelta thinning_duration) | 69 base::TimeDelta thinning_duration) |
69 : client_(client), | 70 : client_(client), |
70 show_delay_(show_delay), | 71 show_delay_(show_delay), |
71 fade_out_delay_(fade_out_delay), | 72 fade_out_delay_(fade_out_delay), |
72 fade_out_resize_delay_(fade_out_resize_delay), | 73 fade_out_resize_delay_(fade_out_resize_delay), |
73 need_trigger_scrollbar_show_(false), | 74 need_trigger_scrollbar_show_(false), |
74 is_animating_(false), | 75 is_animating_(false), |
75 scroll_layer_id_(scroll_layer_id), | 76 scroll_layer_id_(scroll_layer_id), |
76 currently_scrolling_(false), | 77 currently_scrolling_(false), |
77 scroll_gesture_has_scrolled_(false), | 78 show_in_fast_scroll_(false), |
78 opacity_(0.0f), | 79 opacity_(0.0f), |
79 fade_out_duration_(fade_out_duration), | 80 fade_out_duration_(fade_out_duration), |
| 81 show_scrollbars_on_scroll_gesture_(true), |
80 need_thinning_animation_(true), | 82 need_thinning_animation_(true), |
81 weak_factory_(this) { | 83 weak_factory_(this) { |
82 vertical_controller_ = SingleScrollbarAnimationControllerThinning::Create( | 84 vertical_controller_ = SingleScrollbarAnimationControllerThinning::Create( |
83 scroll_layer_id, ScrollbarOrientation::VERTICAL, client, | 85 scroll_layer_id, ScrollbarOrientation::VERTICAL, client, |
84 thinning_duration); | 86 thinning_duration); |
85 horizontal_controller_ = SingleScrollbarAnimationControllerThinning::Create( | 87 horizontal_controller_ = SingleScrollbarAnimationControllerThinning::Create( |
86 scroll_layer_id, ScrollbarOrientation::HORIZONTAL, client, | 88 scroll_layer_id, ScrollbarOrientation::HORIZONTAL, client, |
87 thinning_duration); | 89 thinning_duration); |
88 ApplyOpacityToScrollbars(0.0f); | 90 ApplyOpacityToScrollbars(0.0f); |
89 } | 91 } |
(...skipping 23 matching lines...) Expand all Loading... |
113 } | 115 } |
114 | 116 |
115 void ScrollbarAnimationController::StopAnimation() { | 117 void ScrollbarAnimationController::StopAnimation() { |
116 delayed_scrollbar_show_.Cancel(); | 118 delayed_scrollbar_show_.Cancel(); |
117 delayed_scrollbar_fade_out_.Cancel(); | 119 delayed_scrollbar_fade_out_.Cancel(); |
118 is_animating_ = false; | 120 is_animating_ = false; |
119 } | 121 } |
120 | 122 |
121 void ScrollbarAnimationController::PostDelayedShow() { | 123 void ScrollbarAnimationController::PostDelayedShow() { |
122 DCHECK(delayed_scrollbar_fade_out_.IsCancelled()); | 124 DCHECK(delayed_scrollbar_fade_out_.IsCancelled()); |
| 125 delayed_scrollbar_show_.Cancel(); |
123 delayed_scrollbar_show_.Reset(base::Bind(&ScrollbarAnimationController::Show, | 126 delayed_scrollbar_show_.Reset(base::Bind(&ScrollbarAnimationController::Show, |
124 weak_factory_.GetWeakPtr())); | 127 weak_factory_.GetWeakPtr())); |
125 client_->PostDelayedScrollbarAnimationTask(delayed_scrollbar_show_.callback(), | 128 client_->PostDelayedScrollbarAnimationTask(delayed_scrollbar_show_.callback(), |
126 show_delay_); | 129 show_delay_); |
127 } | 130 } |
128 | 131 |
129 void ScrollbarAnimationController::PostDelayedFadeOut(bool on_resize) { | 132 void ScrollbarAnimationController::PostDelayedFadeOut(bool on_resize) { |
130 DCHECK(delayed_scrollbar_show_.IsCancelled()); | 133 DCHECK(delayed_scrollbar_show_.IsCancelled()); |
131 base::TimeDelta delay = on_resize ? fade_out_resize_delay_ : fade_out_delay_; | 134 base::TimeDelta delay = on_resize ? fade_out_resize_delay_ : fade_out_delay_; |
| 135 delayed_scrollbar_fade_out_.Cancel(); |
132 delayed_scrollbar_fade_out_.Reset( | 136 delayed_scrollbar_fade_out_.Reset( |
133 base::Bind(&ScrollbarAnimationController::StartAnimation, | 137 base::Bind(&ScrollbarAnimationController::StartAnimation, |
134 weak_factory_.GetWeakPtr())); | 138 weak_factory_.GetWeakPtr())); |
135 client_->PostDelayedScrollbarAnimationTask( | 139 client_->PostDelayedScrollbarAnimationTask( |
136 delayed_scrollbar_fade_out_.callback(), delay); | 140 delayed_scrollbar_fade_out_.callback(), delay); |
137 } | 141 } |
138 | 142 |
139 bool ScrollbarAnimationController::Animate(base::TimeTicks now) { | 143 bool ScrollbarAnimationController::Animate(base::TimeTicks now) { |
140 bool animated = false; | 144 bool animated = false; |
141 | 145 |
(...skipping 17 matching lines...) Expand all Loading... |
159 return animated; | 163 return animated; |
160 } | 164 } |
161 | 165 |
162 float ScrollbarAnimationController::AnimationProgressAtTime( | 166 float ScrollbarAnimationController::AnimationProgressAtTime( |
163 base::TimeTicks now) { | 167 base::TimeTicks now) { |
164 base::TimeDelta delta = now - last_awaken_time_; | 168 base::TimeDelta delta = now - last_awaken_time_; |
165 float progress = delta.InSecondsF() / fade_out_duration_.InSecondsF(); | 169 float progress = delta.InSecondsF() / fade_out_duration_.InSecondsF(); |
166 return std::max(std::min(progress, 1.f), 0.f); | 170 return std::max(std::min(progress, 1.f), 0.f); |
167 } | 171 } |
168 | 172 |
169 void ScrollbarAnimationController::DidScrollBegin() { | |
170 currently_scrolling_ = true; | |
171 } | |
172 | |
173 void ScrollbarAnimationController::RunAnimationFrame(float progress) { | 173 void ScrollbarAnimationController::RunAnimationFrame(float progress) { |
174 ApplyOpacityToScrollbars(1.f - progress); | 174 ApplyOpacityToScrollbars(1.f - progress); |
175 if (progress == 1.f) | 175 if (progress == 1.f) |
176 StopAnimation(); | 176 StopAnimation(); |
177 } | 177 } |
178 | 178 |
179 void ScrollbarAnimationController::DidScrollUpdate(bool on_resize) { | 179 void ScrollbarAnimationController::DidScrollBegin() { |
| 180 currently_scrolling_ = true; |
| 181 } |
| 182 |
| 183 void ScrollbarAnimationController::DidScrollEnd() { |
| 184 bool has_scrolled = show_in_fast_scroll_; |
| 185 show_in_fast_scroll_ = false; |
| 186 |
| 187 currently_scrolling_ = false; |
| 188 |
| 189 // We don't fade out scrollbar if they need thinning animation and mouse is |
| 190 // near. |
| 191 if (need_thinning_animation_ && MouseIsNearAnyScrollbar()) |
| 192 return; |
| 193 |
| 194 if (has_scrolled) |
| 195 PostDelayedFadeOut(false); |
| 196 } |
| 197 |
| 198 void ScrollbarAnimationController::DidScrollUpdate() { |
180 if (need_thinning_animation_ && Captured()) | 199 if (need_thinning_animation_ && Captured()) |
181 return; | 200 return; |
182 | 201 |
183 StopAnimation(); | 202 StopAnimation(); |
184 | 203 |
185 // As an optimization, we avoid spamming fade delay tasks during active fast | 204 // As an optimization, we avoid spamming fade delay tasks during active fast |
186 // scrolls. But if we're not within one, we need to post every scroll update. | 205 // scrolls. But if we're not within one, we need to post every scroll update. |
187 if (!currently_scrolling_) { | 206 if (!currently_scrolling_) { |
188 // We don't fade out scrollbar if they need thinning animation and mouse is | 207 // We don't fade out scrollbar if they need thinning animation and mouse is |
189 // near. | 208 // near. |
190 if (!need_thinning_animation_ || !MouseIsNearAnyScrollbar()) | 209 if (!need_thinning_animation_ || !MouseIsNearAnyScrollbar()) |
191 PostDelayedFadeOut(on_resize); | 210 PostDelayedFadeOut(false); |
192 } else { | 211 } else { |
193 scroll_gesture_has_scrolled_ = true; | 212 show_in_fast_scroll_ = true; |
194 } | 213 } |
195 | 214 |
196 Show(); | 215 Show(); |
197 | 216 |
198 if (need_thinning_animation_) { | 217 if (need_thinning_animation_) { |
199 vertical_controller_->UpdateThumbThicknessScale(); | 218 vertical_controller_->UpdateThumbThicknessScale(); |
200 horizontal_controller_->UpdateThumbThicknessScale(); | 219 horizontal_controller_->UpdateThumbThicknessScale(); |
201 } | 220 } |
202 } | 221 } |
203 | 222 |
204 void ScrollbarAnimationController::DidScrollEnd() { | 223 void ScrollbarAnimationController::WillUpdateScroll() { |
205 bool has_scrolled = scroll_gesture_has_scrolled_; | 224 if (show_scrollbars_on_scroll_gesture_) |
206 scroll_gesture_has_scrolled_ = false; | 225 DidScrollUpdate(); |
| 226 } |
207 | 227 |
208 currently_scrolling_ = false; | 228 void ScrollbarAnimationController::DidResize() { |
| 229 StopAnimation(); |
| 230 Show(); |
209 | 231 |
210 // We don't fade out scrollbar if they need thinning animation and mouse is | 232 // As an optimization, we avoid spamming fade delay tasks during active fast |
211 // near. | 233 // scrolls. |
212 if (need_thinning_animation_ && MouseIsNearAnyScrollbar()) | 234 if (!currently_scrolling_) { |
213 return; | 235 PostDelayedFadeOut(true); |
214 | 236 } else { |
215 if (has_scrolled) | 237 show_in_fast_scroll_ = true; |
216 PostDelayedFadeOut(false); | 238 } |
217 } | 239 } |
218 | 240 |
219 void ScrollbarAnimationController::DidMouseDown() { | 241 void ScrollbarAnimationController::DidMouseDown() { |
220 if (!need_thinning_animation_ || ScrollbarsHidden()) | 242 if (!need_thinning_animation_ || ScrollbarsHidden()) |
221 return; | 243 return; |
222 | 244 |
223 vertical_controller_->DidMouseDown(); | 245 vertical_controller_->DidMouseDown(); |
224 horizontal_controller_->DidMouseDown(); | 246 horizontal_controller_->DidMouseDown(); |
225 } | 247 } |
226 | 248 |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
366 if (opacity_ != opacity) | 388 if (opacity_ != opacity) |
367 client_->SetNeedsRedrawForScrollbarAnimation(); | 389 client_->SetNeedsRedrawForScrollbarAnimation(); |
368 | 390 |
369 opacity_ = opacity; | 391 opacity_ = opacity; |
370 | 392 |
371 if (previouslyVisible != currentlyVisible) | 393 if (previouslyVisible != currentlyVisible) |
372 client_->DidChangeScrollbarVisibility(); | 394 client_->DidChangeScrollbarVisibility(); |
373 } | 395 } |
374 | 396 |
375 } // namespace cc | 397 } // namespace cc |
OLD | NEW |