OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/top_controls_manager.h" | 5 #include "cc/top_controls_manager.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/time.h" | 10 #include "base/time.h" |
11 #include "cc/keyframed_animation_curve.h" | 11 #include "cc/keyframed_animation_curve.h" |
12 #include "cc/layer_tree_impl.h" | 12 #include "cc/layer_tree_impl.h" |
13 #include "cc/timing_function.h" | 13 #include "cc/timing_function.h" |
14 #include "cc/top_controls_manager_client.h" | 14 #include "cc/top_controls_manager_client.h" |
15 #include "ui/gfx/transform.h" | 15 #include "ui/gfx/transform.h" |
16 #include "ui/gfx/vector2d_f.h" | 16 #include "ui/gfx/vector2d_f.h" |
17 | 17 |
18 namespace cc { | 18 namespace cc { |
19 namespace { | 19 namespace { |
20 // These constants were chosen empirically for their visually pleasant behavior. | 20 // These constants were chosen empirically for their visually pleasant behavior. |
21 // Contact tedchoc@chromium.org for questions about changing these values. | 21 // Contact tedchoc@chromium.org for questions about changing these values. |
22 const float kShowHideThreshold = 0.5f; | |
23 const int64 kShowHideMaxDurationMs = 175; | 22 const int64 kShowHideMaxDurationMs = 175; |
24 } | 23 } |
25 | 24 |
26 // static | 25 // static |
27 scoped_ptr<TopControlsManager> TopControlsManager::Create( | 26 scoped_ptr<TopControlsManager> TopControlsManager::Create( |
28 TopControlsManagerClient* client, float top_controls_height) { | 27 TopControlsManagerClient* client, |
29 return make_scoped_ptr(new TopControlsManager(client, top_controls_height)); | 28 float top_controls_height, |
29 float top_controls_show_threshold, | |
30 float top_controls_hide_threshold) { | |
31 return make_scoped_ptr(new TopControlsManager(client, | |
32 top_controls_height, | |
33 top_controls_show_threshold, | |
34 top_controls_hide_threshold)); | |
30 } | 35 } |
31 | 36 |
32 TopControlsManager::TopControlsManager(TopControlsManagerClient* client, | 37 TopControlsManager::TopControlsManager(TopControlsManagerClient* client, |
33 float top_controls_height) | 38 float top_controls_height, |
39 float top_controls_show_threshold, | |
40 float top_controls_hide_threshold) | |
34 : client_(client), | 41 : client_(client), |
35 animation_direction_(NO_ANIMATION), | 42 animation_direction_(NO_ANIMATION), |
43 top_controls_show_threshold_(top_controls_show_threshold), | |
44 top_controls_hide_threshold_(top_controls_hide_threshold), | |
36 is_overlay_mode_(false), | 45 is_overlay_mode_(false), |
37 in_scroll_gesture_(false), | 46 in_scroll_gesture_(false), |
38 top_controls_height_(top_controls_height), | 47 top_controls_height_(top_controls_height), |
39 controls_top_offset_(0), | 48 controls_top_offset_(0), |
40 content_top_offset_(top_controls_height), | 49 content_top_offset_(top_controls_height), |
41 previous_root_scroll_offset_(0.f), | 50 previous_root_scroll_offset_(0.f), |
42 scroll_start_offset_(0.f) { | 51 scroll_start_offset_(0.f), |
52 current_scroll_delta_(0.f) { | |
43 CHECK(client_); | 53 CHECK(client_); |
44 } | 54 } |
45 | 55 |
46 TopControlsManager::~TopControlsManager() { | 56 TopControlsManager::~TopControlsManager() { |
47 } | 57 } |
48 | 58 |
49 void TopControlsManager::UpdateDrawPositions() { | 59 void TopControlsManager::UpdateDrawPositions() { |
50 if (!client_->haveRootScrollLayer()) | 60 if (!client_->haveRootScrollLayer()) |
51 return; | 61 return; |
52 | 62 |
53 // If the scroll position has changed underneath us (i.e. a javascript | 63 // If the scroll position has changed underneath us (i.e. a javascript |
54 // scroll), then simulate a scroll that covers the delta. | 64 // scroll), then simulate a scroll that covers the delta. |
55 float scroll_total_y = RootScrollLayerTotalScrollY(); | 65 float scroll_total_y = RootScrollLayerTotalScrollY(); |
56 if (!in_scroll_gesture_ | 66 if (!in_scroll_gesture_ |
57 && scroll_total_y != previous_root_scroll_offset_) { | 67 && scroll_total_y != previous_root_scroll_offset_) { |
58 ScrollBy(gfx::Vector2dF(0, scroll_total_y - previous_root_scroll_offset_)); | 68 ScrollBy(gfx::Vector2dF(0, scroll_total_y - previous_root_scroll_offset_)); |
59 StartAnimationIfNecessary(); | 69 StartAnimationIfNecessary(); |
60 previous_root_scroll_offset_ = RootScrollLayerTotalScrollY(); | 70 previous_root_scroll_offset_ = RootScrollLayerTotalScrollY(); |
61 } | 71 } |
62 } | 72 } |
63 | 73 |
64 void TopControlsManager::ScrollBegin() { | 74 void TopControlsManager::ScrollBegin() { |
65 ResetAnimations(); | 75 ResetAnimations(); |
66 in_scroll_gesture_ = true; | 76 in_scroll_gesture_ = true; |
67 scroll_start_offset_ = RootScrollLayerTotalScrollY() + controls_top_offset_; | 77 scroll_start_offset_ = RootScrollLayerTotalScrollY() + controls_top_offset_; |
78 current_scroll_delta_ = 0.f; | |
68 } | 79 } |
69 | 80 |
70 gfx::Vector2dF TopControlsManager::ScrollBy( | 81 gfx::Vector2dF TopControlsManager::ScrollBy( |
71 const gfx::Vector2dF pending_delta) { | 82 const gfx::Vector2dF pending_delta) { |
72 if (pending_delta.y() == 0) | 83 if (pending_delta.y() == 0) |
73 return pending_delta; | 84 return pending_delta; |
74 | 85 |
86 current_scroll_delta_ += pending_delta.y(); | |
87 | |
75 float scroll_total_y = RootScrollLayerTotalScrollY(); | 88 float scroll_total_y = RootScrollLayerTotalScrollY(); |
76 if (in_scroll_gesture_ && | 89 if (in_scroll_gesture_ && |
77 ((pending_delta.y() > 0 && scroll_total_y < scroll_start_offset_) || | 90 ((pending_delta.y() > 0 && scroll_total_y < scroll_start_offset_) || |
78 (pending_delta.y() < 0 && | 91 (pending_delta.y() < 0 && |
79 scroll_total_y > scroll_start_offset_ + top_controls_height_))) { | 92 scroll_total_y > scroll_start_offset_ + top_controls_height_))) { |
80 return pending_delta; | 93 return pending_delta; |
81 } | 94 } |
82 | 95 |
83 ResetAnimations(); | 96 ResetAnimations(); |
84 return ScrollInternal(pending_delta); | 97 return ScrollInternal(pending_delta); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
129 client_->setActiveTreeNeedsUpdateDrawProperties(); | 142 client_->setActiveTreeNeedsUpdateDrawProperties(); |
130 } | 143 } |
131 | 144 |
132 return pending_delta - applied_delta; | 145 return pending_delta - applied_delta; |
133 } | 146 } |
134 | 147 |
135 void TopControlsManager::ScrollEnd() { | 148 void TopControlsManager::ScrollEnd() { |
136 StartAnimationIfNecessary(); | 149 StartAnimationIfNecessary(); |
137 previous_root_scroll_offset_ = RootScrollLayerTotalScrollY(); | 150 previous_root_scroll_offset_ = RootScrollLayerTotalScrollY(); |
138 in_scroll_gesture_ = false; | 151 in_scroll_gesture_ = false; |
152 current_scroll_delta_ = 0.f; | |
139 } | 153 } |
140 | 154 |
141 void TopControlsManager::Animate(base::TimeTicks monotonic_time) { | 155 void TopControlsManager::Animate(base::TimeTicks monotonic_time) { |
142 if (!top_controls_animation_ || !client_->haveRootScrollLayer()) | 156 if (!top_controls_animation_ || !client_->haveRootScrollLayer()) |
143 return; | 157 return; |
144 | 158 |
145 double time = (monotonic_time - base::TimeTicks()).InMillisecondsF(); | 159 double time = (monotonic_time - base::TimeTicks()).InMillisecondsF(); |
146 float new_offset = top_controls_animation_->getValue(time); | 160 float new_offset = top_controls_animation_->getValue(time); |
147 gfx::Vector2dF scroll_vector(0.f, -(new_offset - controls_top_offset_)); | 161 gfx::Vector2dF scroll_vector(0.f, -(new_offset - controls_top_offset_)); |
148 ScrollInternal(scroll_vector); | 162 ScrollInternal(scroll_vector); |
(...skipping 23 matching lines...) Expand all Loading... | |
172 scoped_ptr<TimingFunction>())); | 186 scoped_ptr<TimingFunction>())); |
173 float max_ending_offset = | 187 float max_ending_offset = |
174 (direction == SHOWING_CONTROLS ? 1 : -1) * top_controls_height_; | 188 (direction == SHOWING_CONTROLS ? 1 : -1) * top_controls_height_; |
175 top_controls_animation_->addKeyframe( | 189 top_controls_animation_->addKeyframe( |
176 FloatKeyframe::create(start_time + kShowHideMaxDurationMs, | 190 FloatKeyframe::create(start_time + kShowHideMaxDurationMs, |
177 controls_top_offset_ + max_ending_offset, | 191 controls_top_offset_ + max_ending_offset, |
178 EaseTimingFunction::create())); | 192 EaseTimingFunction::create())); |
179 animation_direction_ = direction; | 193 animation_direction_ = direction; |
180 } | 194 } |
181 | 195 |
182 void TopControlsManager::StartAnimationIfNecessary() { | 196 void TopControlsManager::StartAnimationIfNecessary() { |
Ted C
2013/02/07 00:40:26
Can you add a unittest that verifies the new anima
David Trainor- moved to gerrit
2013/02/11 19:38:28
Done.
| |
183 float scroll_total_y = RootScrollLayerTotalScrollY(); | |
184 | |
185 if (controls_top_offset_ != 0 | 197 if (controls_top_offset_ != 0 |
186 && controls_top_offset_ != -top_controls_height_) { | 198 && controls_top_offset_ != -top_controls_height_) { |
187 AnimationDirection show_controls = | 199 AnimationDirection show_controls = NO_ANIMATION; |
188 controls_top_offset_ >= -(top_controls_height_ * kShowHideThreshold) ? | 200 |
189 SHOWING_CONTROLS : HIDING_CONTROLS; | 201 if (controls_top_offset_ >= |
190 if (!top_controls_animation_ || animation_direction_ != show_controls) { | 202 -(top_controls_height_ * top_controls_hide_threshold_)) { |
Ted C
2013/02/07 00:40:26
Looking at this, should we initialize top_controls
David Trainor- moved to gerrit
2013/02/11 19:38:28
Done.
| |
203 // If we're showing so much that the hide threshold won't trigger, show. | |
204 show_controls = SHOWING_CONTROLS; | |
205 } else if (controls_top_offset_ <= | |
206 -(top_controls_height_ * (1.f - top_controls_show_threshold_))) { | |
Ted C
2013/02/07 00:40:26
same for this?
David Trainor- moved to gerrit
2013/02/11 19:38:28
Done.
| |
207 // If we're showing so little that the show threshold won't trigger, hide. | |
208 show_controls = HIDING_CONTROLS; | |
209 } else { | |
210 // If we could be either showing or hiding, we determine which one to | |
211 // do based on whether or not the total scroll delta was moving up or | |
212 // down. | |
213 show_controls = current_scroll_delta_ <= 0.f ? | |
214 SHOWING_CONTROLS : HIDING_CONTROLS; | |
215 } | |
216 | |
217 if (show_controls != NO_ANIMATION && | |
218 (!top_controls_animation_ || animation_direction_ != show_controls)) { | |
191 SetupAnimation(show_controls); | 219 SetupAnimation(show_controls); |
192 client_->setNeedsRedraw(); | 220 client_->setNeedsRedraw(); |
193 } | 221 } |
194 } | 222 } |
195 } | 223 } |
196 | 224 |
197 bool TopControlsManager::IsAnimationCompleteAtTime(base::TimeTicks time) { | 225 bool TopControlsManager::IsAnimationCompleteAtTime(base::TimeTicks time) { |
198 if (!top_controls_animation_) | 226 if (!top_controls_animation_) |
199 return true; | 227 return true; |
200 | 228 |
201 double time_ms = (time - base::TimeTicks()).InMillisecondsF(); | 229 double time_ms = (time - base::TimeTicks()).InMillisecondsF(); |
202 float new_offset = top_controls_animation_->getValue(time_ms); | 230 float new_offset = top_controls_animation_->getValue(time_ms); |
203 | 231 |
204 if ((animation_direction_ == SHOWING_CONTROLS && new_offset >= 0) || | 232 if ((animation_direction_ == SHOWING_CONTROLS && new_offset >= 0) || |
205 (animation_direction_ == HIDING_CONTROLS | 233 (animation_direction_ == HIDING_CONTROLS |
206 && new_offset <= -top_controls_height_)) { | 234 && new_offset <= -top_controls_height_)) { |
207 return true; | 235 return true; |
208 } | 236 } |
209 return false; | 237 return false; |
210 } | 238 } |
211 | 239 |
212 } // namespace cc | 240 } // namespace cc |
OLD | NEW |