Chromium Code Reviews| Index: chrome/renderer/webview_animating_overlay.cc |
| diff --git a/chrome/renderer/webview_animating_overlay.cc b/chrome/renderer/webview_animating_overlay.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..10cc32327ffeadc00191c5ae82a0901b40091580 |
| --- /dev/null |
| +++ b/chrome/renderer/webview_animating_overlay.cc |
| @@ -0,0 +1,115 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/renderer/webview_animating_overlay.h" |
| + |
| +#include "content/public/renderer/render_view.h" |
| +#include "third_party/skia/include/core/SkCanvas.h" |
| +#include "third_party/skia/include/core/SkPaint.h" |
| +#include "third_party/skia/include/core/SkRect.h" |
| +#include "third_party/skia/include/effects/SkGradientShader.h" |
| +#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| +#include "ui/gfx/size.h" |
| +#include "ui/gfx/skia_util.h" |
| + |
| +const int kAnimationDurationMiliseconds = 500; |
| + |
| +WebViewAnimatingOverlay::WebViewAnimatingOverlay( |
| + content::RenderView* render_view) |
| + : render_view_(render_view), |
| + state_(HIDDEN) { |
| +} |
| + |
| +WebViewAnimatingOverlay::~WebViewAnimatingOverlay() { |
| + if (render_view_->GetWebView()) |
| + render_view_->GetWebView()->removePageOverlay(this); |
| +} |
| + |
| +void WebViewAnimatingOverlay::Show() { |
| + if (state_ == ANIMATING_IN || state_ == VISIBLE) |
| + return; |
| + |
| + if (state_ == ANIMATING_OUT) { |
| + start_time_ = base::Time::Now() - GetAnimationTimeRemaining(); |
| + } else { |
|
Nico
2012/08/14 21:14:12
DCHECK(state_ == HIDDEN);
Alternatively, you coul
sail
2012/08/15 05:26:20
Done.
|
| + start_time_ = base::Time::Now(); |
| + if (render_view_->GetWebView()) |
| + render_view_->GetWebView()->addPageOverlay(this, 0); |
| + } |
| + |
| + state_ = ANIMATING_IN; |
| + timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(16), this, |
|
Nico
2012/08/14 21:14:12
constant
sail
2012/08/15 05:26:20
Done.
|
| + &WebViewAnimatingOverlay::OnTimer); |
| +} |
| + |
| +void WebViewAnimatingOverlay::Hide() { |
| + if (state_ == ANIMATING_OUT || state_ == HIDDEN) |
| + return; |
| + |
| + if (state_ == ANIMATING_IN) |
| + start_time_ = base::Time::Now() - GetAnimationTimeRemaining(); |
| + else |
| + start_time_ = base::Time::Now(); |
| + |
| + state_ = ANIMATING_OUT; |
| + timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(16), this, |
| + &WebViewAnimatingOverlay::OnTimer); |
| +} |
| + |
| +void WebViewAnimatingOverlay::paintPageOverlay(WebKit::WebCanvas* canvas) { |
| + SkRect rect = gfx::RectToSkRect(gfx::Rect(render_view_->GetSize())); |
| + |
| + // The center of the radial gradient should be near the top middle. |
| + SkPoint center_point; |
| + center_point.iset(rect.width() * 0.5, rect.height() * 0.05); |
| + |
| + // Animate in or out using the alpha. |
| + int alpha = GetAnimationProgress() * 191; |
|
Nico
2012/08/14 21:14:12
This looks like a linear animation. Could ui/base/
sail
2012/08/15 05:26:20
Done.
|
| + SkColor colors[] = { |
| + SkColorSetARGB(alpha, 255, 255, 255), |
| + SkColorSetARGB(alpha, 127, 127, 127) |
| + }; |
| + |
| + SkShader* shader = SkGradientShader::CreateRadial(center_point, |
| + SkIntToScalar(rect.width()), colors, NULL, arraysize(colors), |
| + SkShader::kClamp_TileMode); |
| + SkPaint paint; |
| + paint.setShader(shader); |
| + paint.setDither(true); |
| + shader->unref(); |
|
jamesr
2012/08/14 19:08:24
Could you use SkAutoTUnref<> here instead of manua
sail
2012/08/15 05:26:20
Done.
|
| + |
| + canvas->drawRect(rect, paint); |
| +} |
| + |
| +void WebViewAnimatingOverlay::OnTimer() { |
| + if (GetAnimationTimeRemaining() <= base::TimeDelta()) { |
| + timer_.Stop(); |
| + if (state_ == ANIMATING_IN) { |
| + state_ = VISIBLE; |
| + } else { |
| + DCHECK(state_ == ANIMATING_OUT); |
| + state_ = HIDDEN; |
| + if (render_view_->GetWebView()) |
| + render_view_->GetWebView()->removePageOverlay(this); |
| + } |
| + } |
| + render_view_->Repaint(render_view_->GetSize()); |
| +} |
| + |
| +base::TimeDelta WebViewAnimatingOverlay::GetAnimationTimeRemaining() const { |
| + base::Time end_time = start_time_ + |
| + base::TimeDelta::FromMilliseconds(kAnimationDurationMiliseconds); |
| + return end_time - base::Time::Now(); |
|
jamesr
2012/08/14 19:08:24
we drive most web-based animations of of base::Tim
sail
2012/08/15 05:26:20
Done.
Switched to ui/base/animation which already
|
| +} |
| + |
| +float WebViewAnimatingOverlay::GetAnimationProgress() const { |
| + if (state_ != ANIMATING_IN && state_ != ANIMATING_OUT) |
| + return 1.0; |
| + |
| + base::TimeDelta delta = base::Time::Now() - start_time_; |
| + float progress = delta.InMillisecondsF() / kAnimationDurationMiliseconds; |
| + if (state_ == ANIMATING_OUT) |
| + progress = 1.0 - progress; |
| + return std::max(std::min(progress, 1.0f), 0.0f); |
| +} |