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 "base/time.h" | 10 #include "base/time.h" |
(...skipping 22 matching lines...) Expand all Loading... | |
33 top_controls_show_threshold, | 33 top_controls_show_threshold, |
34 top_controls_hide_threshold)); | 34 top_controls_hide_threshold)); |
35 } | 35 } |
36 | 36 |
37 TopControlsManager::TopControlsManager(TopControlsManagerClient* client, | 37 TopControlsManager::TopControlsManager(TopControlsManagerClient* client, |
38 float top_controls_height, | 38 float top_controls_height, |
39 float top_controls_show_threshold, | 39 float top_controls_show_threshold, |
40 float top_controls_hide_threshold) | 40 float top_controls_hide_threshold) |
41 : client_(client), | 41 : client_(client), |
42 animation_direction_(NO_ANIMATION), | 42 animation_direction_(NO_ANIMATION), |
43 visibility_restriction_(NONE), | 43 permitted_state_(BOTH), |
44 controls_top_offset_(0.f), | 44 controls_top_offset_(0.f), |
45 top_controls_height_(top_controls_height), | 45 top_controls_height_(top_controls_height), |
46 current_scroll_delta_(0.f), | 46 current_scroll_delta_(0.f), |
47 controls_scroll_begin_offset_(0.f), | 47 controls_scroll_begin_offset_(0.f), |
48 top_controls_show_height_( | 48 top_controls_show_height_( |
49 top_controls_height * top_controls_hide_threshold), | 49 top_controls_height * top_controls_hide_threshold), |
50 top_controls_hide_height_( | 50 top_controls_hide_height_( |
51 top_controls_height * (1.f - top_controls_show_threshold)) { | 51 top_controls_height * (1.f - top_controls_show_threshold)) { |
52 CHECK(client_); | 52 CHECK(client_); |
53 } | 53 } |
54 | 54 |
55 TopControlsManager::~TopControlsManager() { | 55 TopControlsManager::~TopControlsManager() { |
56 } | 56 } |
57 | 57 |
58 void TopControlsManager::UpdateTopControlsState(bool enable_hiding, | 58 void TopControlsManager::UpdateTopControlsState(TopControlsState constraints, |
59 bool enable_showing, | 59 TopControlsState current, |
60 bool animate) { | 60 bool animate) { |
61 float final_controls_position = 0.f; | 61 if (constraints == SHOWN) { |
62 | 62 DCHECK(current != HIDDEN); |
aelias_OOO_until_Jul13
2013/05/02 18:44:23
Nit, formulate it as:
DCHECK(!(constraints == SHO
Michael van Ouwerkerk
2013/05/08 16:21:15
Done.
| |
63 if (enable_hiding && enable_showing) { | 63 } else if (constraints == HIDDEN) { |
64 visibility_restriction_ = NONE; | 64 DCHECK(current != SHOWN); |
65 } else if (enable_showing || !enable_hiding) { | |
66 visibility_restriction_ = ALWAYS_SHOWN; | |
67 } else { | |
68 visibility_restriction_ = ALWAYS_HIDDEN; | |
69 final_controls_position = -top_controls_height_; | |
70 } | 65 } |
71 | 66 |
72 if (visibility_restriction_ != NONE && | 67 permitted_state_ = constraints; |
73 final_controls_position != controls_top_offset_) { | 68 |
74 ResetAnimations(); | 69 // Don't do anything if it doesn't matter which state the controls are in. |
70 if (constraints == BOTH && current == BOTH) return; | |
aelias_OOO_until_Jul13
2013/05/02 18:44:23
Style: return on next line
Michael van Ouwerkerk
2013/05/08 16:21:15
Done.
| |
71 | |
72 ResetAnimations(); | |
73 if (constraints == SHOWN || current == SHOWN) { | |
75 if (animate) { | 74 if (animate) { |
76 SetupAnimation(visibility_restriction_ == ALWAYS_SHOWN ? | 75 SetupAnimation(SHOWING_CONTROLS); |
77 SHOWING_CONTROLS : HIDING_CONTROLS); | |
78 } else { | 76 } else { |
79 controls_top_offset_ = final_controls_position; | 77 controls_top_offset_ = 0.f; |
80 } | 78 } |
81 client_->DidChangeTopControlsPosition(); | |
82 } | 79 } |
80 if (constraints == HIDDEN || current == HIDDEN) { | |
81 if (animate) { | |
82 SetupAnimation(HIDING_CONTROLS); | |
83 } else { | |
84 controls_top_offset_ = -top_controls_height_; | |
85 } | |
86 } | |
87 client_->DidChangeTopControlsPosition(); | |
aelias_OOO_until_Jul13
2013/05/02 18:44:23
Please track the old value of controls_top_offset_
Michael van Ouwerkerk
2013/05/08 16:21:15
Done.
| |
83 } | 88 } |
84 | 89 |
85 void TopControlsManager::ScrollBegin() { | 90 void TopControlsManager::ScrollBegin() { |
86 ResetAnimations(); | 91 ResetAnimations(); |
87 current_scroll_delta_ = 0.f; | 92 current_scroll_delta_ = 0.f; |
88 controls_scroll_begin_offset_ = controls_top_offset_; | 93 controls_scroll_begin_offset_ = controls_top_offset_; |
89 } | 94 } |
90 | 95 |
91 gfx::Vector2dF TopControlsManager::ScrollBy( | 96 gfx::Vector2dF TopControlsManager::ScrollBy( |
92 const gfx::Vector2dF pending_delta) { | 97 const gfx::Vector2dF pending_delta) { |
93 if (visibility_restriction_ == ALWAYS_SHOWN && pending_delta.y() > 0) | 98 if (permitted_state_ == SHOWN && pending_delta.y() > 0) |
94 return pending_delta; | 99 return pending_delta; |
95 else if (visibility_restriction_ == ALWAYS_HIDDEN && pending_delta.y() < 0) | 100 else if (permitted_state_ == HIDDEN && pending_delta.y() < 0) |
96 return pending_delta; | 101 return pending_delta; |
97 | 102 |
98 current_scroll_delta_ += pending_delta.y(); | 103 current_scroll_delta_ += pending_delta.y(); |
99 | 104 |
100 float old_offset = controls_top_offset_; | 105 float old_offset = controls_top_offset_; |
101 SetControlsTopOffset(controls_scroll_begin_offset_ - current_scroll_delta_); | 106 SetControlsTopOffset(controls_scroll_begin_offset_ - current_scroll_delta_); |
102 | 107 |
103 // If the controls are fully visible, treat the current position as the | 108 // If the controls are fully visible, treat the current position as the |
104 // new baseline even if the gesture didn't end. | 109 // new baseline even if the gesture didn't end. |
105 if (controls_top_offset_ == 0.f) { | 110 if (controls_top_offset_ == 0.f) { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
146 } | 151 } |
147 | 152 |
148 void TopControlsManager::ResetAnimations() { | 153 void TopControlsManager::ResetAnimations() { |
149 if (top_controls_animation_) | 154 if (top_controls_animation_) |
150 top_controls_animation_.reset(); | 155 top_controls_animation_.reset(); |
151 | 156 |
152 animation_direction_ = NO_ANIMATION; | 157 animation_direction_ = NO_ANIMATION; |
153 } | 158 } |
154 | 159 |
155 void TopControlsManager::SetupAnimation(AnimationDirection direction) { | 160 void TopControlsManager::SetupAnimation(AnimationDirection direction) { |
161 DCHECK(direction != NO_ANIMATION); | |
162 | |
163 if (direction == SHOWING_CONTROLS && controls_top_offset_ == 0) | |
164 return; | |
165 | |
166 if (direction == HIDING_CONTROLS && | |
167 controls_top_offset_ == -top_controls_height_) { | |
168 return; | |
169 } | |
170 | |
171 if (top_controls_animation_ && animation_direction_ == direction) | |
172 return; | |
173 | |
156 top_controls_animation_ = KeyframedFloatAnimationCurve::Create(); | 174 top_controls_animation_ = KeyframedFloatAnimationCurve::Create(); |
157 double start_time = | 175 double start_time = |
158 (base::TimeTicks::Now() - base::TimeTicks()).InMillisecondsF(); | 176 (base::TimeTicks::Now() - base::TimeTicks()).InMillisecondsF(); |
159 top_controls_animation_->AddKeyframe( | 177 top_controls_animation_->AddKeyframe( |
160 FloatKeyframe::Create(start_time, controls_top_offset_, | 178 FloatKeyframe::Create(start_time, controls_top_offset_, |
161 scoped_ptr<TimingFunction>())); | 179 scoped_ptr<TimingFunction>())); |
162 float max_ending_offset = | 180 float max_ending_offset = |
163 (direction == SHOWING_CONTROLS ? 1 : -1) * top_controls_height_; | 181 (direction == SHOWING_CONTROLS ? 1 : -1) * top_controls_height_; |
164 top_controls_animation_->AddKeyframe( | 182 top_controls_animation_->AddKeyframe( |
165 FloatKeyframe::Create(start_time + kShowHideMaxDurationMs, | 183 FloatKeyframe::Create(start_time + kShowHideMaxDurationMs, |
166 controls_top_offset_ + max_ending_offset, | 184 controls_top_offset_ + max_ending_offset, |
167 EaseTimingFunction::Create())); | 185 EaseTimingFunction::Create())); |
168 animation_direction_ = direction; | 186 animation_direction_ = direction; |
187 client_->DidChangeTopControlsPosition(); | |
169 } | 188 } |
170 | 189 |
171 void TopControlsManager::StartAnimationIfNecessary() { | 190 void TopControlsManager::StartAnimationIfNecessary() { |
172 if (controls_top_offset_ != 0 | 191 if (controls_top_offset_ != 0 |
173 && controls_top_offset_ != -top_controls_height_) { | 192 && controls_top_offset_ != -top_controls_height_) { |
174 AnimationDirection show_controls = NO_ANIMATION; | 193 AnimationDirection show_controls = NO_ANIMATION; |
175 | 194 |
176 if (controls_top_offset_ >= -top_controls_show_height_) { | 195 if (controls_top_offset_ >= -top_controls_show_height_) { |
177 // If we're showing so much that the hide threshold won't trigger, show. | 196 // If we're showing so much that the hide threshold won't trigger, show. |
178 show_controls = SHOWING_CONTROLS; | 197 show_controls = SHOWING_CONTROLS; |
179 } else if (controls_top_offset_ <= -top_controls_hide_height_) { | 198 } else if (controls_top_offset_ <= -top_controls_hide_height_) { |
180 // If we're showing so little that the show threshold won't trigger, hide. | 199 // If we're showing so little that the show threshold won't trigger, hide. |
181 show_controls = HIDING_CONTROLS; | 200 show_controls = HIDING_CONTROLS; |
182 } else { | 201 } else { |
183 // If we could be either showing or hiding, we determine which one to | 202 // If we could be either showing or hiding, we determine which one to |
184 // do based on whether or not the total scroll delta was moving up or | 203 // do based on whether or not the total scroll delta was moving up or |
185 // down. | 204 // down. |
186 show_controls = current_scroll_delta_ <= 0.f ? | 205 show_controls = current_scroll_delta_ <= 0.f ? |
187 SHOWING_CONTROLS : HIDING_CONTROLS; | 206 SHOWING_CONTROLS : HIDING_CONTROLS; |
188 } | 207 } |
189 | 208 |
190 if (show_controls != NO_ANIMATION && | 209 if (show_controls != NO_ANIMATION) |
191 (!top_controls_animation_ || animation_direction_ != show_controls)) { | |
192 SetupAnimation(show_controls); | 210 SetupAnimation(show_controls); |
193 client_->DidChangeTopControlsPosition(); | |
194 } | |
195 } | 211 } |
196 } | 212 } |
197 | 213 |
198 bool TopControlsManager::IsAnimationCompleteAtTime(base::TimeTicks time) { | 214 bool TopControlsManager::IsAnimationCompleteAtTime(base::TimeTicks time) { |
199 if (!top_controls_animation_) | 215 if (!top_controls_animation_) |
200 return true; | 216 return true; |
201 | 217 |
202 double time_ms = (time - base::TimeTicks()).InMillisecondsF(); | 218 double time_ms = (time - base::TimeTicks()).InMillisecondsF(); |
203 float new_offset = top_controls_animation_->GetValue(time_ms); | 219 float new_offset = top_controls_animation_->GetValue(time_ms); |
204 | 220 |
205 if ((animation_direction_ == SHOWING_CONTROLS && new_offset >= 0) || | 221 if ((animation_direction_ == SHOWING_CONTROLS && new_offset >= 0) || |
206 (animation_direction_ == HIDING_CONTROLS | 222 (animation_direction_ == HIDING_CONTROLS |
207 && new_offset <= -top_controls_height_)) { | 223 && new_offset <= -top_controls_height_)) { |
208 return true; | 224 return true; |
209 } | 225 } |
210 return false; | 226 return false; |
211 } | 227 } |
212 | 228 |
213 } // namespace cc | 229 } // namespace cc |
OLD | NEW |