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 "cc/animation/keyframed_animation_curve.h"
11 #include "cc/animation/timing_function.h"
12 #include "cc/input/top_controls_manager_client.h"
13 #include "cc/output/begin_frame_args.h"
14 #include "cc/trees/layer_tree_impl.h"
15 #include "ui/gfx/frame_time.h"
16 #include "ui/gfx/geometry/vector2d_f.h"
17 #include "ui/gfx/transform.h"
21 // These constants were chosen empirically for their visually pleasant behavior.
22 // Contact tedchoc@chromium.org for questions about changing these values.
23 const int64 kShowHideMaxDurationMs
= 200;
27 scoped_ptr
<TopControlsManager
> TopControlsManager::Create(
28 TopControlsManagerClient
* client
,
29 float top_controls_show_threshold
,
30 float top_controls_hide_threshold
) {
31 return make_scoped_ptr(new TopControlsManager(client
,
32 top_controls_show_threshold
,
33 top_controls_hide_threshold
));
36 TopControlsManager::TopControlsManager(TopControlsManagerClient
* client
,
37 float top_controls_show_threshold
,
38 float top_controls_hide_threshold
)
40 animation_direction_(NO_ANIMATION
),
41 permitted_state_(BOTH
),
42 accumulated_scroll_delta_(0.f
),
43 baseline_content_offset_(0.f
),
44 top_controls_show_threshold_(top_controls_hide_threshold
),
45 top_controls_hide_threshold_(top_controls_show_threshold
),
46 pinch_gesture_active_(false) {
50 TopControlsManager::~TopControlsManager() {
53 float TopControlsManager::ControlsTopOffset() const {
54 return ContentTopOffset() - TopControlsHeight();
57 float TopControlsManager::ContentTopOffset() const {
58 return TopControlsShownRatio() * TopControlsHeight();
61 float TopControlsManager::TopControlsShownRatio() const {
62 return client_
->CurrentTopControlsShownRatio();
65 float TopControlsManager::TopControlsHeight() const {
66 return client_
->TopControlsHeight();
69 void TopControlsManager::UpdateTopControlsState(TopControlsState constraints
,
70 TopControlsState current
,
72 DCHECK(!(constraints
== SHOWN
&& current
== HIDDEN
));
73 DCHECK(!(constraints
== HIDDEN
&& current
== SHOWN
));
75 permitted_state_
= constraints
;
77 // Don't do anything if it doesn't matter which state the controls are in.
78 if (constraints
== BOTH
&& current
== BOTH
)
81 // Don't do anything if there is no change in offset.
82 float final_shown_ratio
= 1.f
;
83 if (constraints
== HIDDEN
|| current
== HIDDEN
)
84 final_shown_ratio
= 0.f
;
85 if (final_shown_ratio
== TopControlsShownRatio())
89 SetupAnimation(final_shown_ratio
? SHOWING_CONTROLS
: HIDING_CONTROLS
);
92 client_
->SetCurrentTopControlsShownRatio(final_shown_ratio
);
96 void TopControlsManager::ScrollBegin() {
97 DCHECK(!pinch_gesture_active_
);
102 gfx::Vector2dF
TopControlsManager::ScrollBy(
103 const gfx::Vector2dF
& pending_delta
) {
104 if (pinch_gesture_active_
)
105 return pending_delta
;
107 if (permitted_state_
== SHOWN
&& pending_delta
.y() > 0)
108 return pending_delta
;
109 else if (permitted_state_
== HIDDEN
&& pending_delta
.y() < 0)
110 return pending_delta
;
112 accumulated_scroll_delta_
+= pending_delta
.y();
114 float old_offset
= ContentTopOffset();
115 client_
->SetCurrentTopControlsShownRatio(
116 (baseline_content_offset_
- accumulated_scroll_delta_
) /
117 TopControlsHeight());
119 // If the controls are fully visible, treat the current position as the
120 // new baseline even if the gesture didn't end.
121 if (TopControlsShownRatio() == 1.f
)
126 gfx::Vector2dF
applied_delta(0.f
, old_offset
- ContentTopOffset());
127 return pending_delta
- applied_delta
;
130 void TopControlsManager::ScrollEnd() {
131 DCHECK(!pinch_gesture_active_
);
132 StartAnimationIfNecessary();
135 void TopControlsManager::PinchBegin() {
136 DCHECK(!pinch_gesture_active_
);
137 pinch_gesture_active_
= true;
138 StartAnimationIfNecessary();
141 void TopControlsManager::PinchEnd() {
142 DCHECK(pinch_gesture_active_
);
143 // Pinch{Begin,End} will always occur within the scope of Scroll{Begin,End},
144 // so return to a state expected by the remaining scroll sequence.
145 pinch_gesture_active_
= false;
149 void TopControlsManager::MainThreadHasStoppedFlinging() {
150 StartAnimationIfNecessary();
153 gfx::Vector2dF
TopControlsManager::Animate(base::TimeTicks monotonic_time
) {
154 if (!top_controls_animation_
|| !client_
->HaveRootScrollLayer())
155 return gfx::Vector2dF();
157 base::TimeDelta time
= monotonic_time
- base::TimeTicks();
159 float old_offset
= ContentTopOffset();
160 client_
->SetCurrentTopControlsShownRatio(
161 top_controls_animation_
->GetValue(time
));
163 if (IsAnimationCompleteAtTime(monotonic_time
))
166 gfx::Vector2dF
scroll_delta(0.f
, ContentTopOffset() - old_offset
);
170 void TopControlsManager::ResetAnimations() {
171 top_controls_animation_
= nullptr;
172 animation_direction_
= NO_ANIMATION
;
175 void TopControlsManager::SetupAnimation(AnimationDirection direction
) {
176 DCHECK_NE(NO_ANIMATION
, direction
);
177 DCHECK_IMPLIES(direction
== HIDING_CONTROLS
, TopControlsShownRatio() > 0.f
);
178 DCHECK_IMPLIES(direction
== SHOWING_CONTROLS
, TopControlsShownRatio() < 1.f
);
180 if (top_controls_animation_
&& animation_direction_
== direction
)
183 top_controls_animation_
= KeyframedFloatAnimationCurve::Create();
184 base::TimeDelta start_time
= gfx::FrameTime::Now() - base::TimeTicks();
185 top_controls_animation_
->AddKeyframe(
186 FloatKeyframe::Create(start_time
, TopControlsShownRatio(), nullptr));
187 float max_ending_ratio
= (direction
== SHOWING_CONTROLS
? 1 : -1);
188 top_controls_animation_
->AddKeyframe(FloatKeyframe::Create(
189 start_time
+ base::TimeDelta::FromMilliseconds(kShowHideMaxDurationMs
),
190 TopControlsShownRatio() + max_ending_ratio
,
191 EaseTimingFunction::Create()));
192 animation_direction_
= direction
;
193 client_
->DidChangeTopControlsPosition();
196 void TopControlsManager::StartAnimationIfNecessary() {
197 if (TopControlsShownRatio() == 0.f
|| TopControlsShownRatio() == 1.f
)
200 if (TopControlsShownRatio() >= 1.f
- top_controls_hide_threshold_
) {
201 // If we're showing so much that the hide threshold won't trigger, show.
202 SetupAnimation(SHOWING_CONTROLS
);
203 } else if (TopControlsShownRatio() <= top_controls_show_threshold_
) {
204 // If we're showing so little that the show threshold won't trigger, hide.
205 SetupAnimation(HIDING_CONTROLS
);
207 // If we could be either showing or hiding, we determine which one to
208 // do based on whether or not the total scroll delta was moving up or
210 SetupAnimation(accumulated_scroll_delta_
<= 0.f
? SHOWING_CONTROLS
215 bool TopControlsManager::IsAnimationCompleteAtTime(base::TimeTicks time
) {
216 if (!top_controls_animation_
)
219 base::TimeDelta animation_time
= time
- base::TimeTicks();
220 float new_ratio
= top_controls_animation_
->GetValue(animation_time
);
222 if ((animation_direction_
== SHOWING_CONTROLS
&& new_ratio
>= 1.f
) ||
223 (animation_direction_
== HIDING_CONTROLS
&& new_ratio
<= 0.f
)) {
229 void TopControlsManager::ResetBaseline() {
230 accumulated_scroll_delta_
= 0.f
;
231 baseline_content_offset_
= ContentTopOffset();