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), |
36 in_scroll_gesture_(false), | 43 in_scroll_gesture_(false), |
37 top_controls_height_(top_controls_height), | 44 top_controls_height_(top_controls_height), |
38 controls_top_offset_(0), | 45 controls_top_offset_(0), |
39 content_top_offset_(top_controls_height), | 46 content_top_offset_(top_controls_height), |
40 previous_root_scroll_offset_(0.f), | 47 previous_root_scroll_offset_(0.f), |
41 scroll_start_offset_(0.f) { | 48 scroll_start_offset_(0.f), |
| 49 current_scroll_delta_(0.f), |
| 50 top_controls_show_height_( |
| 51 top_controls_height * top_controls_hide_threshold), |
| 52 top_controls_hide_height_( |
| 53 top_controls_height * (1.f - top_controls_show_threshold)) { |
42 CHECK(client_); | 54 CHECK(client_); |
43 } | 55 } |
44 | 56 |
45 TopControlsManager::~TopControlsManager() { | 57 TopControlsManager::~TopControlsManager() { |
46 } | 58 } |
47 | 59 |
48 void TopControlsManager::ScrollBegin() { | 60 void TopControlsManager::ScrollBegin() { |
49 ResetAnimations(); | 61 ResetAnimations(); |
50 in_scroll_gesture_ = true; | 62 in_scroll_gesture_ = true; |
51 scroll_start_offset_ = RootScrollLayerTotalScrollY() + controls_top_offset_; | 63 scroll_start_offset_ = RootScrollLayerTotalScrollY() + controls_top_offset_; |
| 64 current_scroll_delta_ = 0.f; |
52 } | 65 } |
53 | 66 |
54 gfx::Vector2dF TopControlsManager::ScrollBy( | 67 gfx::Vector2dF TopControlsManager::ScrollBy( |
55 const gfx::Vector2dF pending_delta) { | 68 const gfx::Vector2dF pending_delta) { |
56 if (pending_delta.y() == 0) | 69 if (pending_delta.y() == 0) |
57 return pending_delta; | 70 return pending_delta; |
58 | 71 |
| 72 current_scroll_delta_ += pending_delta.y(); |
| 73 |
59 float scroll_total_y = RootScrollLayerTotalScrollY(); | 74 float scroll_total_y = RootScrollLayerTotalScrollY(); |
60 if (in_scroll_gesture_ && | 75 if (in_scroll_gesture_ && |
61 ((pending_delta.y() > 0 && scroll_total_y < scroll_start_offset_) || | 76 ((pending_delta.y() > 0 && scroll_total_y < scroll_start_offset_) || |
62 (pending_delta.y() < 0 && | 77 (pending_delta.y() < 0 && |
63 scroll_total_y > scroll_start_offset_ + top_controls_height_))) { | 78 scroll_total_y > scroll_start_offset_ + top_controls_height_))) { |
64 return pending_delta; | 79 return pending_delta; |
65 } | 80 } |
66 | 81 |
67 ResetAnimations(); | 82 ResetAnimations(); |
68 return ScrollInternal(pending_delta); | 83 return ScrollInternal(pending_delta); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 client_->setActiveTreeNeedsUpdateDrawProperties(); | 122 client_->setActiveTreeNeedsUpdateDrawProperties(); |
108 } | 123 } |
109 | 124 |
110 return pending_delta - applied_delta; | 125 return pending_delta - applied_delta; |
111 } | 126 } |
112 | 127 |
113 void TopControlsManager::ScrollEnd() { | 128 void TopControlsManager::ScrollEnd() { |
114 StartAnimationIfNecessary(); | 129 StartAnimationIfNecessary(); |
115 previous_root_scroll_offset_ = RootScrollLayerTotalScrollY(); | 130 previous_root_scroll_offset_ = RootScrollLayerTotalScrollY(); |
116 in_scroll_gesture_ = false; | 131 in_scroll_gesture_ = false; |
| 132 current_scroll_delta_ = 0.f; |
117 } | 133 } |
118 | 134 |
119 void TopControlsManager::Animate(base::TimeTicks monotonic_time) { | 135 void TopControlsManager::Animate(base::TimeTicks monotonic_time) { |
120 if (!top_controls_animation_ || !client_->haveRootScrollLayer()) | 136 if (!top_controls_animation_ || !client_->haveRootScrollLayer()) |
121 return; | 137 return; |
122 | 138 |
123 double time = (monotonic_time - base::TimeTicks()).InMillisecondsF(); | 139 double time = (monotonic_time - base::TimeTicks()).InMillisecondsF(); |
124 float new_offset = top_controls_animation_->getValue(time); | 140 float new_offset = top_controls_animation_->getValue(time); |
125 gfx::Vector2dF scroll_vector(0.f, -(new_offset - controls_top_offset_)); | 141 gfx::Vector2dF scroll_vector(0.f, -(new_offset - controls_top_offset_)); |
126 ScrollInternal(scroll_vector); | 142 ScrollInternal(scroll_vector); |
(...skipping 24 matching lines...) Expand all Loading... |
151 float max_ending_offset = | 167 float max_ending_offset = |
152 (direction == SHOWING_CONTROLS ? 1 : -1) * top_controls_height_; | 168 (direction == SHOWING_CONTROLS ? 1 : -1) * top_controls_height_; |
153 top_controls_animation_->addKeyframe( | 169 top_controls_animation_->addKeyframe( |
154 FloatKeyframe::create(start_time + kShowHideMaxDurationMs, | 170 FloatKeyframe::create(start_time + kShowHideMaxDurationMs, |
155 controls_top_offset_ + max_ending_offset, | 171 controls_top_offset_ + max_ending_offset, |
156 EaseTimingFunction::create())); | 172 EaseTimingFunction::create())); |
157 animation_direction_ = direction; | 173 animation_direction_ = direction; |
158 } | 174 } |
159 | 175 |
160 void TopControlsManager::StartAnimationIfNecessary() { | 176 void TopControlsManager::StartAnimationIfNecessary() { |
161 float scroll_total_y = RootScrollLayerTotalScrollY(); | |
162 | |
163 if (controls_top_offset_ != 0 | 177 if (controls_top_offset_ != 0 |
164 && controls_top_offset_ != -top_controls_height_) { | 178 && controls_top_offset_ != -top_controls_height_) { |
165 AnimationDirection show_controls = | 179 AnimationDirection show_controls = NO_ANIMATION; |
166 controls_top_offset_ >= -(top_controls_height_ * kShowHideThreshold) ? | 180 |
167 SHOWING_CONTROLS : HIDING_CONTROLS; | 181 if (controls_top_offset_ >= -top_controls_show_height_) { |
168 if (!top_controls_animation_ || animation_direction_ != show_controls) { | 182 // If we're showing so much that the hide threshold won't trigger, show. |
| 183 show_controls = SHOWING_CONTROLS; |
| 184 } else if (controls_top_offset_ <= -top_controls_hide_height_) { |
| 185 // If we're showing so little that the show threshold won't trigger, hide. |
| 186 show_controls = HIDING_CONTROLS; |
| 187 } else { |
| 188 // If we could be either showing or hiding, we determine which one to |
| 189 // do based on whether or not the total scroll delta was moving up or |
| 190 // down. |
| 191 show_controls = current_scroll_delta_ <= 0.f ? |
| 192 SHOWING_CONTROLS : HIDING_CONTROLS; |
| 193 } |
| 194 |
| 195 if (show_controls != NO_ANIMATION && |
| 196 (!top_controls_animation_ || animation_direction_ != show_controls)) { |
169 SetupAnimation(show_controls); | 197 SetupAnimation(show_controls); |
170 client_->setNeedsRedraw(); | 198 client_->setNeedsRedraw(); |
171 } | 199 } |
172 } | 200 } |
173 } | 201 } |
174 | 202 |
175 bool TopControlsManager::IsAnimationCompleteAtTime(base::TimeTicks time) { | 203 bool TopControlsManager::IsAnimationCompleteAtTime(base::TimeTicks time) { |
176 if (!top_controls_animation_) | 204 if (!top_controls_animation_) |
177 return true; | 205 return true; |
178 | 206 |
179 double time_ms = (time - base::TimeTicks()).InMillisecondsF(); | 207 double time_ms = (time - base::TimeTicks()).InMillisecondsF(); |
180 float new_offset = top_controls_animation_->getValue(time_ms); | 208 float new_offset = top_controls_animation_->getValue(time_ms); |
181 | 209 |
182 if ((animation_direction_ == SHOWING_CONTROLS && new_offset >= 0) || | 210 if ((animation_direction_ == SHOWING_CONTROLS && new_offset >= 0) || |
183 (animation_direction_ == HIDING_CONTROLS | 211 (animation_direction_ == HIDING_CONTROLS |
184 && new_offset <= -top_controls_height_)) { | 212 && new_offset <= -top_controls_height_)) { |
185 return true; | 213 return true; |
186 } | 214 } |
187 return false; | 215 return false; |
188 } | 216 } |
189 | 217 |
190 } // namespace cc | 218 } // namespace cc |
OLD | NEW |