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_height,
30 float top_controls_show_threshold,
31 float top_controls_hide_threshold) {
32 return make_scoped_ptr(new TopControlsManager(client,
34 top_controls_show_threshold,
35 top_controls_hide_threshold));
38 TopControlsManager::TopControlsManager(TopControlsManagerClient* client,
39 float top_controls_height,
40 float top_controls_show_threshold,
41 float top_controls_hide_threshold)
43 animation_direction_(NO_ANIMATION),
44 permitted_state_(BOTH),
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 float TopControlsManager::ControlsTopOffset() {
60 return client_->ControlsTopOffset();
63 float TopControlsManager::ContentTopOffset() {
64 return client_->ControlsTopOffset() + top_controls_height_;
67 void TopControlsManager::UpdateTopControlsState(TopControlsState constraints,
68 TopControlsState current,
70 DCHECK(!(constraints == SHOWN && current == HIDDEN));
71 DCHECK(!(constraints == HIDDEN && current == SHOWN));
73 permitted_state_ = constraints;
75 // Don't do anything if it doesn't matter which state the controls are in.
76 if (constraints == BOTH && current == BOTH)
79 // Don't do anything if there is no change in offset.
80 float final_controls_position = 0.f;
81 if (constraints == HIDDEN || current == HIDDEN) {
82 final_controls_position = -top_controls_height_;
84 if (final_controls_position == client_->ControlsTopOffset()) {
88 AnimationDirection animation_direction = SHOWING_CONTROLS;
89 if (constraints == HIDDEN || current == HIDDEN)
90 animation_direction = HIDING_CONTROLS;
93 SetupAnimation(animation_direction);
95 client_->SetControlsTopOffset(final_controls_position);
97 client_->DidChangeTopControlsPosition();
100 void TopControlsManager::ScrollBegin() {
101 DCHECK(!pinch_gesture_active_);
103 current_scroll_delta_ = 0.f;
104 controls_scroll_begin_offset_ = client_->ControlsTopOffset();
107 gfx::Vector2dF TopControlsManager::ScrollBy(
108 const gfx::Vector2dF& pending_delta) {
109 if (pinch_gesture_active_)
110 return pending_delta;
112 if (permitted_state_ == SHOWN && pending_delta.y() > 0)
113 return pending_delta;
114 else if (permitted_state_ == HIDDEN && pending_delta.y() < 0)
115 return pending_delta;
117 current_scroll_delta_ += pending_delta.y();
119 float old_offset = client_->ControlsTopOffset();
120 SetControlsTopOffset(controls_scroll_begin_offset_ - current_scroll_delta_);
122 // If the controls are fully visible, treat the current position as the
123 // new baseline even if the gesture didn't end.
124 if (client_->ControlsTopOffset() == 0.f) {
125 current_scroll_delta_ = 0.f;
126 controls_scroll_begin_offset_ = 0.f;
131 gfx::Vector2dF applied_delta(0.f, old_offset - client_->ControlsTopOffset());
132 return pending_delta - applied_delta;
135 void TopControlsManager::ScrollEnd() {
136 DCHECK(!pinch_gesture_active_);
137 StartAnimationIfNecessary();
140 void TopControlsManager::PinchBegin() {
141 DCHECK(!pinch_gesture_active_);
142 pinch_gesture_active_ = true;
143 StartAnimationIfNecessary();
146 void TopControlsManager::PinchEnd() {
147 DCHECK(pinch_gesture_active_);
148 // Pinch{Begin,End} will always occur within the scope of Scroll{Begin,End},
149 // so return to a state expected by the remaining scroll sequence.
150 pinch_gesture_active_ = false;
154 void TopControlsManager::SetControlsTopOffset(float controls_top_offset) {
155 controls_top_offset = std::max(controls_top_offset, -top_controls_height_);
156 controls_top_offset = std::min(controls_top_offset, 0.f);
158 if (client_->ControlsTopOffset() == controls_top_offset)
161 client_->SetControlsTopOffset(controls_top_offset);
163 client_->DidChangeTopControlsPosition();
166 gfx::Vector2dF TopControlsManager::Animate(base::TimeTicks monotonic_time) {
167 if (!top_controls_animation_ || !client_->HaveRootScrollLayer())
168 return gfx::Vector2dF();
170 double time = (monotonic_time - base::TimeTicks()).InMillisecondsF();
172 float old_offset = client_->ControlsTopOffset();
173 SetControlsTopOffset(top_controls_animation_->GetValue(time));
175 if (IsAnimationCompleteAtTime(monotonic_time))
178 gfx::Vector2dF scroll_delta(0.f, client_->ControlsTopOffset() - old_offset);
182 void TopControlsManager::ResetAnimations() {
183 top_controls_animation_ = nullptr;
184 animation_direction_ = NO_ANIMATION;
187 void TopControlsManager::SetupAnimation(AnimationDirection direction) {
188 DCHECK(direction != NO_ANIMATION);
190 if (direction == SHOWING_CONTROLS && client_->ControlsTopOffset() == 0)
193 if (direction == HIDING_CONTROLS &&
194 client_->ControlsTopOffset() == -top_controls_height_) {
198 if (top_controls_animation_ && animation_direction_ == direction)
201 top_controls_animation_ = KeyframedFloatAnimationCurve::Create();
203 (gfx::FrameTime::Now() - base::TimeTicks()).InMillisecondsF();
204 top_controls_animation_->AddKeyframe(
205 FloatKeyframe::Create(start_time, client_->ControlsTopOffset(), nullptr));
206 float max_ending_offset =
207 (direction == SHOWING_CONTROLS ? 1 : -1) * top_controls_height_;
208 top_controls_animation_->AddKeyframe(
209 FloatKeyframe::Create(start_time + kShowHideMaxDurationMs,
210 client_->ControlsTopOffset() + max_ending_offset,
211 EaseTimingFunction::Create()));
212 animation_direction_ = direction;
213 client_->DidChangeTopControlsPosition();
216 void TopControlsManager::StartAnimationIfNecessary() {
217 if (client_->ControlsTopOffset() != 0
218 && client_->ControlsTopOffset() != -top_controls_height_) {
219 AnimationDirection show_controls = NO_ANIMATION;
221 if (client_->ControlsTopOffset() >= -top_controls_show_height_) {
222 // If we're showing so much that the hide threshold won't trigger, show.
223 show_controls = SHOWING_CONTROLS;
224 } else if (client_->ControlsTopOffset() <= -top_controls_hide_height_) {
225 // If we're showing so little that the show threshold won't trigger, hide.
226 show_controls = HIDING_CONTROLS;
228 // If we could be either showing or hiding, we determine which one to
229 // do based on whether or not the total scroll delta was moving up or
231 show_controls = current_scroll_delta_ <= 0.f ?
232 SHOWING_CONTROLS : HIDING_CONTROLS;
235 if (show_controls != NO_ANIMATION)
236 SetupAnimation(show_controls);
240 bool TopControlsManager::IsAnimationCompleteAtTime(base::TimeTicks time) {
241 if (!top_controls_animation_)
244 double time_ms = (time - base::TimeTicks()).InMillisecondsF();
245 float new_offset = top_controls_animation_->GetValue(time_ms);
247 if ((animation_direction_ == SHOWING_CONTROLS && new_offset >= 0) ||
248 (animation_direction_ == HIDING_CONTROLS
249 && new_offset <= -top_controls_height_)) {