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
3 // found in the LICENSE file.
5 #include "cc/input/top_controls_manager.h"
9 #include "base/logging.h"
10 #include "base/time/time.h"
11 #include "cc/animation/keyframed_animation_curve.h"
12 #include "cc/animation/timing_function.h"
13 #include "cc/input/top_controls_manager_client.h"
14 #include "cc/trees/layer_tree_impl.h"
15 #include "ui/gfx/transform.h"
16 #include "ui/gfx/vector2d_f.h"
20 // These constants were chosen empirically for their visually pleasant behavior.
21 // Contact tedchoc@chromium.org for questions about changing these values.
22 const int64 kShowHideMaxDurationMs
= 200;
26 scoped_ptr
<TopControlsManager
> TopControlsManager::Create(
27 TopControlsManagerClient
* client
,
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
,
33 top_controls_show_threshold
,
34 top_controls_hide_threshold
));
37 TopControlsManager::TopControlsManager(TopControlsManagerClient
* client
,
38 float top_controls_height
,
39 float top_controls_show_threshold
,
40 float top_controls_hide_threshold
)
42 animation_direction_(NO_ANIMATION
),
43 permitted_state_(BOTH
),
44 controls_top_offset_(0.f
),
45 top_controls_height_(top_controls_height
),
46 current_scroll_delta_(0.f
),
47 controls_scroll_begin_offset_(0.f
),
48 top_controls_show_height_(
49 top_controls_height
* top_controls_hide_threshold
),
50 top_controls_hide_height_(
51 top_controls_height
* (1.f
- top_controls_show_threshold
)),
52 pinch_gesture_active_(false) {
56 TopControlsManager::~TopControlsManager() {
59 void TopControlsManager::UpdateTopControlsState(TopControlsState constraints
,
60 TopControlsState current
,
62 DCHECK(!(constraints
== SHOWN
&& current
== HIDDEN
));
63 DCHECK(!(constraints
== HIDDEN
&& current
== SHOWN
));
65 permitted_state_
= constraints
;
67 // Don't do anything if it doesn't matter which state the controls are in.
68 if (constraints
== BOTH
&& current
== BOTH
)
71 // Don't do anything if there is no change in offset.
72 float final_controls_position
= 0.f
;
73 if (constraints
== HIDDEN
|| current
== HIDDEN
) {
74 final_controls_position
= -top_controls_height_
;
76 if (final_controls_position
== controls_top_offset_
) {
80 AnimationDirection animation_direction
= SHOWING_CONTROLS
;
81 if (constraints
== HIDDEN
|| current
== HIDDEN
)
82 animation_direction
= HIDING_CONTROLS
;
85 SetupAnimation(animation_direction
);
87 controls_top_offset_
= final_controls_position
;
89 client_
->DidChangeTopControlsPosition();
92 void TopControlsManager::ScrollBegin() {
93 DCHECK(!pinch_gesture_active_
);
95 current_scroll_delta_
= 0.f
;
96 controls_scroll_begin_offset_
= controls_top_offset_
;
99 gfx::Vector2dF
TopControlsManager::ScrollBy(
100 const gfx::Vector2dF pending_delta
) {
101 if (pinch_gesture_active_
)
102 return pending_delta
;
104 if (permitted_state_
== SHOWN
&& pending_delta
.y() > 0)
105 return pending_delta
;
106 else if (permitted_state_
== HIDDEN
&& pending_delta
.y() < 0)
107 return pending_delta
;
109 current_scroll_delta_
+= pending_delta
.y();
111 float old_offset
= controls_top_offset_
;
112 SetControlsTopOffset(controls_scroll_begin_offset_
- current_scroll_delta_
);
114 // If the controls are fully visible, treat the current position as the
115 // new baseline even if the gesture didn't end.
116 if (controls_top_offset_
== 0.f
) {
117 current_scroll_delta_
= 0.f
;
118 controls_scroll_begin_offset_
= 0.f
;
123 gfx::Vector2dF
applied_delta(0.f
, old_offset
- controls_top_offset_
);
124 return pending_delta
- applied_delta
;
127 void TopControlsManager::ScrollEnd() {
128 DCHECK(!pinch_gesture_active_
);
129 StartAnimationIfNecessary();
132 void TopControlsManager::PinchBegin() {
133 DCHECK(!pinch_gesture_active_
);
134 pinch_gesture_active_
= true;
135 StartAnimationIfNecessary();
138 void TopControlsManager::PinchEnd() {
139 DCHECK(pinch_gesture_active_
);
140 // Pinch{Begin,End} will always occur within the scope of Scroll{Begin,End},
141 // so return to a state expected by the remaining scroll sequence.
142 pinch_gesture_active_
= false;
146 void TopControlsManager::SetControlsTopOffset(float controls_top_offset
) {
147 controls_top_offset
= std::max(controls_top_offset
, -top_controls_height_
);
148 controls_top_offset
= std::min(controls_top_offset
, 0.f
);
150 if (controls_top_offset_
== controls_top_offset
)
153 controls_top_offset_
= controls_top_offset
;
155 client_
->DidChangeTopControlsPosition();
158 gfx::Vector2dF
TopControlsManager::Animate(base::TimeTicks monotonic_time
) {
159 if (!top_controls_animation_
|| !client_
->HaveRootScrollLayer())
160 return gfx::Vector2dF();
162 double time
= (monotonic_time
- base::TimeTicks()).InMillisecondsF();
164 float old_offset
= controls_top_offset_
;
165 SetControlsTopOffset(top_controls_animation_
->GetValue(time
));
167 if (IsAnimationCompleteAtTime(monotonic_time
))
170 gfx::Vector2dF
scroll_delta(0.f
, controls_top_offset_
- old_offset
);
174 void TopControlsManager::ResetAnimations() {
175 if (top_controls_animation_
)
176 top_controls_animation_
.reset();
178 animation_direction_
= NO_ANIMATION
;
181 void TopControlsManager::SetupAnimation(AnimationDirection direction
) {
182 DCHECK(direction
!= NO_ANIMATION
);
184 if (direction
== SHOWING_CONTROLS
&& controls_top_offset_
== 0)
187 if (direction
== HIDING_CONTROLS
&&
188 controls_top_offset_
== -top_controls_height_
) {
192 if (top_controls_animation_
&& animation_direction_
== direction
)
195 top_controls_animation_
= KeyframedFloatAnimationCurve::Create();
197 (base::TimeTicks::Now() - base::TimeTicks()).InMillisecondsF();
198 top_controls_animation_
->AddKeyframe(
199 FloatKeyframe::Create(start_time
, controls_top_offset_
,
200 scoped_ptr
<TimingFunction
>()));
201 float max_ending_offset
=
202 (direction
== SHOWING_CONTROLS
? 1 : -1) * top_controls_height_
;
203 top_controls_animation_
->AddKeyframe(
204 FloatKeyframe::Create(start_time
+ kShowHideMaxDurationMs
,
205 controls_top_offset_
+ max_ending_offset
,
206 EaseTimingFunction::Create()));
207 animation_direction_
= direction
;
208 client_
->DidChangeTopControlsPosition();
211 void TopControlsManager::StartAnimationIfNecessary() {
212 if (controls_top_offset_
!= 0
213 && controls_top_offset_
!= -top_controls_height_
) {
214 AnimationDirection show_controls
= NO_ANIMATION
;
216 if (controls_top_offset_
>= -top_controls_show_height_
) {
217 // If we're showing so much that the hide threshold won't trigger, show.
218 show_controls
= SHOWING_CONTROLS
;
219 } else if (controls_top_offset_
<= -top_controls_hide_height_
) {
220 // If we're showing so little that the show threshold won't trigger, hide.
221 show_controls
= HIDING_CONTROLS
;
223 // If we could be either showing or hiding, we determine which one to
224 // do based on whether or not the total scroll delta was moving up or
226 show_controls
= current_scroll_delta_
<= 0.f
?
227 SHOWING_CONTROLS
: HIDING_CONTROLS
;
230 if (show_controls
!= NO_ANIMATION
)
231 SetupAnimation(show_controls
);
235 bool TopControlsManager::IsAnimationCompleteAtTime(base::TimeTicks time
) {
236 if (!top_controls_animation_
)
239 double time_ms
= (time
- base::TimeTicks()).InMillisecondsF();
240 float new_offset
= top_controls_animation_
->GetValue(time_ms
);
242 if ((animation_direction_
== SHOWING_CONTROLS
&& new_offset
>= 0) ||
243 (animation_direction_
== HIDING_CONTROLS
244 && new_offset
<= -top_controls_height_
)) {