Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / android / edge_effect_l.cc
blob728db67dbe8f4fb78cd013c27df7f171c07c057b
1 // Copyright 2014 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 "content/browser/android/edge_effect_l.h"
7 #include "cc/layers/ui_resource_layer.h"
8 #include "content/browser/android/animation_utils.h"
9 #include "ui/android/resources/resource_manager.h"
10 #include "ui/android/resources/system_ui_resource_type.h"
12 namespace content {
14 namespace {
16 // Time it will take the effect to fully recede in ms
17 const int kRecedeTimeMs = 600;
19 // Time it will take before a pulled glow begins receding in ms
20 const int kPullTimeMs = 167;
22 // Time it will take for a pulled glow to decay to partial strength before
23 // release
24 const int kPullDecayTimeMs = 2000;
26 const float kMaxAlpha = 0.5f;
28 const float kPullGlowBegin = 0.f;
30 // Min/max velocity that will be absorbed
31 const float kMinVelocity = 100.f;
32 const float kMaxVelocity = 10000.f;
34 const float kEpsilon = 0.001f;
36 const float kSin = 0.5f; // sin(PI / 6)
37 const float kCos = 0.866f; // cos(PI / 6);
39 // How much dragging should effect the height of the glow image.
40 // Number determined by user testing.
41 const float kPullDistanceAlphaGlowFactor = 0.8f;
43 const int kVelocityGlowFactor = 6;
45 const ui::SystemUIResourceType kResourceId = ui::OVERSCROLL_GLOW_L;
47 } // namespace
49 EdgeEffectL::EdgeEffectL(ui::ResourceManager* resource_manager)
50 : resource_manager_(resource_manager),
51 glow_(cc::UIResourceLayer::Create()),
52 glow_alpha_(0),
53 glow_scale_y_(0),
54 glow_alpha_start_(0),
55 glow_alpha_finish_(0),
56 glow_scale_y_start_(0),
57 glow_scale_y_finish_(0),
58 displacement_(0.5f),
59 target_displacement_(0.5f),
60 state_(STATE_IDLE),
61 pull_distance_(0) {
62 // Prevent the provided layers from drawing until the effect is activated.
63 glow_->SetIsDrawable(false);
66 EdgeEffectL::~EdgeEffectL() {
67 glow_->RemoveFromParent();
70 bool EdgeEffectL::IsFinished() const {
71 return state_ == STATE_IDLE;
74 void EdgeEffectL::Finish() {
75 glow_->SetIsDrawable(false);
76 pull_distance_ = 0;
77 state_ = STATE_IDLE;
80 void EdgeEffectL::Pull(base::TimeTicks current_time,
81 float delta_distance,
82 float displacement) {
83 target_displacement_ = displacement;
84 if (state_ == STATE_PULL_DECAY && current_time - start_time_ < duration_) {
85 return;
87 if (state_ != STATE_PULL) {
88 glow_scale_y_ = std::max(kPullGlowBegin, glow_scale_y_);
90 state_ = STATE_PULL;
92 start_time_ = current_time;
93 duration_ = base::TimeDelta::FromMilliseconds(kPullTimeMs);
95 float abs_delta_distance = std::abs(delta_distance);
96 pull_distance_ += delta_distance;
98 glow_alpha_ = glow_alpha_start_ = std::min(
99 kMaxAlpha,
100 glow_alpha_ + (abs_delta_distance * kPullDistanceAlphaGlowFactor));
102 if (pull_distance_ == 0) {
103 glow_scale_y_ = glow_scale_y_start_ = 0;
104 } else {
105 float scale = 1.f -
106 1.f / std::sqrt(std::abs(pull_distance_) * bounds_.height()) -
107 0.3f;
108 glow_scale_y_ = glow_scale_y_start_ = std::max(0.f, scale) / 0.7f;
111 glow_alpha_finish_ = glow_alpha_;
112 glow_scale_y_finish_ = glow_scale_y_;
115 void EdgeEffectL::Release(base::TimeTicks current_time) {
116 pull_distance_ = 0;
118 if (state_ != STATE_PULL && state_ != STATE_PULL_DECAY)
119 return;
121 state_ = STATE_RECEDE;
122 glow_alpha_start_ = glow_alpha_;
123 glow_scale_y_start_ = glow_scale_y_;
125 glow_alpha_finish_ = 0.f;
126 glow_scale_y_finish_ = 0.f;
128 start_time_ = current_time;
129 duration_ = base::TimeDelta::FromMilliseconds(kRecedeTimeMs);
132 void EdgeEffectL::Absorb(base::TimeTicks current_time, float velocity) {
133 state_ = STATE_ABSORB;
135 velocity = Clamp(std::abs(velocity), kMinVelocity, kMaxVelocity);
137 start_time_ = current_time;
138 // This should never be less than 1 millisecond.
139 duration_ = base::TimeDelta::FromMilliseconds(0.15f + (velocity * 0.02f));
141 // The glow depends more on the velocity, and therefore starts out
142 // nearly invisible.
143 glow_alpha_start_ = 0.3f;
144 glow_scale_y_start_ = std::max(glow_scale_y_, 0.f);
146 // Growth for the size of the glow should be quadratic to properly respond
147 // to a user's scrolling speed. The faster the scrolling speed, the more
148 // intense the effect should be for both the size and the saturation.
149 glow_scale_y_finish_ =
150 std::min(0.025f + (velocity * (velocity / 100) * 0.00015f) / 2.f, 1.f);
151 // Alpha should change for the glow as well as size.
152 glow_alpha_finish_ = Clamp(
153 glow_alpha_start_, velocity * kVelocityGlowFactor * .00001f, kMaxAlpha);
154 target_displacement_ = 0.5;
157 bool EdgeEffectL::Update(base::TimeTicks current_time) {
158 if (IsFinished())
159 return false;
161 const double dt = (current_time - start_time_).InMilliseconds();
162 const double t = std::min(dt / duration_.InMilliseconds(), 1.);
163 const float interp = static_cast<float>(Damp(t, 1.));
165 glow_alpha_ = Lerp(glow_alpha_start_, glow_alpha_finish_, interp);
166 glow_scale_y_ = Lerp(glow_scale_y_start_, glow_scale_y_finish_, interp);
167 displacement_ = (displacement_ + target_displacement_) / 2.f;
169 if (t >= 1.f - kEpsilon) {
170 switch (state_) {
171 case STATE_ABSORB:
172 state_ = STATE_RECEDE;
173 start_time_ = current_time;
174 duration_ = base::TimeDelta::FromMilliseconds(kRecedeTimeMs);
176 glow_alpha_start_ = glow_alpha_;
177 glow_scale_y_start_ = glow_scale_y_;
179 glow_alpha_finish_ = 0.f;
180 glow_scale_y_finish_ = 0.f;
181 break;
182 case STATE_PULL:
183 state_ = STATE_PULL_DECAY;
184 start_time_ = current_time;
185 duration_ = base::TimeDelta::FromMilliseconds(kPullDecayTimeMs);
187 glow_alpha_start_ = glow_alpha_;
188 glow_scale_y_start_ = glow_scale_y_;
190 // After pull, the glow should fade to nothing.
191 glow_alpha_finish_ = 0.f;
192 glow_scale_y_finish_ = 0.f;
193 break;
194 case STATE_PULL_DECAY:
195 state_ = STATE_RECEDE;
196 break;
197 case STATE_RECEDE:
198 Finish();
199 break;
200 default:
201 break;
205 bool one_last_frame = false;
206 if (state_ == STATE_RECEDE && glow_scale_y_ <= 0) {
207 Finish();
208 one_last_frame = true;
211 return !IsFinished() || one_last_frame;
214 float EdgeEffectL::GetAlpha() const {
215 return IsFinished() ? 0.f : glow_alpha_;
218 void EdgeEffectL::ApplyToLayers(const gfx::SizeF& size,
219 const gfx::Transform& transform) {
220 if (IsFinished())
221 return;
223 // An empty window size, while meaningless, is also relatively harmless, and
224 // will simply prevent any drawing of the layers.
225 if (size.IsEmpty()) {
226 glow_->SetIsDrawable(false);
227 return;
230 const float r = size.width() * 0.75f / kSin;
231 const float y = kCos * r;
232 const float h = r - y;
233 const float o_r = size.height() * 0.75f / kSin;
234 const float o_y = kCos * o_r;
235 const float o_h = o_r - o_y;
236 const float base_glow_scale = h > 0.f ? std::min(o_h / h, 1.f) : 1.f;
237 bounds_ = gfx::Size(size.width(), (int)std::min(size.height(), h));
238 gfx::Size image_bounds(
239 r, std::min(1.f, glow_scale_y_) * base_glow_scale * bounds_.height());
241 glow_->SetIsDrawable(true);
242 glow_->SetUIResourceId(resource_manager_->GetUIResourceId(
243 ui::ANDROID_RESOURCE_TYPE_SYSTEM, kResourceId));
244 glow_->SetTransformOrigin(gfx::Point3F(bounds_.width() * 0.5f, 0, 0));
245 glow_->SetBounds(image_bounds);
246 glow_->SetContentsOpaque(false);
247 glow_->SetOpacity(Clamp(glow_alpha_, 0.f, 1.f));
249 const float displacement = Clamp(displacement_, 0.f, 1.f) - 0.5f;
250 const float displacement_offset_x = bounds_.width() * displacement * 0.5f;
251 const float image_offset_x = (bounds_.width() - image_bounds.width()) * 0.5f;
252 gfx::Transform offset_transform;
253 offset_transform.Translate(image_offset_x - displacement_offset_x, 0);
254 offset_transform.ConcatTransform(transform);
255 glow_->SetTransform(offset_transform);
258 void EdgeEffectL::SetParent(cc::Layer* parent) {
259 if (glow_->parent() != parent)
260 parent->AddChild(glow_);
263 // static
264 void EdgeEffectL::PreloadResources(ui::ResourceManager* resource_manager) {
265 DCHECK(resource_manager);
266 resource_manager->PreloadResource(ui::ANDROID_RESOURCE_TYPE_SYSTEM,
267 kResourceId);
270 } // namespace content