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/input/top_controls_manager.h" | 5 #include "cc/input/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 "cc/animation/keyframed_animation_curve.h" | 10 #include "cc/animation/keyframed_animation_curve.h" |
(...skipping 24 matching lines...) Expand all Loading... |
35 top_controls_hide_threshold)); | 35 top_controls_hide_threshold)); |
36 } | 36 } |
37 | 37 |
38 TopControlsManager::TopControlsManager(TopControlsManagerClient* client, | 38 TopControlsManager::TopControlsManager(TopControlsManagerClient* client, |
39 float top_controls_height, | 39 float top_controls_height, |
40 float top_controls_show_threshold, | 40 float top_controls_show_threshold, |
41 float top_controls_hide_threshold) | 41 float top_controls_hide_threshold) |
42 : client_(client), | 42 : client_(client), |
43 animation_direction_(NO_ANIMATION), | 43 animation_direction_(NO_ANIMATION), |
44 permitted_state_(BOTH), | 44 permitted_state_(BOTH), |
45 controls_top_offset_(0.f), | |
46 top_controls_height_(top_controls_height), | 45 top_controls_height_(top_controls_height), |
47 current_scroll_delta_(0.f), | 46 current_scroll_delta_(0.f), |
48 controls_scroll_begin_offset_(0.f), | 47 controls_scroll_begin_offset_(0.f), |
49 top_controls_show_height_( | 48 top_controls_show_height_( |
50 top_controls_height * top_controls_hide_threshold), | 49 top_controls_height * top_controls_hide_threshold), |
51 top_controls_hide_height_( | 50 top_controls_hide_height_( |
52 top_controls_height * (1.f - top_controls_show_threshold)), | 51 top_controls_height * (1.f - top_controls_show_threshold)), |
53 pinch_gesture_active_(false) { | 52 pinch_gesture_active_(false) { |
54 CHECK(client_); | 53 CHECK(client_); |
55 } | 54 } |
56 | 55 |
57 TopControlsManager::~TopControlsManager() { | 56 TopControlsManager::~TopControlsManager() { |
58 } | 57 } |
59 | 58 |
| 59 float TopControlsManager::ControlsTopOffset() { |
| 60 return client_->ControlsTopOffset(); |
| 61 } |
| 62 |
| 63 float TopControlsManager::ContentTopOffset() { |
| 64 return client_->ControlsTopOffset() + top_controls_height_; |
| 65 } |
| 66 |
60 void TopControlsManager::UpdateTopControlsState(TopControlsState constraints, | 67 void TopControlsManager::UpdateTopControlsState(TopControlsState constraints, |
61 TopControlsState current, | 68 TopControlsState current, |
62 bool animate) { | 69 bool animate) { |
63 DCHECK(!(constraints == SHOWN && current == HIDDEN)); | 70 DCHECK(!(constraints == SHOWN && current == HIDDEN)); |
64 DCHECK(!(constraints == HIDDEN && current == SHOWN)); | 71 DCHECK(!(constraints == HIDDEN && current == SHOWN)); |
65 | 72 |
66 permitted_state_ = constraints; | 73 permitted_state_ = constraints; |
67 | 74 |
68 // Don't do anything if it doesn't matter which state the controls are in. | 75 // Don't do anything if it doesn't matter which state the controls are in. |
69 if (constraints == BOTH && current == BOTH) | 76 if (constraints == BOTH && current == BOTH) |
70 return; | 77 return; |
71 | 78 |
72 // Don't do anything if there is no change in offset. | 79 // Don't do anything if there is no change in offset. |
73 float final_controls_position = 0.f; | 80 float final_controls_position = 0.f; |
74 if (constraints == HIDDEN || current == HIDDEN) { | 81 if (constraints == HIDDEN || current == HIDDEN) { |
75 final_controls_position = -top_controls_height_; | 82 final_controls_position = -top_controls_height_; |
76 } | 83 } |
77 if (final_controls_position == controls_top_offset_) { | 84 if (final_controls_position == client_->ControlsTopOffset()) { |
78 return; | 85 return; |
79 } | 86 } |
80 | 87 |
81 AnimationDirection animation_direction = SHOWING_CONTROLS; | 88 AnimationDirection animation_direction = SHOWING_CONTROLS; |
82 if (constraints == HIDDEN || current == HIDDEN) | 89 if (constraints == HIDDEN || current == HIDDEN) |
83 animation_direction = HIDING_CONTROLS; | 90 animation_direction = HIDING_CONTROLS; |
84 ResetAnimations(); | 91 ResetAnimations(); |
85 if (animate) { | 92 if (animate) { |
86 SetupAnimation(animation_direction); | 93 SetupAnimation(animation_direction); |
87 } else { | 94 } else { |
88 controls_top_offset_ = final_controls_position; | 95 client_->SetControlsTopOffset(final_controls_position); |
89 } | 96 } |
90 client_->DidChangeTopControlsPosition(); | 97 client_->DidChangeTopControlsPosition(); |
91 } | 98 } |
92 | 99 |
93 void TopControlsManager::ScrollBegin() { | 100 void TopControlsManager::ScrollBegin() { |
94 DCHECK(!pinch_gesture_active_); | 101 DCHECK(!pinch_gesture_active_); |
95 ResetAnimations(); | 102 ResetAnimations(); |
96 current_scroll_delta_ = 0.f; | 103 current_scroll_delta_ = 0.f; |
97 controls_scroll_begin_offset_ = controls_top_offset_; | 104 controls_scroll_begin_offset_ = client_->ControlsTopOffset(); |
98 } | 105 } |
99 | 106 |
100 gfx::Vector2dF TopControlsManager::ScrollBy( | 107 gfx::Vector2dF TopControlsManager::ScrollBy( |
101 const gfx::Vector2dF& pending_delta) { | 108 const gfx::Vector2dF& pending_delta) { |
102 if (pinch_gesture_active_) | 109 if (pinch_gesture_active_) |
103 return pending_delta; | 110 return pending_delta; |
104 | 111 |
105 if (permitted_state_ == SHOWN && pending_delta.y() > 0) | 112 if (permitted_state_ == SHOWN && pending_delta.y() > 0) |
106 return pending_delta; | 113 return pending_delta; |
107 else if (permitted_state_ == HIDDEN && pending_delta.y() < 0) | 114 else if (permitted_state_ == HIDDEN && pending_delta.y() < 0) |
108 return pending_delta; | 115 return pending_delta; |
109 | 116 |
110 current_scroll_delta_ += pending_delta.y(); | 117 current_scroll_delta_ += pending_delta.y(); |
111 | 118 |
112 float old_offset = controls_top_offset_; | 119 float old_offset = client_->ControlsTopOffset(); |
113 SetControlsTopOffset(controls_scroll_begin_offset_ - current_scroll_delta_); | 120 SetControlsTopOffset(controls_scroll_begin_offset_ - current_scroll_delta_); |
114 | 121 |
115 // If the controls are fully visible, treat the current position as the | 122 // If the controls are fully visible, treat the current position as the |
116 // new baseline even if the gesture didn't end. | 123 // new baseline even if the gesture didn't end. |
117 if (controls_top_offset_ == 0.f) { | 124 if (client_->ControlsTopOffset() == 0.f) { |
118 current_scroll_delta_ = 0.f; | 125 current_scroll_delta_ = 0.f; |
119 controls_scroll_begin_offset_ = 0.f; | 126 controls_scroll_begin_offset_ = 0.f; |
120 } | 127 } |
121 | 128 |
122 ResetAnimations(); | 129 ResetAnimations(); |
123 | 130 |
124 gfx::Vector2dF applied_delta(0.f, old_offset - controls_top_offset_); | 131 gfx::Vector2dF applied_delta(0.f, old_offset - client_->ControlsTopOffset()); |
125 return pending_delta - applied_delta; | 132 return pending_delta - applied_delta; |
126 } | 133 } |
127 | 134 |
128 void TopControlsManager::ScrollEnd() { | 135 void TopControlsManager::ScrollEnd() { |
129 DCHECK(!pinch_gesture_active_); | 136 DCHECK(!pinch_gesture_active_); |
130 StartAnimationIfNecessary(); | 137 StartAnimationIfNecessary(); |
131 } | 138 } |
132 | 139 |
133 void TopControlsManager::PinchBegin() { | 140 void TopControlsManager::PinchBegin() { |
134 DCHECK(!pinch_gesture_active_); | 141 DCHECK(!pinch_gesture_active_); |
135 pinch_gesture_active_ = true; | 142 pinch_gesture_active_ = true; |
136 StartAnimationIfNecessary(); | 143 StartAnimationIfNecessary(); |
137 } | 144 } |
138 | 145 |
139 void TopControlsManager::PinchEnd() { | 146 void TopControlsManager::PinchEnd() { |
140 DCHECK(pinch_gesture_active_); | 147 DCHECK(pinch_gesture_active_); |
141 // Pinch{Begin,End} will always occur within the scope of Scroll{Begin,End}, | 148 // Pinch{Begin,End} will always occur within the scope of Scroll{Begin,End}, |
142 // so return to a state expected by the remaining scroll sequence. | 149 // so return to a state expected by the remaining scroll sequence. |
143 pinch_gesture_active_ = false; | 150 pinch_gesture_active_ = false; |
144 ScrollBegin(); | 151 ScrollBegin(); |
145 } | 152 } |
146 | 153 |
147 void TopControlsManager::SetControlsTopOffset(float controls_top_offset) { | 154 void TopControlsManager::SetControlsTopOffset(float controls_top_offset) { |
148 controls_top_offset = std::max(controls_top_offset, -top_controls_height_); | 155 controls_top_offset = std::max(controls_top_offset, -top_controls_height_); |
149 controls_top_offset = std::min(controls_top_offset, 0.f); | 156 controls_top_offset = std::min(controls_top_offset, 0.f); |
150 | 157 |
151 if (controls_top_offset_ == controls_top_offset) | 158 if (client_->ControlsTopOffset() == controls_top_offset) |
152 return; | 159 return; |
153 | 160 |
154 controls_top_offset_ = controls_top_offset; | 161 client_->SetControlsTopOffset(controls_top_offset); |
155 | 162 |
156 client_->DidChangeTopControlsPosition(); | 163 client_->DidChangeTopControlsPosition(); |
157 } | 164 } |
158 | 165 |
159 gfx::Vector2dF TopControlsManager::Animate(base::TimeTicks monotonic_time) { | 166 gfx::Vector2dF TopControlsManager::Animate(base::TimeTicks monotonic_time) { |
160 if (!top_controls_animation_ || !client_->HaveRootScrollLayer()) | 167 if (!top_controls_animation_ || !client_->HaveRootScrollLayer()) |
161 return gfx::Vector2dF(); | 168 return gfx::Vector2dF(); |
162 | 169 |
163 double time = (monotonic_time - base::TimeTicks()).InMillisecondsF(); | 170 double time = (monotonic_time - base::TimeTicks()).InMillisecondsF(); |
164 | 171 |
165 float old_offset = controls_top_offset_; | 172 float old_offset = client_->ControlsTopOffset(); |
166 SetControlsTopOffset(top_controls_animation_->GetValue(time)); | 173 SetControlsTopOffset(top_controls_animation_->GetValue(time)); |
167 | 174 |
168 if (IsAnimationCompleteAtTime(monotonic_time)) | 175 if (IsAnimationCompleteAtTime(monotonic_time)) |
169 ResetAnimations(); | 176 ResetAnimations(); |
170 | 177 |
171 gfx::Vector2dF scroll_delta(0.f, controls_top_offset_ - old_offset); | 178 gfx::Vector2dF scroll_delta(0.f, client_->ControlsTopOffset() - old_offset); |
172 return scroll_delta; | 179 return scroll_delta; |
173 } | 180 } |
174 | 181 |
175 void TopControlsManager::ResetAnimations() { | 182 void TopControlsManager::ResetAnimations() { |
176 if (top_controls_animation_) | 183 if (top_controls_animation_) |
177 top_controls_animation_.reset(); | 184 top_controls_animation_.reset(); |
178 | 185 |
179 animation_direction_ = NO_ANIMATION; | 186 animation_direction_ = NO_ANIMATION; |
180 } | 187 } |
181 | 188 |
182 void TopControlsManager::SetupAnimation(AnimationDirection direction) { | 189 void TopControlsManager::SetupAnimation(AnimationDirection direction) { |
183 DCHECK(direction != NO_ANIMATION); | 190 DCHECK(direction != NO_ANIMATION); |
184 | 191 |
185 if (direction == SHOWING_CONTROLS && controls_top_offset_ == 0) | 192 if (direction == SHOWING_CONTROLS && client_->ControlsTopOffset() == 0) |
186 return; | 193 return; |
187 | 194 |
188 if (direction == HIDING_CONTROLS && | 195 if (direction == HIDING_CONTROLS && |
189 controls_top_offset_ == -top_controls_height_) { | 196 client_->ControlsTopOffset() == -top_controls_height_) { |
190 return; | 197 return; |
191 } | 198 } |
192 | 199 |
193 if (top_controls_animation_ && animation_direction_ == direction) | 200 if (top_controls_animation_ && animation_direction_ == direction) |
194 return; | 201 return; |
195 | 202 |
196 top_controls_animation_ = KeyframedFloatAnimationCurve::Create(); | 203 top_controls_animation_ = KeyframedFloatAnimationCurve::Create(); |
197 double start_time = | 204 double start_time = |
198 (gfx::FrameTime::Now() - base::TimeTicks()).InMillisecondsF(); | 205 (gfx::FrameTime::Now() - base::TimeTicks()).InMillisecondsF(); |
199 top_controls_animation_->AddKeyframe( | 206 top_controls_animation_->AddKeyframe( |
200 FloatKeyframe::Create(start_time, controls_top_offset_, | 207 FloatKeyframe::Create(start_time, client_->ControlsTopOffset(), |
201 scoped_ptr<TimingFunction>())); | 208 scoped_ptr<TimingFunction>())); |
202 float max_ending_offset = | 209 float max_ending_offset = |
203 (direction == SHOWING_CONTROLS ? 1 : -1) * top_controls_height_; | 210 (direction == SHOWING_CONTROLS ? 1 : -1) * top_controls_height_; |
204 top_controls_animation_->AddKeyframe( | 211 top_controls_animation_->AddKeyframe( |
205 FloatKeyframe::Create(start_time + kShowHideMaxDurationMs, | 212 FloatKeyframe::Create(start_time + kShowHideMaxDurationMs, |
206 controls_top_offset_ + max_ending_offset, | 213 client_->ControlsTopOffset() + max_ending_offset, |
207 EaseTimingFunction::Create())); | 214 EaseTimingFunction::Create())); |
208 animation_direction_ = direction; | 215 animation_direction_ = direction; |
209 client_->DidChangeTopControlsPosition(); | 216 client_->DidChangeTopControlsPosition(); |
210 } | 217 } |
211 | 218 |
212 void TopControlsManager::StartAnimationIfNecessary() { | 219 void TopControlsManager::StartAnimationIfNecessary() { |
213 if (controls_top_offset_ != 0 | 220 if (client_->ControlsTopOffset() != 0 |
214 && controls_top_offset_ != -top_controls_height_) { | 221 && client_->ControlsTopOffset() != -top_controls_height_) { |
215 AnimationDirection show_controls = NO_ANIMATION; | 222 AnimationDirection show_controls = NO_ANIMATION; |
216 | 223 |
217 if (controls_top_offset_ >= -top_controls_show_height_) { | 224 if (client_->ControlsTopOffset() >= -top_controls_show_height_) { |
218 // If we're showing so much that the hide threshold won't trigger, show. | 225 // If we're showing so much that the hide threshold won't trigger, show. |
219 show_controls = SHOWING_CONTROLS; | 226 show_controls = SHOWING_CONTROLS; |
220 } else if (controls_top_offset_ <= -top_controls_hide_height_) { | 227 } else if (client_->ControlsTopOffset() <= -top_controls_hide_height_) { |
221 // If we're showing so little that the show threshold won't trigger, hide. | 228 // If we're showing so little that the show threshold won't trigger, hide. |
222 show_controls = HIDING_CONTROLS; | 229 show_controls = HIDING_CONTROLS; |
223 } else { | 230 } else { |
224 // If we could be either showing or hiding, we determine which one to | 231 // If we could be either showing or hiding, we determine which one to |
225 // do based on whether or not the total scroll delta was moving up or | 232 // do based on whether or not the total scroll delta was moving up or |
226 // down. | 233 // down. |
227 show_controls = current_scroll_delta_ <= 0.f ? | 234 show_controls = current_scroll_delta_ <= 0.f ? |
228 SHOWING_CONTROLS : HIDING_CONTROLS; | 235 SHOWING_CONTROLS : HIDING_CONTROLS; |
229 } | 236 } |
230 | 237 |
(...skipping 11 matching lines...) Expand all Loading... |
242 | 249 |
243 if ((animation_direction_ == SHOWING_CONTROLS && new_offset >= 0) || | 250 if ((animation_direction_ == SHOWING_CONTROLS && new_offset >= 0) || |
244 (animation_direction_ == HIDING_CONTROLS | 251 (animation_direction_ == HIDING_CONTROLS |
245 && new_offset <= -top_controls_height_)) { | 252 && new_offset <= -top_controls_height_)) { |
246 return true; | 253 return true; |
247 } | 254 } |
248 return false; | 255 return false; |
249 } | 256 } |
250 | 257 |
251 } // namespace cc | 258 } // namespace cc |
OLD | NEW |