Enable snappy for IndexedDB.
[chromium-blink-merge.git] / cc / input / top_controls_manager.cc
blob925830cf40e59fb94a953d31e1d2e7e97a63b37d
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"
7 #include <algorithm>
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"
18 namespace cc {
19 namespace {
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;
25 // static
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,
32 top_controls_height,
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)
41 : client_(client),
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 CHECK(client_);
55 TopControlsManager::~TopControlsManager() {
58 void TopControlsManager::UpdateTopControlsState(TopControlsState constraints,
59 TopControlsState current,
60 bool animate) {
61 DCHECK(!(constraints == SHOWN && current == HIDDEN));
62 DCHECK(!(constraints == HIDDEN && current == SHOWN));
64 permitted_state_ = constraints;
66 // Don't do anything if it doesn't matter which state the controls are in.
67 if (constraints == BOTH && current == BOTH)
68 return;
70 // Don't do anything if there is no change in offset.
71 float final_controls_position = 0.f;
72 if (constraints == HIDDEN || current == HIDDEN) {
73 final_controls_position = -top_controls_height_;
75 if (final_controls_position == controls_top_offset_) {
76 return;
79 AnimationDirection animation_direction = SHOWING_CONTROLS;
80 if (constraints == HIDDEN || current == HIDDEN)
81 animation_direction = HIDING_CONTROLS;
82 ResetAnimations();
83 if (animate) {
84 SetupAnimation(animation_direction);
85 } else {
86 controls_top_offset_ = final_controls_position;
88 client_->DidChangeTopControlsPosition();
91 void TopControlsManager::ScrollBegin() {
92 ResetAnimations();
93 current_scroll_delta_ = 0.f;
94 controls_scroll_begin_offset_ = controls_top_offset_;
97 gfx::Vector2dF TopControlsManager::ScrollBy(
98 const gfx::Vector2dF pending_delta) {
99 if (permitted_state_ == SHOWN && pending_delta.y() > 0)
100 return pending_delta;
101 else if (permitted_state_ == HIDDEN && pending_delta.y() < 0)
102 return pending_delta;
104 current_scroll_delta_ += pending_delta.y();
106 float old_offset = controls_top_offset_;
107 SetControlsTopOffset(controls_scroll_begin_offset_ - current_scroll_delta_);
109 // If the controls are fully visible, treat the current position as the
110 // new baseline even if the gesture didn't end.
111 if (controls_top_offset_ == 0.f) {
112 current_scroll_delta_ = 0.f;
113 controls_scroll_begin_offset_ = 0.f;
116 ResetAnimations();
118 gfx::Vector2dF applied_delta(0.f, old_offset - controls_top_offset_);
119 return pending_delta - applied_delta;
122 void TopControlsManager::ScrollEnd() {
123 StartAnimationIfNecessary();
126 void TopControlsManager::SetControlsTopOffset(float controls_top_offset) {
127 controls_top_offset = std::max(controls_top_offset, -top_controls_height_);
128 controls_top_offset = std::min(controls_top_offset, 0.f);
130 if (controls_top_offset_ == controls_top_offset)
131 return;
133 controls_top_offset_ = controls_top_offset;
135 client_->DidChangeTopControlsPosition();
138 gfx::Vector2dF TopControlsManager::Animate(base::TimeTicks monotonic_time) {
139 if (!top_controls_animation_ || !client_->HaveRootScrollLayer())
140 return gfx::Vector2dF();
142 double time = (monotonic_time - base::TimeTicks()).InMillisecondsF();
144 float old_offset = controls_top_offset_;
145 SetControlsTopOffset(top_controls_animation_->GetValue(time));
147 if (IsAnimationCompleteAtTime(monotonic_time))
148 ResetAnimations();
150 gfx::Vector2dF scroll_delta(0.f, controls_top_offset_ - old_offset);
151 return scroll_delta;
154 void TopControlsManager::ResetAnimations() {
155 if (top_controls_animation_)
156 top_controls_animation_.reset();
158 animation_direction_ = NO_ANIMATION;
161 void TopControlsManager::SetupAnimation(AnimationDirection direction) {
162 DCHECK(direction != NO_ANIMATION);
164 if (direction == SHOWING_CONTROLS && controls_top_offset_ == 0)
165 return;
167 if (direction == HIDING_CONTROLS &&
168 controls_top_offset_ == -top_controls_height_) {
169 return;
172 if (top_controls_animation_ && animation_direction_ == direction)
173 return;
175 top_controls_animation_ = KeyframedFloatAnimationCurve::Create();
176 double start_time =
177 (base::TimeTicks::Now() - base::TimeTicks()).InMillisecondsF();
178 top_controls_animation_->AddKeyframe(
179 FloatKeyframe::Create(start_time, controls_top_offset_,
180 scoped_ptr<TimingFunction>()));
181 float max_ending_offset =
182 (direction == SHOWING_CONTROLS ? 1 : -1) * top_controls_height_;
183 top_controls_animation_->AddKeyframe(
184 FloatKeyframe::Create(start_time + kShowHideMaxDurationMs,
185 controls_top_offset_ + max_ending_offset,
186 EaseTimingFunction::Create()));
187 animation_direction_ = direction;
188 client_->DidChangeTopControlsPosition();
191 void TopControlsManager::StartAnimationIfNecessary() {
192 if (controls_top_offset_ != 0
193 && controls_top_offset_ != -top_controls_height_) {
194 AnimationDirection show_controls = NO_ANIMATION;
196 if (controls_top_offset_ >= -top_controls_show_height_) {
197 // If we're showing so much that the hide threshold won't trigger, show.
198 show_controls = SHOWING_CONTROLS;
199 } else if (controls_top_offset_ <= -top_controls_hide_height_) {
200 // If we're showing so little that the show threshold won't trigger, hide.
201 show_controls = HIDING_CONTROLS;
202 } else {
203 // If we could be either showing or hiding, we determine which one to
204 // do based on whether or not the total scroll delta was moving up or
205 // down.
206 show_controls = current_scroll_delta_ <= 0.f ?
207 SHOWING_CONTROLS : HIDING_CONTROLS;
210 if (show_controls != NO_ANIMATION)
211 SetupAnimation(show_controls);
215 bool TopControlsManager::IsAnimationCompleteAtTime(base::TimeTicks time) {
216 if (!top_controls_animation_)
217 return true;
219 double time_ms = (time - base::TimeTicks()).InMillisecondsF();
220 float new_offset = top_controls_animation_->GetValue(time_ms);
222 if ((animation_direction_ == SHOWING_CONTROLS && new_offset >= 0) ||
223 (animation_direction_ == HIDING_CONTROLS
224 && new_offset <= -top_controls_height_)) {
225 return true;
227 return false;
230 } // namespace cc